ezlogs-nextjs 0.1.4 → 0.3.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.
Files changed (40) hide show
  1. package/README.md +106 -11
  2. package/bin/install-triggers.cjs +49 -11
  3. package/dist/actors/index.d.cts +1 -1
  4. package/dist/actors/index.d.ts +1 -1
  5. package/dist/{configuration-3Pei_XZ_.d.cts → configuration-OHLNivON.d.cts} +11 -20
  6. package/dist/{configuration-3Pei_XZ_.d.ts → configuration-OHLNivON.d.ts} +11 -20
  7. package/dist/db/drizzle.cjs +154 -18
  8. package/dist/db/drizzle.js +154 -18
  9. package/dist/db/prisma.cjs +242 -45
  10. package/dist/db/prisma.d.cts +39 -1
  11. package/dist/db/prisma.d.ts +39 -1
  12. package/dist/db/prisma.js +241 -46
  13. package/dist/db/supabase.cjs +154 -18
  14. package/dist/db/supabase.js +154 -18
  15. package/dist/index.cjs +761 -245
  16. package/dist/index.d.cts +2 -2
  17. package/dist/index.d.ts +2 -2
  18. package/dist/index.js +761 -245
  19. package/dist/jobs/bullmq.cjs +973 -16
  20. package/dist/jobs/bullmq.d.cts +8 -3
  21. package/dist/jobs/bullmq.d.ts +8 -3
  22. package/dist/jobs/bullmq.js +973 -16
  23. package/dist/jobs/inngest.cjs +880 -14
  24. package/dist/jobs/inngest.d.cts +7 -5
  25. package/dist/jobs/inngest.d.ts +7 -5
  26. package/dist/jobs/inngest.js +880 -14
  27. package/dist/jobs/supabase-queues.cjs +878 -16
  28. package/dist/jobs/supabase-queues.js +878 -16
  29. package/dist/jobs/trigger.cjs +914 -42
  30. package/dist/jobs/trigger.js +914 -42
  31. package/dist/jobs/upstash-workflow.cjs +914 -14
  32. package/dist/jobs/upstash-workflow.d.cts +16 -2
  33. package/dist/jobs/upstash-workflow.d.ts +16 -2
  34. package/dist/jobs/upstash-workflow.js +914 -15
  35. package/dist/triggers/index.cjs +405 -76
  36. package/dist/triggers/index.d.cts +36 -41
  37. package/dist/triggers/index.d.ts +36 -41
  38. package/dist/triggers/index.js +404 -74
  39. package/package.json +5 -1
  40. package/sql/postgres/00_install.sql +244 -21
package/README.md CHANGED
@@ -105,19 +105,67 @@ Restart your dev server, hit any route, and watch for
105
105
  `[ezlogs] FlushScheduler started` in the terminal. Events appear in
106
106
  your EZLogs dashboard within a few seconds.
107
107
 
108
- ### 4. (Optional) Full DB diffs on Postgres
108
+ Sending to a local EZLogs server over plain `http://` (development)?
109
+ Events are still sent, but the agent warns on every start; pass
110
+ `allowInsecureTransport: true` to `ezlogs.init` to silence it. Keep
111
+ `https://` in production.
109
112
 
110
- One command installs Postgres triggers that capture every business-
111
- table write with full from→to change diffs:
113
+ ### 4. Database changes
114
+
115
+ Steps 1-3 capture requests, Server Actions and jobs. Database changes
116
+ need one more step, depending on your stack:
117
+
118
+ | Your app writes with | Do this | You get |
119
+ |---|---|---|
120
+ | **Supabase** (supabase-js) | `npm i pg`, set `DATABASE_URL` to the Postgres connection string, run `npx ezlogs-nextjs install-triggers`. Nothing to wrap: the agent tags supabase-js requests and the trigger reads the tag. | Every write (`.from()`, `.rpc()`, bulk) with from→to diffs, linked to its request |
121
+ | **Prisma** on Postgres | triggers (as above) + `new PrismaClient().$extends(ezlogsPrisma())` (below) | Same |
122
+ | **Drizzle** (node-postgres) or raw **`pg`** | triggers (as above). The agent patches `pg` itself. | Same |
123
+ | postgres.js, Neon HTTP, scripts | triggers (as above) | Writes show up as their own actions, not linked to the request |
124
+ | MySQL, SQLite, PlanetScale, no trigger access | a per-ORM adapter: `ezlogsPrisma()`, `ezlogsDrizzleLogger`, `wrapSupabase` ([docs/database.md](./docs/database.md)) | New values only, no before-values |
112
125
 
113
126
  ```bash
127
+ npm i pg # the trigger installer and reader need it
114
128
  npx ezlogs-nextjs install-triggers
115
129
  ```
116
130
 
117
- Migration tables, sessions, and the agent's own audit table are
118
- skipped automatically. Without this, the agent still captures DB
119
- writes via per-ORM adapters — you just lose the from-value half of
120
- each change. See [docs/database.md](./docs/database.md) for trade-offs.
131
+ Run it from your app's directory with the package installed there
132
+ (with bun: `bunx ezlogs-nextjs install-triggers` or
133
+ `./node_modules/.bin/ezlogs-nextjs install-triggers`); if the package
134
+ isn't installed locally, npx / bunx download whatever version is
135
+ published, and the CLI warns. Migration tables, sessions, and the
136
+ agent's own audit table are skipped automatically. If `pg` or the
137
+ audit table is missing at runtime, the agent logs one warning at
138
+ startup and leaves the trigger path off.
139
+
140
+ **Recommended setup for Prisma** (plain `new PrismaClient()`, no driver
141
+ adapter): triggers **plus** the extension, which tags each write with
142
+ the request so DB changes join the right action and actor.
143
+
144
+ ```ts
145
+ // lib/prisma.ts
146
+ import { PrismaClient } from "@prisma/client";
147
+ import { ezlogsPrisma } from "ezlogs-nextjs/prisma";
148
+
149
+ export const prisma = new PrismaClient().$extends(ezlogsPrisma());
150
+ ```
151
+
152
+ Each write inside a captured request runs as
153
+ `$transaction([set_config(correlation id), your query])` (Prisma's
154
+ documented RLS-extension pattern); reads are untouched. Writes inside
155
+ your own `prisma.$transaction(...)` are not tagged: they still show up,
156
+ as separate actions without an actor. Without the extension, a plain
157
+ PrismaClient's writes are never tagged.
158
+
159
+ Without triggers, the extension still sends Prisma writes itself (new
160
+ values only — Prisma doesn't expose the old row). With triggers it
161
+ sends nothing for tracked tables; the trigger rows are the single
162
+ source, so there are no duplicates (also for `@@map`-ed models).
163
+
164
+ **Upgrading:** from 0.2.x to 0.3.0 there is nothing to re-run (no SQL
165
+ changes). From 0.1.x, run `npx ezlogs-nextjs install-triggers --tables=none`
166
+ **before** deploying the new agent (functions only, no table locks),
167
+ deploy right after, and later re-run `npx ezlogs-nextjs install-triggers`
168
+ at a quiet moment. Details in [CHANGELOG.md](./CHANGELOG.md).
121
169
 
122
170
  ---
123
171
 
@@ -125,10 +173,10 @@ each change. See [docs/database.md](./docs/database.md) for trade-offs.
125
173
 
126
174
  | Source | What | Where it comes from |
127
175
  |---|---|---|
128
- | HTTP | Method, path, status, duration, sanitized params, GraphQL op | App Router + Pages Router (auto-wrapped) |
129
- | Server Actions | Action name, args, outcome, redirect/notFound handled as success | File-level + inline `"use server"` (auto-wrapped) |
130
- | Database | `model_class`, `operation`, full from→to diffs (trigger path) | Postgres triggers, Prisma, Drizzle, supabase-js |
131
- | Background jobs | `job_class`, `job_id`, `queue`, `retry_count` | BullMQ, Inngest, Trigger.dev, pgmq, Upstash Workflow |
176
+ | HTTP | Method, path, status, duration, sanitized params (incl. route params like `[id]`), GraphQL op, the `error` / `message` of a JSON error response | App Router + Pages Router (auto-wrapped) |
177
+ | Server Actions | Action name, args, caller IP, outcome (a returned `{ success: false, error }` is a failure with that message), redirect/notFound handled as success | File-level + inline `"use server"` (auto-wrapped) |
178
+ | Database | `model_class`, `operation`, full from→to diffs incl. json leaves (trigger path), record name | Postgres triggers, Prisma, Drizzle, supabase-js |
179
+ | Background jobs | `job_class`, `job_id`, `queue`, `retry_count`, sanitized `arguments`, plus the DB writes the job made | BullMQ, Inngest, Trigger.dev, pgmq, Upstash Workflow |
132
180
 
133
181
  Every event in one inbound request shares a `correlation_id` so the
134
182
  server stitches them into a single Action card.
@@ -171,6 +219,53 @@ The agent is designed to be **invisible** to your application.
171
219
  Your application's reliability is more important than capturing every
172
220
  event. EZLogs is best-effort, not guaranteed delivery.
173
221
 
222
+ ## Security: what is captured and redacted
223
+
224
+ - **Request params / Server Action args.** Keys matching a sensitive
225
+ name are sent as `"[FILTERED]"`: password, token, secret, api key,
226
+ credential, auth, signature, card number / cvv, ssn, … (substring),
227
+ and the short words `pin`, `otp`, `totp`, `iban`, `mfa`,
228
+ `passphrase` (whole word, so `shipping` / `options` are kept).
229
+ camelCase keys are covered (`apiKey`, `mdmUnlockPin`). Add your own
230
+ fragments with `excludedGraphqlVariableKeys`.
231
+ - **Unnamed Server Action arguments** (`signIn(email, password)`) have
232
+ no key to judge by, so every positional string is sent as
233
+ `"[FILTERED]"` unless it has a strict generated-id shape (integer,
234
+ UUID, cuid, cuid2, ULID, nanoid — with digit / mixed-case checks, so
235
+ words like `inactive` stay masked). Numbers and booleans are kept,
236
+ except Luhn-valid card numbers (15-16 digits always, 13-19 for payment-named actions) and 4-8
237
+ digit values when the action name has a secret word (`setPin`,
238
+ `verifyOtp`, `addCard`). Residual risk: a PIN passed to an innocently
239
+ named action (`update(4821)`) is shown. Object / FormData arguments
240
+ keep their field names and get the key rule above — pass an object
241
+ if you want values shown.
242
+ - **Database rows (trigger path).** Columns with sensitive names are
243
+ removed by the trigger itself, so they are never written to
244
+ `ezlogs_audit_log`. An update that only touches such columns writes
245
+ no audit row. Sensitive keys inside json/jsonb columns are dropped
246
+ by the agent before sending. DB values are dropped, not sent as
247
+ `"[FILTERED]"` (that placeholder is for request params and job
248
+ arguments).
249
+ - **Retention of `ezlogs_audit_log`.** Rows are deleted as soon as the
250
+ agent sends them. Rows nobody claimed (untagged writes, or tagged
251
+ rows older than 5 minutes) are sent as their own actions by a
252
+ throttled sweep. Rows older than 24 hours are deleted unsent. The
253
+ agent's role needs `DELETE` on the table; without it the agent warns
254
+ once and you must schedule
255
+ `DELETE FROM ezlogs_audit_log WHERE captured_at < now() - interval '24 hours'`.
256
+ - **Access to `ezlogs_audit_log`.** Row-level security is on and all
257
+ privileges are revoked from `PUBLIC` and Supabase's `anon` /
258
+ `authenticated`, so the table is not reachable through PostgREST.
259
+ The installing role (owner) and the trigger are unaffected; a reader
260
+ connecting as another role gets one startup warning with the GRANT +
261
+ CREATE POLICY it needs.
262
+ - **Correlation is never guessed.** A DB write is grouped with a
263
+ request only when it carries that request's id (set by the agent on
264
+ `pg` connections, or via the `x-correlation-id` header PostgREST
265
+ exposes for supabase-js). Untagged writes show up as their own
266
+ actions without an actor. An inbound `x-ezlogs-correlation-id` is
267
+ honored only in the agent's own format and if under an hour old.
268
+
174
269
  ## Wire-format parity
175
270
 
176
271
  The Node agent emits byte-identical events to the Ruby agent
@@ -1,21 +1,54 @@
1
1
  #!/usr/bin/env node
2
- // CLI shim for `npx @ezlogs/nextjs install-triggers`.
2
+ // CLI for `npx ezlogs-nextjs install-triggers` (also installed as the
3
+ // `install-triggers` bin).
3
4
  //
4
5
  // Lazy-imports the dist CJS bundle + `pg` so we don't load either when
5
- // the customer is just installing the package. `pg` is a devDependency
6
- // of the agent for tests; in customer projects it's expected to be
7
- // present transitively (drizzle-orm, prisma, etc. all pull it in) or
8
- // the customer can install it explicitly. We don't list it as a peer
9
- // because the only path that needs it is this CLI.
6
+ // the customer is just installing the package. `pg` is resolved from
7
+ // the customer's project first (the directory the command runs in),
8
+ // so it works with npm, pnpm and bun layouts where `pg` isn't hoisted
9
+ // next to this package. We don't list it as a peer because only this
10
+ // CLI and the trigger reader need it.
10
11
  //
11
12
  // Exits non-zero on any install failure so CI scripts can catch it.
12
13
 
14
+ const fs = require("node:fs");
15
+ const path = require("node:path");
16
+
17
+ // The ezlogs-nextjs this project would load (nearest node_modules up
18
+ // from the working directory, so monorepo hoisting counts), or null.
19
+ function installedFromProject() {
20
+ for (let dir = process.cwd(); ; dir = path.dirname(dir)) {
21
+ const candidate = path.join(dir, "node_modules", "ezlogs-nextjs");
22
+ if (fs.existsSync(candidate)) return fs.realpathSync(candidate);
23
+ if (path.dirname(dir) === dir) return null;
24
+ }
25
+ }
26
+
13
27
  (async () => {
14
28
  const { runInstall, parseArgs } = require("../dist/triggers/index.cjs");
29
+ const { version } = require("../package.json");
15
30
 
16
31
  const argv = process.argv.slice(2);
32
+ const command = argv.find((a) => !a.startsWith("-"));
33
+ if (command !== undefined && command !== "install-triggers") {
34
+ console.error(
35
+ `[ezlogs] Unknown command "${command}". Usage: npx ezlogs-nextjs install-triggers [--help]`,
36
+ );
37
+ process.exit(1);
38
+ }
17
39
  const args = parseArgs(argv);
18
40
 
41
+ // `npx` / `bunx` download the package to a temp dir when it isn't
42
+ // installed in this project — a version that may differ from the one
43
+ // your app runs, and a place `pg` can't be found from.
44
+ if (installedFromProject() !== fs.realpathSync(path.join(__dirname, ".."))) {
45
+ console.warn(
46
+ `[ezlogs] Running ezlogs-nextjs ${version} from ${path.dirname(__dirname)}, not from ` +
47
+ "this project's node_modules. Run it from your app's directory after " +
48
+ "`npm install ezlogs-nextjs` (or `./node_modules/.bin/ezlogs-nextjs install-triggers`).",
49
+ );
50
+ }
51
+
19
52
  // For --help / --print / unknown-engine paths the programmatic entry
20
53
  // doesn't need a real exec. Forward without a connection.
21
54
  if (args.help || args.print) {
@@ -25,13 +58,14 @@
25
58
 
26
59
  let pg;
27
60
  try {
28
- pg = require("pg");
61
+ pg = require(require.resolve("pg", { paths: [process.cwd(), __dirname] }));
29
62
  } catch {
30
63
  console.error(
31
- "[ezlogs] install-triggers needs `pg` installed in your project " +
32
- "(it ships as a devDependency of the agent itself, not a peer). " +
33
- "Run `npm install --save-dev pg` and try again, or run with " +
34
- "--print and pipe the SQL into your own tool.",
64
+ `[ezlogs] install-triggers needs the \`pg\` package, and it was not found from ${process.cwd()}.\n` +
65
+ " Install it in your app (the agent also needs it at runtime to read the audit log):\n" +
66
+ " npm install pg (or: pnpm add pg / yarn add pg / bun add pg)\n" +
67
+ " then run this command again from your app's directory.\n" +
68
+ " Or print the SQL and run it with your own tool: npx ezlogs-nextjs install-triggers --print",
35
69
  );
36
70
  process.exit(1);
37
71
  }
@@ -56,6 +90,10 @@
56
90
 
57
91
  let exitCode = 1;
58
92
  try {
93
+ // A statement stuck behind a long transaction would make the app's
94
+ // writes queue behind it. Give up after 5s instead; re-running the
95
+ // (idempotent) install finishes the job.
96
+ await client.query("SET lock_timeout = '5s'");
59
97
  exitCode = await runInstall(args, {
60
98
  out: (l) => console.log(l),
61
99
  exec: (sql) => client.query(sql),
@@ -1,4 +1,4 @@
1
- import { a as ActorFromRequest } from '../configuration-3Pei_XZ_.cjs';
1
+ import { a as ActorFromRequest } from '../configuration-OHLNivON.cjs';
2
2
 
3
3
  /**
4
4
  * Returns an `ActorFromRequest` that pulls the current user from
@@ -1,4 +1,4 @@
1
- import { a as ActorFromRequest } from '../configuration-3Pei_XZ_.js';
1
+ import { a as ActorFromRequest } from '../configuration-OHLNivON.js';
2
2
 
3
3
  /**
4
4
  * Returns an `ActorFromRequest` that pulls the current user from
@@ -178,39 +178,30 @@ interface InitOptions {
178
178
  */
179
179
  patchDatabaseClient?: unknown;
180
180
  /**
181
- * Time-window fallback for stacks where SET LOCAL can't propagate
182
- * (supabase-js, PostgREST, anything that auto-commits per request
183
- * and doesn't expose explicit transactions). When true, the agent
184
- * runs `SELECT MAX(id) FROM ezlogs_audit_log` at the start of every
185
- * request and remembers the value. At end of request the reader
186
- * drains rows whose `correlation_id` matches OR (`correlation_id`
187
- * is NULL and `id` is greater than the snapshot) — i.e. rows the
188
- * trigger fired during this request even though the customer's
189
- * client couldn't tag them.
190
- *
191
- * Default false. Enable for supabase-js apps. Adds one cheap
192
- * `SELECT MAX(id)` per request. See AGENT_PARITY_NOTES.md for the
193
- * concurrency caveat (concurrent same-actor mutations may blur).
181
+ * @deprecated Since 0.2.0 this option is ignored. The old time-window
182
+ * fallback attributed untagged audit rows to whichever request was
183
+ * open, which under concurrency stamped other users' writes with the
184
+ * wrong actor. Untagged rows are now reported as their own actions.
185
+ * supabase-js writes are tagged exactly via the request header (re-run
186
+ * `install-triggers` after upgrading).
194
187
  */
195
188
  databaseSnapshotMode?: boolean;
196
189
  /**
197
190
  * One-line install for the trigger-based capture. Pass your Postgres
198
191
  * connection string; the agent does the rest:
199
192
  * 1. Lazy-imports `pg` and constructs a Pool (4 connections, max).
200
- * `pg` must be in your project's dependencies — the agent will
201
- * log a debug message and skip the trigger path if it's missing.
193
+ * `pg` must be in your project's dependencies — the agent logs
194
+ * one warning at startup and skips the trigger path if it's
195
+ * missing, or if `ezlogs_audit_log` is not installed.
202
196
  * 2. Wires that Pool as `databaseReader` for the audit-log drain.
203
197
  * 3. Patches `pg.Client.prototype.query` so every customer query
204
198
  * auto-injects SET LOCAL. Drizzle (node-postgres) and Prisma
205
199
  * (`@prisma/adapter-pg`) both run on `pg` and inherit zero-touch
206
200
  * capture without any extra wiring on the customer's side.
207
- * 4. Enables `databaseSnapshotMode` — the time-window fallback
208
- * that catches supabase-js / PostgREST writes whose audit row
209
- * didn't get our SET LOCAL.
210
201
  *
211
202
  * Customers who want to override individual pieces can still pass
212
- * `databaseReader` / `patchDatabaseClient` / `databaseSnapshotMode`
213
- * directly; explicit values win over the auto-wired pieces.
203
+ * `databaseReader` / `patchDatabaseClient` directly; explicit values
204
+ * win over the auto-wired pieces.
214
205
  */
215
206
  databaseUrl?: string;
216
207
  logLevel?: LogLevel;
@@ -178,39 +178,30 @@ interface InitOptions {
178
178
  */
179
179
  patchDatabaseClient?: unknown;
180
180
  /**
181
- * Time-window fallback for stacks where SET LOCAL can't propagate
182
- * (supabase-js, PostgREST, anything that auto-commits per request
183
- * and doesn't expose explicit transactions). When true, the agent
184
- * runs `SELECT MAX(id) FROM ezlogs_audit_log` at the start of every
185
- * request and remembers the value. At end of request the reader
186
- * drains rows whose `correlation_id` matches OR (`correlation_id`
187
- * is NULL and `id` is greater than the snapshot) — i.e. rows the
188
- * trigger fired during this request even though the customer's
189
- * client couldn't tag them.
190
- *
191
- * Default false. Enable for supabase-js apps. Adds one cheap
192
- * `SELECT MAX(id)` per request. See AGENT_PARITY_NOTES.md for the
193
- * concurrency caveat (concurrent same-actor mutations may blur).
181
+ * @deprecated Since 0.2.0 this option is ignored. The old time-window
182
+ * fallback attributed untagged audit rows to whichever request was
183
+ * open, which under concurrency stamped other users' writes with the
184
+ * wrong actor. Untagged rows are now reported as their own actions.
185
+ * supabase-js writes are tagged exactly via the request header (re-run
186
+ * `install-triggers` after upgrading).
194
187
  */
195
188
  databaseSnapshotMode?: boolean;
196
189
  /**
197
190
  * One-line install for the trigger-based capture. Pass your Postgres
198
191
  * connection string; the agent does the rest:
199
192
  * 1. Lazy-imports `pg` and constructs a Pool (4 connections, max).
200
- * `pg` must be in your project's dependencies — the agent will
201
- * log a debug message and skip the trigger path if it's missing.
193
+ * `pg` must be in your project's dependencies — the agent logs
194
+ * one warning at startup and skips the trigger path if it's
195
+ * missing, or if `ezlogs_audit_log` is not installed.
202
196
  * 2. Wires that Pool as `databaseReader` for the audit-log drain.
203
197
  * 3. Patches `pg.Client.prototype.query` so every customer query
204
198
  * auto-injects SET LOCAL. Drizzle (node-postgres) and Prisma
205
199
  * (`@prisma/adapter-pg`) both run on `pg` and inherit zero-touch
206
200
  * capture without any extra wiring on the customer's side.
207
- * 4. Enables `databaseSnapshotMode` — the time-window fallback
208
- * that catches supabase-js / PostgREST writes whose audit row
209
- * didn't get our SET LOCAL.
210
201
  *
211
202
  * Customers who want to override individual pieces can still pass
212
- * `databaseReader` / `patchDatabaseClient` / `databaseSnapshotMode`
213
- * directly; explicit values win over the auto-wired pieces.
203
+ * `databaseReader` / `patchDatabaseClient` directly; explicit values
204
+ * win over the auto-wired pieces.
214
205
  */
215
206
  databaseUrl?: string;
216
207
  logLevel?: LogLevel;
@@ -111,7 +111,6 @@ var Configuration = class {
111
111
  displayNameFor = {};
112
112
  isServerActionError = null;
113
113
  databaseReader = null;
114
- databaseSnapshotMode = false;
115
114
  databaseUrl = null;
116
115
  /**
117
116
  * Tables that have the `ezlogs_audit` trigger attached. Detected once
@@ -163,9 +162,6 @@ var Configuration = class {
163
162
  if (options.databaseReader !== void 0) {
164
163
  this.databaseReader = options.databaseReader;
165
164
  }
166
- if (options.databaseSnapshotMode !== void 0) {
167
- this.databaseSnapshotMode = options.databaseSnapshotMode;
168
- }
169
165
  if (options.databaseUrl !== void 0) {
170
166
  this.databaseUrl = options.databaseUrl;
171
167
  }
@@ -493,6 +489,57 @@ var storage2 = createScopedStorage(
493
489
  function getCurrentActor() {
494
490
  return storage2.read();
495
491
  }
492
+ var SENSITIVE_WORD_PATTERNS = Object.freeze([
493
+ "pin",
494
+ "otp",
495
+ "totp",
496
+ "iban",
497
+ "mfa",
498
+ "passphrase"
499
+ ]);
500
+ function snakeLower(key) {
501
+ return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
502
+ }
503
+ function matchesSensitiveKey(key, substringPatterns) {
504
+ const lower = key.toLowerCase();
505
+ const snake = snakeLower(key);
506
+ if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
507
+ return true;
508
+ }
509
+ const words = snake.split(/[^a-z0-9]+/);
510
+ return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
511
+ }
512
+ function passesLuhn(digits) {
513
+ let sum = 0;
514
+ for (let i = 0; i < digits.length; i += 1) {
515
+ let d = Number(digits[digits.length - 1 - i]);
516
+ if (i % 2 === 1) {
517
+ d *= 2;
518
+ if (d > 9) d -= 9;
519
+ }
520
+ sum += d;
521
+ }
522
+ return sum % 10 === 0;
523
+ }
524
+ var SECRET_TEXT_PATTERNS = [
525
+ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
526
+ /\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
527
+ /\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
528
+ /\bsk-[A-Za-z0-9_-]{16,}/g,
529
+ /\bgh[pousr]_[A-Za-z0-9]{20,}/g,
530
+ /\bxox[abposr]-[A-Za-z0-9-]+/g,
531
+ /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
532
+ ];
533
+ var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
534
+ function scrubSecretsInText(text, options = {}) {
535
+ let out = text;
536
+ for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
537
+ out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
538
+ const digits = match.replace(/[ -]/g, "");
539
+ return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
540
+ });
541
+ return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
542
+ }
496
543
 
497
544
  // src/db/shared.ts
498
545
  var IGNORED_ATTRIBUTES = Object.freeze([
@@ -501,8 +548,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
501
548
  "created_at",
502
549
  "updatedAt",
503
550
  "updated_at",
504
- "deletedAt",
505
- "deleted_at",
506
551
  // Auth.js / NextAuth standard columns
507
552
  "emailVerified",
508
553
  "expires"
@@ -525,7 +570,19 @@ var DB_SENSITIVE_PATTERNS = Object.freeze([
525
570
  "salt",
526
571
  "digest",
527
572
  "signature",
528
- "hmac"
573
+ "hmac",
574
+ // 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
575
+ // the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
576
+ // `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
577
+ // SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
578
+ "credential",
579
+ "passwd",
580
+ "passcode",
581
+ "apikey",
582
+ "card_number",
583
+ "cardnumber",
584
+ "cvv",
585
+ "cvc"
529
586
  ]);
530
587
  function emitDbEvent(input) {
531
588
  try {
@@ -534,14 +591,17 @@ function emitDbEvent(input) {
534
591
  if (config.allExcludedTables().includes(input.modelClass)) return;
535
592
  if (config.databaseReader && !input._fromTrigger) {
536
593
  const tracked = config.triggerTrackedTables;
537
- if (tracked === null || tracked.has(input.modelClass)) return;
594
+ if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
538
595
  }
539
596
  if (input.operation === "update") {
540
597
  const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
541
598
  if (meaningful.length === 0) return;
542
599
  }
543
600
  const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
544
- const context = buildContext(input);
601
+ const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
602
+ const context = buildContext(
603
+ related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
604
+ );
545
605
  const event = build({
546
606
  source_type: "database_callback",
547
607
  source_data: {
@@ -550,9 +610,10 @@ function emitDbEvent(input) {
550
610
  },
551
611
  outcome: "success",
552
612
  correlation_id: current(),
553
- resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
613
+ resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
554
614
  context,
555
- duration_ms: null
615
+ duration_ms: null,
616
+ timestamp: input.timestamp ?? null
556
617
  });
557
618
  pushEvent(event);
558
619
  } catch (error) {
@@ -596,7 +657,7 @@ function filterMeaningfulAttributes(attrs) {
596
657
  return Object.keys(out).length === 0 ? null : out;
597
658
  }
598
659
  function filterMeaningfulChanges(changes) {
599
- return changes.filter((c) => {
660
+ return flattenNestedChanges(changes).filter((c) => {
600
661
  if (!isMeaningfulAttribute(c.attribute)) return false;
601
662
  if (!isScalar(c.to)) return false;
602
663
  if (c.from === void 0) return true;
@@ -604,14 +665,67 @@ function filterMeaningfulChanges(changes) {
604
665
  return valuesActuallyChanged(c.from, c.to);
605
666
  });
606
667
  }
668
+ var MAX_NESTED_DEPTH = 3;
669
+ var MAX_NESTED_LEAVES = 20;
670
+ var MAX_UNKNOWN_FROM_LEAVES = 5;
671
+ function flattenNestedChanges(changes) {
672
+ if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
673
+ const out = [];
674
+ for (const c of changes) {
675
+ if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
676
+ out.push(c);
677
+ continue;
678
+ }
679
+ if (!isMeaningfulAttribute(c.attribute)) continue;
680
+ const leaves = [];
681
+ collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
682
+ const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
683
+ out.push(...leaves.slice(0, cap).map(scrubLeaf));
684
+ }
685
+ return out;
686
+ }
687
+ function collectLeafChanges(path, from, to, depth, out) {
688
+ if (out.length > MAX_NESTED_LEAVES) return;
689
+ const fromObj = isPlainRecord(from);
690
+ const toObj = isPlainRecord(to);
691
+ if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
692
+ const f = fromObj ? from : {};
693
+ const t = toObj ? to : {};
694
+ for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
695
+ const before = from === void 0 ? void 0 : f[key] ?? null;
696
+ collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
697
+ }
698
+ return;
699
+ }
700
+ if (from !== void 0 && sameJson(from, to)) return;
701
+ out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
702
+ }
703
+ function scrubLeaf(change) {
704
+ const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
705
+ const out = { attribute: change.attribute, to: scrub(change.to) };
706
+ if (change.from !== void 0) out.from = scrub(change.from);
707
+ return out;
708
+ }
709
+ function isPlainRecord(value) {
710
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
711
+ const proto = Object.getPrototypeOf(value);
712
+ return proto === Object.prototype || proto === null;
713
+ }
714
+ function sameJson(a, b) {
715
+ if (a === b) return true;
716
+ try {
717
+ return JSON.stringify(a) === JSON.stringify(b);
718
+ } catch {
719
+ return false;
720
+ }
721
+ }
607
722
  function isMeaningfulAttribute(name) {
608
723
  if (IGNORED_ATTRIBUTES.includes(name)) return false;
609
724
  if (isDbSensitive(name)) return false;
610
725
  return true;
611
726
  }
612
727
  function isDbSensitive(name) {
613
- const lower = name.toLowerCase();
614
- return DB_SENSITIVE_PATTERNS.some((p) => lower.includes(p));
728
+ return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
615
729
  }
616
730
  function isScalar(value) {
617
731
  if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
@@ -642,22 +756,44 @@ function isPrismaDecimal(value) {
642
756
  const obj = value;
643
757
  return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
644
758
  }
759
+ var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
760
+ var MAX_DISPLAY_NAME_LENGTH = 120;
645
761
  function resolveDisplayName(modelClass, instance) {
646
762
  try {
647
763
  const config = configuration();
648
764
  const resolver = config.displayNameFor[modelClass];
649
765
  const resolved = applyResolver(resolver, instance);
650
766
  if (resolved !== null) return resolved;
651
- for (const field of ["name", "title", "number"]) {
652
- const value = instance[field];
653
- if (value !== null && value !== void 0 && String(value).length > 0) {
654
- return String(value);
767
+ const columns = /* @__PURE__ */ new Map();
768
+ for (const key of Object.keys(instance)) {
769
+ if (isDbSensitive(key)) continue;
770
+ const norm = key.toLowerCase().replace(/_/g, "");
771
+ if (!columns.has(norm)) columns.set(norm, instance[key]);
772
+ }
773
+ const text = (norm) => {
774
+ const value = columns.get(norm);
775
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
776
+ return null;
655
777
  }
778
+ const str = String(value).trim();
779
+ return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
780
+ };
781
+ for (const column of DISPLAY_NAME_COLUMNS) {
782
+ const value = text(column);
783
+ if (value !== null) return value;
656
784
  }
785
+ return text(`${singularNorm(modelClass)}number`);
657
786
  } catch {
658
787
  }
659
788
  return null;
660
789
  }
790
+ function singularNorm(modelClass) {
791
+ const norm = modelClass.toLowerCase().replace(/_/g, "");
792
+ if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
793
+ if (/(ss|us|is)$/.test(norm)) return norm;
794
+ if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
795
+ return norm.endsWith("s") ? norm.slice(0, -1) : norm;
796
+ }
661
797
  function applyResolver(resolver, instance) {
662
798
  if (typeof resolver === "string") {
663
799
  const value = instance[resolver];