{"version":3,"file":"session-user-CNAfvsdw.mjs","names":[],"sources":["../src/astro/session-user.ts"],"sourcesContent":["import { after } from \"../after.js\";\n\n/**\n * Backstop timeout for resolving the session user. A live session-store read\n * settles in a few ms, so this only ever fires on a genuinely stalled read.\n */\nexport const SESSION_GET_TIMEOUT_MS = 3_000;\n\n/**\n * Resolve the Astro session user without risking an isolate-wide hang.\n *\n * On Cloudflare Workers, a request cancelled mid-`session.get()` (client\n * disconnect, context teardown) can leave the underlying session-store read as\n * a promise that never settles — neither resolving nor rejecting. Awaiting it\n * directly hangs the request, and because the stalled promise is shared at the\n * isolate level, every later session-bearing request hangs too (observed as\n * 0-CPU, multi-minute, `canceled` responses; see #1274). A surrounding\n * try/catch cannot help: the promise never rejects.\n *\n * Two layers, mirroring the reclaimable-cache pattern used elsewhere in core:\n *  1. `after()` anchors the read so a cancelled request still drives it to\n *     completion — the promise settles and the isolate is not poisoned for\n *     subsequent requests (prevents the hang rather than merely surviving it).\n *  2. A timeout is a fail-closed backstop: a still-stalled (or rejecting) read\n *     resolves to `undefined`, and every caller treats the absence of a session\n *     user as unauthenticated (anonymous on public routes, 401/redirect on\n *     protected ones). It can only ever drop privileges for that one request,\n *     never grant them.\n *\n * Used by every session read on the request path: the main middleware (the\n * first read on a session-bearing request), the auth middleware, and the\n * preview-snapshot route (which bypasses the auth middleware).\n */\nexport async function resolveSessionUser<T>(\n\tsession: { get(key: \"user\"): Promise<T> } | undefined,\n\ttimeoutMs = SESSION_GET_TIMEOUT_MS,\n): Promise<T | undefined> {\n\tif (!session) return undefined;\n\tconst read: Promise<T | undefined> = Promise.resolve(session.get(\"user\")).catch(() => undefined);\n\t// Keep the worker alive past response/cancellation so the read completes and\n\t// the shared promise settles — this is what prevents the isolate poisoning.\n\tafter(() =>\n\t\tread.then(\n\t\t\t() => undefined,\n\t\t\t() => undefined,\n\t\t),\n\t);\n\tlet timer: ReturnType<typeof setTimeout> | undefined;\n\tconst timeout = new Promise<undefined>((resolve) => {\n\t\ttimer = setTimeout(resolve, timeoutMs, undefined);\n\t});\n\ttry {\n\t\treturn await Promise.race([read, timeout]);\n\t} finally {\n\t\tclearTimeout(timer);\n\t}\n}\n"],"mappings":";;;;;;;AAMA,MAAa,yBAAyB;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BtC,eAAsB,mBACrB,SACA,YAAY,wBACa;AACzB,KAAI,CAAC,QAAS,QAAO;CACrB,MAAM,OAA+B,QAAQ,QAAQ,QAAQ,IAAI,OAAO,CAAC,CAAC,YAAY,OAAU;AAGhG,aACC,KAAK,WACE,cACA,OACN,CACD;CACD,IAAI;CACJ,MAAM,UAAU,IAAI,SAAoB,YAAY;AACnD,UAAQ,WAAW,SAAS,WAAW,OAAU;GAChD;AACF,KAAI;AACH,SAAO,MAAM,QAAQ,KAAK,CAAC,MAAM,QAAQ,CAAC;WACjC;AACT,eAAa,MAAM"}