ezlogs-nextjs 0.3.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.
- package/README.md +15 -2
- package/bin/install-triggers.cjs +33 -5
- package/dist/actors/index.cjs +5 -37
- package/dist/actors/index.js +2 -34
- package/dist/chunk-C5OSM557.js +39 -0
- package/dist/chunk-CE3R7DVB.js +1127 -0
- package/dist/chunk-CU45I2WR.js +498 -0
- package/dist/chunk-DKRNNI4I.js +180 -0
- package/dist/chunk-SIAA4J6H.js +17 -0
- package/dist/chunk-UQHEW6ER.js +119 -0
- package/dist/chunk-UWHYJH4M.cjs +126 -0
- package/dist/chunk-V5YNKKTG.cjs +42 -0
- package/dist/chunk-WASXTDLW.cjs +1151 -0
- package/dist/chunk-Y5NSHKWQ.cjs +182 -0
- package/dist/chunk-YX2XPLOA.cjs +508 -0
- package/dist/chunk-ZDSQYCYV.cjs +21 -0
- package/dist/db/drizzle.cjs +6 -821
- package/dist/db/drizzle.js +5 -820
- package/dist/db/prisma.cjs +28 -853
- package/dist/db/prisma.js +14 -839
- package/dist/db/supabase.cjs +21 -836
- package/dist/db/supabase.js +14 -829
- package/dist/index.cjs +132 -2362
- package/dist/index.js +34 -2270
- package/dist/jobs/bullmq.cjs +13 -1465
- package/dist/jobs/bullmq.js +7 -1459
- package/dist/jobs/inngest.cjs +14 -1422
- package/dist/jobs/inngest.js +8 -1416
- package/dist/jobs/supabase-queues.cjs +15 -1467
- package/dist/jobs/supabase-queues.js +10 -1462
- package/dist/jobs/trigger.cjs +9 -1454
- package/dist/jobs/trigger.js +5 -1450
- package/dist/jobs/upstash-workflow.cjs +30 -1456
- package/dist/jobs/upstash-workflow.d.cts +2 -1
- package/dist/jobs/upstash-workflow.d.ts +2 -1
- package/dist/jobs/upstash-workflow.js +27 -1453
- package/dist/nextjs-plugin/index.cjs +1 -1
- package/dist/nextjs-plugin/index.js +1 -1
- package/dist/nextjs-plugin/loader.cjs +21 -25
- package/dist/nextjs-plugin/loader.js +1 -5
- package/dist/triggers/index.cjs +40 -1434
- package/dist/triggers/index.js +6 -1421
- package/package.json +2 -1
package/dist/db/prisma.cjs
CHANGED
|
@@ -1,833 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
// src/logger.ts
|
|
11
|
-
var LEVEL_RANK = {
|
|
12
|
-
debug: 10,
|
|
13
|
-
info: 20,
|
|
14
|
-
warn: 30,
|
|
15
|
-
error: 40,
|
|
16
|
-
silent: 100
|
|
17
|
-
};
|
|
18
|
-
var LEVEL_KEY = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:log-level");
|
|
19
|
-
function readLevel() {
|
|
20
|
-
const g = globalThis;
|
|
21
|
-
return g[LEVEL_KEY] ?? "warn";
|
|
22
|
-
}
|
|
23
|
-
function shouldLog(level) {
|
|
24
|
-
return LEVEL_RANK[level] >= LEVEL_RANK[readLevel()];
|
|
25
|
-
}
|
|
26
|
-
function safeWrite(level, message) {
|
|
27
|
-
try {
|
|
28
|
-
if (!shouldLog(level)) return;
|
|
29
|
-
const line = `[ezlogs] ${message}`;
|
|
30
|
-
if (level === "error" || level === "warn") {
|
|
31
|
-
console.error(line);
|
|
32
|
-
} else {
|
|
33
|
-
console.log(line);
|
|
34
|
-
}
|
|
35
|
-
} catch {
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
var logger = {
|
|
39
|
-
debug: (message) => safeWrite("debug", message),
|
|
40
|
-
info: (message) => safeWrite("info", message),
|
|
41
|
-
warn: (message) => safeWrite("warn", message),
|
|
42
|
-
error: (message) => safeWrite("error", message)
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// src/edge-fallback.ts
|
|
46
|
-
var ALS_CTOR = loadAsyncLocalStorage();
|
|
47
|
-
function loadAsyncLocalStorage() {
|
|
48
|
-
const fromGlobal = globalThis.AsyncLocalStorage;
|
|
49
|
-
if (typeof fromGlobal === "function") return fromGlobal;
|
|
50
|
-
try {
|
|
51
|
-
const req = typeof __require !== "undefined" ? __require : (
|
|
52
|
-
// In ESM, `createRequire` is the official escape hatch.
|
|
53
|
-
// We pull it via globalThis to avoid a static `import`
|
|
54
|
-
// that would itself be browser-bundled.
|
|
55
|
-
void 0
|
|
56
|
-
);
|
|
57
|
-
if (typeof req === "function") {
|
|
58
|
-
const moduleName = ["async", "hooks"].join("_");
|
|
59
|
-
const mod = req("node:" + moduleName);
|
|
60
|
-
if (typeof mod.AsyncLocalStorage === "function") return mod.AsyncLocalStorage;
|
|
61
|
-
}
|
|
62
|
-
} catch {
|
|
63
|
-
}
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
function makeAsyncContextCapture() {
|
|
67
|
-
if (!ALS_CTOR) return null;
|
|
68
|
-
const ctor = ALS_CTOR;
|
|
69
|
-
if (typeof ctor.snapshot !== "function") return null;
|
|
70
|
-
const snapshot = ctor.snapshot.bind(ctor);
|
|
71
|
-
return () => {
|
|
72
|
-
const runSnapshot = snapshot();
|
|
73
|
-
return (fn) => runSnapshot(fn);
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
function createScopedStorage(key = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:default-scoped-storage")) {
|
|
77
|
-
const globalStore = globalThis;
|
|
78
|
-
let entry = globalStore[key];
|
|
79
|
-
if (!entry) {
|
|
80
|
-
const als2 = (() => {
|
|
81
|
-
if (!ALS_CTOR) return null;
|
|
82
|
-
try {
|
|
83
|
-
return new ALS_CTOR();
|
|
84
|
-
} catch {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
})();
|
|
88
|
-
entry = { als: als2, edgeFallback: null };
|
|
89
|
-
globalStore[key] = entry;
|
|
90
|
-
}
|
|
91
|
-
const als = entry.als;
|
|
92
|
-
const stateRef = entry;
|
|
93
|
-
return {
|
|
94
|
-
read() {
|
|
95
|
-
if (als) return als.getStore()?.value ?? null;
|
|
96
|
-
return stateRef.edgeFallback?.value ?? null;
|
|
97
|
-
},
|
|
98
|
-
run(value, callback) {
|
|
99
|
-
if (als) return als.run({ value }, callback);
|
|
100
|
-
const previous = stateRef.edgeFallback;
|
|
101
|
-
stateRef.edgeFallback = { value };
|
|
102
|
-
try {
|
|
103
|
-
return callback();
|
|
104
|
-
} finally {
|
|
105
|
-
stateRef.edgeFallback = previous;
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
update(value) {
|
|
109
|
-
if (als) {
|
|
110
|
-
const store = als.getStore();
|
|
111
|
-
if (store) store.value = value;
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
stateRef.edgeFallback = { value };
|
|
115
|
-
},
|
|
116
|
-
_resetForTests() {
|
|
117
|
-
stateRef.edgeFallback = null;
|
|
118
|
-
},
|
|
119
|
-
_isAsyncLocalStorage() {
|
|
120
|
-
return als !== null;
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// src/correlation.ts
|
|
126
|
-
var storage = createScopedStorage(
|
|
127
|
-
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:correlation-storage")
|
|
128
|
-
);
|
|
129
|
-
function current() {
|
|
130
|
-
return storage.read();
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// src/configuration.ts
|
|
134
|
-
var DEFAULT_EXCLUDED_PATHS = Object.freeze([
|
|
135
|
-
// Next.js / Vercel infrastructure noise
|
|
136
|
-
"/_next/*",
|
|
137
|
-
"/_vercel/*",
|
|
138
|
-
"/__nextjs_*",
|
|
139
|
-
// NextAuth.js / Auth.js — skip session-refresh polls and CSRF/provider
|
|
140
|
-
// metadata GETs (high-frequency, no business meaning), but CAPTURE
|
|
141
|
-
// POSTs (callbacks, signin, signout, register). The POSTs are the
|
|
142
|
-
// actual auth events users care about; the GETs are pure plumbing.
|
|
143
|
-
Object.freeze({ path: "/api/auth/*", methods: ["GET", "HEAD"] }),
|
|
144
|
-
// Generic infrastructure
|
|
145
|
-
"/health*",
|
|
146
|
-
"/api/health",
|
|
147
|
-
"/up",
|
|
148
|
-
"/alive",
|
|
149
|
-
"/ready",
|
|
150
|
-
"/metrics",
|
|
151
|
-
"/favicon.ico",
|
|
152
|
-
"/.well-known*",
|
|
153
|
-
"/robots.txt",
|
|
154
|
-
"/sitemap.xml",
|
|
155
|
-
"/*.hot-update.*",
|
|
156
|
-
// Auth routes (common patterns across Clerk, NextAuth, custom auth)
|
|
157
|
-
"*/login*",
|
|
158
|
-
"*/logout*",
|
|
159
|
-
"*/sign-in*",
|
|
160
|
-
"*/sign-out*",
|
|
161
|
-
"*/signin*",
|
|
162
|
-
"*/signout*",
|
|
163
|
-
"/session*"
|
|
164
|
-
]);
|
|
165
|
-
var DEFAULT_EXCLUDED_TABLES = Object.freeze([
|
|
166
|
-
// Prisma migration ledger
|
|
167
|
-
"_prisma_migrations",
|
|
168
|
-
// Auth.js / NextAuth Prisma adapter — session/verification-token churn
|
|
169
|
-
// is auth machinery, not business activity. Account/User stay captured.
|
|
170
|
-
"Session",
|
|
171
|
-
"VerificationToken",
|
|
172
|
-
// Common cross-stack convention names for the same things
|
|
173
|
-
"sessions",
|
|
174
|
-
"session",
|
|
175
|
-
"verification_tokens"
|
|
176
|
-
]);
|
|
177
|
-
var DEFAULT_EXCLUDED_JOB_CLASSES = Object.freeze([]);
|
|
178
|
-
var DEFAULT_EXCLUDED_GRAPHQL_OPERATIONS = Object.freeze(["IntrospectionQuery", "__*"]);
|
|
179
|
-
var Configuration = class {
|
|
180
|
-
serverUrl = null;
|
|
181
|
-
projectToken = null;
|
|
182
|
-
captureHttp = true;
|
|
183
|
-
captureDb = true;
|
|
184
|
-
captureJobs = true;
|
|
185
|
-
bufferSize = 1e4;
|
|
186
|
-
/** Default 3000 ms — equivalent to Ruby's 3 seconds. */
|
|
187
|
-
sendInterval = 3e3;
|
|
188
|
-
retryAttempts = 3;
|
|
189
|
-
logLevel = "warn";
|
|
190
|
-
patchGlobalFetch = true;
|
|
191
|
-
actorFromSupabase = true;
|
|
192
|
-
allowInsecureTransport = false;
|
|
193
|
-
excludedPaths = [];
|
|
194
|
-
excludedTables = [];
|
|
195
|
-
excludedJobClasses = [];
|
|
196
|
-
excludedGraphqlOperations = [];
|
|
197
|
-
excludedGraphqlVariableKeys = [];
|
|
198
|
-
actorFromRequest = null;
|
|
199
|
-
displayNameFor = {};
|
|
200
|
-
isServerActionError = null;
|
|
201
|
-
databaseReader = null;
|
|
202
|
-
databaseUrl = null;
|
|
203
|
-
/**
|
|
204
|
-
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
205
|
-
* at agent startup by reading `information_schema.triggers`. The
|
|
206
|
-
* supabase / drizzle / prisma adapters use this to decide whether
|
|
207
|
-
* their per-mutation emit is redundant: if the table is trigger-
|
|
208
|
-
* tracked the audit-log reader will surface the same write with full
|
|
209
|
-
* old/new state, so the adapter's emit is suppressed. Tables NOT in
|
|
210
|
-
* this set fall through to the adapter so they're still captured.
|
|
211
|
-
*
|
|
212
|
-
* `null` means "detection hasn't run yet or failed" — in that state
|
|
213
|
-
* `databaseReader`-wired apps suppress every adapter emit (the
|
|
214
|
-
* original Phase 5 behavior). After detection completes the agent
|
|
215
|
-
* gives the more accurate per-table answer.
|
|
216
|
-
*/
|
|
217
|
-
triggerTrackedTables = null;
|
|
218
|
-
apply(options) {
|
|
219
|
-
this.serverUrl = options.serverUrl;
|
|
220
|
-
this.projectToken = options.projectToken;
|
|
221
|
-
if (options.captureHttp !== void 0) this.captureHttp = options.captureHttp;
|
|
222
|
-
if (options.captureDb !== void 0) this.captureDb = options.captureDb;
|
|
223
|
-
if (options.captureJobs !== void 0) this.captureJobs = options.captureJobs;
|
|
224
|
-
if (options.bufferSize !== void 0) this.bufferSize = options.bufferSize;
|
|
225
|
-
if (options.sendInterval !== void 0) this.sendInterval = options.sendInterval;
|
|
226
|
-
if (options.retryAttempts !== void 0) this.retryAttempts = options.retryAttempts;
|
|
227
|
-
if (options.excludedPaths) this.excludedPaths = [...options.excludedPaths];
|
|
228
|
-
if (options.excludedTables) this.excludedTables = [...options.excludedTables];
|
|
229
|
-
if (options.excludedJobClasses) {
|
|
230
|
-
this.excludedJobClasses = [...options.excludedJobClasses];
|
|
231
|
-
}
|
|
232
|
-
if (options.excludedGraphqlOperations) {
|
|
233
|
-
this.excludedGraphqlOperations = [...options.excludedGraphqlOperations];
|
|
234
|
-
}
|
|
235
|
-
if (options.excludedGraphqlVariableKeys) {
|
|
236
|
-
this.excludedGraphqlVariableKeys = [...options.excludedGraphqlVariableKeys];
|
|
237
|
-
}
|
|
238
|
-
if (options.actorFromRequest) this.actorFromRequest = options.actorFromRequest;
|
|
239
|
-
if (options.displayNameFor) this.displayNameFor = { ...options.displayNameFor };
|
|
240
|
-
if (options.isServerActionError) this.isServerActionError = options.isServerActionError;
|
|
241
|
-
if (options.patchGlobalFetch !== void 0) {
|
|
242
|
-
this.patchGlobalFetch = options.patchGlobalFetch;
|
|
243
|
-
}
|
|
244
|
-
if (options.allowInsecureTransport !== void 0) {
|
|
245
|
-
this.allowInsecureTransport = options.allowInsecureTransport;
|
|
246
|
-
}
|
|
247
|
-
if (options.actorFromSupabase !== void 0) {
|
|
248
|
-
this.actorFromSupabase = options.actorFromSupabase;
|
|
249
|
-
}
|
|
250
|
-
if (options.databaseReader !== void 0) {
|
|
251
|
-
this.databaseReader = options.databaseReader;
|
|
252
|
-
}
|
|
253
|
-
if (options.databaseUrl !== void 0) {
|
|
254
|
-
this.databaseUrl = options.databaseUrl;
|
|
255
|
-
}
|
|
256
|
-
if (options.logLevel) this.logLevel = options.logLevel;
|
|
257
|
-
}
|
|
258
|
-
allExcludedPaths() {
|
|
259
|
-
return [...DEFAULT_EXCLUDED_PATHS, ...this.excludedPaths];
|
|
260
|
-
}
|
|
261
|
-
allExcludedTables() {
|
|
262
|
-
return [...DEFAULT_EXCLUDED_TABLES, ...this.excludedTables];
|
|
263
|
-
}
|
|
264
|
-
allExcludedJobClasses() {
|
|
265
|
-
return [...DEFAULT_EXCLUDED_JOB_CLASSES, ...this.excludedJobClasses];
|
|
266
|
-
}
|
|
267
|
-
allExcludedGraphqlOperations() {
|
|
268
|
-
return [...DEFAULT_EXCLUDED_GRAPHQL_OPERATIONS, ...this.excludedGraphqlOperations];
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
var GLOBAL_CONFIG_KEY = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:configuration");
|
|
272
|
-
function configuration() {
|
|
273
|
-
const g = globalThis;
|
|
274
|
-
if (!g[GLOBAL_CONFIG_KEY]) {
|
|
275
|
-
g[GLOBAL_CONFIG_KEY] = new Configuration();
|
|
276
|
-
}
|
|
277
|
-
return g[GLOBAL_CONFIG_KEY];
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// src/event-builder.ts
|
|
281
|
-
var SENSITIVE_KEYS = Object.freeze([
|
|
282
|
-
"password",
|
|
283
|
-
"token",
|
|
284
|
-
"secret",
|
|
285
|
-
"api_key",
|
|
286
|
-
"credit_card"
|
|
287
|
-
]);
|
|
288
|
-
var VALID_SOURCE_TYPES = [
|
|
289
|
-
"http_request",
|
|
290
|
-
"background_job",
|
|
291
|
-
"database_callback"
|
|
292
|
-
];
|
|
293
|
-
var VALID_OUTCOMES = ["success", "failure"];
|
|
294
|
-
function build(input) {
|
|
295
|
-
const source_type = validateSourceType(input.source_type);
|
|
296
|
-
const source_data_raw = validateSourceData(input.source_data);
|
|
297
|
-
const outcome = validateOutcome(input.outcome);
|
|
298
|
-
const timestamp = validateTimestamp(input.timestamp ?? null);
|
|
299
|
-
const resource_ids = validateResourceIds(input.resource_ids ?? []);
|
|
300
|
-
const context_raw = validateContext(input.context ?? null);
|
|
301
|
-
const duration_ms = validateDurationMs();
|
|
302
|
-
const error_message = input.error_message ?? null;
|
|
303
|
-
validateOutcomeErrorConsistency(outcome, error_message);
|
|
304
|
-
const source_data = sanitizeRecord(source_data_raw);
|
|
305
|
-
const context = context_raw === null ? null : sanitizeRecord(context_raw);
|
|
306
|
-
const correlation_id = input.correlation_id ?? null;
|
|
307
|
-
const event = {
|
|
308
|
-
timestamp,
|
|
309
|
-
source_type,
|
|
310
|
-
source_data,
|
|
311
|
-
outcome,
|
|
312
|
-
correlation_id,
|
|
313
|
-
resource_ids,
|
|
314
|
-
context,
|
|
315
|
-
duration_ms,
|
|
316
|
-
error_message
|
|
317
|
-
};
|
|
318
|
-
return event;
|
|
319
|
-
}
|
|
320
|
-
function validateSourceType(value) {
|
|
321
|
-
const normalized = String(value);
|
|
322
|
-
if (!VALID_SOURCE_TYPES.includes(normalized)) {
|
|
323
|
-
throw new ArgError(
|
|
324
|
-
`source_type must be one of ${VALID_SOURCE_TYPES.join(", ")}, got: ${normalized}`
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
return normalized;
|
|
328
|
-
}
|
|
329
|
-
function validateSourceData(value) {
|
|
330
|
-
if (value === null || value === void 0) {
|
|
331
|
-
throw new ArgError("source_data is required");
|
|
332
|
-
}
|
|
333
|
-
if (!isPlainObject(value)) {
|
|
334
|
-
throw new ArgError(
|
|
335
|
-
`source_data must be an object, got: ${describe(value)}`
|
|
336
|
-
);
|
|
337
|
-
}
|
|
338
|
-
return value;
|
|
339
|
-
}
|
|
340
|
-
function validateOutcome(value) {
|
|
341
|
-
const normalized = String(value);
|
|
342
|
-
if (!VALID_OUTCOMES.includes(normalized)) {
|
|
343
|
-
throw new ArgError(
|
|
344
|
-
`outcome must be one of ${VALID_OUTCOMES.join(", ")}, got: ${normalized}`
|
|
345
|
-
);
|
|
346
|
-
}
|
|
347
|
-
return normalized;
|
|
348
|
-
}
|
|
349
|
-
function validateTimestamp(value) {
|
|
350
|
-
if (value === null) {
|
|
351
|
-
return (/* @__PURE__ */ new Date()).toISOString();
|
|
352
|
-
}
|
|
353
|
-
if (typeof value === "string") {
|
|
354
|
-
const parsed = new Date(value);
|
|
355
|
-
if (Number.isNaN(parsed.getTime())) {
|
|
356
|
-
throw new ArgError(
|
|
357
|
-
`timestamp must be valid ISO 8601 format, got: ${value}`
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
return value;
|
|
361
|
-
}
|
|
362
|
-
if (value instanceof Date) {
|
|
363
|
-
if (Number.isNaN(value.getTime())) {
|
|
364
|
-
throw new ArgError("timestamp must be a valid Date");
|
|
365
|
-
}
|
|
366
|
-
return value.toISOString();
|
|
367
|
-
}
|
|
368
|
-
throw new ArgError(
|
|
369
|
-
`timestamp must be a string or Date, got: ${describe(value)}`
|
|
370
|
-
);
|
|
371
|
-
}
|
|
372
|
-
function validateResourceIds(value) {
|
|
373
|
-
if (!Array.isArray(value)) {
|
|
374
|
-
throw new ArgError(`resource_ids must be an array, got: ${describe(value)}`);
|
|
375
|
-
}
|
|
376
|
-
value.forEach((entry, index) => {
|
|
377
|
-
if (!isPlainObject(entry)) {
|
|
378
|
-
throw new ArgError(
|
|
379
|
-
`resource_ids[${index}] must be an object, got: ${describe(entry)}`
|
|
380
|
-
);
|
|
381
|
-
}
|
|
382
|
-
const e = entry;
|
|
383
|
-
if (!("resource_type" in e) || !("resource_id" in e)) {
|
|
384
|
-
throw new ArgError(
|
|
385
|
-
`resource_ids[${index}] must have resource_type and resource_id keys, got: ${Object.keys(e).join(", ")}`
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
|
-
if (typeof e.resource_type !== "string") {
|
|
389
|
-
throw new ArgError(
|
|
390
|
-
`resource_ids[${index}].resource_type must be a string, got: ${describe(e.resource_type)}`
|
|
391
|
-
);
|
|
392
|
-
}
|
|
393
|
-
if (typeof e.resource_id !== "string") {
|
|
394
|
-
throw new ArgError(
|
|
395
|
-
`resource_ids[${index}].resource_id must be a string, got: ${describe(e.resource_id)}`
|
|
396
|
-
);
|
|
397
|
-
}
|
|
398
|
-
});
|
|
399
|
-
return value;
|
|
400
|
-
}
|
|
401
|
-
function validateContext(value) {
|
|
402
|
-
if (value === null) return null;
|
|
403
|
-
if (!isPlainObject(value)) {
|
|
404
|
-
throw new ArgError(
|
|
405
|
-
`context must be an object or null, got: ${describe(value)}`
|
|
406
|
-
);
|
|
407
|
-
}
|
|
408
|
-
return value;
|
|
409
|
-
}
|
|
410
|
-
function validateDurationMs(value) {
|
|
411
|
-
return null;
|
|
412
|
-
}
|
|
413
|
-
function validateOutcomeErrorConsistency(outcome, error_message) {
|
|
414
|
-
if (outcome === "failure") {
|
|
415
|
-
if (error_message === null || error_message === "") {
|
|
416
|
-
throw new ArgError("error_message is required when outcome is 'failure'");
|
|
417
|
-
}
|
|
418
|
-
} else if (outcome === "success") {
|
|
419
|
-
if (error_message !== null) {
|
|
420
|
-
throw new ArgError("error_message must be null when outcome is 'success'");
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
function sanitizeRecord(input) {
|
|
425
|
-
const result = {};
|
|
426
|
-
for (const key of Object.keys(input)) {
|
|
427
|
-
const value = input[key];
|
|
428
|
-
if (isSensitiveKey(key)) {
|
|
429
|
-
result[key] = "[FILTERED]";
|
|
430
|
-
} else if (isPlainObject(value)) {
|
|
431
|
-
result[key] = sanitizeRecord(value);
|
|
432
|
-
} else if (Array.isArray(value)) {
|
|
433
|
-
result[key] = value.map(
|
|
434
|
-
(item) => isPlainObject(item) ? sanitizeRecord(item) : item
|
|
435
|
-
);
|
|
436
|
-
} else {
|
|
437
|
-
result[key] = value;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
return result;
|
|
441
|
-
}
|
|
442
|
-
function isSensitiveKey(key) {
|
|
443
|
-
const lower = key.toLowerCase();
|
|
444
|
-
return SENSITIVE_KEYS.some((p) => lower.includes(p));
|
|
445
|
-
}
|
|
446
|
-
function isPlainObject(value) {
|
|
447
|
-
if (value === null || typeof value !== "object") return false;
|
|
448
|
-
const proto = Object.getPrototypeOf(value);
|
|
449
|
-
return proto === Object.prototype || proto === null;
|
|
450
|
-
}
|
|
451
|
-
function describe(value) {
|
|
452
|
-
if (value === null) return "null";
|
|
453
|
-
if (Array.isArray(value)) return "Array";
|
|
454
|
-
return typeof value;
|
|
455
|
-
}
|
|
456
|
-
var ArgError = class extends Error {
|
|
457
|
-
constructor(message) {
|
|
458
|
-
super(message);
|
|
459
|
-
this.name = "ArgError";
|
|
460
|
-
}
|
|
461
|
-
};
|
|
462
|
-
|
|
463
|
-
// src/pipeline/buffer.ts
|
|
464
|
-
var GLOBAL_QUEUE_KEY = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:buffer-queue");
|
|
465
|
-
function getQueue() {
|
|
466
|
-
const g = globalThis;
|
|
467
|
-
if (!g[GLOBAL_QUEUE_KEY]) g[GLOBAL_QUEUE_KEY] = [];
|
|
468
|
-
return g[GLOBAL_QUEUE_KEY];
|
|
469
|
-
}
|
|
470
|
-
function pushEvent(event) {
|
|
471
|
-
try {
|
|
472
|
-
const queue = getQueue();
|
|
473
|
-
const max = maxSize();
|
|
474
|
-
if (queue.length >= max) {
|
|
475
|
-
queue.shift();
|
|
476
|
-
logger.warn(`Buffer full (${max}), dropped oldest event`);
|
|
477
|
-
}
|
|
478
|
-
queue.push(event);
|
|
479
|
-
} catch (error) {
|
|
480
|
-
logger.error(`pushEvent failed: ${describe2(error)}`);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
function maxSize() {
|
|
484
|
-
try {
|
|
485
|
-
return configuration().bufferSize;
|
|
486
|
-
} catch {
|
|
487
|
-
return 1e4;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
function describe2(error) {
|
|
491
|
-
if (error instanceof Error) return `${error.name}: ${error.message}`;
|
|
492
|
-
return String(error);
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
// src/actor.ts
|
|
496
|
-
var storage2 = createScopedStorage(
|
|
497
|
-
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:actor-storage")
|
|
498
|
-
);
|
|
499
|
-
function getCurrentActor() {
|
|
500
|
-
return storage2.read();
|
|
501
|
-
}
|
|
502
|
-
var SENSITIVE_WORD_PATTERNS = Object.freeze([
|
|
503
|
-
"pin",
|
|
504
|
-
"otp",
|
|
505
|
-
"totp",
|
|
506
|
-
"iban",
|
|
507
|
-
"mfa",
|
|
508
|
-
"passphrase"
|
|
509
|
-
]);
|
|
510
|
-
function snakeLower(key) {
|
|
511
|
-
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
512
|
-
}
|
|
513
|
-
function matchesSensitiveKey(key, substringPatterns) {
|
|
514
|
-
const lower = key.toLowerCase();
|
|
515
|
-
const snake = snakeLower(key);
|
|
516
|
-
if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
|
|
517
|
-
return true;
|
|
518
|
-
}
|
|
519
|
-
const words = snake.split(/[^a-z0-9]+/);
|
|
520
|
-
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
521
|
-
}
|
|
522
|
-
function passesLuhn(digits) {
|
|
523
|
-
let sum = 0;
|
|
524
|
-
for (let i = 0; i < digits.length; i += 1) {
|
|
525
|
-
let d = Number(digits[digits.length - 1 - i]);
|
|
526
|
-
if (i % 2 === 1) {
|
|
527
|
-
d *= 2;
|
|
528
|
-
if (d > 9) d -= 9;
|
|
529
|
-
}
|
|
530
|
-
sum += d;
|
|
531
|
-
}
|
|
532
|
-
return sum % 10 === 0;
|
|
533
|
-
}
|
|
534
|
-
var SECRET_TEXT_PATTERNS = [
|
|
535
|
-
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
|
|
536
|
-
/\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
|
|
537
|
-
/\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
|
|
538
|
-
/\bsk-[A-Za-z0-9_-]{16,}/g,
|
|
539
|
-
/\bgh[pousr]_[A-Za-z0-9]{20,}/g,
|
|
540
|
-
/\bxox[abposr]-[A-Za-z0-9-]+/g,
|
|
541
|
-
/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
|
|
542
|
-
];
|
|
543
|
-
var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
|
|
544
|
-
function scrubSecretsInText(text, options = {}) {
|
|
545
|
-
let out = text;
|
|
546
|
-
for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
|
|
547
|
-
out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
|
|
548
|
-
const digits = match.replace(/[ -]/g, "");
|
|
549
|
-
return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
|
|
550
|
-
});
|
|
551
|
-
return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
// src/db/shared.ts
|
|
555
|
-
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
556
|
-
"id",
|
|
557
|
-
"createdAt",
|
|
558
|
-
"created_at",
|
|
559
|
-
"updatedAt",
|
|
560
|
-
"updated_at",
|
|
561
|
-
// Auth.js / NextAuth standard columns
|
|
562
|
-
"emailVerified",
|
|
563
|
-
"expires"
|
|
564
|
-
]);
|
|
565
|
-
var DB_SENSITIVE_PATTERNS = Object.freeze([
|
|
566
|
-
"password",
|
|
567
|
-
"token",
|
|
568
|
-
"secret",
|
|
569
|
-
"api_key",
|
|
570
|
-
"credit_card",
|
|
571
|
-
"ssn",
|
|
572
|
-
"social_security",
|
|
573
|
-
"encrypted",
|
|
574
|
-
"private_key",
|
|
575
|
-
"public_key",
|
|
576
|
-
"signing_key",
|
|
577
|
-
"pem",
|
|
578
|
-
"cipher",
|
|
579
|
-
"nonce",
|
|
580
|
-
"salt",
|
|
581
|
-
"digest",
|
|
582
|
-
"signature",
|
|
583
|
-
"hmac",
|
|
584
|
-
// 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
|
|
585
|
-
// the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
|
|
586
|
-
// `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
|
|
587
|
-
// SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
|
|
588
|
-
"credential",
|
|
589
|
-
"passwd",
|
|
590
|
-
"passcode",
|
|
591
|
-
"apikey",
|
|
592
|
-
"card_number",
|
|
593
|
-
"cardnumber",
|
|
594
|
-
"cvv",
|
|
595
|
-
"cvc"
|
|
596
|
-
]);
|
|
597
|
-
function emitDbEvent(input) {
|
|
598
|
-
try {
|
|
599
|
-
const config = configuration();
|
|
600
|
-
if (!config.captureDb) return;
|
|
601
|
-
if (config.allExcludedTables().includes(input.modelClass)) return;
|
|
602
|
-
if (config.databaseReader && !input._fromTrigger) {
|
|
603
|
-
const tracked = config.triggerTrackedTables;
|
|
604
|
-
if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
|
|
605
|
-
}
|
|
606
|
-
if (input.operation === "update") {
|
|
607
|
-
const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
|
|
608
|
-
if (meaningful.length === 0) return;
|
|
609
|
-
}
|
|
610
|
-
const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
|
|
611
|
-
const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
|
|
612
|
-
const context = buildContext(
|
|
613
|
-
related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
|
|
614
|
-
);
|
|
615
|
-
const event = build({
|
|
616
|
-
source_type: "database_callback",
|
|
617
|
-
source_data: {
|
|
618
|
-
model_class: input.modelClass,
|
|
619
|
-
operation: input.operation
|
|
620
|
-
},
|
|
621
|
-
outcome: "success",
|
|
622
|
-
correlation_id: current(),
|
|
623
|
-
resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
624
|
-
context,
|
|
625
|
-
duration_ms: null,
|
|
626
|
-
timestamp: input.timestamp ?? null
|
|
627
|
-
});
|
|
628
|
-
pushEvent(event);
|
|
629
|
-
} catch (error) {
|
|
630
|
-
logger.debug(`db emit failed: ${describeError(error)}`);
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
function buildContext(input) {
|
|
634
|
-
const context = {};
|
|
635
|
-
const actor = getCurrentActor();
|
|
636
|
-
if (actor) context.actor = actor;
|
|
637
|
-
if ((input.operation === "create" || input.operation === "destroy") && input.initialAttributes) {
|
|
638
|
-
const filtered = filterMeaningfulAttributes(input.initialAttributes);
|
|
639
|
-
if (filtered !== null) context.initial_attributes = filtered;
|
|
640
|
-
}
|
|
641
|
-
if (input.operation === "update" && input.changes) {
|
|
642
|
-
const filtered = filterMeaningfulChanges(input.changes);
|
|
643
|
-
if (filtered.length > 0) {
|
|
644
|
-
context.changes = filtered.map((c) => {
|
|
645
|
-
const out = {
|
|
646
|
-
attribute: c.attribute,
|
|
647
|
-
to: formatForJson(c.to)
|
|
648
|
-
};
|
|
649
|
-
if (c.from !== void 0) out.from = formatForJson(c.from);
|
|
650
|
-
return out;
|
|
651
|
-
});
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
const displayName = input.displayName ?? (input.modelInstance ? resolveDisplayName(input.modelClass, input.modelInstance) : null);
|
|
655
|
-
if (displayName !== null) context.display_name = displayName;
|
|
656
|
-
return Object.keys(context).length === 0 ? null : context;
|
|
657
|
-
}
|
|
658
|
-
function filterMeaningfulAttributes(attrs) {
|
|
659
|
-
const out = {};
|
|
660
|
-
for (const key of Object.keys(attrs)) {
|
|
661
|
-
if (!isMeaningfulAttribute(key)) continue;
|
|
662
|
-
const value = attrs[key];
|
|
663
|
-
if (value === null || value === void 0) continue;
|
|
664
|
-
if (!isScalar(value)) continue;
|
|
665
|
-
out[key] = formatForJson(value);
|
|
666
|
-
}
|
|
667
|
-
return Object.keys(out).length === 0 ? null : out;
|
|
668
|
-
}
|
|
669
|
-
function filterMeaningfulChanges(changes) {
|
|
670
|
-
return flattenNestedChanges(changes).filter((c) => {
|
|
671
|
-
if (!isMeaningfulAttribute(c.attribute)) return false;
|
|
672
|
-
if (!isScalar(c.to)) return false;
|
|
673
|
-
if (c.from === void 0) return true;
|
|
674
|
-
if (!isScalar(c.from)) return false;
|
|
675
|
-
return valuesActuallyChanged(c.from, c.to);
|
|
676
|
-
});
|
|
677
|
-
}
|
|
678
|
-
var MAX_NESTED_DEPTH = 3;
|
|
679
|
-
var MAX_NESTED_LEAVES = 20;
|
|
680
|
-
var MAX_UNKNOWN_FROM_LEAVES = 5;
|
|
681
|
-
function flattenNestedChanges(changes) {
|
|
682
|
-
if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
|
|
683
|
-
const out = [];
|
|
684
|
-
for (const c of changes) {
|
|
685
|
-
if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
|
|
686
|
-
out.push(c);
|
|
687
|
-
continue;
|
|
688
|
-
}
|
|
689
|
-
if (!isMeaningfulAttribute(c.attribute)) continue;
|
|
690
|
-
const leaves = [];
|
|
691
|
-
collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
|
|
692
|
-
const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
|
|
693
|
-
out.push(...leaves.slice(0, cap).map(scrubLeaf));
|
|
694
|
-
}
|
|
695
|
-
return out;
|
|
696
|
-
}
|
|
697
|
-
function collectLeafChanges(path, from, to, depth, out) {
|
|
698
|
-
if (out.length > MAX_NESTED_LEAVES) return;
|
|
699
|
-
const fromObj = isPlainRecord(from);
|
|
700
|
-
const toObj = isPlainRecord(to);
|
|
701
|
-
if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
|
|
702
|
-
const f = fromObj ? from : {};
|
|
703
|
-
const t = toObj ? to : {};
|
|
704
|
-
for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
|
|
705
|
-
const before = from === void 0 ? void 0 : f[key] ?? null;
|
|
706
|
-
collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
|
|
707
|
-
}
|
|
708
|
-
return;
|
|
709
|
-
}
|
|
710
|
-
if (from !== void 0 && sameJson(from, to)) return;
|
|
711
|
-
out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
|
|
712
|
-
}
|
|
713
|
-
function scrubLeaf(change) {
|
|
714
|
-
const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
|
|
715
|
-
const out = { attribute: change.attribute, to: scrub(change.to) };
|
|
716
|
-
if (change.from !== void 0) out.from = scrub(change.from);
|
|
717
|
-
return out;
|
|
718
|
-
}
|
|
719
|
-
function isPlainRecord(value) {
|
|
720
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
721
|
-
const proto = Object.getPrototypeOf(value);
|
|
722
|
-
return proto === Object.prototype || proto === null;
|
|
723
|
-
}
|
|
724
|
-
function sameJson(a, b) {
|
|
725
|
-
if (a === b) return true;
|
|
726
|
-
try {
|
|
727
|
-
return JSON.stringify(a) === JSON.stringify(b);
|
|
728
|
-
} catch {
|
|
729
|
-
return false;
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
function isMeaningfulAttribute(name) {
|
|
733
|
-
if (IGNORED_ATTRIBUTES.includes(name)) return false;
|
|
734
|
-
if (isDbSensitive(name)) return false;
|
|
735
|
-
return true;
|
|
736
|
-
}
|
|
737
|
-
function isDbSensitive(name) {
|
|
738
|
-
return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
|
|
739
|
-
}
|
|
740
|
-
function isScalar(value) {
|
|
741
|
-
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
|
|
742
|
-
return true;
|
|
743
|
-
}
|
|
744
|
-
if (isPrismaDecimal(value)) return true;
|
|
745
|
-
if (Buffer.isBuffer?.(value)) return false;
|
|
746
|
-
if (Array.isArray(value)) return value.every((v) => isScalar(v));
|
|
747
|
-
return false;
|
|
748
|
-
}
|
|
749
|
-
function valuesActuallyChanged(from, to) {
|
|
750
|
-
if (from === null && to === null) return false;
|
|
751
|
-
if (from instanceof Date && to instanceof Date) {
|
|
752
|
-
return from.getTime() !== to.getTime();
|
|
753
|
-
}
|
|
754
|
-
return from !== to;
|
|
755
|
-
}
|
|
756
|
-
function formatForJson(value) {
|
|
757
|
-
if (value === null || value === void 0) return value;
|
|
758
|
-
if (value instanceof Date) return value.toISOString();
|
|
759
|
-
if (typeof value === "bigint") return value.toString();
|
|
760
|
-
if (isPrismaDecimal(value)) return Number(value.toString());
|
|
761
|
-
if (Array.isArray(value)) return value.map(formatForJson);
|
|
762
|
-
return value;
|
|
763
|
-
}
|
|
764
|
-
function isPrismaDecimal(value) {
|
|
765
|
-
if (value === null || typeof value !== "object") return false;
|
|
766
|
-
const obj = value;
|
|
767
|
-
return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
|
|
768
|
-
}
|
|
769
|
-
var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
|
|
770
|
-
var MAX_DISPLAY_NAME_LENGTH = 120;
|
|
771
|
-
function resolveDisplayName(modelClass, instance) {
|
|
772
|
-
try {
|
|
773
|
-
const config = configuration();
|
|
774
|
-
const resolver = config.displayNameFor[modelClass];
|
|
775
|
-
const resolved = applyResolver(resolver, instance);
|
|
776
|
-
if (resolved !== null) return resolved;
|
|
777
|
-
const columns = /* @__PURE__ */ new Map();
|
|
778
|
-
for (const key of Object.keys(instance)) {
|
|
779
|
-
if (isDbSensitive(key)) continue;
|
|
780
|
-
const norm = key.toLowerCase().replace(/_/g, "");
|
|
781
|
-
if (!columns.has(norm)) columns.set(norm, instance[key]);
|
|
782
|
-
}
|
|
783
|
-
const text = (norm) => {
|
|
784
|
-
const value = columns.get(norm);
|
|
785
|
-
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
|
|
786
|
-
return null;
|
|
787
|
-
}
|
|
788
|
-
const str = String(value).trim();
|
|
789
|
-
return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
|
|
790
|
-
};
|
|
791
|
-
for (const column of DISPLAY_NAME_COLUMNS) {
|
|
792
|
-
const value = text(column);
|
|
793
|
-
if (value !== null) return value;
|
|
794
|
-
}
|
|
795
|
-
return text(`${singularNorm(modelClass)}number`);
|
|
796
|
-
} catch {
|
|
797
|
-
}
|
|
798
|
-
return null;
|
|
799
|
-
}
|
|
800
|
-
function singularNorm(modelClass) {
|
|
801
|
-
const norm = modelClass.toLowerCase().replace(/_/g, "");
|
|
802
|
-
if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
|
|
803
|
-
if (/(ss|us|is)$/.test(norm)) return norm;
|
|
804
|
-
if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
|
|
805
|
-
return norm.endsWith("s") ? norm.slice(0, -1) : norm;
|
|
806
|
-
}
|
|
807
|
-
function applyResolver(resolver, instance) {
|
|
808
|
-
if (typeof resolver === "string") {
|
|
809
|
-
const value = instance[resolver];
|
|
810
|
-
if (value !== null && value !== void 0 && String(value).length > 0) {
|
|
811
|
-
return String(value);
|
|
812
|
-
}
|
|
813
|
-
return null;
|
|
814
|
-
}
|
|
815
|
-
if (typeof resolver === "function") {
|
|
816
|
-
try {
|
|
817
|
-
const value = resolver(instance);
|
|
818
|
-
if (typeof value === "string" && value.length > 0) return value;
|
|
819
|
-
if (value !== null && value !== void 0 && String(value).length > 0) {
|
|
820
|
-
return String(value);
|
|
821
|
-
}
|
|
822
|
-
} catch {
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
return null;
|
|
826
|
-
}
|
|
827
|
-
function describeError(error) {
|
|
828
|
-
if (error instanceof Error) return `${error.name}: ${error.message}`;
|
|
829
|
-
return String(error);
|
|
830
|
-
}
|
|
3
|
+
var chunkWASXTDLW_cjs = require('../chunk-WASXTDLW.cjs');
|
|
4
|
+
var chunkV5YNKKTG_cjs = require('../chunk-V5YNKKTG.cjs');
|
|
5
|
+
require('../chunk-ZDSQYCYV.cjs');
|
|
831
6
|
|
|
832
7
|
// src/db/prisma.ts
|
|
833
8
|
var CAPTURED_OPERATIONS = /* @__PURE__ */ new Set([
|
|
@@ -866,7 +41,7 @@ function buildPrismaExtension(client, schemaClient = client) {
|
|
|
866
41
|
try {
|
|
867
42
|
recordPrismaOperation(ctx, result, tableNameFor(schemaClient, ctx.model));
|
|
868
43
|
} catch (error) {
|
|
869
|
-
logger.debug(`prisma capture failed: ${
|
|
44
|
+
chunkV5YNKKTG_cjs.logger.debug(`prisma capture failed: ${describeError(error)}`);
|
|
870
45
|
}
|
|
871
46
|
return result;
|
|
872
47
|
}
|
|
@@ -886,15 +61,15 @@ async function runTagged(client, ctx) {
|
|
|
886
61
|
if (!shouldTag(client, ctx)) return ctx.query(ctx.args);
|
|
887
62
|
try {
|
|
888
63
|
const results = await client.$transaction([
|
|
889
|
-
client.$executeRawUnsafe(SET_CORRELATION_SQL, current()),
|
|
64
|
+
client.$executeRawUnsafe(SET_CORRELATION_SQL, chunkWASXTDLW_cjs.current()),
|
|
890
65
|
ctx.query(ctx.args)
|
|
891
66
|
]);
|
|
892
67
|
return results[1];
|
|
893
68
|
} catch (error) {
|
|
894
69
|
if (!isTransactionUnsupported(error)) throw error;
|
|
895
70
|
taggingDisabled.add(client);
|
|
896
|
-
logger.warn(
|
|
897
|
-
`this Prisma client can't run transactions, so DB writes won't be linked to their request (they still show up as their own actions): ${
|
|
71
|
+
chunkV5YNKKTG_cjs.logger.warn(
|
|
72
|
+
`this Prisma client can't run transactions, so DB writes won't be linked to their request (they still show up as their own actions): ${describeError(error)}`
|
|
898
73
|
);
|
|
899
74
|
return ctx.query(ctx.args);
|
|
900
75
|
}
|
|
@@ -905,12 +80,12 @@ function _resetPrismaTaggingForTests() {
|
|
|
905
80
|
}
|
|
906
81
|
function shouldTag(client, ctx) {
|
|
907
82
|
if (!client || taggingDisabled.has(client)) return false;
|
|
908
|
-
if (configuration().databaseReader == null) return false;
|
|
83
|
+
if (chunkWASXTDLW_cjs.configuration().databaseReader == null) return false;
|
|
909
84
|
if (!isPostgresClient(client)) return false;
|
|
910
85
|
if (typeof client.$transaction !== "function" || typeof client.$executeRawUnsafe !== "function") {
|
|
911
86
|
return false;
|
|
912
87
|
}
|
|
913
|
-
if (!current()) return false;
|
|
88
|
+
if (!chunkWASXTDLW_cjs.current()) return false;
|
|
914
89
|
const params = ctx.__internalParams;
|
|
915
90
|
return params != null && !params.transaction;
|
|
916
91
|
}
|
|
@@ -936,7 +111,7 @@ function recordPrismaOperation(ctx, result, table) {
|
|
|
936
111
|
const dbOperation = OPERATION_TO_DB_OPERATION[ctx.operation];
|
|
937
112
|
if (!dbOperation) return;
|
|
938
113
|
if (isBatchResult(result)) {
|
|
939
|
-
emitDbEvent({
|
|
114
|
+
chunkWASXTDLW_cjs.emitDbEvent({
|
|
940
115
|
modelClass: ctx.model,
|
|
941
116
|
table,
|
|
942
117
|
operation: dbOperation,
|
|
@@ -944,10 +119,10 @@ function recordPrismaOperation(ctx, result, table) {
|
|
|
944
119
|
// For batch updates we still capture the `args.data` shape so
|
|
945
120
|
// downstream sees what columns were touched even if we can't
|
|
946
121
|
// identify a specific row.
|
|
947
|
-
...dbOperation === "update" &&
|
|
122
|
+
...dbOperation === "update" && isPlainObject(ctx.args.data) ? {
|
|
948
123
|
changes: extractChangesFromUpdateData(ctx.args.data)
|
|
949
124
|
} : {},
|
|
950
|
-
...dbOperation === "create" &&
|
|
125
|
+
...dbOperation === "create" && isPlainObject(ctx.args.data) ? {
|
|
951
126
|
initialAttributes: ctx.args.data
|
|
952
127
|
} : {}
|
|
953
128
|
});
|
|
@@ -962,8 +137,8 @@ function recordPrismaOperation(ctx, result, table) {
|
|
|
962
137
|
emitSingleRowEvent(ctx, dbOperation, result, table);
|
|
963
138
|
}
|
|
964
139
|
function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
965
|
-
if (!
|
|
966
|
-
emitDbEvent({
|
|
140
|
+
if (!isPlainObject(row)) {
|
|
141
|
+
chunkWASXTDLW_cjs.emitDbEvent({
|
|
967
142
|
modelClass: ctx.model,
|
|
968
143
|
table,
|
|
969
144
|
operation: dbOperation,
|
|
@@ -973,15 +148,15 @@ function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
|
973
148
|
}
|
|
974
149
|
const rowObj = row;
|
|
975
150
|
const resourceId = rowObj.id ?? null;
|
|
976
|
-
const displayName = resolveDisplayName(ctx.model, rowObj);
|
|
151
|
+
const displayName = chunkWASXTDLW_cjs.resolveDisplayName(ctx.model, rowObj);
|
|
977
152
|
if (dbOperation === "create") {
|
|
978
|
-
emitDbEvent({
|
|
153
|
+
chunkWASXTDLW_cjs.emitDbEvent({
|
|
979
154
|
modelClass: ctx.model,
|
|
980
155
|
table,
|
|
981
156
|
operation: "create",
|
|
982
157
|
resourceId,
|
|
983
158
|
initialAttributes: {
|
|
984
|
-
...
|
|
159
|
+
...isPlainObject(ctx.args.data) ? ctx.args.data : {},
|
|
985
160
|
...rowObj
|
|
986
161
|
},
|
|
987
162
|
displayName,
|
|
@@ -990,7 +165,7 @@ function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
|
990
165
|
return;
|
|
991
166
|
}
|
|
992
167
|
if (dbOperation === "update") {
|
|
993
|
-
emitDbEvent({
|
|
168
|
+
chunkWASXTDLW_cjs.emitDbEvent({
|
|
994
169
|
modelClass: ctx.model,
|
|
995
170
|
table,
|
|
996
171
|
operation: "update",
|
|
@@ -1001,7 +176,7 @@ function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
|
1001
176
|
});
|
|
1002
177
|
return;
|
|
1003
178
|
}
|
|
1004
|
-
emitDbEvent({
|
|
179
|
+
chunkWASXTDLW_cjs.emitDbEvent({
|
|
1005
180
|
modelClass: ctx.model,
|
|
1006
181
|
table,
|
|
1007
182
|
operation: "destroy",
|
|
@@ -1012,7 +187,7 @@ function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
|
1012
187
|
});
|
|
1013
188
|
}
|
|
1014
189
|
function extractUpdateChangesFromArgs(ctx, resultRow) {
|
|
1015
|
-
const data =
|
|
190
|
+
const data = isPlainObject(ctx.args.data) ? ctx.args.data : ctx.operation === "upsert" && isPlainObject(ctx.args.update) ? ctx.args.update : null;
|
|
1016
191
|
if (!data) return [];
|
|
1017
192
|
return Object.keys(data).map((attribute) => ({
|
|
1018
193
|
attribute,
|
|
@@ -1026,15 +201,15 @@ function extractChangesFromUpdateData(data) {
|
|
|
1026
201
|
}));
|
|
1027
202
|
}
|
|
1028
203
|
function isBatchResult(value) {
|
|
1029
|
-
return
|
|
204
|
+
return isPlainObject(value) && typeof value.count === "number" && Object.keys(value).length === 1;
|
|
1030
205
|
}
|
|
1031
|
-
function
|
|
206
|
+
function isPlainObject(value) {
|
|
1032
207
|
if (value === null || typeof value !== "object") return false;
|
|
1033
208
|
if (Array.isArray(value)) return false;
|
|
1034
209
|
if (value instanceof Date) return false;
|
|
1035
210
|
return true;
|
|
1036
211
|
}
|
|
1037
|
-
function
|
|
212
|
+
function describeError(error) {
|
|
1038
213
|
if (error instanceof Error) return `${error.name}: ${error.message}`;
|
|
1039
214
|
return String(error);
|
|
1040
215
|
}
|
|
@@ -1092,16 +267,16 @@ function patchPrismaClient(prisma, adapter) {
|
|
|
1092
267
|
if (prisma[EZLOGS_PRISMA_BRAND] === true) {
|
|
1093
268
|
return prisma;
|
|
1094
269
|
}
|
|
1095
|
-
const captureAndRun = makeAsyncContextCapture();
|
|
270
|
+
const captureAndRun = chunkWASXTDLW_cjs.makeAsyncContextCapture();
|
|
1096
271
|
if (!captureAndRun) {
|
|
1097
|
-
logger.debug(
|
|
272
|
+
chunkV5YNKKTG_cjs.logger.debug(
|
|
1098
273
|
"patchPrismaClient: AsyncLocalStorage.snapshot() unavailable; skipping ALS bind, applying ezlogsPrisma() only."
|
|
1099
274
|
);
|
|
1100
275
|
return applyEzlogsExtension(prisma);
|
|
1101
276
|
}
|
|
1102
277
|
const patchedAdapter = adapter && typeof adapter === "object" && adapter[EZLOGS_ADAPTER_BRAND] === true ? adapter : null;
|
|
1103
278
|
if (adapter && !patchedAdapter) {
|
|
1104
|
-
logger.debug(
|
|
279
|
+
chunkV5YNKKTG_cjs.logger.debug(
|
|
1105
280
|
"patchPrismaClient: adapter is not the output of patchPrismaPgAdapter(). Correlation_id will be NULL on concurrent requests. Wrap the adapter first."
|
|
1106
281
|
);
|
|
1107
282
|
}
|
|
@@ -1112,7 +287,7 @@ function patchPrismaClient(prisma, adapter) {
|
|
|
1112
287
|
function applyEzlogsExtension(prisma) {
|
|
1113
288
|
const client = prisma;
|
|
1114
289
|
if (typeof client.$extends !== "function") {
|
|
1115
|
-
logger.debug(
|
|
290
|
+
chunkV5YNKKTG_cjs.logger.debug(
|
|
1116
291
|
"patchPrismaClient: client has no $extends; skipping ezlogsPrisma extension."
|
|
1117
292
|
);
|
|
1118
293
|
return prisma;
|
|
@@ -1120,7 +295,7 @@ function applyEzlogsExtension(prisma) {
|
|
|
1120
295
|
try {
|
|
1121
296
|
return client.$extends(buildPrismaExtension(null, prisma));
|
|
1122
297
|
} catch (error) {
|
|
1123
|
-
logger.debug(`patchPrismaClient: $extends failed: ${
|
|
298
|
+
chunkV5YNKKTG_cjs.logger.debug(`patchPrismaClient: $extends failed: ${describeError(error)}`);
|
|
1124
299
|
return prisma;
|
|
1125
300
|
}
|
|
1126
301
|
}
|