import { resolveRuntimeCapabilities } from "../contracts/runtime-capabilities.ts"; import { INSTALLED_RUNTIME_CAPABILITIES } from "../features/installed-runtime-capabilities.ts"; import type { TelemetryPort } from "../application/ports/telemetry-port.ts"; import { createCompositionRoot } from "./composition-root.ts"; import { loadReleaseManifest } from "./load-release-manifest.ts"; import { loadRuntimeConfig } from "./load-runtime-config.ts"; import { createOptionalRuntimeHost, type OptionalRuntimeHost, type OptionalRuntimeHostInput, } from "./optional-runtime-host.ts"; import type { ProductServiceWorkerWiring } from "./product-composition-contract.ts"; import { createRuntimeAdapters } from "./runtime-adapters.ts"; export type RuntimeCompositionDependencies = Readonly<{ fetcher?: typeof fetch; host?: Record; }>; /** * §6.7 steps 7 and 14. The static capability selection is compiled against the * runtime overrides, and the optional host objects are created without any * start side effect. Nothing observable happens until the first committed React * effect calls `optional.startAfterMount()`. */ export async function createRuntimeComposition( dependencies: RuntimeCompositionDependencies = {}, ) { // 06-05. The optional host reports a Service Worker that did not start // through the same telemetry port as the application. let telemetry: TelemetryPort | undefined; // 1103/09-05. `composeProduct` (product-owned) supplies the worker's // blockers and observer; this template file only passes them on. let productServiceWorker: ProductServiceWorkerWiring | null | undefined; const root = await createCompositionRoot({ loadConfig: () => loadRuntimeConfig({ fetcher: dependencies.fetcher }), loadRelease: (runtime) => loadReleaseManifest(runtime, { fetcher: dependencies.fetcher }), createAdapters: async ({ config: runtime, release }) => { const adapters = await createRuntimeAdapters({ runtime, release, fetcher: dependencies.fetcher, host: dependencies.host, }); telemetry = adapters.outputPorts.telemetry; productServiceWorker = adapters.productServiceWorker; return adapters; }, }); const capabilities = resolveRuntimeCapabilities( INSTALLED_RUNTIME_CAPABILITIES, root.config.config.CAPABILITY_OVERRIDES, ); const optional: OptionalRuntimeHost = createOptionalRuntimeHost({ capabilities, routerBasePath: root.config.build.routerBasePath, buildId: root.release.buildId, ...(telemetry ? { telemetry } : {}), ...serviceWorkerWiring(productServiceWorker), ...(dependencies.host ? { host: dependencies.host as Parameters< typeof createOptionalRuntimeHost >[0]["host"], } : {}), }); return Object.freeze({ ...root, capabilities, optional, async dispose(): Promise { // §20.3. Optional runtime first, then the base infrastructure: the query // cache is cleared only after realtime and worker admission has closed, // so a late effect cannot repopulate a cleared cache. await optional.stop("APPLICATION_SHUTDOWN"); root.infrastructure.dispose(); }, }); } export type RuntimeComposition = Awaited< ReturnType >; /** * 1103/09-05. The product's wiring in the optional host's terms. The blocker * list is copied once; the page controller already treats a throwing blocker * as blocking. A throwing observer is product code and must not break the * worker flow, so its throw is dropped here. */ function serviceWorkerWiring( wiring: ProductServiceWorkerWiring | null | undefined, ): Pick { if (!wiring) return {}; return { ...(wiring.blockers ? { blockers: Object.freeze([...wiring.blockers]) } : {}), ...(wiring.observe ? { observe: (observation: Readonly<{ event: string; outcome: string }>) => { try { wiring.observe?.(observation); } catch { // A failing product sink never changes the worker's outcome. } }, } : {}), }; }