문제: composeProductSessionOwner는 config와 raw fetcher만 받는다. owner는 http보다 먼저 만들어져 세션 조회·토큰 호출을 직접 해야 하는데, 응답하지 않는 호출 하나가 세션을 recovery-pending에 영원히 둔다. 픽스처의 쿠키 owner 스케치가 바로 그렇게 쓴다. 로그인 리다이렉트에 필요한 페이지 (location·history)와 sessionStorage도 bootstrap lint가 전역 접근을 막아 제품 파일에서 얻을 길이 없다. 고른 방법: ProductSessionOwnerContext에 넷을 더한다. - fetch: 헤더와 본문 전체에 마감 하나(REQUEST_TIMEOUT_MS가 상한, 호출이 줄일 수만 있다). 본문은 64 KiB까지 메모리로 읽어 새 Response로 준다. redirect는 항상 error. fetcher가 signal을 무시해도 경합으로 끝난다. - readCookie: composeProduct와 같은 host cookie 읽기. - page: href·replaceUrl·assign. host에 없으면 throw로 닫힌다. replaceUrl은 라우터의 history state를 그대로 둔다. - redirectStorage: host sessionStorage. 토큰이 아니라 리다이렉트 한 번 동안의 상태만 둔다고 계약에 적었다. 조립은 새 파일 product-session-owner-context.ts에 두고 runtime-adapters.ts는 호출 한 곳만 바꿨다. fetch 구현 (session-owner-fetch.ts)은 처음 쓸 때 동적 import로 읽는다. 초기 번들 여유가 3 KB 남짓이라 demo 모드나 owner 없는 제품이 이 코드를 내려받지 않게 했다. 버린 대안: - platform 커널 createAbortableOperation 사용. 커널이 boot가 이미 읽는 배럴과 같은 청크로 묶여 지연 청크로 빼도 초기 번들에 남았다(빌드로 확인). 그래서 read-bounded-boot-json.ts와 같은 마감 방식을 쓴다. - 마감만 주고 본문은 owner가 읽게 두기. 본문이 멈추면 다시 무한 대기다. 위험·한계: fetch는 필수 필드라 ProductSessionOwnerContext를 직접 만드는 제품 테스트는 컴파일이 깨진다. 기존 fetcher는 그대로 둔다. 지연 청크 적재 시간은 마감에 들어가지 않는다(라우트 청크와 같다). 검증: tests/unit/product-session-owner-context.test.ts 12건(구현 전 모듈 없음으로 실패 확인), product-composition-fixture·runtime-adapters 테스트 통과, check:types, eslint 통과. build + check:bundle: initial JS 202,242 / 204,800 B gzip(09-11 포함, 기준 201,848), 지연 청크 session-owner-fetch 0.81 kB gzip. Refs: docs/reviews/2026-11-03-maturity-review/09-app-core.md 09-03
103 lines
3.4 KiB
TypeScript
103 lines
3.4 KiB
TypeScript
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<SessionOwnerFetch> | 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<string, unknown>): 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<string, unknown>,
|
|
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 }),
|
|
});
|
|
}
|