Skip to content

@xtandard/audit/context

Ambient request context (ADR 0013): open a scope per request with the actor, tenant, IP, user agent and request id, and every record() inside it picks them up — no threading through call sites. Pattern borrowed from drizzle-audit’s context.

Uses node:async_hooks AsyncLocalStorage (Bun, Node, Deno, Cloudflare Workers with nodejs_compat). Framework adapters wrap this in an auditContext() middleware.

import { … } from "@xtandard/audit/context";
Name Summary
auditContextFromRequest Extract { ip, userAgent, requestId } from a web-standard Request.
getAuditContext The current scope, or null outside any scope. Never throws.
runWithAuditContext Run fn inside a scope. Nested scopes inherit and override the outer one (context keys merge).
setAuditContext Patch the current scope for the rest of the current async execution (enterWith semantics) — for middleware that cannot wrap next(), or to attach the actor once auth resolves.
Name Summary
AuditScope What a request scope carries.
RequestContextOptions Options for auditContextFromRequest.