import type { ProductSessionOwnerContext, ProductSessionOwnerPage, SessionOwnerFetch, } from "./product-composition-contract.ts"; import type { RuntimeConfig } from "./runtime-config-schema.ts"; /** * 1103/09-03. What the composition root hands a product session owner. The * owner is asked at boot, before `http` exists, so it talks to its session * endpoint or identity provider itself; these are the host facilities it needs * for that, each bounded or fail-closed. */ /** * `session-owner-fetch.ts` on first use. The initial bundle has no headroom * for code only an external-mode product owner runs; the chunk is the size of * a route chunk and loads the same way. */ function lazySessionOwnerFetch( options: Readonly<{ fetcher?: typeof fetch; timeoutMs: number }>, ): SessionOwnerFetch { let loaded: Promise | null = null; return (input, init) => { loaded ??= import("./session-owner-fetch.ts").then( ({ createSessionOwnerFetch }) => createSessionOwnerFetch(options), (error: unknown) => { loaded = null; throw error; }, ); return loaded.then((bounded) => bounded(input, init)); }; } function hostMember(target: unknown, name: string): unknown { if (!target || typeof target !== "object") return undefined; try { return Reflect.get(target, name); } catch { return undefined; } } /** * The page, read from the host when called. A host without `location` or * `history` (a worker, a test) throws, so an owner that needs the page fails * closed instead of signing in somewhere else. */ function hostPage(host: Record): ProductSessionOwnerPage { const unavailable = (): never => { throw new TypeError("The page is unavailable to the session owner."); }; const location = () => hostMember(host, "location"); return Object.freeze({ href() { const href = hostMember(location(), "href"); return typeof href === "string" ? href : unavailable(); }, replaceUrl(url: string) { const history = hostMember(host, "history"); const replaceState = hostMember(history, "replaceState"); if (typeof replaceState !== "function") unavailable(); // The router keeps its own entry state; replacing it would lose its index. Reflect.apply(replaceState as (...args: unknown[]) => void, history, [ hostMember(history, "state") ?? null, "", url, ]); }, assign(url: string) { const page = location(); const assign = hostMember(page, "assign"); if (typeof assign !== "function") unavailable(); Reflect.apply(assign as (url: string) => void, page, [url]); }, }); } export function createProductSessionOwnerContext( host: Record, input: Readonly<{ config: RuntimeConfig; fetcher?: typeof fetch; readCookie(name: string): string | undefined; redirectStorage?: Storage; }>, ): ProductSessionOwnerContext { return Object.freeze({ config: input.config, ...(input.fetcher === undefined ? {} : { fetcher: input.fetcher }), fetch: lazySessionOwnerFetch({ ...(input.fetcher === undefined ? {} : { fetcher: input.fetcher }), timeoutMs: input.config.REQUEST_TIMEOUT_MS, }), readCookie: input.readCookie, page: hostPage(host), ...(input.redirectStorage === undefined ? {} : { redirectStorage: input.redirectStorage }), }); }