ezlogs-nextjs 0.3.1 → 0.4.2

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 (76) hide show
  1. package/README.md +149 -30
  2. package/bin/install-triggers.cjs +7 -2
  3. package/dist/actors/index.cjs +4 -4
  4. package/dist/actors/index.d.cts +1 -1
  5. package/dist/actors/index.d.ts +1 -1
  6. package/dist/actors/index.js +1 -1
  7. package/dist/{chunk-C5OSM557.js → chunk-FCFVIRQF.js} +7 -0
  8. package/dist/chunk-GI6BM4MR.js +443 -0
  9. package/dist/{chunk-YX2XPLOA.cjs → chunk-GMHOUAKD.cjs} +63 -34
  10. package/dist/{chunk-CU45I2WR.js → chunk-MHBS5FEP.js} +48 -20
  11. package/dist/chunk-MMLQXSSD.cjs +461 -0
  12. package/dist/{chunk-CE3R7DVB.js → chunk-NINIAL7M.js} +60 -384
  13. package/dist/{chunk-Y5NSHKWQ.cjs → chunk-NX4EL7BY.cjs} +57 -7
  14. package/dist/chunk-PS4U2GFJ.cjs +52 -0
  15. package/dist/chunk-R6IWVTX3.cjs +212 -0
  16. package/dist/{chunk-V5YNKKTG.cjs → chunk-TWTG5ILJ.cjs} +7 -0
  17. package/dist/chunk-U22T7QYY.js +200 -0
  18. package/dist/chunk-YMIGZYFH.js +48 -0
  19. package/dist/{chunk-WASXTDLW.cjs → chunk-YXZZNZG6.cjs} +71 -403
  20. package/dist/{chunk-DKRNNI4I.js → chunk-Z2TM3LGG.js} +53 -3
  21. package/dist/{configuration-OHLNivON.d.ts → configuration-CJ5yKVlP.d.cts} +14 -8
  22. package/dist/{configuration-OHLNivON.d.cts → configuration-CJ5yKVlP.d.ts} +14 -8
  23. package/dist/db/drizzle.cjs +5 -4
  24. package/dist/db/drizzle.js +3 -2
  25. package/dist/db/postgres-js.cjs +98 -0
  26. package/dist/db/postgres-js.d.cts +11 -0
  27. package/dist/db/postgres-js.d.ts +11 -0
  28. package/dist/db/postgres-js.js +95 -0
  29. package/dist/db/prisma.cjs +21 -20
  30. package/dist/db/prisma.d.cts +19 -2
  31. package/dist/db/prisma.d.ts +19 -2
  32. package/dist/db/prisma.js +6 -5
  33. package/dist/db/supabase.cjs +19 -12
  34. package/dist/db/supabase.d.cts +3 -2
  35. package/dist/db/supabase.d.ts +3 -2
  36. package/dist/db/supabase.js +11 -4
  37. package/dist/index.cjs +596 -165
  38. package/dist/index.d.cts +5 -3
  39. package/dist/index.d.ts +5 -3
  40. package/dist/index.js +492 -62
  41. package/dist/jobs/bullmq.cjs +63 -18
  42. package/dist/jobs/bullmq.d.cts +0 -4
  43. package/dist/jobs/bullmq.d.ts +0 -4
  44. package/dist/jobs/bullmq.js +59 -14
  45. package/dist/jobs/inngest.cjs +61 -24
  46. package/dist/jobs/inngest.d.cts +5 -1
  47. package/dist/jobs/inngest.d.ts +5 -1
  48. package/dist/jobs/inngest.js +55 -18
  49. package/dist/jobs/supabase-queues.cjs +17 -15
  50. package/dist/jobs/supabase-queues.d.cts +1 -1
  51. package/dist/jobs/supabase-queues.d.ts +1 -1
  52. package/dist/jobs/supabase-queues.js +15 -13
  53. package/dist/jobs/trigger.cjs +11 -12
  54. package/dist/jobs/trigger.d.cts +4 -3
  55. package/dist/jobs/trigger.d.ts +4 -3
  56. package/dist/jobs/trigger.js +7 -8
  57. package/dist/jobs/upstash-workflow.cjs +43 -35
  58. package/dist/jobs/upstash-workflow.d.cts +2 -8
  59. package/dist/jobs/upstash-workflow.d.ts +2 -8
  60. package/dist/jobs/upstash-workflow.js +38 -30
  61. package/dist/nextjs-plugin/index.cjs +39 -3
  62. package/dist/nextjs-plugin/index.d.cts +8 -1
  63. package/dist/nextjs-plugin/index.d.ts +8 -1
  64. package/dist/nextjs-plugin/index.js +39 -3
  65. package/dist/nextjs-plugin/loader.cjs +41 -3
  66. package/dist/nextjs-plugin/loader.d.cts +1 -0
  67. package/dist/nextjs-plugin/loader.d.ts +1 -0
  68. package/dist/nextjs-plugin/loader.js +42 -4
  69. package/dist/triggers/index.cjs +145 -92
  70. package/dist/triggers/index.d.cts +20 -5
  71. package/dist/triggers/index.d.ts +20 -5
  72. package/dist/triggers/index.js +125 -82
  73. package/package.json +11 -1
  74. package/sql/postgres/00_install.sql +1 -1
  75. package/dist/chunk-UQHEW6ER.js +0 -119
  76. package/dist/chunk-UWHYJH4M.cjs +0 -126
@@ -1,9 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var chunkUWHYJH4M_cjs = require('../chunk-UWHYJH4M.cjs');
4
- require('../chunk-YX2XPLOA.cjs');
5
- require('../chunk-WASXTDLW.cjs');
6
- var chunkV5YNKKTG_cjs = require('../chunk-V5YNKKTG.cjs');
3
+ var chunkR6IWVTX3_cjs = require('../chunk-R6IWVTX3.cjs');
4
+ require('../chunk-GMHOUAKD.cjs');
5
+ require('../chunk-YXZZNZG6.cjs');
6
+ var chunkMMLQXSSD_cjs = require('../chunk-MMLQXSSD.cjs');
7
+ var chunkTWTG5ILJ_cjs = require('../chunk-TWTG5ILJ.cjs');
7
8
  require('../chunk-ZDSQYCYV.cjs');
8
9
 
9
10
  // src/jobs/bullmq.ts
@@ -11,12 +12,39 @@ var WRAPPED_FLAG = "__ezlogsBullWrapped";
11
12
  function wrapBullQueue(queue) {
12
13
  if (queue[WRAPPED_FLAG]) return queue;
13
14
  const originalAdd = queue.add.bind(queue);
15
+ let pending = null;
16
+ const inOrder = (token, issue) => {
17
+ if (!token && !pending) return issue(null);
18
+ let issued;
19
+ const done = new Promise((resolve) => issued = resolve);
20
+ const result = Promise.all([pending, token]).then(([, t]) => {
21
+ try {
22
+ return issue(t ?? null);
23
+ } finally {
24
+ issued();
25
+ }
26
+ });
27
+ pending = done;
28
+ void done.then(() => {
29
+ if (pending === done) pending = null;
30
+ });
31
+ return result;
32
+ };
14
33
  queue.add = ((name, data, opts) => {
15
- return originalAdd(name, stampPayload(data), opts);
34
+ const stamped = stampPayload(data);
35
+ return inOrder(pendingActorTokenFor(stamped), (t) => originalAdd(name, withActor(stamped, t), opts));
16
36
  });
17
37
  if (typeof queue.addBulk === "function") {
18
38
  const originalAddBulk = queue.addBulk.bind(queue);
19
- const wrappedAddBulk = (jobs) => originalAddBulk(jobs.map((j) => ({ ...j, data: stampPayload(j.data) })));
39
+ const wrappedAddBulk = (jobs) => {
40
+ const stamped = jobs.map((j) => ({ ...j, data: stampPayload(j.data) }));
41
+ const ours = stamped.map((j) => isOurs(j.data));
42
+ const token = ours.includes(true) ? chunkR6IWVTX3_cjs.signedActorToken(chunkMMLQXSSD_cjs.current()) : null;
43
+ return inOrder(
44
+ token,
45
+ (t) => originalAddBulk(stamped.map((j, i) => ours[i] ? { ...j, data: withActor(j.data, t) } : j))
46
+ );
47
+ };
20
48
  queue.addBulk = wrappedAddBulk;
21
49
  }
22
50
  Object.defineProperty(queue, WRAPPED_FLAG, {
@@ -28,32 +56,39 @@ function wrapBullQueue(queue) {
28
56
  }
29
57
  function wrapBullWorker(worker) {
30
58
  if (worker[WRAPPED_FLAG]) return worker;
31
- const canWrapProcessor = typeof worker.processFn === "function" && typeof worker.callProcessJob === "function";
59
+ const internals = worker;
60
+ const canWrapProcessor = typeof internals.processFn === "function" && typeof internals.callProcessJob === "function";
32
61
  if (!canWrapProcessor && !warnedNoProcessor) {
33
62
  warnedNoProcessor = true;
34
- chunkV5YNKKTG_cjs.logger.debug(
63
+ chunkTWTG5ILJ_cjs.logger.debug(
35
64
  "wrapBullWorker: this BullMQ Worker has no processFn/callProcessJob; capturing job events from completed/failed only (DB writes inside jobs are not linked)."
36
65
  );
37
66
  }
38
67
  if (canWrapProcessor) {
39
- const original = worker.processFn;
68
+ const original = internals.processFn;
40
69
  const reported = /* @__PURE__ */ new WeakSet();
41
- worker.processFn = function ezlogsProcess(...args) {
70
+ internals.processFn = function ezlogsProcess(...args) {
42
71
  const job = args[0] ?? {};
43
72
  if (typeof args[0] === "object" && args[0] !== null) reported.add(args[0]);
44
- return chunkUWHYJH4M_cjs.runJobWithCapture(
73
+ const correlationId = chunkR6IWVTX3_cjs.extractCorrelationFromPayload(job.data);
74
+ const run = (actor) => chunkR6IWVTX3_cjs.runJobWithCapture(
45
75
  {
46
76
  jobClass: job.name ?? worker.name ?? "BullMQJob",
47
77
  jobId: job.id !== void 0 && job.id !== null ? String(job.id) : null,
48
78
  queue: job.queueName ?? worker.name ?? null,
49
79
  // attemptsMade counts finished attempts: 0 on the first run.
50
80
  retryCount: typeof job.attemptsMade === "number" ? job.attemptsMade : null,
51
- correlationId: chunkUWHYJH4M_cjs.extractCorrelationFromPayload(job.data),
81
+ correlationId,
82
+ // The enqueuing request's human actor, only when its token verifies.
83
+ actor,
52
84
  payload: job.data,
53
85
  isControlFlow: isBullControlFlow
54
86
  },
55
87
  () => Promise.resolve(original.apply(this, args))
56
88
  );
89
+ const token = job.data?.[chunkR6IWVTX3_cjs.ACTOR_PAYLOAD_KEY];
90
+ if (!correlationId || token === void 0) return run(null);
91
+ return chunkR6IWVTX3_cjs.readActorToken(token, correlationId).then(run);
57
92
  };
58
93
  worker.on("failed", (...args) => {
59
94
  const job = args[0];
@@ -94,10 +129,10 @@ function markWrapped(worker) {
94
129
  }
95
130
  function captureJob(job, worker, outcome, errorMessage) {
96
131
  try {
97
- const correlationId = chunkUWHYJH4M_cjs.extractCorrelationFromPayload(job.data);
132
+ const correlationId = chunkR6IWVTX3_cjs.extractCorrelationFromPayload(job.data);
98
133
  const startedAt = typeof job.timestamp === "number" ? new Date(job.timestamp) : /* @__PURE__ */ new Date();
99
134
  const durationMs = job.timestamp ? Date.now() - job.timestamp : null;
100
- chunkUWHYJH4M_cjs.emitJobEvent({
135
+ chunkR6IWVTX3_cjs.emitJobEvent({
101
136
  jobClass: job.name ?? worker.name ?? "BullMQJob",
102
137
  jobId: job.id !== void 0 && job.id !== null ? String(job.id) : null,
103
138
  queue: job.queueName ?? worker.name ?? null,
@@ -110,16 +145,26 @@ function captureJob(job, worker, outcome, errorMessage) {
110
145
  payload: job.data
111
146
  });
112
147
  } catch (error) {
113
- chunkV5YNKKTG_cjs.logger.debug(`bullmq capture failed: ${describeError(error)}`);
148
+ chunkTWTG5ILJ_cjs.logger.debug(`bullmq capture failed: ${describeError(error)}`);
114
149
  }
115
150
  }
151
+ function pendingActorTokenFor(data) {
152
+ return isOurs(data) ? chunkR6IWVTX3_cjs.signedActorToken(chunkMMLQXSSD_cjs.current()) : null;
153
+ }
154
+ function isOurs(data) {
155
+ if (data === null || typeof data !== "object" || Array.isArray(data)) return false;
156
+ const obj = data;
157
+ const id = chunkMMLQXSSD_cjs.current();
158
+ return Boolean(id) && obj[chunkR6IWVTX3_cjs.CORRELATION_PAYLOAD_KEY] === id && !(chunkR6IWVTX3_cjs.ACTOR_PAYLOAD_KEY in obj);
159
+ }
160
+ function withActor(data, token) {
161
+ return token ? { ...data, [chunkR6IWVTX3_cjs.ACTOR_PAYLOAD_KEY]: token } : data;
162
+ }
116
163
  function stampPayload(data) {
117
164
  if (data === null || typeof data !== "object" || Array.isArray(data)) {
118
165
  return data;
119
166
  }
120
- const obj = data;
121
- chunkUWHYJH4M_cjs.stampCorrelationOnPayload(obj, chunkUWHYJH4M_cjs.CORRELATION_PAYLOAD_KEY);
122
- return obj;
167
+ return chunkR6IWVTX3_cjs.stampCorrelationOnPayload(data, chunkR6IWVTX3_cjs.CORRELATION_PAYLOAD_KEY);
123
168
  }
124
169
  function describeError(error) {
125
170
  if (error instanceof Error) return `${error.name}: ${error.message}`;
@@ -19,10 +19,6 @@ interface BullWorkerLike {
19
19
  on: Function;
20
20
  off?: Function;
21
21
  name?: string;
22
- /** BullMQ internal: the processor `callProcessJob` invokes. */
23
- processFn?: Function;
24
- /** BullMQ internal: calls `this.processFn(job, token, signal)`. */
25
- callProcessJob?: Function;
26
22
  }
27
23
  /**
28
24
  * Patches `queue.add` (and `queue.addBulk` when present) so each
@@ -19,10 +19,6 @@ interface BullWorkerLike {
19
19
  on: Function;
20
20
  off?: Function;
21
21
  name?: string;
22
- /** BullMQ internal: the processor `callProcessJob` invokes. */
23
- processFn?: Function;
24
- /** BullMQ internal: calls `this.processFn(job, token, signal)`. */
25
- callProcessJob?: Function;
26
22
  }
27
23
  /**
28
24
  * Patches `queue.add` (and `queue.addBulk` when present) so each
@@ -1,7 +1,8 @@
1
- import { runJobWithCapture, extractCorrelationFromPayload, emitJobEvent, stampCorrelationOnPayload, CORRELATION_PAYLOAD_KEY } from '../chunk-UQHEW6ER.js';
2
- import '../chunk-CU45I2WR.js';
3
- import '../chunk-CE3R7DVB.js';
4
- import { logger } from '../chunk-C5OSM557.js';
1
+ import { extractCorrelationFromPayload, ACTOR_PAYLOAD_KEY, readActorToken, emitJobEvent, signedActorToken, CORRELATION_PAYLOAD_KEY, stampCorrelationOnPayload, runJobWithCapture } from '../chunk-U22T7QYY.js';
2
+ import '../chunk-MHBS5FEP.js';
3
+ import '../chunk-NINIAL7M.js';
4
+ import { current } from '../chunk-GI6BM4MR.js';
5
+ import { logger } from '../chunk-FCFVIRQF.js';
5
6
  import '../chunk-SIAA4J6H.js';
6
7
 
7
8
  // src/jobs/bullmq.ts
@@ -9,12 +10,39 @@ var WRAPPED_FLAG = "__ezlogsBullWrapped";
9
10
  function wrapBullQueue(queue) {
10
11
  if (queue[WRAPPED_FLAG]) return queue;
11
12
  const originalAdd = queue.add.bind(queue);
13
+ let pending = null;
14
+ const inOrder = (token, issue) => {
15
+ if (!token && !pending) return issue(null);
16
+ let issued;
17
+ const done = new Promise((resolve) => issued = resolve);
18
+ const result = Promise.all([pending, token]).then(([, t]) => {
19
+ try {
20
+ return issue(t ?? null);
21
+ } finally {
22
+ issued();
23
+ }
24
+ });
25
+ pending = done;
26
+ void done.then(() => {
27
+ if (pending === done) pending = null;
28
+ });
29
+ return result;
30
+ };
12
31
  queue.add = ((name, data, opts) => {
13
- return originalAdd(name, stampPayload(data), opts);
32
+ const stamped = stampPayload(data);
33
+ return inOrder(pendingActorTokenFor(stamped), (t) => originalAdd(name, withActor(stamped, t), opts));
14
34
  });
15
35
  if (typeof queue.addBulk === "function") {
16
36
  const originalAddBulk = queue.addBulk.bind(queue);
17
- const wrappedAddBulk = (jobs) => originalAddBulk(jobs.map((j) => ({ ...j, data: stampPayload(j.data) })));
37
+ const wrappedAddBulk = (jobs) => {
38
+ const stamped = jobs.map((j) => ({ ...j, data: stampPayload(j.data) }));
39
+ const ours = stamped.map((j) => isOurs(j.data));
40
+ const token = ours.includes(true) ? signedActorToken(current()) : null;
41
+ return inOrder(
42
+ token,
43
+ (t) => originalAddBulk(stamped.map((j, i) => ours[i] ? { ...j, data: withActor(j.data, t) } : j))
44
+ );
45
+ };
18
46
  queue.addBulk = wrappedAddBulk;
19
47
  }
20
48
  Object.defineProperty(queue, WRAPPED_FLAG, {
@@ -26,7 +54,8 @@ function wrapBullQueue(queue) {
26
54
  }
27
55
  function wrapBullWorker(worker) {
28
56
  if (worker[WRAPPED_FLAG]) return worker;
29
- const canWrapProcessor = typeof worker.processFn === "function" && typeof worker.callProcessJob === "function";
57
+ const internals = worker;
58
+ const canWrapProcessor = typeof internals.processFn === "function" && typeof internals.callProcessJob === "function";
30
59
  if (!canWrapProcessor && !warnedNoProcessor) {
31
60
  warnedNoProcessor = true;
32
61
  logger.debug(
@@ -34,24 +63,30 @@ function wrapBullWorker(worker) {
34
63
  );
35
64
  }
36
65
  if (canWrapProcessor) {
37
- const original = worker.processFn;
66
+ const original = internals.processFn;
38
67
  const reported = /* @__PURE__ */ new WeakSet();
39
- worker.processFn = function ezlogsProcess(...args) {
68
+ internals.processFn = function ezlogsProcess(...args) {
40
69
  const job = args[0] ?? {};
41
70
  if (typeof args[0] === "object" && args[0] !== null) reported.add(args[0]);
42
- return runJobWithCapture(
71
+ const correlationId = extractCorrelationFromPayload(job.data);
72
+ const run = (actor) => runJobWithCapture(
43
73
  {
44
74
  jobClass: job.name ?? worker.name ?? "BullMQJob",
45
75
  jobId: job.id !== void 0 && job.id !== null ? String(job.id) : null,
46
76
  queue: job.queueName ?? worker.name ?? null,
47
77
  // attemptsMade counts finished attempts: 0 on the first run.
48
78
  retryCount: typeof job.attemptsMade === "number" ? job.attemptsMade : null,
49
- correlationId: extractCorrelationFromPayload(job.data),
79
+ correlationId,
80
+ // The enqueuing request's human actor, only when its token verifies.
81
+ actor,
50
82
  payload: job.data,
51
83
  isControlFlow: isBullControlFlow
52
84
  },
53
85
  () => Promise.resolve(original.apply(this, args))
54
86
  );
87
+ const token = job.data?.[ACTOR_PAYLOAD_KEY];
88
+ if (!correlationId || token === void 0) return run(null);
89
+ return readActorToken(token, correlationId).then(run);
55
90
  };
56
91
  worker.on("failed", (...args) => {
57
92
  const job = args[0];
@@ -111,13 +146,23 @@ function captureJob(job, worker, outcome, errorMessage) {
111
146
  logger.debug(`bullmq capture failed: ${describeError(error)}`);
112
147
  }
113
148
  }
149
+ function pendingActorTokenFor(data) {
150
+ return isOurs(data) ? signedActorToken(current()) : null;
151
+ }
152
+ function isOurs(data) {
153
+ if (data === null || typeof data !== "object" || Array.isArray(data)) return false;
154
+ const obj = data;
155
+ const id = current();
156
+ return Boolean(id) && obj[CORRELATION_PAYLOAD_KEY] === id && !(ACTOR_PAYLOAD_KEY in obj);
157
+ }
158
+ function withActor(data, token) {
159
+ return token ? { ...data, [ACTOR_PAYLOAD_KEY]: token } : data;
160
+ }
114
161
  function stampPayload(data) {
115
162
  if (data === null || typeof data !== "object" || Array.isArray(data)) {
116
163
  return data;
117
164
  }
118
- const obj = data;
119
- stampCorrelationOnPayload(obj, CORRELATION_PAYLOAD_KEY);
120
- return obj;
165
+ return stampCorrelationOnPayload(data, CORRELATION_PAYLOAD_KEY);
121
166
  }
122
167
  function describeError(error) {
123
168
  if (error instanceof Error) return `${error.name}: ${error.message}`;
@@ -1,27 +1,38 @@
1
1
  'use strict';
2
2
 
3
- var chunkUWHYJH4M_cjs = require('../chunk-UWHYJH4M.cjs');
4
- require('../chunk-YX2XPLOA.cjs');
5
- var chunkWASXTDLW_cjs = require('../chunk-WASXTDLW.cjs');
6
- var chunkV5YNKKTG_cjs = require('../chunk-V5YNKKTG.cjs');
3
+ var chunkR6IWVTX3_cjs = require('../chunk-R6IWVTX3.cjs');
4
+ require('../chunk-GMHOUAKD.cjs');
5
+ require('../chunk-YXZZNZG6.cjs');
6
+ var chunkMMLQXSSD_cjs = require('../chunk-MMLQXSSD.cjs');
7
+ var chunkTWTG5ILJ_cjs = require('../chunk-TWTG5ILJ.cjs');
7
8
  require('../chunk-ZDSQYCYV.cjs');
8
9
 
9
10
  // src/jobs/inngest.ts
11
+ var warnedUnsupported = false;
10
12
  function ezlogsInngestMiddleware() {
11
- return {
12
- name: "ezlogs-nextjs",
13
+ const middleware = function ezlogsInngestV4() {
14
+ if (!warnedUnsupported) {
15
+ warnedUnsupported = true;
16
+ chunkTWTG5ILJ_cjs.logger.warn(
17
+ "ezlogsInngestMiddleware supports Inngest v3 only; on this Inngest version it does nothing (jobs are not captured)"
18
+ );
19
+ }
20
+ return {};
21
+ };
22
+ Object.defineProperty(middleware, "name", { value: "ezlogs-nextjs" });
23
+ return Object.assign(middleware, {
13
24
  init() {
14
25
  return {
15
26
  // Hook called once per function run (e.g. one trigger fires
16
27
  // one function). Returns per-run hooks that fire as the run
17
28
  // proceeds. We track the run via closure-scoped state.
18
29
  onFunctionRun(args) {
19
- const fnId = args.fn?.id ?? "inngest-function";
30
+ const fnId = functionId(args.fn) ?? "inngest-function";
20
31
  const runId = args.ctx?.runId ?? null;
21
32
  const attempt = typeof args.ctx?.attempt === "number" ? args.ctx.attempt : 0;
22
33
  const eventName = args.ctx?.event?.name ?? null;
23
34
  const eventData = args.ctx?.event?.data;
24
- const correlationId = chunkUWHYJH4M_cjs.extractCorrelationFromPayload(args.ctx?.event?.data) ?? chunkWASXTDLW_cjs.generate();
35
+ const correlationId = chunkR6IWVTX3_cjs.extractCorrelationFromPayload(args.ctx?.event?.data) ?? chunkMMLQXSSD_cjs.generate();
25
36
  const startedAt = /* @__PURE__ */ new Date();
26
37
  const startMs = startedAt.getTime();
27
38
  return {
@@ -33,7 +44,7 @@ function ezlogsInngestMiddleware() {
33
44
  const error = result?.error;
34
45
  const outcome = error ? "failure" : "success";
35
46
  const errorMessage = error ? formatError(error) : null;
36
- chunkUWHYJH4M_cjs.emitJobEvent({
47
+ chunkR6IWVTX3_cjs.emitJobEvent({
37
48
  jobClass: fnId,
38
49
  jobId: runId,
39
50
  queue: eventName,
@@ -46,7 +57,7 @@ function ezlogsInngestMiddleware() {
46
57
  payload: eventData
47
58
  });
48
59
  } catch (error) {
49
- chunkV5YNKKTG_cjs.logger.debug(`inngest capture failed: ${describeError(error)}`);
60
+ chunkTWTG5ILJ_cjs.logger.debug(`inngest capture failed: ${describeError(error)}`);
50
61
  }
51
62
  }
52
63
  };
@@ -56,43 +67,69 @@ function ezlogsInngestMiddleware() {
56
67
  // downstream function's onFunctionRun can read it back.
57
68
  onSendEvent() {
58
69
  return {
70
+ // Inngest awaits this hook (MaybePromise) and sends the
71
+ // payloads it returns. Stamped copies: the caller's event
72
+ // objects are never written (one reused across requests
73
+ // would carry the first request's id and actor).
59
74
  transformInput(args) {
60
75
  try {
61
- const id = chunkWASXTDLW_cjs.current();
76
+ const id = chunkMMLQXSSD_cjs.current();
62
77
  if (!id) return;
63
78
  if (!Array.isArray(args.payloads)) return;
64
- for (const payload of args.payloads) {
65
- if (!payload || typeof payload !== "object") continue;
79
+ const stamped = [];
80
+ const payloads = args.payloads.map((payload) => {
81
+ if (!payload || typeof payload !== "object") return payload;
66
82
  const data = payload.data ?? {};
67
83
  if (typeof data !== "object" || data === null || Array.isArray(data)) {
68
- continue;
84
+ return payload;
69
85
  }
70
86
  if (typeof data.ezlogs_correlation_id === "string") {
71
- continue;
87
+ return payload;
72
88
  }
73
- data.ezlogs_correlation_id = id;
74
- payload.data = data;
75
- }
89
+ const copy = { ...data, ezlogs_correlation_id: id };
90
+ if (!(chunkR6IWVTX3_cjs.ACTOR_PAYLOAD_KEY in copy)) stamped.push(copy);
91
+ return { ...payload, data: copy };
92
+ });
93
+ const pending = stamped.length > 0 ? chunkR6IWVTX3_cjs.signedActorToken(id) : null;
94
+ if (!pending) return { payloads };
95
+ return pending.then((token) => {
96
+ if (token) for (const data of stamped) data[chunkR6IWVTX3_cjs.ACTOR_PAYLOAD_KEY] = token;
97
+ return { payloads };
98
+ });
76
99
  } catch (error) {
77
- chunkV5YNKKTG_cjs.logger.debug(`inngest send-stamp failed: ${describeError(error)}`);
100
+ chunkTWTG5ILJ_cjs.logger.debug(`inngest send-stamp failed: ${describeError(error)}`);
78
101
  }
79
102
  }
80
103
  };
81
104
  }
82
105
  };
83
106
  }
84
- };
107
+ });
85
108
  }
86
109
  async function runInsideInngestCorrelation(event, callback) {
87
- const id = chunkUWHYJH4M_cjs.extractCorrelationFromPayload(event?.data) ?? chunkWASXTDLW_cjs.generate();
110
+ const carried = chunkR6IWVTX3_cjs.extractCorrelationFromPayload(event?.data);
111
+ const id = carried ?? chunkMMLQXSSD_cjs.generate();
112
+ const token = event?.data?.[chunkR6IWVTX3_cjs.ACTOR_PAYLOAD_KEY];
113
+ const actor = carried && token !== void 0 ? await chunkR6IWVTX3_cjs.readActorToken(token, carried) : null;
88
114
  const startedAt = /* @__PURE__ */ new Date();
89
- return chunkWASXTDLW_cjs.runWithCorrelation(id, async () => {
115
+ const body = async () => {
90
116
  try {
91
117
  return await callback();
92
118
  } finally {
93
- await chunkUWHYJH4M_cjs.flushJobAuditLog(startedAt);
119
+ await chunkR6IWVTX3_cjs.flushJobAuditLog(startedAt);
94
120
  }
95
- });
121
+ };
122
+ const scoped = () => body().finally(chunkMMLQXSSD_cjs.endCorrelationScope);
123
+ return chunkMMLQXSSD_cjs.runWithCorrelation(id, () => chunkMMLQXSSD_cjs.runWithActorScope(scoped, actor));
124
+ }
125
+ function functionId(fn) {
126
+ try {
127
+ const id = typeof fn?.id === "function" ? fn.id() : fn?.id;
128
+ if (typeof id === "string" && id) return id;
129
+ } catch {
130
+ }
131
+ const optsId = fn?.opts?.id;
132
+ return typeof optsId === "string" && optsId ? optsId : null;
96
133
  }
97
134
  function formatError(error) {
98
135
  if (error instanceof Error) {
@@ -1,9 +1,13 @@
1
1
  /**
2
- * Returns an Inngest middleware object. Pass it via:
2
+ * Returns an Inngest v3 middleware. Pass it via:
3
3
  * new Inngest({ middleware: [ezlogsInngestMiddleware()] })
4
4
  *
5
5
  * Returned `unknown` because Inngest's `Middleware` type is parameterized
6
6
  * over the client and we don't want to take a hard type dep.
7
+ *
8
+ * Inngest v4 has a different middleware API (a class it calls `new`
9
+ * on) that this adapter doesn't support. There the middleware does
10
+ * nothing and warns once, instead of crashing `inngest.send`.
7
11
  */
8
12
  declare function ezlogsInngestMiddleware(): unknown;
9
13
  /**
@@ -1,9 +1,13 @@
1
1
  /**
2
- * Returns an Inngest middleware object. Pass it via:
2
+ * Returns an Inngest v3 middleware. Pass it via:
3
3
  * new Inngest({ middleware: [ezlogsInngestMiddleware()] })
4
4
  *
5
5
  * Returned `unknown` because Inngest's `Middleware` type is parameterized
6
6
  * over the client and we don't want to take a hard type dep.
7
+ *
8
+ * Inngest v4 has a different middleware API (a class it calls `new`
9
+ * on) that this adapter doesn't support. There the middleware does
10
+ * nothing and warns once, instead of crashing `inngest.send`.
7
11
  */
8
12
  declare function ezlogsInngestMiddleware(): unknown;
9
13
  /**
@@ -1,20 +1,31 @@
1
- import { extractCorrelationFromPayload, emitJobEvent, flushJobAuditLog } from '../chunk-UQHEW6ER.js';
2
- import '../chunk-CU45I2WR.js';
3
- import { current, generate, runWithCorrelation } from '../chunk-CE3R7DVB.js';
4
- import { logger } from '../chunk-C5OSM557.js';
1
+ import { ACTOR_PAYLOAD_KEY, signedActorToken, extractCorrelationFromPayload, emitJobEvent, readActorToken, flushJobAuditLog } from '../chunk-U22T7QYY.js';
2
+ import '../chunk-MHBS5FEP.js';
3
+ import '../chunk-NINIAL7M.js';
4
+ import { current, generate, runWithCorrelation, runWithActorScope, endCorrelationScope } from '../chunk-GI6BM4MR.js';
5
+ import { logger } from '../chunk-FCFVIRQF.js';
5
6
  import '../chunk-SIAA4J6H.js';
6
7
 
7
8
  // src/jobs/inngest.ts
9
+ var warnedUnsupported = false;
8
10
  function ezlogsInngestMiddleware() {
9
- return {
10
- name: "ezlogs-nextjs",
11
+ const middleware = function ezlogsInngestV4() {
12
+ if (!warnedUnsupported) {
13
+ warnedUnsupported = true;
14
+ logger.warn(
15
+ "ezlogsInngestMiddleware supports Inngest v3 only; on this Inngest version it does nothing (jobs are not captured)"
16
+ );
17
+ }
18
+ return {};
19
+ };
20
+ Object.defineProperty(middleware, "name", { value: "ezlogs-nextjs" });
21
+ return Object.assign(middleware, {
11
22
  init() {
12
23
  return {
13
24
  // Hook called once per function run (e.g. one trigger fires
14
25
  // one function). Returns per-run hooks that fire as the run
15
26
  // proceeds. We track the run via closure-scoped state.
16
27
  onFunctionRun(args) {
17
- const fnId = args.fn?.id ?? "inngest-function";
28
+ const fnId = functionId(args.fn) ?? "inngest-function";
18
29
  const runId = args.ctx?.runId ?? null;
19
30
  const attempt = typeof args.ctx?.attempt === "number" ? args.ctx.attempt : 0;
20
31
  const eventName = args.ctx?.event?.name ?? null;
@@ -54,23 +65,35 @@ function ezlogsInngestMiddleware() {
54
65
  // downstream function's onFunctionRun can read it back.
55
66
  onSendEvent() {
56
67
  return {
68
+ // Inngest awaits this hook (MaybePromise) and sends the
69
+ // payloads it returns. Stamped copies: the caller's event
70
+ // objects are never written (one reused across requests
71
+ // would carry the first request's id and actor).
57
72
  transformInput(args) {
58
73
  try {
59
74
  const id = current();
60
75
  if (!id) return;
61
76
  if (!Array.isArray(args.payloads)) return;
62
- for (const payload of args.payloads) {
63
- if (!payload || typeof payload !== "object") continue;
77
+ const stamped = [];
78
+ const payloads = args.payloads.map((payload) => {
79
+ if (!payload || typeof payload !== "object") return payload;
64
80
  const data = payload.data ?? {};
65
81
  if (typeof data !== "object" || data === null || Array.isArray(data)) {
66
- continue;
82
+ return payload;
67
83
  }
68
84
  if (typeof data.ezlogs_correlation_id === "string") {
69
- continue;
85
+ return payload;
70
86
  }
71
- data.ezlogs_correlation_id = id;
72
- payload.data = data;
73
- }
87
+ const copy = { ...data, ezlogs_correlation_id: id };
88
+ if (!(ACTOR_PAYLOAD_KEY in copy)) stamped.push(copy);
89
+ return { ...payload, data: copy };
90
+ });
91
+ const pending = stamped.length > 0 ? signedActorToken(id) : null;
92
+ if (!pending) return { payloads };
93
+ return pending.then((token) => {
94
+ if (token) for (const data of stamped) data[ACTOR_PAYLOAD_KEY] = token;
95
+ return { payloads };
96
+ });
74
97
  } catch (error) {
75
98
  logger.debug(`inngest send-stamp failed: ${describeError(error)}`);
76
99
  }
@@ -79,18 +102,32 @@ function ezlogsInngestMiddleware() {
79
102
  }
80
103
  };
81
104
  }
82
- };
105
+ });
83
106
  }
84
107
  async function runInsideInngestCorrelation(event, callback) {
85
- const id = extractCorrelationFromPayload(event?.data) ?? generate();
108
+ const carried = extractCorrelationFromPayload(event?.data);
109
+ const id = carried ?? generate();
110
+ const token = event?.data?.[ACTOR_PAYLOAD_KEY];
111
+ const actor = carried && token !== void 0 ? await readActorToken(token, carried) : null;
86
112
  const startedAt = /* @__PURE__ */ new Date();
87
- return runWithCorrelation(id, async () => {
113
+ const body = async () => {
88
114
  try {
89
115
  return await callback();
90
116
  } finally {
91
117
  await flushJobAuditLog(startedAt);
92
118
  }
93
- });
119
+ };
120
+ const scoped = () => body().finally(endCorrelationScope);
121
+ return runWithCorrelation(id, () => runWithActorScope(scoped, actor));
122
+ }
123
+ function functionId(fn) {
124
+ try {
125
+ const id = typeof fn?.id === "function" ? fn.id() : fn?.id;
126
+ if (typeof id === "string" && id) return id;
127
+ } catch {
128
+ }
129
+ const optsId = fn?.opts?.id;
130
+ return typeof optsId === "string" && optsId ? optsId : null;
94
131
  }
95
132
  function formatError(error) {
96
133
  if (error instanceof Error) {
@@ -1,9 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var chunkUWHYJH4M_cjs = require('../chunk-UWHYJH4M.cjs');
4
- require('../chunk-YX2XPLOA.cjs');
5
- require('../chunk-WASXTDLW.cjs');
6
- require('../chunk-V5YNKKTG.cjs');
3
+ var chunkR6IWVTX3_cjs = require('../chunk-R6IWVTX3.cjs');
4
+ require('../chunk-GMHOUAKD.cjs');
5
+ require('../chunk-YXZZNZG6.cjs');
6
+ require('../chunk-MMLQXSSD.cjs');
7
+ require('../chunk-TWTG5ILJ.cjs');
7
8
  require('../chunk-ZDSQYCYV.cjs');
8
9
 
9
10
  // src/jobs/supabase-queues.ts
@@ -40,19 +41,21 @@ function wrapPgmqSchema(schema) {
40
41
  }
41
42
  function stampPgmqMessage(args) {
42
43
  if ("message" in args && isPlainObject(args.message)) {
43
- chunkUWHYJH4M_cjs.stampCorrelationOnPayload(args.message);
44
- } else if (Array.isArray(args.messages)) {
45
- for (const msg of args.messages) {
46
- if (isPlainObject(msg)) {
47
- chunkUWHYJH4M_cjs.stampCorrelationOnPayload(msg);
48
- }
49
- }
44
+ return { ...args, message: chunkR6IWVTX3_cjs.stampCorrelationOnPayload(args.message) };
45
+ }
46
+ if (Array.isArray(args.messages)) {
47
+ return {
48
+ ...args,
49
+ messages: args.messages.map(
50
+ (msg) => isPlainObject(msg) ? chunkR6IWVTX3_cjs.stampCorrelationOnPayload(msg) : msg
51
+ )
52
+ };
50
53
  }
51
54
  return args;
52
55
  }
53
56
  async function captureQueueMessage(meta, work) {
54
- const correlationId = chunkUWHYJH4M_cjs.extractCorrelationFromPayload(meta.message);
55
- return chunkUWHYJH4M_cjs.runJobWithCapture(
57
+ const correlationId = chunkR6IWVTX3_cjs.extractCorrelationFromPayload(meta.message);
58
+ return chunkR6IWVTX3_cjs.runJobWithCapture(
56
59
  {
57
60
  jobClass: meta.queueName,
58
61
  jobId: meta.messageId !== void 0 && meta.messageId !== null ? String(meta.messageId) : null,
@@ -66,8 +69,7 @@ async function captureQueueMessage(meta, work) {
66
69
  }
67
70
  function stampPgmqPayload(message) {
68
71
  if (!isPlainObject(message)) return message;
69
- chunkUWHYJH4M_cjs.stampCorrelationOnPayload(message);
70
- return message;
72
+ return chunkR6IWVTX3_cjs.stampCorrelationOnPayload(message);
71
73
  }
72
74
  function isPlainObject(value) {
73
75
  if (value === null || typeof value !== "object") return false;
@@ -30,7 +30,7 @@ declare function captureQueueMessage<T>(meta: {
30
30
  /**
31
31
  * Imperatively stamp the correlation id onto a pgmq message body
32
32
  * before passing it to a non-wrapped client. Available for users
33
- * who want explicit control.
33
+ * who want explicit control. Returns a stamped copy; send that one.
34
34
  */
35
35
  declare function stampPgmqPayload(message: unknown): unknown;
36
36