@auditstream/node 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +347 -2
- package/dist/context-DOOKYTIY.d.mts +79 -0
- package/dist/context-pPVY39Lw.mjs +141 -0
- package/dist/express.d.mts +32 -0
- package/dist/express.mjs +56 -0
- package/dist/hono.d.mts +31 -0
- package/dist/hono.mjs +24 -0
- package/dist/index.d.mts +395 -0
- package/dist/index.mjs +859 -0
- package/dist/next.d.mts +73 -0
- package/dist/next.mjs +46 -0
- package/package.json +75 -3
package/dist/hono.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Context, MiddlewareHandler } from "hono";
|
|
2
|
+
import { i as AuditContext, l as NestedActor, n as ActorType, t as Actor } from "./context-DOOKYTIY.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Options for the Hono {@link auditstream} middleware.
|
|
5
|
+
*
|
|
6
|
+
* Auditstream never parses sessions, cookies, or JWTs on its own; pass an `actor`
|
|
7
|
+
* resolver that maps the Hono `Context` to an {@link Actor} or `undefined`.
|
|
8
|
+
*/
|
|
9
|
+
export interface HonoAuditOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Resolves the actor for the incoming Hono request.
|
|
12
|
+
*
|
|
13
|
+
* Return `undefined` to run downstream middleware and route handlers with no active scope.
|
|
14
|
+
*/
|
|
15
|
+
readonly actor: (c: Context) => Actor | undefined | PromiseLike<Actor | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Optional execution context resolver for the request (for example `requestId` or `source`).
|
|
18
|
+
* Only called when `actor` resolves to an {@link Actor}.
|
|
19
|
+
*/
|
|
20
|
+
readonly context?: (c: Context) => AuditContext | undefined | PromiseLike<AuditContext | undefined>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates Hono middleware that opens one `audit.as()` actor scope per request and
|
|
24
|
+
* awaits `next()` inside the scope so the actor remains active until downstream handlers settle.
|
|
25
|
+
*
|
|
26
|
+
* **Cloudflare Workers:** requires the `nodejs_als` compatibility flag, or a compatibility
|
|
27
|
+
* date of `2026-08-04` or later. `@auditstream/node` uses `AsyncLocalStorage.run()` only
|
|
28
|
+
* (never `enterWith()`), so it runs cleanly on Node.js, Bun, Deno, and Cloudflare Workers.
|
|
29
|
+
*/
|
|
30
|
+
export declare function auditstream(options: HonoAuditOptions): MiddlewareHandler;
|
|
31
|
+
export type { Actor, ActorType, AuditContext, NestedActor };
|
package/dist/hono.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { r as audit } from "./context-pPVY39Lw.mjs";
|
|
2
|
+
// src/hono.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates Hono middleware that opens one `audit.as()` actor scope per request and
|
|
5
|
+
* awaits `next()` inside the scope so the actor remains active until downstream handlers settle.
|
|
6
|
+
*
|
|
7
|
+
* **Cloudflare Workers:** requires the `nodejs_als` compatibility flag, or a compatibility
|
|
8
|
+
* date of `2026-08-04` or later. `@auditstream/node` uses `AsyncLocalStorage.run()` only
|
|
9
|
+
* (never `enterWith()`), so it runs cleanly on Node.js, Bun, Deno, and Cloudflare Workers.
|
|
10
|
+
*/
|
|
11
|
+
function auditstream(options) {
|
|
12
|
+
return async (c, next) => {
|
|
13
|
+
const actor = await options.actor(c);
|
|
14
|
+
if (actor === void 0) {
|
|
15
|
+
await next();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const context = options.context ? await options.context(c) : void 0;
|
|
19
|
+
await audit.as(actor, async () => {
|
|
20
|
+
await next();
|
|
21
|
+
}, context === void 0 ? void 0 : { context });
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export { auditstream };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import { a as AuditScope, c as MAX_PAYLOAD_BYTES, i as AuditContext, l as NestedActor, n as ActorType, o as AuditstreamError, r as AuditAsOptions, s as AuditstreamErrorCode, t as Actor } from "./context-DOOKYTIY.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Maximum character length of an unpadded base64url actor payload in a trailing SQL comment
|
|
4
|
+
* (`ceil(512 * 4 / 3) = 683` per Auditstream Specification 0.1.0).
|
|
5
|
+
*/
|
|
6
|
+
export declare const MAX_ENCODED_COMMENT_CHARS: 683;
|
|
7
|
+
/**
|
|
8
|
+
* Mechanism that produced a decoded actor payload (`spec/transport.md`).
|
|
9
|
+
*/
|
|
10
|
+
type ActorSource = "set_config" | "comment" | "jwt" | "invalid";
|
|
11
|
+
/**
|
|
12
|
+
* Result returned by {@link decodeScope} when inspecting a SQL statement's trailing comment.
|
|
13
|
+
*/
|
|
14
|
+
interface DecodedTransport {
|
|
15
|
+
/** Decoded actor object, or `null` when absent or malformed. */
|
|
16
|
+
readonly actor: Record<string, unknown> | null;
|
|
17
|
+
/** Decoded execution context object, or `null` when absent or malformed. */
|
|
18
|
+
readonly context: Record<string, unknown> | null;
|
|
19
|
+
/** Transport source (`"comment"`, `"invalid"`, or `null` when no `auditstream` tag is present). */
|
|
20
|
+
readonly actorSource: "comment" | "invalid" | null;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Validates `scope` against Auditstream Specification 0.1.0, serializes it into canonical
|
|
24
|
+
* compact UTF-8 JSON (key order `type`, `id`, `onBehalfOf`, `tenant`, `context`), and
|
|
25
|
+
* returns the RFC 4648 unpadded base64url representation suitable for `/*auditstream='...'*\/`.
|
|
26
|
+
*
|
|
27
|
+
* Uses `TextEncoder` and a self-contained base64url encoder with no `Buffer` dependency so
|
|
28
|
+
* it runs cleanly on Node.js, Bun, Deno, and Cloudflare Workers (`nodejs_als` only).
|
|
29
|
+
*
|
|
30
|
+
* @throws {@link AuditstreamError} if the actor/context is invalid or exceeds 512 UTF-8 bytes.
|
|
31
|
+
*/
|
|
32
|
+
export declare function encodeScope(scope: AuditScope): string;
|
|
33
|
+
/**
|
|
34
|
+
* Validates `scope` against Auditstream Specification 0.1.0 and returns the canonical
|
|
35
|
+
* compact UTF-8 JSON string for `SELECT set_config('auditstream.actor', $1, true)`.
|
|
36
|
+
*
|
|
37
|
+
* @throws {@link AuditstreamError} if the actor/context is invalid or exceeds 512 UTF-8 bytes.
|
|
38
|
+
*/
|
|
39
|
+
export declare function setConfigValue(scope: AuditScope): string;
|
|
40
|
+
/**
|
|
41
|
+
* Returns `true` if `sql` already ends with a trailing comment containing an `auditstream`
|
|
42
|
+
* sqlcommenter key (with optional trailing ASCII whitespace or semicolons).
|
|
43
|
+
*/
|
|
44
|
+
export declare function isTagged(sql: string): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Appends ` /*auditstream='<base64url>'*\/` to the end of `sql` for the active or supplied
|
|
47
|
+
* {@link AuditScope}.
|
|
48
|
+
*
|
|
49
|
+
* - Defaults `scope` to `audit.current()`.
|
|
50
|
+
* - Returns `sql` unchanged when no scope is active or when `sql` is already tagged.
|
|
51
|
+
* - Never tags a statement twice; appends after any existing SQL comments.
|
|
52
|
+
* - Throws {@link AuditstreamError} before building any SQL text when the scope is invalid
|
|
53
|
+
* or exceeds 512 UTF-8 bytes.
|
|
54
|
+
*/
|
|
55
|
+
export declare function tagSql(sql: string, scope?: AuditScope | undefined): string;
|
|
56
|
+
/**
|
|
57
|
+
* Decodes a trailing `/*auditstream='...'*\/` SQL comment from `sql` following the exact
|
|
58
|
+
* extraction and validation rules in `spec/transport.md`.
|
|
59
|
+
*
|
|
60
|
+
* - Returns `{ actor: null, context: null, actorSource: null }` when no trailing comment
|
|
61
|
+
* or no `auditstream` key is present.
|
|
62
|
+
* - Returns `{ actor: null, context: null, actorSource: "invalid" }` when an `auditstream`
|
|
63
|
+
* key is present but malformed, oversized (over 512 UTF-8 bytes or 683 chars), padded,
|
|
64
|
+
* invalid UTF-8, non-JSON, or violates the `spec/actor.md` actor/context schema
|
|
65
|
+
* (including the 5-value `type` enum, non-empty `id`/`tenant`, depth-1 `onBehalfOf`,
|
|
66
|
+
* and object `context` with non-empty known fields).
|
|
67
|
+
* - Preserves unknown additive keys in `actor`, `onBehalfOf`, and `context`.
|
|
68
|
+
* - Never throws on malformed input.
|
|
69
|
+
*/
|
|
70
|
+
export declare function decodeScope(sql: string): DecodedTransport;
|
|
71
|
+
/**
|
|
72
|
+
* Auditstream history schema version supported by Specification 0.1.0.
|
|
73
|
+
*/
|
|
74
|
+
export declare const SUPPORTED_SCHEMA_VERSION: 1;
|
|
75
|
+
/**
|
|
76
|
+
* Default row limit applied by `audit.history()` when `limit` is omitted.
|
|
77
|
+
*/
|
|
78
|
+
export declare const DEFAULT_HISTORY_LIMIT: 50;
|
|
79
|
+
/**
|
|
80
|
+
* Maximum row limit accepted by `audit.history()`.
|
|
81
|
+
*/
|
|
82
|
+
export declare const MAX_HISTORY_LIMIT: 1000;
|
|
83
|
+
/**
|
|
84
|
+
* Origin of a recorded audit event (`"db"` for trigger capture, `"app"` for `auditstream.log_event()`).
|
|
85
|
+
*/
|
|
86
|
+
type EventSourceKind = "db" | "app";
|
|
87
|
+
/**
|
|
88
|
+
* Database DML operations recorded by capture triggers.
|
|
89
|
+
*/
|
|
90
|
+
type DbOp = "INSERT" | "UPDATE" | "DELETE" | "TRUNCATE";
|
|
91
|
+
/**
|
|
92
|
+
* Recorded event operation: one of the database DML operations (`INSERT`, `UPDATE`, `DELETE`,
|
|
93
|
+
* `TRUNCATE`) or an application action name from `auditstream.log_event()`.
|
|
94
|
+
*/
|
|
95
|
+
type AuditOp = DbOp | (string & {});
|
|
96
|
+
/**
|
|
97
|
+
* Primitive JSON value preserved inside row snapshots and `record_id`.
|
|
98
|
+
*
|
|
99
|
+
* Arbitrary-precision PostgreSQL `numeric` values that cannot be represented exactly as an
|
|
100
|
+
* IEEE-754 `number` are preserved as verbatim decimal strings by {@link parseExportRecord}.
|
|
101
|
+
*/
|
|
102
|
+
type JsonPrimitive = string | number | boolean | null;
|
|
103
|
+
/**
|
|
104
|
+
* Recursive JSON value used inside history row snapshots (`record_id`, `before`, `after`).
|
|
105
|
+
*/
|
|
106
|
+
type JsonValue = JsonPrimitive | readonly JsonValue[] | {
|
|
107
|
+
readonly [key: string]: JsonValue;
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* JSON object mapping column or field names to JSON values.
|
|
111
|
+
*/
|
|
112
|
+
type JsonObject = {
|
|
113
|
+
readonly [key: string]: JsonValue;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Single row from `auditstream.changes` matching Auditstream Specification 0.1.0
|
|
117
|
+
* (`spec/history-table.md`).
|
|
118
|
+
*
|
|
119
|
+
* `id` (`bigint`) and `xact_id` (`xid8`) are typed as strings because JavaScript database
|
|
120
|
+
* drivers return 64-bit PostgreSQL integers as strings to avoid IEEE-754 precision loss.
|
|
121
|
+
*/
|
|
122
|
+
interface HistoryEntry {
|
|
123
|
+
/** Monotonic insert sequence (`bigint` returned as a decimal string; insert order, not commit order). */
|
|
124
|
+
readonly id: string;
|
|
125
|
+
/** Stable UUID identifying the event for downstream deduplication. */
|
|
126
|
+
readonly event_id: string;
|
|
127
|
+
/** RFC 3339 UTC timestamp with microsecond precision when the row was recorded. */
|
|
128
|
+
readonly occurred_at: string;
|
|
129
|
+
/** 64-bit PostgreSQL transaction ID (`xid8` returned as a decimal string). */
|
|
130
|
+
readonly xact_id: string;
|
|
131
|
+
/** Origin of the event (`"db"` or `"app"`). */
|
|
132
|
+
readonly source: EventSourceKind;
|
|
133
|
+
/** Audited table schema for `"db"` events, or `null` for `"app"` events. */
|
|
134
|
+
readonly schema_name: string | null;
|
|
135
|
+
/** Audited table name for `"db"` events, or `null` for `"app"` events. */
|
|
136
|
+
readonly table_name: string | null;
|
|
137
|
+
/** Audited table OID (`TG_RELID`) for `"db"` events, or `null` for `"app"` events. */
|
|
138
|
+
readonly table_oid: number | null;
|
|
139
|
+
/** DML operation (`INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`) or application action name. */
|
|
140
|
+
readonly op: AuditOp;
|
|
141
|
+
/** Primary key JSON object (or `target` for `"app"` events), or `null`. */
|
|
142
|
+
readonly record_id: JsonObject | null;
|
|
143
|
+
/** Pre-change row image minus excluded columns (`UPDATE` and `DELETE` only), or `null`. */
|
|
144
|
+
readonly before: JsonObject | null;
|
|
145
|
+
/** Post-change row image minus excluded columns (or `data` for `"app"` events), or `null`. */
|
|
146
|
+
readonly after: JsonObject | null;
|
|
147
|
+
/** Changed column names on `UPDATE`, or `null` otherwise. */
|
|
148
|
+
readonly changed: readonly string[] | null;
|
|
149
|
+
/** Resolved actor payload (without `context`), or `null` when absent or malformed. */
|
|
150
|
+
readonly actor: Actor | null;
|
|
151
|
+
/** Resolved execution context payload, or `null` when absent or malformed. */
|
|
152
|
+
readonly context: AuditContext | null;
|
|
153
|
+
/** Transport mechanism that supplied the actor (`set_config`, `comment`, `jwt`, `invalid`), or `null`. */
|
|
154
|
+
readonly actor_source: ActorSource | null;
|
|
155
|
+
/** Database session role (`session_user`) that initiated the write. */
|
|
156
|
+
readonly db_user: string;
|
|
157
|
+
/** History table layout version (`1` in Specification 0.1.0). */
|
|
158
|
+
readonly schema_version: number;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Single exported audit event record matching Auditstream Specification 0.1.0
|
|
162
|
+
* (`spec/export.md` and `spec/schemas/event.schema.json`).
|
|
163
|
+
*
|
|
164
|
+
* Internal history columns (`id`, `xact_id`, and `table_oid`) are intentionally omitted.
|
|
165
|
+
*/
|
|
166
|
+
interface ExportRecord {
|
|
167
|
+
/** Stable UUID identifying the event for downstream deduplication. */
|
|
168
|
+
readonly event_id: string;
|
|
169
|
+
/** RFC 3339 UTC timestamp with microsecond precision. */
|
|
170
|
+
readonly occurred_at: string;
|
|
171
|
+
/** Origin of the event (`"db"` or `"app"`). */
|
|
172
|
+
readonly source: EventSourceKind;
|
|
173
|
+
/** Audited table schema for `"db"` events, or `null` for `"app"` events. */
|
|
174
|
+
readonly schema_name: string | null;
|
|
175
|
+
/** Audited table name for `"db"` events, or `null` for `"app"` events. */
|
|
176
|
+
readonly table_name: string | null;
|
|
177
|
+
/** DML operation (`INSERT`, `UPDATE`, `DELETE`, `TRUNCATE`) or application action name. */
|
|
178
|
+
readonly op: AuditOp;
|
|
179
|
+
/** Primary key JSON object (or `target` for `"app"` events), or `null`. */
|
|
180
|
+
readonly record_id: JsonObject | null;
|
|
181
|
+
/** Pre-change row image minus excluded columns (`UPDATE` and `DELETE` only), or `null`. */
|
|
182
|
+
readonly before: JsonObject | null;
|
|
183
|
+
/** Post-change row image minus excluded columns (or `data` for `"app"` events), or `null`. */
|
|
184
|
+
readonly after: JsonObject | null;
|
|
185
|
+
/** Changed column names on `UPDATE`, or `null` otherwise. */
|
|
186
|
+
readonly changed: readonly string[] | null;
|
|
187
|
+
/** Resolved actor payload (without `context`), or `null` when absent or malformed. */
|
|
188
|
+
readonly actor: Actor | null;
|
|
189
|
+
/** Resolved execution context payload, or `null` when absent or malformed. */
|
|
190
|
+
readonly context: AuditContext | null;
|
|
191
|
+
/** Transport mechanism that supplied the actor (`set_config`, `comment`, `jwt`, `invalid`), or `null`. */
|
|
192
|
+
readonly actor_source: ActorSource | null;
|
|
193
|
+
/** Database session role (`session_user`) that initiated the write. */
|
|
194
|
+
readonly db_user: string;
|
|
195
|
+
/** Export schema version (`1` in Specification 0.1.0). */
|
|
196
|
+
readonly schema_version: number;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Ordered parameter tuple passed to an {@link AuditHistoryExecutor}:
|
|
200
|
+
* `[tableName, recordIdJson, limit]`.
|
|
201
|
+
*/
|
|
202
|
+
type AuditHistoryValues = [table: string, recordIdJson: string, limit: number];
|
|
203
|
+
/**
|
|
204
|
+
* Result shape accepted from an {@link AuditHistoryExecutor} (either a `pg`-style `{ rows }`
|
|
205
|
+
* object or a direct row array such as `postgres.js` or `@neondatabase/serverless`).
|
|
206
|
+
*/
|
|
207
|
+
type AuditHistoryQueryResult = readonly Record<string, unknown>[] | {
|
|
208
|
+
readonly rows: readonly Record<string, unknown>[];
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* Callback that executes one parameterized `SELECT` against the history table using the
|
|
212
|
+
* application's own database connection (which holds `SELECT` on `auditstream.changes`).
|
|
213
|
+
*/
|
|
214
|
+
type AuditHistoryExecutor = (text: string, values: AuditHistoryValues) => AuditHistoryQueryResult | PromiseLike<AuditHistoryQueryResult>;
|
|
215
|
+
/**
|
|
216
|
+
* Query options accepted by `audit.history()`.
|
|
217
|
+
*/
|
|
218
|
+
interface AuditHistoryQuery {
|
|
219
|
+
/** Audited table name (`table_name`). */
|
|
220
|
+
readonly table: string;
|
|
221
|
+
/** Primary key JSON object matching `record_id = $2::jsonb` (for example `{ id: 101 }`). */
|
|
222
|
+
readonly recordId: JsonObject;
|
|
223
|
+
/**
|
|
224
|
+
* Audit schema name (defaults to `"auditstream"`).
|
|
225
|
+
* Must match `^[a-z_][a-z0-9_]*$`; invalid schema names throw {@link AuditstreamError}
|
|
226
|
+
* before any SQL is sent.
|
|
227
|
+
*/
|
|
228
|
+
readonly schema?: string | undefined;
|
|
229
|
+
/** Maximum number of rows to return (default `50`, maximum `1000`). */
|
|
230
|
+
readonly limit?: number | undefined;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Parses and validates a single JSON Lines export record against Auditstream Specification 0.1.0
|
|
234
|
+
* (`spec/export.md` and `spec/schemas/event.schema.json`).
|
|
235
|
+
*
|
|
236
|
+
* - Validates every required field (`event_id`, `occurred_at`, `source`, `op`, `db_user`,
|
|
237
|
+
* `schema_version`) and requires `schema_version === 1`. Throws {@link AuditstreamError}
|
|
238
|
+
* (`INVALID_RECORD`) on malformed JSON, missing fields, invalid types, or unsupported
|
|
239
|
+
* `schema_version`.
|
|
240
|
+
* - Ignores unknown additive fields for forward compatibility.
|
|
241
|
+
* - **Arbitrary-precision `numeric` preservation:** uses ES2025 `JSON.parse` reviver source text
|
|
242
|
+
* access (`context.source`) so PostgreSQL `numeric` values that would lose precision in
|
|
243
|
+
* IEEE-754 binary64 (such as 30-digit decimals or integers beyond `Number.MAX_SAFE_INTEGER`)
|
|
244
|
+
* are returned verbatim as strings rather than rounded doubles.
|
|
245
|
+
* - **Runtime support for `context.source`:** supported on Node.js 22.12+ (all supported Node
|
|
246
|
+
* versions), Deno 2+, Bun 1.2+, and Cloudflare Workers (V8 12.4+). Runtimes without
|
|
247
|
+
* `JSON.parse` source text access fall back to standard JavaScript `number` values.
|
|
248
|
+
*/
|
|
249
|
+
export declare function parseExportRecord(line: string): ExportRecord;
|
|
250
|
+
/**
|
|
251
|
+
* Maximum Unicode code point length of an application event `action` per Auditstream Specification 0.1.0.
|
|
252
|
+
*/
|
|
253
|
+
export declare const MAX_ACTION_LENGTH: 64;
|
|
254
|
+
/**
|
|
255
|
+
* Parameterized SQL statement executed by `audit.log()` to record an application event through
|
|
256
|
+
* `auditstream.log_event(text, jsonb, jsonb)`.
|
|
257
|
+
*/
|
|
258
|
+
export declare const LOG_EVENT_SQL: "SELECT auditstream.log_event($1::text, $2::jsonb, $3::jsonb)";
|
|
259
|
+
/**
|
|
260
|
+
* Ordered parameter tuple passed to an {@link AuditLogExecutor}:
|
|
261
|
+
* `[action, targetJsonOrNull, dataJsonOrNull]`.
|
|
262
|
+
*/
|
|
263
|
+
type AuditLogValues = [action: string, target: string | null, data: string | null];
|
|
264
|
+
/**
|
|
265
|
+
* Callback that executes one parameterized SQL statement on the caller's audited database client.
|
|
266
|
+
*/
|
|
267
|
+
type AuditLogExecutor = (text: string, values: AuditLogValues) => unknown | PromiseLike<unknown>;
|
|
268
|
+
/**
|
|
269
|
+
* Application-level audit event payload accepted by `audit.log()`.
|
|
270
|
+
*/
|
|
271
|
+
interface AuditLogEvent {
|
|
272
|
+
/**
|
|
273
|
+
* Application action name written to `auditstream.changes.op`.
|
|
274
|
+
* Must have trimmed length between 1 and 64 characters and must not be
|
|
275
|
+
* `INSERT`, `UPDATE`, `DELETE`, or `TRUNCATE` (case-insensitive).
|
|
276
|
+
*/
|
|
277
|
+
readonly action: string;
|
|
278
|
+
/** Optional JSON object written to `auditstream.changes.record_id` (`NULL` when omitted or `null`). */
|
|
279
|
+
readonly target?: Record<string, unknown> | null | undefined;
|
|
280
|
+
/** Optional JSON object written to `auditstream.changes.after` (`NULL` when omitted or `null`). */
|
|
281
|
+
readonly data?: Record<string, unknown> | null | undefined;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Maximum number of `op` entries accepted in a single `audit.query()` `op` array filter.
|
|
285
|
+
*/
|
|
286
|
+
export declare const MAX_QUERY_OPS: 20;
|
|
287
|
+
/**
|
|
288
|
+
* Mutable primitive-only parameter array passed to an {@link AuditQueryExecutor}.
|
|
289
|
+
*
|
|
290
|
+
* Compatible with `pg` (`pool.query(text, values)`), `postgres.js` (`sql.unsafe(text, values)`),
|
|
291
|
+
* `@neondatabase/serverless` (`sql.query(text, values)`), and Prisma
|
|
292
|
+
* (`prisma.$queryRawUnsafe(text, ...values)`).
|
|
293
|
+
*/
|
|
294
|
+
type AuditQueryValues = (string | number)[];
|
|
295
|
+
/**
|
|
296
|
+
* Callback that executes one parameterized `SELECT` against `{schema}.changes` using the
|
|
297
|
+
* application's own database connection (which holds `SELECT` on `auditstream.changes`).
|
|
298
|
+
*/
|
|
299
|
+
type AuditQueryExecutor = (text: string, values: AuditQueryValues) => AuditHistoryQueryResult | PromiseLike<AuditHistoryQueryResult>;
|
|
300
|
+
/**
|
|
301
|
+
* Closed filter and keyset-pagination options accepted by `audit.query()`.
|
|
302
|
+
*
|
|
303
|
+
* At least one bounding filter (`recordId`, `since`, or `until`) is required unless
|
|
304
|
+
* `unbounded: true` is passed explicitly.
|
|
305
|
+
*/
|
|
306
|
+
interface AuditQuery {
|
|
307
|
+
/** Filter by audited table name (`table_name = $N::text`). */
|
|
308
|
+
readonly table?: string | undefined;
|
|
309
|
+
/** Filter by audited schema name (`schema_name = $N::text`). */
|
|
310
|
+
readonly schemaName?: string | undefined;
|
|
311
|
+
/** Filter by exact primary key or application event target (`record_id = $N::jsonb`). */
|
|
312
|
+
readonly recordId?: JsonObject | undefined;
|
|
313
|
+
/** Filter by actor identifier matching `actor->>'id'` or `actor->'onBehalfOf'->>'id'`. */
|
|
314
|
+
readonly actor?: string | undefined;
|
|
315
|
+
/** Filter by tenant identifier matching `actor->>'tenant'` or `actor->'onBehalfOf'->>'tenant'`. */
|
|
316
|
+
readonly tenant?: string | undefined;
|
|
317
|
+
/** Filter by event origin (`"db"` for triggers, `"app"` for `audit.log()`). */
|
|
318
|
+
readonly source?: EventSourceKind | undefined;
|
|
319
|
+
/** Filter by a single operation or up to 20 operations (`op = $N::text` or `op IN (...)`). */
|
|
320
|
+
readonly op?: AuditOp | readonly AuditOp[] | undefined;
|
|
321
|
+
/** Filter by operation prefix using `pg_catalog.starts_with(op, $N::text)` (never `LIKE`). */
|
|
322
|
+
readonly opPrefix?: string | undefined;
|
|
323
|
+
/** Inclusive lower bound on `occurred_at` (`occurred_at >= $N::timestamptz`). */
|
|
324
|
+
readonly since?: Date | string | undefined;
|
|
325
|
+
/** Exclusive upper bound on `occurred_at` (`occurred_at < $N::timestamptz`). */
|
|
326
|
+
readonly until?: Date | string | undefined;
|
|
327
|
+
/**
|
|
328
|
+
* Audit schema name (defaults to `"auditstream"`).
|
|
329
|
+
* Must match `^[a-z_][a-z0-9_]*$`; invalid schema names throw {@link AuditstreamError}
|
|
330
|
+
* before any SQL is sent.
|
|
331
|
+
*/
|
|
332
|
+
readonly schema?: string | undefined;
|
|
333
|
+
/** Maximum number of rows to return per page (`1..=1000`, default `50`). */
|
|
334
|
+
readonly limit?: number | undefined;
|
|
335
|
+
/** Opaque keyset pagination cursor returned as `nextCursor` by a previous page. */
|
|
336
|
+
readonly cursor?: string | undefined;
|
|
337
|
+
/**
|
|
338
|
+
* Explicitly allow a query with none of `recordId`, `since`, or `until`.
|
|
339
|
+
* Defaults to `false` to prevent accidental unbounded cross-partition scans.
|
|
340
|
+
*/
|
|
341
|
+
readonly unbounded?: boolean | undefined;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Keyset-paginated page returned by `audit.query()`.
|
|
345
|
+
*/
|
|
346
|
+
interface AuditQueryPage {
|
|
347
|
+
/** Matching history rows for this page, ordered newest first (`occurred_at DESC, id DESC`). */
|
|
348
|
+
readonly entries: readonly HistoryEntry[];
|
|
349
|
+
/** Opaque cursor to pass as `query.cursor` for the next page, or `null` when no more rows exist. */
|
|
350
|
+
readonly nextCursor: string | null;
|
|
351
|
+
/** `true` when at least one older matching row exists beyond this page. */
|
|
352
|
+
readonly hasMore: boolean;
|
|
353
|
+
}
|
|
354
|
+
/** Package identifier exported by the workspace skeleton. */
|
|
355
|
+
export declare const PACKAGE_NAME: "@auditstream/node";
|
|
356
|
+
/**
|
|
357
|
+
* Public `audit` namespace owning actor context propagation (`audit.as()`, `audit.current()`),
|
|
358
|
+
* application event logging (`audit.log()`), and typed record history lookups (`audit.history()`).
|
|
359
|
+
*/
|
|
360
|
+
export interface AuditNamespace {
|
|
361
|
+
/**
|
|
362
|
+
* Validates `actor` and optional `options.context`, runs `fn` inside an isolated
|
|
363
|
+
* `AsyncLocalStorage` scope, and awaits the result inside the scope so lazy thenables
|
|
364
|
+
* (such as Prisma queries) retain the actor.
|
|
365
|
+
*/
|
|
366
|
+
as<T>(actor: Actor, fn: () => T | PromiseLike<T>, options?: AuditAsOptions): Promise<Awaited<T>>;
|
|
367
|
+
/**
|
|
368
|
+
* Returns the active `{ actor, context }` scope, or `undefined` outside any `audit.as()` call.
|
|
369
|
+
*/
|
|
370
|
+
current(): AuditScope | undefined;
|
|
371
|
+
/**
|
|
372
|
+
* Records an application-level audit event through `auditstream.log_event($1::text, $2::jsonb, $3::jsonb)`
|
|
373
|
+
* using the caller's audited database client (`exec`).
|
|
374
|
+
*
|
|
375
|
+
* Validates the event against Specification 0.1.0 before calling `exec`, runs `exec` inside the
|
|
376
|
+
* caller's active `audit.as()` scope, propagates errors from `exec` unchanged, and resolves to `undefined`.
|
|
377
|
+
*/
|
|
378
|
+
log(exec: AuditLogExecutor, event: AuditLogEvent): Promise<void>;
|
|
379
|
+
/**
|
|
380
|
+
* Reads recorded audit history for a single record from `{schema}.changes`, ordered newest first
|
|
381
|
+
* (`occurred_at DESC, id DESC`), using the caller's database connection (`exec`).
|
|
382
|
+
*/
|
|
383
|
+
history(exec: AuditHistoryExecutor, query: AuditHistoryQuery): Promise<HistoryEntry[]>;
|
|
384
|
+
/**
|
|
385
|
+
* Queries `{schema}.changes` (including application events logged with `audit.log()`) using
|
|
386
|
+
* validated filters and lossless microsecond keyset pagination (`occurred_at DESC, id DESC`).
|
|
387
|
+
*/
|
|
388
|
+
query(exec: AuditQueryExecutor, query: AuditQuery): Promise<AuditQueryPage>;
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Shared Auditstream runtime entry point providing `audit.as()`, `audit.current()`,
|
|
392
|
+
* `audit.log()`, `audit.history()`, and `audit.query()`.
|
|
393
|
+
*/
|
|
394
|
+
export declare const audit: AuditNamespace;
|
|
395
|
+
export { type Actor, type ActorSource, type ActorType, type AuditAsOptions, type AuditContext, type AuditHistoryExecutor, type AuditHistoryQuery, type AuditHistoryQueryResult, type AuditHistoryValues, type AuditLogEvent, type AuditLogExecutor, type AuditLogValues, type AuditOp, type AuditQuery, type AuditQueryExecutor, type AuditQueryPage, type AuditQueryValues, type AuditScope, AuditstreamError, type AuditstreamErrorCode, type DbOp, type DecodedTransport, type EventSourceKind, type ExportRecord, type HistoryEntry, type JsonObject, type JsonPrimitive, type JsonValue, MAX_PAYLOAD_BYTES, type NestedActor };
|