/** * Server-side rendering context. * * Encapsulates everything a render path may need to know about the incoming * request and to produce response-side metadata (head tags, asset hints, * nonces, etc.). The context is propagated explicitly through the public * async APIs (`renderToStringAsync`, `renderToStream`, `renderToResponse`) * and is available to template loaders. * * @module bquery/ssr */ import { generateNonce } from '../security/csp'; import { isPrototypePollutionKey } from '../core/utils/object'; import { createAssetManager, createHeadManager, type AssetManager, type HeadManager } from './head'; /** Options for `createSSRContext()`. */ export interface CreateSSRContextOptions { /** Pre-built request to use as the source of URL/headers/cookies/etc. */ request?: Request; /** Override the URL (defaults to `request.url` or `'http://localhost/'`). */ url?: string | URL; /** Override the user agent string. */ userAgent?: string; /** Override the request locale; defaults to `Accept-Language` parsing. */ locale?: string; /** Provide an `AbortSignal` for cancellation. Default: `request.signal`. */ signal?: AbortSignal; /** Pre-computed CSP nonce. If omitted and `crypto.getRandomValues` exists, a fresh nonce is generated. */ nonce?: string; /** Render mode hint — used by streaming/string renderers for diagnostics. */ mode?: 'string' | 'stream'; /** Optional error sink invoked for non-fatal render errors. */ onError?: (error: unknown) => void; } /** Public SSR context shape. */ export interface SSRContext { /** The originating `Request` (may be a synthetic one if none was provided). */ request: Request; /** Parsed URL of the request. */ url: URL; /** Request headers (via `Request.headers`). */ headers: Headers; /** Resolved cookie map from `Cookie` header. */ cookies: Record; /** Best-effort user agent string. */ userAgent: string; /** Best-effort locale parsed from `Accept-Language`. */ locale: string; /** Cancellation signal — render paths must respect it. */ signal: AbortSignal; /** CSP nonce applied to all generated `