Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/utils.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424import * as catalog from '@okikio/catalog';import type { CatalogEntryIdentity } from '@okikio/catalog';import type { EndpointDefinition } from '@okikio/server/endpoint';import { leafEndpoints } from '../service/definition.ts';import { validate as validateService } from '../service/compile.ts';import type { CompiledService, ServiceDefinition, ServiceManifest, ServiceRoute, ServiceRouteManifestEntry, ServiceSelection,} from '../service/types.ts';import type { CompiledGateway, CompiledGatewayRoute, CompileGatewayOptions, GatewayCachePolicy, GatewayCredentialPolicy, GatewayDefinition, GatewayManifest, GatewayPolicy, GatewayRedirectPolicy, GatewayRouteManifestEntry, GatewayServiceArtifact, GatewayValidationIssue, GatewayValidationResult, GatewayValidationSubject,} from './types.ts';
const defaultCache: GatewayCachePolicy = Object.freeze({ kind: 'gateway-cache', mode: 'no-store' });const defaultCredentials: GatewayCredentialPolicy = Object.freeze({ kind: 'gateway-credentials', requestCookies: 'strip', requestAuthorization: 'strip', responseCookies: 'strip' });const defaultRedirects: GatewayRedirectPolicy = Object.freeze({ kind: 'gateway-redirects', mode: 'rewrite-origin', allowedOrigins: Object.freeze([]) });
/** Error raised when a gateway cannot be compiled safely. */export class GatewayCompilationError extends Error { readonly issues: readonly GatewayValidationIssue[];
constructor(issues: readonly GatewayValidationIssue[]) { super(`Gateway compilation failed with ${issues.length} issue${issues.length === 1 ? '' : 's'}:\n${issues.map((issue) => `- ${issue.message}`).join('\n')}`); this.name = 'GatewayCompilationError'; this.issues = Object.freeze([...issues]); }}
/** Validate mount and policy ownership without requiring service artifacts. */export function validate(definition: GatewayDefinition): GatewayValidationResult { const issues: GatewayValidationIssue[] = []; const mountedServices = new Map<string, ServiceDefinition>(); const availableEndpoints = new Set<EndpointDefinition>(); for (const mounted of definition.services) { const service = serviceOf(mounted.target); const existing = mountedServices.get(service.id); if (existing && existing !== service) { issues.push(issue('duplicate-mount', `Service ID ${JSON.stringify(service.id)} belongs to different definitions.`, mounted)); } if (existing === service) { issues.push(issue('duplicate-mount', `Service ${JSON.stringify(service.id)} is mounted more than once.`, mounted)); } mountedServices.set(service.id, service); for (const endpoint of selectedEndpoints(mounted.target)) availableEndpoints.add(endpoint); } for (const policy of definition.policies) { for (const target of policy.endpoints) { if (!availableEndpoints.has(target)) { issues.push(issue('policy-target-outside-gateway', `Policy ${JSON.stringify(policy.id)} targets endpoint ${JSON.stringify(target.id)} outside gateway ${JSON.stringify(definition.id)}.`, policy)); } } } return issues.length === 0 ? Object.freeze({ valid: true }) : Object.freeze({ valid: false, issues: Object.freeze(issues) });}
/** Compile import-safe service definitions into one deterministic edge route table. */export function compile<Definition extends GatewayDefinition>( definition: Definition, options: CompileGatewayOptions = {},): CompiledGateway<Definition> { const validation = validate(definition); const issues: GatewayValidationIssue[] = validation.valid ? [] : [...validation.issues]; const artifacts = indexArtifacts(options.services ?? [], issues); const policyTargets = new Map(definition.policies.map((policy) => [policy, new Set(policy.endpoints)] as const)); const routes: CompiledGatewayRoute[] = []; const routeOwners = new Map<string, CompiledGatewayRoute>();
for (const mounted of definition.services) { const service = serviceOf(mounted.target); const serviceValidation = validateService(service); if (!serviceValidation.valid) { for (const serviceIssue of serviceValidation.issues) { issues.push(issue('invalid-definition', `Mounted service ${JSON.stringify(service.id)} is invalid: ${serviceIssue.message}`, service)); } continue; }
const selected = new Set(selectedEndpoints(mounted.target)); const artifact = artifacts.get(service.id); for (const route of serviceValidation.routes) { if (!selected.has(route.endpoint) || isInternal(route)) continue; const artifactRoute = artifact?.manifest.routes.find((candidate) => candidate.id === route.id); if (artifact !== undefined && artifactRoute === undefined) { issues.push(issue('invalid-definition', `Service artifact ${JSON.stringify(service.id)} is missing public route ${JSON.stringify(route.id)}.`, artifact.manifest)); continue; } if (artifactRoute !== undefined && !sameServiceRoute(route, artifactRoute)) { issues.push(issue('invalid-definition', `Service artifact route ${JSON.stringify(route.id)} does not match its imported definition.`, artifactRoute)); continue; } const policies = definition.policies.filter((policy) => policyTargets.get(policy)?.has(route.endpoint)); const effective = effectivePolicy(policies, issues, route); const compiledRoute: CompiledGatewayRoute = Object.freeze({ id: `${definition.id}:${route.id}`, gateway: definition, serviceId: service.id, serviceRouteId: route.id, endpointId: route.endpoint.id, operationId: route.operation.operationId, method: route.method.toUpperCase() as Uppercase<typeof route.method>, path: route.path, origin: mounted.origin.toString(), authenticate: effective.authenticate, assertions: effective.assertions, ...(effective.timeout !== undefined ? { timeout: effective.timeout } : {}), ...(effective.bodyLimit !== undefined ? { bodyLimit: effective.bodyLimit } : {}), cache: effective.cache, credentials: effective.credentials, redirects: effective.redirects, observers: definition.observers, }); const key = `${compiledRoute.method} ${normalizeRouteShape(compiledRoute.path)}`; const owner = routeOwners.get(key); if (owner && owner.serviceId !== compiledRoute.serviceId) { issues.push(issue('route-conflict', `${compiledRoute.method} ${compiledRoute.path} is owned by both ${owner.serviceId} and ${compiledRoute.serviceId}.`, compiledRoute)); continue; } routeOwners.set(key, compiledRoute); routes.push(compiledRoute); } } if (issues.length > 0) throw new GatewayCompilationError(Object.freeze(issues)); const frozenRoutes = Object.freeze(routes.toSorted(compareRoutes)); return Object.freeze({ kind: 'compiled-gateway', definition, routes: frozenRoutes, manifest: manifest(definition, frozenRoutes), });}
/** Return the JSON-safe gateway manifest retained by a compiled gateway. */export function document(compiled: CompiledGateway): GatewayManifest { return compiled.manifest;}
/** * Indexes artifacts for exact validation and lookup in later phases. * * It compiles and executes trusted service routing without letting gateway policy become service-domain behavior. * * @internal */function indexArtifacts( input: readonly GatewayServiceArtifact[], issues: GatewayValidationIssue[],): ReadonlyMap<string, Readonly<{ readonly manifest: ServiceManifest }>> { const result = new Map<string, Readonly<{ readonly manifest: ServiceManifest }>>(); for (const artifact of input) { const manifest = isCompiledService(artifact) ? artifact.manifest : artifact; const existing = result.get(manifest.id); if (existing && existing.manifest !== manifest) { issues.push(issue('invalid-definition', `More than one service artifact uses ID ${JSON.stringify(manifest.id)}.`, artifact)); continue; } result.set(manifest.id, Object.freeze({ manifest })); } return result;}
/** * Resolves the effective policy after inherited and contributed state is combined by compiled gateway routing. * * Gateway internals route only definitions selected by the composition root and keep gateway trust policy out of service-domain behavior. * * @internal */function effectivePolicy( policies: readonly GatewayPolicy[], issues: GatewayValidationIssue[], route: ServiceRoute | ServiceRouteManifestEntry,): Readonly<{ readonly authenticate: readonly CatalogEntryIdentity[]; readonly assertions: readonly CatalogEntryIdentity[]; readonly timeout?: Temporal.Duration; readonly bodyLimit?: number; readonly cache: GatewayCachePolicy; readonly credentials: GatewayCredentialPolicy; readonly redirects: GatewayRedirectPolicy;}> { const authenticate = Object.freeze(policies.flatMap((policy) => definitionValues(policy.authenticate))); const assertions = Object.freeze(policies.flatMap((policy) => definitionValues(policy.assertion))); const timeout = singleValue(policies.flatMap((policy) => policy.timeout === undefined ? [] : [policy.timeout]), 'timeout', route, issues, sameDuration); const bodyLimit = singleValue(policies.flatMap((policy) => policy.bodyLimit === undefined ? [] : [policy.bodyLimit]), 'body limit', route, issues); const cache = singleValue(policies.flatMap((policy) => policy.cache === undefined ? [] : [policy.cache]), 'cache policy', route, issues, sameCache) ?? defaultCache; const credentials = singleValue(policies.flatMap((policy) => policy.credentials === undefined ? [] : [policy.credentials]), 'credential policy', route, issues, sameCredentials) ?? defaultCredentials; const redirects = singleValue(policies.flatMap((policy) => policy.redirects === undefined ? [] : [policy.redirects]), 'redirect policy', route, issues, sameRedirects) ?? defaultRedirects; return Object.freeze({ authenticate, assertions, ...(timeout !== undefined ? { timeout } : {}), ...(bodyLimit !== undefined ? { bodyLimit } : {}), cache, credentials, redirects, });}
/** * Returns the single value in the representation expected by compiled gateway routing. * * Gateway internals route only definitions selected by the composition root and keep gateway trust policy out of service-domain behavior. * * @internal */function singleValue<Value>( values: readonly Value[], label: string, route: ServiceRoute | ServiceRouteManifestEntry, issues: GatewayValidationIssue[], equal: (left: Value, right: Value) => boolean = Object.is,): Value | undefined { const first = values[0]; if (first === undefined) return undefined; for (const value of values.slice(1)) { if (!equal(first, value)) { issues.push(issue('conflicting-policy', `Route ${JSON.stringify(route.id)} receives conflicting ${label} values.`, route)); break; } } return first;}
/** Return whether a validated service route is intentionally unavailable to the public gateway. */function isInternal(route: ServiceRoute): boolean { return route.endpoint.internal === true || route.operation.internal === true || route.groups.some((group) => group.internal === true);}
/** Compare an optional compiled service manifest with its import-safe route definition. */function sameServiceRoute(route: ServiceRoute, manifest: ServiceRouteManifestEntry): boolean { return manifest.id === route.id && manifest.method === route.method.toUpperCase() && manifest.path === route.path && manifest.endpointId === route.endpoint.id && manifest.operationId === route.operation.operationId;}
/** * Selects endpoints needed by compiled gateway routing without changing the source definition. * * @internal */function selectedEndpoints(target: ServiceDefinition | ServiceSelection): readonly EndpointDefinition[] { return target.kind === 'service-selection' ? target.endpoints : leafEndpoints(target.endpoints);}
/** * Returns the service definition that owns a compiled gateway route. * * @internal */function serviceOf(target: ServiceDefinition | ServiceSelection): ServiceDefinition { return target.kind === 'service-selection' ? target.service : target;}
/** * Builds the manifest that records the state consumed by compiled gateway routing. * * @internal */function manifest(definition: GatewayDefinition, routes: readonly CompiledGatewayRoute[]): GatewayManifest { return Object.freeze({ id: definition.id, routes: Object.freeze(routes.map(routeManifest)), services: Object.freeze([...new Set(routes.map((route) => route.serviceId))]), });}
/** * Builds the route manifest that records the state consumed by compiled gateway routing. * * Gateway internals route only definitions selected by the composition root and keep gateway trust policy out of service-domain behavior. * * @internal */function routeManifest(route: CompiledGatewayRoute): GatewayRouteManifestEntry { return Object.freeze({ id: route.id, serviceId: route.serviceId, serviceRouteId: route.serviceRouteId, endpointId: route.endpointId, operationId: route.operationId, method: route.method, path: route.path, origin: route.origin, authentication: Object.freeze(route.authenticate.map(definitionId)), assertions: Object.freeze(route.assertions.map(definitionId)), ...(route.timeout !== undefined ? { timeout: route.timeout.toString() } : {}), ...(route.bodyLimit !== undefined ? { bodyLimit: route.bodyLimit } : {}), cache: route.cache.mode, credentials: Object.freeze({ requestCookies: route.credentials.requestCookies, requestAuthorization: route.credentials.requestAuthorization, responseCookies: route.credentials.responseCookies, }), redirects: route.redirects.mode, observers: Object.freeze(route.observers.map((observer) => observer.id)), });}
/** * Derives the definition id from exact imported definitions used by compiled gateway routing. * * @internal */function definitionId(value: unknown, index: number): string { return typeof value === 'object' && value !== null && typeof (value as { id?: unknown }).id === 'string' ? (value as { id: string }).id : `anonymous:${index}`;}
/** * Returns the definition values consumed by compiled gateway routing. * * @internal */function definitionValues<Entry extends import('@okikio/catalog').CatalogEntryIdentity>( input: import('@okikio/catalog').DefinitionInput<Entry> | undefined,): readonly Entry[] { return input === undefined ? Object.freeze([]) : catalog.values(input);}
/** * Checks whether duration are equivalent for the purposes of compiled gateway routing. * * @internal */function sameDuration(left: Temporal.Duration, right: Temporal.Duration): boolean { return left.toString() === right.toString();}
/** * Checks whether cache are equivalent for the purposes of compiled gateway routing. * * @internal */function sameCache(left: GatewayCachePolicy, right: GatewayCachePolicy): boolean { return left.mode === right.mode;}
/** * Checks whether credentials are equivalent for the purposes of compiled gateway routing. * * @internal */function sameCredentials(left: GatewayCredentialPolicy, right: GatewayCredentialPolicy): boolean { return left.requestCookies === right.requestCookies && left.requestAuthorization === right.requestAuthorization && left.responseCookies === right.responseCookies;}
/** * Checks whether redirects are equivalent for the purposes of compiled gateway routing. * * @internal */function sameRedirects(left: GatewayRedirectPolicy, right: GatewayRedirectPolicy): boolean { return left.mode === right.mode && left.allowedOrigins.join('\n') === right.allowedOrigins.join('\n');}
/** * Normalizes route shape into the canonical internal form used by later phases. * * @internal */function normalizeRouteShape(path: string): string { return path.replace(/:[^/]+/g, ':parameter').replace(/\/+/g, '/');}
/** * Compares routes using the stable ordering required by compiled gateway routing. * * @internal */function compareRoutes(left: CompiledGatewayRoute, right: CompiledGatewayRoute): number { return left.path.localeCompare(right.path) || left.method.localeCompare(right.method) || left.id.localeCompare(right.id);}
/** * Checks whether compiled service satisfies the condition required by compiled gateway routing. * * @internal */function isCompiledService(value: GatewayServiceArtifact): value is CompiledService { return (value as CompiledService).kind === 'compiled-service';}
/** * Creates one structured gateway validation issue. * * @internal */function issue( code: GatewayValidationIssue['code'], message: string, definition?: GatewayValidationSubject,): GatewayValidationIssue { return definition === undefined ? Object.freeze({ code, message }) : Object.freeze({ code, message, definition });}