@auditstream/node 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/next.d.mts CHANGED
@@ -1,2 +1,73 @@
1
- /** Subpath identifier exported by the workspace skeleton. */
2
- export declare const SUBPATH: "@auditstream/node/next";
1
+ import { i as AuditContext, l as NestedActor, n as ActorType, t as Actor } from "./context-DOOKYTIY.mjs";
2
+ /**
3
+ * Options for {@link withAudit}, wrapping a Next.js Route Handler.
4
+ *
5
+ * **Next.js 16 `proxy` header rule:** Next.js 16 renamed `middleware` to `proxy`. Because
6
+ * data leaves `proxy` only through headers, cookies, rewrites, redirects, or the URL,
7
+ * `proxy` cannot hold an `AsyncLocalStorage` scope for downstream Route Handlers or Server
8
+ * Actions. If `proxy` forwards identity in a request header, it **MUST overwrite or strip
9
+ * that header on every incoming request**; otherwise a client can spoof an actor header.
10
+ */
11
+ export interface NextAuditOptions<TRequest extends Request = Request> {
12
+ /**
13
+ * Resolves the actor for the incoming request.
14
+ *
15
+ * Return `undefined` to execute the Route Handler with no active Auditstream scope.
16
+ */
17
+ readonly actor: (request: TRequest) => Actor | undefined | PromiseLike<Actor | undefined>;
18
+ /**
19
+ * Optional execution context resolver for the request (for example `requestId` or `source`).
20
+ * Only called when `actor` resolves to an {@link Actor}.
21
+ */
22
+ readonly context?: (request: TRequest) => AuditContext | undefined | PromiseLike<AuditContext | undefined>;
23
+ }
24
+ /**
25
+ * Options for {@link auditAction}, wrapping a Next.js Server Action.
26
+ *
27
+ * **Next.js 16 `proxy` header rule:** `proxy` cannot propagate `AsyncLocalStorage` into
28
+ * Server Actions. If `proxy` forwards identity in a request header, it **MUST overwrite or
29
+ * strip that header on every incoming request** so clients cannot forge actor identity.
30
+ */
31
+ export interface NextActionAuditOptions<TArgs extends readonly unknown[] = readonly unknown[]> {
32
+ /**
33
+ * Resolves the actor for the Server Action invocation.
34
+ *
35
+ * Return `undefined` to execute the action with no active Auditstream scope.
36
+ */
37
+ readonly actor: (...args: TArgs) => Actor | undefined | PromiseLike<Actor | undefined>;
38
+ /**
39
+ * Optional execution context resolver for the action invocation.
40
+ * Only called when `actor` resolves to an {@link Actor}.
41
+ */
42
+ readonly context?: (...args: TArgs) => AuditContext | undefined | PromiseLike<AuditContext | undefined>;
43
+ }
44
+ /**
45
+ * Wraps a Next.js Route Handler so each request executes inside one `audit.as()` scope.
46
+ *
47
+ * Does not import `next` at runtime and works with both `NextRequest` and standard Web
48
+ * `Request` objects. Awaits the handler inside `AsyncLocalStorage.run()` so lazy thenables
49
+ * (such as Prisma queries) retain the active actor.
50
+ *
51
+ * **Next.js 16 `proxy` header rule:** `proxy` cannot carry an `AsyncLocalStorage` scope into
52
+ * Route Handlers. If `proxy` forwards identity in a request header, it MUST overwrite or
53
+ * strip that header on every request so clients cannot spoof an actor.
54
+ *
55
+ * **Cloudflare Workers / edge-like runtimes:** requires the `nodejs_als` compatibility flag
56
+ * or a compatibility date of `2026-08-04` or later (`AsyncLocalStorage.run()` only).
57
+ */
58
+ export declare function withAudit<TRequest extends Request = Request, TArgs extends readonly unknown[] = [], TResult = Response>(handler: (request: TRequest, ...args: TArgs) => TResult | PromiseLike<TResult>, options: NextAuditOptions<TRequest>): (request: TRequest, ...args: TArgs) => Promise<Awaited<TResult>>;
59
+ /**
60
+ * Wraps a Next.js Server Action so each invocation executes inside one `audit.as()` scope.
61
+ *
62
+ * Does not import `next` at runtime. Awaits the action inside `AsyncLocalStorage.run()` so
63
+ * lazy thenables (such as Prisma queries) retain the active actor.
64
+ *
65
+ * **Next.js 16 `proxy` header rule:** `proxy` cannot carry an `AsyncLocalStorage` scope into
66
+ * Server Actions. If `proxy` forwards identity in a request header, it MUST overwrite or
67
+ * strip that header on every request so clients cannot spoof an actor.
68
+ *
69
+ * **Cloudflare Workers / edge-like runtimes:** requires the `nodejs_als` compatibility flag
70
+ * or a compatibility date of `2026-08-04` or later (`AsyncLocalStorage.run()` only).
71
+ */
72
+ export declare function auditAction<TArgs extends readonly unknown[], TResult>(action: (...args: TArgs) => TResult | PromiseLike<TResult>, options: NextActionAuditOptions<TArgs>): (...args: TArgs) => Promise<Awaited<TResult>>;
73
+ export type { Actor, ActorType, AuditContext, NestedActor };
package/dist/next.mjs CHANGED
@@ -1,4 +1,46 @@
1
+ import { r as audit } from "./context-pPVY39Lw.mjs";
1
2
  // src/next.ts
2
- /** Subpath identifier exported by the workspace skeleton. */
3
- const SUBPATH = "@auditstream/node/next";
4
- export { SUBPATH };
3
+ /**
4
+ * Wraps a Next.js Route Handler so each request executes inside one `audit.as()` scope.
5
+ *
6
+ * Does not import `next` at runtime and works with both `NextRequest` and standard Web
7
+ * `Request` objects. Awaits the handler inside `AsyncLocalStorage.run()` so lazy thenables
8
+ * (such as Prisma queries) retain the active actor.
9
+ *
10
+ * **Next.js 16 `proxy` header rule:** `proxy` cannot carry an `AsyncLocalStorage` scope into
11
+ * Route Handlers. If `proxy` forwards identity in a request header, it MUST overwrite or
12
+ * strip that header on every request so clients cannot spoof an actor.
13
+ *
14
+ * **Cloudflare Workers / edge-like runtimes:** requires the `nodejs_als` compatibility flag
15
+ * or a compatibility date of `2026-08-04` or later (`AsyncLocalStorage.run()` only).
16
+ */
17
+ function withAudit(handler, options) {
18
+ return async (request, ...args) => {
19
+ const actor = await options.actor(request);
20
+ if (actor === void 0) return await handler(request, ...args);
21
+ const context = options.context ? await options.context(request) : void 0;
22
+ return await audit.as(actor, () => handler(request, ...args), context === void 0 ? void 0 : { context });
23
+ };
24
+ }
25
+ /**
26
+ * Wraps a Next.js Server Action so each invocation executes inside one `audit.as()` scope.
27
+ *
28
+ * Does not import `next` at runtime. Awaits the action inside `AsyncLocalStorage.run()` so
29
+ * lazy thenables (such as Prisma queries) retain the active actor.
30
+ *
31
+ * **Next.js 16 `proxy` header rule:** `proxy` cannot carry an `AsyncLocalStorage` scope into
32
+ * Server Actions. If `proxy` forwards identity in a request header, it MUST overwrite or
33
+ * strip that header on every request so clients cannot spoof an actor.
34
+ *
35
+ * **Cloudflare Workers / edge-like runtimes:** requires the `nodejs_als` compatibility flag
36
+ * or a compatibility date of `2026-08-04` or later (`AsyncLocalStorage.run()` only).
37
+ */
38
+ function auditAction(action, options) {
39
+ return async (...args) => {
40
+ const actor = await options.actor(...args);
41
+ if (actor === void 0) return await action(...args);
42
+ const context = options.context ? await options.context(...args) : void 0;
43
+ return await audit.as(actor, () => action(...args), context === void 0 ? void 0 : { context });
44
+ };
45
+ }
46
+ export { auditAction, withAudit };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@auditstream/node",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Actor context propagation and event helpers for Auditstream",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,9 +38,9 @@
38
38
  }
39
39
  },
40
40
  "peerDependencies": {
41
- "express": ">=4 <6",
42
- "hono": ">=4 <5",
43
- "next": ">=15 <17"
41
+ "express": "^4.21.0 || ^5.0.0",
42
+ "hono": "^4.0.0",
43
+ "next": "^16.0.0"
44
44
  },
45
45
  "peerDependenciesMeta": {
46
46
  "express": {
@@ -54,7 +54,17 @@
54
54
  }
55
55
  },
56
56
  "devDependencies": {
57
+ "@auditstream/test-utils": "0.1.0",
58
+ "@types/express": "^5.0.6",
57
59
  "@types/node": "^22.20.5",
60
+ "@types/pg": "8.23.1",
61
+ "ajv": "^8.17.1",
62
+ "ajv-formats": "^3.0.1",
63
+ "express": "^5.3.0",
64
+ "fast-check": "^4.11.0",
65
+ "hono": "4.13.20",
66
+ "next": "16.4.6",
67
+ "pg": "8.23.1",
58
68
  "tsdown": "0.23.7",
59
69
  "typescript": "~6.0.3",
60
70
  "vitest": "^5.1.1"
@@ -62,6 +72,7 @@
62
72
  "scripts": {
63
73
  "build": "tsdown",
64
74
  "typecheck": "tsc --noEmit",
65
- "test": "vitest run --config ../../vitest.config.ts"
75
+ "test": "vitest run --config ../../vitest.config.ts",
76
+ "test:integration": "vitest run --config vitest.integration.config.ts"
66
77
  }
67
78
  }