// THE READ-ONLY REQUEST (docs/contributing/architecture.md, "Viewing a World"): a request that // carries `x-volter-read-only: 1` is served with the twin's writes refused — whatever operation it // names and whatever HTTP method it came by (an RPC wire POSTs its reads). The marker only ever // RESTRICTS, so the twin trusts it without auth: a caller who sends it only limits itself. The // World's doors set it on every read-scope request and never classify operations themselves. // // Enforcement lives at the kernel's write seam: every appender (actions.ts) // asks `refuseReadOnlyWrite` before it writes anything, so the first write a read-only request // attempts is refused and nothing is partially applied. The VENDOR's own moves are not the caller's // writes: a pack's catch-up (a renewal, a payout, a file's expiry falling due by the World clock) // runs under `runAsVendorMove` and still lands under a read-only request, as a real vendor renews a // subscription whoever is looking. // // Created on first use, never at import: a browser bundle carries the kernel and // has no AsyncLocalStorage (see serve.ts identitySlot). import { AsyncLocalStorage } from 'node:async_hooks'; import { runWithTraceparent, traceparentForDelivery } from './trace-context.ts'; /** The request header that marks a request read-only. Its one value is `1`. */ export const READ_ONLY_REQUEST_HEADER = 'x-volter-read-only'; /** Whether a request carries the read-only marker. */ export function isReadOnlyRequest(request: Request | { headers: Headers }): boolean { return request.headers.get(READ_ONLY_REQUEST_HEADER)?.trim() === '1'; } /** A write a read-only request attempted: refused before anything was written (rule D3's 405). */ export class ReadOnlyRequestError extends Error { // a field, not a parameter property: Node runs this module with its types stripped (an application importing the // World clock), and stripping takes no parameter property readonly service: string; constructor(service: string) { super(`${service}: this request is read-only (${READ_ONLY_REQUEST_HEADER}: 1); it cannot write`); this.name = 'ReadOnlyRequestError'; this.service = service; } } type WriteScope = { readOnly: boolean; vendorMove: boolean; refusals: ReadOnlyRequestError[] }; let writeScopeStore: AsyncLocalStorage | undefined; const writeScope = (): AsyncLocalStorage => (writeScopeStore ??= new AsyncLocalStorage()); /** Run `fn` as a read-only request: every write it attempts outside a vendor move is refused. The * answer says whether one was — also when the handler caught the refusal and answered something * else, so a caller can answer the refusal whatever the handler made of it. */ export async function runAsReadOnlyRequest(fn: () => T | Promise): Promise<{ value: T; refused: undefined } | { refused: ReadOnlyRequestError }> { const scope: WriteScope = { readOnly: true, vendorMove: false, refusals: [] }; try { const value = await writeScope().run(scope, fn); const refused = scope.refusals[0]; return refused ? { refused } : { value, refused: undefined }; } catch (error) { const refused = scope.refusals[0] ?? (error instanceof ReadOnlyRequestError ? error : undefined); if (refused) return { refused }; throw error; } } /** Run `fn` as the VENDOR's own move (a pack's catch-up): its writes land even under a read-only * request, and are recorded as the World's own moves, never as the caller's API call (inVendorMove). */ export function runAsVendorMove(fn: () => T): T { const current = writeScope().getStore(); // the vendor's move is its own span of the request's trace (a delivery's shape): what it writes is joined to the // trace, never to the request's caller, who did not make it const own = traceparentForDelivery(); const run = own ? () => runWithTraceparent(own, fn) : fn; return writeScope().run(current ? { ...current, vendorMove: true } : { readOnly: false, vendorMove: true, refusals: [] }, run); } /** Whether this runs as the vendor's own move (runAsVendorMove). */ export function inVendorMove(): boolean { return writeScopeStore?.getStore()?.vendorMove === true; } /** Whether a write attempted here would be refused. */ export function writesRefused(): boolean { const scope = writeScopeStore?.getStore(); return scope !== undefined && scope.readOnly && !scope.vendorMove; } /** The write seam's check: throws (and records) the refusal when a write attempted here is a * read-only request's. Called by every appender before it writes anything. */ export function refuseReadOnlyWrite(service: string): void { if (!writesRefused()) return; const error = new ReadOnlyRequestError(service); writeScopeStore!.getStore()!.refusals.push(error); throw error; } /** Request-local service tenancy. Created lazily so imports remain inert. */ let tenantStore: AsyncLocalStorage> | undefined; export function storageTenant(service: string): string | undefined { return tenantStore?.getStore()?.get(service); } export function runWithStorageTenant(service: string, tenant: string | undefined, fn: () => T): T { tenantStore ??= new AsyncLocalStorage>(); const scope = new Map(tenantStore.getStore()); scope.set(service, tenant); return tenantStore.run(scope, fn); }