ezlogs-nextjs 0.2.0 → 0.3.1

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 (51) hide show
  1. package/README.md +53 -16
  2. package/bin/install-triggers.cjs +73 -11
  3. package/dist/actors/index.cjs +5 -37
  4. package/dist/actors/index.js +2 -34
  5. package/dist/chunk-C5OSM557.js +39 -0
  6. package/dist/chunk-CE3R7DVB.js +1127 -0
  7. package/dist/chunk-CU45I2WR.js +498 -0
  8. package/dist/chunk-DKRNNI4I.js +180 -0
  9. package/dist/chunk-SIAA4J6H.js +17 -0
  10. package/dist/chunk-UQHEW6ER.js +119 -0
  11. package/dist/chunk-UWHYJH4M.cjs +126 -0
  12. package/dist/chunk-V5YNKKTG.cjs +42 -0
  13. package/dist/chunk-WASXTDLW.cjs +1151 -0
  14. package/dist/chunk-Y5NSHKWQ.cjs +182 -0
  15. package/dist/chunk-YX2XPLOA.cjs +508 -0
  16. package/dist/chunk-ZDSQYCYV.cjs +21 -0
  17. package/dist/db/drizzle.cjs +6 -713
  18. package/dist/db/drizzle.js +5 -712
  19. package/dist/db/prisma.cjs +52 -763
  20. package/dist/db/prisma.d.cts +3 -1
  21. package/dist/db/prisma.d.ts +3 -1
  22. package/dist/db/prisma.js +38 -749
  23. package/dist/db/supabase.cjs +21 -728
  24. package/dist/db/supabase.js +14 -721
  25. package/dist/index.cjs +204 -2181
  26. package/dist/index.js +107 -2090
  27. package/dist/jobs/bullmq.cjs +61 -552
  28. package/dist/jobs/bullmq.d.cts +8 -3
  29. package/dist/jobs/bullmq.d.ts +8 -3
  30. package/dist/jobs/bullmq.js +58 -549
  31. package/dist/jobs/inngest.cjs +23 -561
  32. package/dist/jobs/inngest.d.cts +7 -5
  33. package/dist/jobs/inngest.d.ts +7 -5
  34. package/dist/jobs/inngest.js +19 -557
  35. package/dist/jobs/supabase-queues.cjs +17 -603
  36. package/dist/jobs/supabase-queues.js +12 -598
  37. package/dist/jobs/trigger.cjs +18 -587
  38. package/dist/jobs/trigger.js +16 -585
  39. package/dist/jobs/upstash-workflow.cjs +62 -584
  40. package/dist/jobs/upstash-workflow.d.cts +17 -2
  41. package/dist/jobs/upstash-workflow.d.ts +17 -2
  42. package/dist/jobs/upstash-workflow.js +59 -582
  43. package/dist/nextjs-plugin/index.cjs +1 -1
  44. package/dist/nextjs-plugin/index.js +1 -1
  45. package/dist/nextjs-plugin/loader.cjs +21 -25
  46. package/dist/nextjs-plugin/loader.js +1 -5
  47. package/dist/triggers/index.cjs +40 -1234
  48. package/dist/triggers/index.d.cts +7 -2
  49. package/dist/triggers/index.d.ts +7 -2
  50. package/dist/triggers/index.js +6 -1221
  51. package/package.json +4 -1
package/README.md CHANGED
@@ -105,22 +105,40 @@ 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). With `DATABASE_URL` set, the agent patches `pg` itself; if your app loads its own copy of `pg` (monorepos), pass it: `ezlogs.init({ ..., patchDatabaseClient: pg })`. | 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. The trigger path needs the `pg` package in your
119
- project (`npm i pg`); if `pg` or the audit table is missing, the agent
120
- logs one warning at startup and leaves the trigger path off. Without
121
- triggers, DB writes are captured only if you wire a per-ORM adapter
122
- (`ezlogsPrisma`, `ezlogsDrizzleLogger`, `wrapSupabase`) — see
123
- [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. They also download it when your
136
+ lockfile's bin entries predate 0.3.0 (seen with bun and a `file:`
137
+ dependency); the CLI then prints the command that runs your project's
138
+ copy: `node node_modules/ezlogs-nextjs/bin/install-triggers.cjs install-triggers`. Migration tables, sessions, and the
139
+ agent's own audit table are skipped automatically. If `pg` or the
140
+ audit table is missing at runtime, the agent logs one warning at
141
+ startup and leaves the trigger path off.
124
142
 
125
143
  **Recommended setup for Prisma** (plain `new PrismaClient()`, no driver
126
144
  adapter): triggers **plus** the extension, which tags each write with
@@ -141,7 +159,23 @@ your own `prisma.$transaction(...)` are not tagged: they still show up,
141
159
  as separate actions without an actor. Without the extension, a plain
142
160
  PrismaClient's writes are never tagged.
143
161
 
144
- **Upgrading from 0.1.x:** run `npx ezlogs-nextjs install-triggers --tables=none`
162
+ **Background workers** (BullMQ and other job processes) are separate
163
+ processes: call `ezlogs.init` there too and build the database client
164
+ the same way (extension for Prisma), or the job's writes show up as
165
+ separate actions without an actor.
166
+
167
+ **Self-check:** if writes made while requests or jobs run in a process
168
+ keep arriving untagged, and that process has never sent a tagged row,
169
+ the agent logs one warning naming the fix. It never guesses which
170
+ request an untagged row belongs to.
171
+
172
+ Without triggers, the extension still sends Prisma writes itself (new
173
+ values only — Prisma doesn't expose the old row). With triggers it
174
+ sends nothing for tracked tables; the trigger rows are the single
175
+ source, so there are no duplicates (also for `@@map`-ed models).
176
+
177
+ **Upgrading:** from 0.2.x to 0.3.0 there is nothing to re-run (no SQL
178
+ changes). From 0.1.x, run `npx ezlogs-nextjs install-triggers --tables=none`
145
179
  **before** deploying the new agent (functions only, no table locks),
146
180
  deploy right after, and later re-run `npx ezlogs-nextjs install-triggers`
147
181
  at a quiet moment. Details in [CHANGELOG.md](./CHANGELOG.md).
@@ -152,10 +186,10 @@ at a quiet moment. Details in [CHANGELOG.md](./CHANGELOG.md).
152
186
 
153
187
  | Source | What | Where it comes from |
154
188
  |---|---|---|
155
- | HTTP | Method, path, status, duration, sanitized params, GraphQL op | App Router + Pages Router (auto-wrapped) |
156
- | Server Actions | Action name, args, outcome, redirect/notFound handled as success | File-level + inline `"use server"` (auto-wrapped) |
157
- | Database | `model_class`, `operation`, full from→to diffs (trigger path) | Postgres triggers, Prisma, Drizzle, supabase-js |
158
- | Background jobs | `job_class`, `job_id`, `queue`, `retry_count` | BullMQ, Inngest, Trigger.dev, pgmq, Upstash Workflow |
189
+ | 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) |
190
+ | 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) |
191
+ | Database | `model_class`, `operation`, full from→to diffs incl. json leaves (trigger path), record name | Postgres triggers, Prisma, Drizzle, supabase-js |
192
+ | 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 |
159
193
 
160
194
  Every event in one inbound request shares a `correlation_id` so the
161
195
  server stitches them into a single Action card.
@@ -221,7 +255,10 @@ event. EZLogs is best-effort, not guaranteed delivery.
221
255
  - **Database rows (trigger path).** Columns with sensitive names are
222
256
  removed by the trigger itself, so they are never written to
223
257
  `ezlogs_audit_log`. An update that only touches such columns writes
224
- no audit row.
258
+ no audit row. Sensitive keys inside json/jsonb columns are dropped
259
+ by the agent before sending. DB values are dropped, not sent as
260
+ `"[FILTERED]"` (that placeholder is for request params and job
261
+ arguments).
225
262
  - **Retention of `ezlogs_audit_log`.** Rows are deleted as soon as the
226
263
  agent sends them. Rows nobody claimed (untagged writes, or tagged
227
264
  rows older than 5 minutes) are sent as their own actions by a
@@ -1,21 +1,81 @@
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. They also do
44
+ // it when it IS installed but the lockfile's bin metadata predates
45
+ // the `ezlogs-nextjs` bin (seen with bun and a `file:` dependency):
46
+ // then point at the project's own copy.
47
+ const local = installedFromProject();
48
+ let runLocal = null;
49
+ if (local !== fs.realpathSync(path.join(__dirname, ".."))) {
50
+ let hint;
51
+ if (local) {
52
+ let localVersion = "?";
53
+ try {
54
+ localVersion = JSON.parse(fs.readFileSync(path.join(local, "package.json"), "utf8")).version;
55
+ } catch {
56
+ /* keep "?" */
57
+ }
58
+ const localBin = path.join(local, "bin", "install-triggers.cjs");
59
+ if (fs.existsSync(localBin)) {
60
+ runLocal = `node ${JSON.stringify(path.relative(process.cwd(), localBin))} install-triggers`;
61
+ }
62
+ hint =
63
+ `This project has ezlogs-nextjs ${localVersion} installed, but npx / bunx did not use it ` +
64
+ "(usually a lockfile whose bin entries predate 0.3.0, e.g. bun with a `file:` dependency). " +
65
+ "Run your project's copy instead:\n" +
66
+ (runLocal ? ` ${runLocal}\n or ` : " ") +
67
+ "./node_modules/.bin/ezlogs-nextjs install-triggers (./node_modules/.bin/install-triggers on older installs).";
68
+ } else {
69
+ hint =
70
+ "Run it from your app's directory after `npm install ezlogs-nextjs` " +
71
+ "(or `./node_modules/.bin/ezlogs-nextjs install-triggers`).";
72
+ }
73
+ console.warn(
74
+ `[ezlogs] Running ezlogs-nextjs ${version} from ${path.dirname(__dirname)}, not from ` +
75
+ `this project's node_modules. ${hint}`,
76
+ );
77
+ }
78
+
19
79
  // For --help / --print / unknown-engine paths the programmatic entry
20
80
  // doesn't need a real exec. Forward without a connection.
21
81
  if (args.help || args.print) {
@@ -25,13 +85,15 @@
25
85
 
26
86
  let pg;
27
87
  try {
28
- pg = require("pg");
88
+ pg = require(require.resolve("pg", { paths: [process.cwd(), __dirname] }));
29
89
  } catch {
30
90
  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.",
91
+ `[ezlogs] install-triggers needs the \`pg\` package, and it was not found from ${process.cwd()}.\n` +
92
+ " Install it in your app (the agent also needs it at runtime to read the audit log):\n" +
93
+ " npm install pg (or: pnpm add pg / yarn add pg / bun add pg)\n" +
94
+ " then run this command again from your app's directory.\n" +
95
+ " Or print the SQL and run it with your own tool: npx ezlogs-nextjs install-triggers --print" +
96
+ (runLocal ? `\n This copy was not run from your project; try: ${runLocal}` : ""),
35
97
  );
36
98
  process.exit(1);
37
99
  }
@@ -1,39 +1,7 @@
1
1
  'use strict';
2
2
 
3
- // src/logger.ts
4
- var LEVEL_RANK = {
5
- debug: 10,
6
- info: 20,
7
- warn: 30,
8
- error: 40,
9
- silent: 100
10
- };
11
- var LEVEL_KEY = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:log-level");
12
- function readLevel() {
13
- const g = globalThis;
14
- return g[LEVEL_KEY] ?? "warn";
15
- }
16
- function shouldLog(level) {
17
- return LEVEL_RANK[level] >= LEVEL_RANK[readLevel()];
18
- }
19
- function safeWrite(level, message) {
20
- try {
21
- if (!shouldLog(level)) return;
22
- const line = `[ezlogs] ${message}`;
23
- if (level === "error" || level === "warn") {
24
- console.error(line);
25
- } else {
26
- console.log(line);
27
- }
28
- } catch {
29
- }
30
- }
31
- var logger = {
32
- debug: (message) => safeWrite("debug", message),
33
- info: (message) => safeWrite("info", message),
34
- warn: (message) => safeWrite("warn", message),
35
- error: (message) => safeWrite("error", message)
36
- };
3
+ var chunkV5YNKKTG_cjs = require('../chunk-V5YNKKTG.cjs');
4
+ require('../chunk-ZDSQYCYV.cjs');
37
5
 
38
6
  // src/actors/clerk.ts
39
7
  function clerkActor() {
@@ -52,7 +20,7 @@ function clerkActor() {
52
20
  const label = typeof claims.email === "string" && claims.email || typeof claims.name === "string" && claims.name || typeof claims.username === "string" && claims.username || void 0;
53
21
  return label ? { id: userId, label } : { id: userId };
54
22
  } catch (error) {
55
- logger.debug(
23
+ chunkV5YNKKTG_cjs.logger.debug(
56
24
  `clerkActor: extraction failed (${error instanceof Error ? error.message : String(error)})`
57
25
  );
58
26
  return null;
@@ -78,7 +46,7 @@ function nextAuthActor(options) {
78
46
  const label = typeof user.name === "string" && user.name || typeof user.email === "string" && user.email || void 0;
79
47
  return label ? { id, label } : { id };
80
48
  } catch (error) {
81
- logger.debug(
49
+ chunkV5YNKKTG_cjs.logger.debug(
82
50
  `nextAuthActor: extraction failed (${error instanceof Error ? error.message : String(error)})`
83
51
  );
84
52
  return null;
@@ -98,7 +66,7 @@ function authJsActor(options) {
98
66
  const label = typeof user.name === "string" && user.name || typeof user.email === "string" && user.email || void 0;
99
67
  return label ? { id, label } : { id };
100
68
  } catch (error) {
101
- logger.debug(
69
+ chunkV5YNKKTG_cjs.logger.debug(
102
70
  `authJsActor: extraction failed (${error instanceof Error ? error.message : String(error)})`
103
71
  );
104
72
  return null;
@@ -1,37 +1,5 @@
1
- // src/logger.ts
2
- var LEVEL_RANK = {
3
- debug: 10,
4
- info: 20,
5
- warn: 30,
6
- error: 40,
7
- silent: 100
8
- };
9
- var LEVEL_KEY = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:log-level");
10
- function readLevel() {
11
- const g = globalThis;
12
- return g[LEVEL_KEY] ?? "warn";
13
- }
14
- function shouldLog(level) {
15
- return LEVEL_RANK[level] >= LEVEL_RANK[readLevel()];
16
- }
17
- function safeWrite(level, message) {
18
- try {
19
- if (!shouldLog(level)) return;
20
- const line = `[ezlogs] ${message}`;
21
- if (level === "error" || level === "warn") {
22
- console.error(line);
23
- } else {
24
- console.log(line);
25
- }
26
- } catch {
27
- }
28
- }
29
- var logger = {
30
- debug: (message) => safeWrite("debug", message),
31
- info: (message) => safeWrite("info", message),
32
- warn: (message) => safeWrite("warn", message),
33
- error: (message) => safeWrite("error", message)
34
- };
1
+ import { logger } from '../chunk-C5OSM557.js';
2
+ import '../chunk-SIAA4J6H.js';
35
3
 
36
4
  // src/actors/clerk.ts
37
5
  function clerkActor() {
@@ -0,0 +1,39 @@
1
+ // src/logger.ts
2
+ var LEVEL_RANK = {
3
+ debug: 10,
4
+ info: 20,
5
+ warn: 30,
6
+ error: 40,
7
+ silent: 100
8
+ };
9
+ var LEVEL_KEY = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:log-level");
10
+ function readLevel() {
11
+ const g = globalThis;
12
+ return g[LEVEL_KEY] ?? "warn";
13
+ }
14
+ function setLogLevel(level) {
15
+ globalThis[LEVEL_KEY] = level;
16
+ }
17
+ function shouldLog(level) {
18
+ return LEVEL_RANK[level] >= LEVEL_RANK[readLevel()];
19
+ }
20
+ function safeWrite(level, message) {
21
+ try {
22
+ if (!shouldLog(level)) return;
23
+ const line = `[ezlogs] ${message}`;
24
+ if (level === "error" || level === "warn") {
25
+ console.error(line);
26
+ } else {
27
+ console.log(line);
28
+ }
29
+ } catch {
30
+ }
31
+ }
32
+ var logger = {
33
+ debug: (message) => safeWrite("debug", message),
34
+ info: (message) => safeWrite("info", message),
35
+ warn: (message) => safeWrite("warn", message),
36
+ error: (message) => safeWrite("error", message)
37
+ };
38
+
39
+ export { logger, setLogLevel };