ezlogs-nextjs 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -3
- package/bin/install-triggers.cjs +4 -0
- package/dist/actors/index.d.cts +1 -1
- package/dist/actors/index.d.ts +1 -1
- package/dist/{configuration-3Pei_XZ_.d.cts → configuration-OHLNivON.d.cts} +11 -20
- package/dist/{configuration-3Pei_XZ_.d.ts → configuration-OHLNivON.d.ts} +11 -20
- package/dist/db/drizzle.cjs +36 -8
- package/dist/db/drizzle.js +36 -8
- package/dist/db/prisma.cjs +101 -18
- package/dist/db/prisma.d.cts +37 -1
- package/dist/db/prisma.d.ts +37 -1
- package/dist/db/prisma.js +100 -19
- package/dist/db/supabase.cjs +36 -8
- package/dist/db/supabase.js +36 -8
- package/dist/index.cjs +485 -222
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +485 -222
- package/dist/jobs/bullmq.cjs +0 -4
- package/dist/jobs/bullmq.js +0 -4
- package/dist/jobs/inngest.cjs +0 -4
- package/dist/jobs/inngest.js +0 -4
- package/dist/jobs/supabase-queues.cjs +0 -4
- package/dist/jobs/supabase-queues.js +0 -4
- package/dist/jobs/trigger.cjs +0 -4
- package/dist/jobs/trigger.js +0 -4
- package/dist/jobs/upstash-workflow.cjs +0 -4
- package/dist/jobs/upstash-workflow.js +0 -4
- package/dist/triggers/index.cjs +192 -63
- package/dist/triggers/index.d.cts +30 -40
- package/dist/triggers/index.d.ts +30 -40
- package/dist/triggers/index.js +191 -61
- package/package.json +4 -2
- package/sql/postgres/00_install.sql +244 -21
package/dist/jobs/bullmq.cjs
CHANGED
|
@@ -301,7 +301,6 @@ var Configuration = class {
|
|
|
301
301
|
displayNameFor = {};
|
|
302
302
|
isServerActionError = null;
|
|
303
303
|
databaseReader = null;
|
|
304
|
-
databaseSnapshotMode = false;
|
|
305
304
|
databaseUrl = null;
|
|
306
305
|
/**
|
|
307
306
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -353,9 +352,6 @@ var Configuration = class {
|
|
|
353
352
|
if (options.databaseReader !== void 0) {
|
|
354
353
|
this.databaseReader = options.databaseReader;
|
|
355
354
|
}
|
|
356
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
357
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
358
|
-
}
|
|
359
355
|
if (options.databaseUrl !== void 0) {
|
|
360
356
|
this.databaseUrl = options.databaseUrl;
|
|
361
357
|
}
|
package/dist/jobs/bullmq.js
CHANGED
|
@@ -299,7 +299,6 @@ var Configuration = class {
|
|
|
299
299
|
displayNameFor = {};
|
|
300
300
|
isServerActionError = null;
|
|
301
301
|
databaseReader = null;
|
|
302
|
-
databaseSnapshotMode = false;
|
|
303
302
|
databaseUrl = null;
|
|
304
303
|
/**
|
|
305
304
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -351,9 +350,6 @@ var Configuration = class {
|
|
|
351
350
|
if (options.databaseReader !== void 0) {
|
|
352
351
|
this.databaseReader = options.databaseReader;
|
|
353
352
|
}
|
|
354
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
355
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
356
|
-
}
|
|
357
353
|
if (options.databaseUrl !== void 0) {
|
|
358
354
|
this.databaseUrl = options.databaseUrl;
|
|
359
355
|
}
|
package/dist/jobs/inngest.cjs
CHANGED
|
@@ -301,7 +301,6 @@ var Configuration = class {
|
|
|
301
301
|
displayNameFor = {};
|
|
302
302
|
isServerActionError = null;
|
|
303
303
|
databaseReader = null;
|
|
304
|
-
databaseSnapshotMode = false;
|
|
305
304
|
databaseUrl = null;
|
|
306
305
|
/**
|
|
307
306
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -353,9 +352,6 @@ var Configuration = class {
|
|
|
353
352
|
if (options.databaseReader !== void 0) {
|
|
354
353
|
this.databaseReader = options.databaseReader;
|
|
355
354
|
}
|
|
356
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
357
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
358
|
-
}
|
|
359
355
|
if (options.databaseUrl !== void 0) {
|
|
360
356
|
this.databaseUrl = options.databaseUrl;
|
|
361
357
|
}
|
package/dist/jobs/inngest.js
CHANGED
|
@@ -299,7 +299,6 @@ var Configuration = class {
|
|
|
299
299
|
displayNameFor = {};
|
|
300
300
|
isServerActionError = null;
|
|
301
301
|
databaseReader = null;
|
|
302
|
-
databaseSnapshotMode = false;
|
|
303
302
|
databaseUrl = null;
|
|
304
303
|
/**
|
|
305
304
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -351,9 +350,6 @@ var Configuration = class {
|
|
|
351
350
|
if (options.databaseReader !== void 0) {
|
|
352
351
|
this.databaseReader = options.databaseReader;
|
|
353
352
|
}
|
|
354
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
355
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
356
|
-
}
|
|
357
353
|
if (options.databaseUrl !== void 0) {
|
|
358
354
|
this.databaseUrl = options.databaseUrl;
|
|
359
355
|
}
|
|
@@ -301,7 +301,6 @@ var Configuration = class {
|
|
|
301
301
|
displayNameFor = {};
|
|
302
302
|
isServerActionError = null;
|
|
303
303
|
databaseReader = null;
|
|
304
|
-
databaseSnapshotMode = false;
|
|
305
304
|
databaseUrl = null;
|
|
306
305
|
/**
|
|
307
306
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -353,9 +352,6 @@ var Configuration = class {
|
|
|
353
352
|
if (options.databaseReader !== void 0) {
|
|
354
353
|
this.databaseReader = options.databaseReader;
|
|
355
354
|
}
|
|
356
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
357
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
358
|
-
}
|
|
359
355
|
if (options.databaseUrl !== void 0) {
|
|
360
356
|
this.databaseUrl = options.databaseUrl;
|
|
361
357
|
}
|
|
@@ -299,7 +299,6 @@ var Configuration = class {
|
|
|
299
299
|
displayNameFor = {};
|
|
300
300
|
isServerActionError = null;
|
|
301
301
|
databaseReader = null;
|
|
302
|
-
databaseSnapshotMode = false;
|
|
303
302
|
databaseUrl = null;
|
|
304
303
|
/**
|
|
305
304
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -351,9 +350,6 @@ var Configuration = class {
|
|
|
351
350
|
if (options.databaseReader !== void 0) {
|
|
352
351
|
this.databaseReader = options.databaseReader;
|
|
353
352
|
}
|
|
354
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
355
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
356
|
-
}
|
|
357
353
|
if (options.databaseUrl !== void 0) {
|
|
358
354
|
this.databaseUrl = options.databaseUrl;
|
|
359
355
|
}
|
package/dist/jobs/trigger.cjs
CHANGED
|
@@ -301,7 +301,6 @@ var Configuration = class {
|
|
|
301
301
|
displayNameFor = {};
|
|
302
302
|
isServerActionError = null;
|
|
303
303
|
databaseReader = null;
|
|
304
|
-
databaseSnapshotMode = false;
|
|
305
304
|
databaseUrl = null;
|
|
306
305
|
/**
|
|
307
306
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -353,9 +352,6 @@ var Configuration = class {
|
|
|
353
352
|
if (options.databaseReader !== void 0) {
|
|
354
353
|
this.databaseReader = options.databaseReader;
|
|
355
354
|
}
|
|
356
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
357
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
358
|
-
}
|
|
359
355
|
if (options.databaseUrl !== void 0) {
|
|
360
356
|
this.databaseUrl = options.databaseUrl;
|
|
361
357
|
}
|
package/dist/jobs/trigger.js
CHANGED
|
@@ -299,7 +299,6 @@ var Configuration = class {
|
|
|
299
299
|
displayNameFor = {};
|
|
300
300
|
isServerActionError = null;
|
|
301
301
|
databaseReader = null;
|
|
302
|
-
databaseSnapshotMode = false;
|
|
303
302
|
databaseUrl = null;
|
|
304
303
|
/**
|
|
305
304
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -351,9 +350,6 @@ var Configuration = class {
|
|
|
351
350
|
if (options.databaseReader !== void 0) {
|
|
352
351
|
this.databaseReader = options.databaseReader;
|
|
353
352
|
}
|
|
354
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
355
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
356
|
-
}
|
|
357
353
|
if (options.databaseUrl !== void 0) {
|
|
358
354
|
this.databaseUrl = options.databaseUrl;
|
|
359
355
|
}
|
|
@@ -301,7 +301,6 @@ var Configuration = class {
|
|
|
301
301
|
displayNameFor = {};
|
|
302
302
|
isServerActionError = null;
|
|
303
303
|
databaseReader = null;
|
|
304
|
-
databaseSnapshotMode = false;
|
|
305
304
|
databaseUrl = null;
|
|
306
305
|
/**
|
|
307
306
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -353,9 +352,6 @@ var Configuration = class {
|
|
|
353
352
|
if (options.databaseReader !== void 0) {
|
|
354
353
|
this.databaseReader = options.databaseReader;
|
|
355
354
|
}
|
|
356
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
357
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
358
|
-
}
|
|
359
355
|
if (options.databaseUrl !== void 0) {
|
|
360
356
|
this.databaseUrl = options.databaseUrl;
|
|
361
357
|
}
|
|
@@ -299,7 +299,6 @@ var Configuration = class {
|
|
|
299
299
|
displayNameFor = {};
|
|
300
300
|
isServerActionError = null;
|
|
301
301
|
databaseReader = null;
|
|
302
|
-
databaseSnapshotMode = false;
|
|
303
302
|
databaseUrl = null;
|
|
304
303
|
/**
|
|
305
304
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -351,9 +350,6 @@ var Configuration = class {
|
|
|
351
350
|
if (options.databaseReader !== void 0) {
|
|
352
351
|
this.databaseReader = options.databaseReader;
|
|
353
352
|
}
|
|
354
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
355
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
356
|
-
}
|
|
357
353
|
if (options.databaseUrl !== void 0) {
|
|
358
354
|
this.databaseUrl = options.databaseUrl;
|
|
359
355
|
}
|
package/dist/triggers/index.cjs
CHANGED
|
@@ -116,7 +116,6 @@ var Configuration = class {
|
|
|
116
116
|
displayNameFor = {};
|
|
117
117
|
isServerActionError = null;
|
|
118
118
|
databaseReader = null;
|
|
119
|
-
databaseSnapshotMode = false;
|
|
120
119
|
databaseUrl = null;
|
|
121
120
|
/**
|
|
122
121
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -168,9 +167,6 @@ var Configuration = class {
|
|
|
168
167
|
if (options.databaseReader !== void 0) {
|
|
169
168
|
this.databaseReader = options.databaseReader;
|
|
170
169
|
}
|
|
171
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
172
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
173
|
-
}
|
|
174
170
|
if (options.databaseUrl !== void 0) {
|
|
175
171
|
this.databaseUrl = options.databaseUrl;
|
|
176
172
|
}
|
|
@@ -359,11 +355,11 @@ async function runInstall(args, options = {}) {
|
|
|
359
355
|
"\n-- --tables=auto: trigger attachment SQL is generated at install time from information_schema. Re-run without --print to apply, or pass --tables=<a,b,c> to use an explicit list."
|
|
360
356
|
);
|
|
361
357
|
} else {
|
|
362
|
-
const
|
|
363
|
-
const
|
|
364
|
-
${
|
|
358
|
+
const trackingSql = buildTrackingSql(args.tables, "postgres");
|
|
359
|
+
const fullSql = trackingSql ? `${installSql}
|
|
360
|
+
${trackingSql}
|
|
365
361
|
` : installSql;
|
|
366
|
-
out(
|
|
362
|
+
out(fullSql);
|
|
367
363
|
}
|
|
368
364
|
return 0;
|
|
369
365
|
}
|
|
@@ -404,12 +400,11 @@ ${trackingSql2}
|
|
|
404
400
|
return 1;
|
|
405
401
|
}
|
|
406
402
|
}
|
|
407
|
-
const trackingSql = buildTrackingSql(tablesToTrack, "postgres");
|
|
408
|
-
const fullSql = trackingSql ? `${installSql}
|
|
409
|
-
${trackingSql}
|
|
410
|
-
` : installSql;
|
|
411
403
|
try {
|
|
412
|
-
await options.exec(
|
|
404
|
+
await options.exec(installSql);
|
|
405
|
+
for (const table of tablesToTrack) {
|
|
406
|
+
await options.exec(buildTrackingSql([table], "postgres"));
|
|
407
|
+
}
|
|
413
408
|
if (tablesToTrack.length === 0) {
|
|
414
409
|
out(
|
|
415
410
|
"[ezlogs] Postgres triggers installed. No tables tracked yet \u2014 run `SELECT ezlogs_track('foo')` to attach the trigger, or re-run with --tables=auto to track every business table."
|
|
@@ -510,14 +505,34 @@ var storage = createScopedStorage(
|
|
|
510
505
|
function getCurrentActor() {
|
|
511
506
|
return storage.read();
|
|
512
507
|
}
|
|
508
|
+
function runWithActorScope(callback) {
|
|
509
|
+
return storage.run(null, callback);
|
|
510
|
+
}
|
|
513
511
|
|
|
514
512
|
// src/correlation.ts
|
|
515
513
|
var storage2 = createScopedStorage(
|
|
516
514
|
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:correlation-storage")
|
|
517
515
|
);
|
|
516
|
+
function generate() {
|
|
517
|
+
const ts = Date.now();
|
|
518
|
+
const hex = randomHexBytes(4);
|
|
519
|
+
return `ezl_${ts}_${hex}`;
|
|
520
|
+
}
|
|
521
|
+
function randomHexBytes(byteCount) {
|
|
522
|
+
const buf = new Uint8Array(byteCount);
|
|
523
|
+
globalThis.crypto.getRandomValues(buf);
|
|
524
|
+
let out = "";
|
|
525
|
+
for (let i = 0; i < buf.length; i++) {
|
|
526
|
+
out += buf[i].toString(16).padStart(2, "0");
|
|
527
|
+
}
|
|
528
|
+
return out;
|
|
529
|
+
}
|
|
518
530
|
function current() {
|
|
519
531
|
return storage2.read();
|
|
520
532
|
}
|
|
533
|
+
function runWithCorrelation(correlationId, callback) {
|
|
534
|
+
return storage2.run(correlationId, callback);
|
|
535
|
+
}
|
|
521
536
|
|
|
522
537
|
// src/triggers/context.ts
|
|
523
538
|
function getDatabaseContext() {
|
|
@@ -777,6 +792,26 @@ function describe2(error) {
|
|
|
777
792
|
if (error instanceof Error) return `${error.name}: ${error.message}`;
|
|
778
793
|
return String(error);
|
|
779
794
|
}
|
|
795
|
+
var SENSITIVE_WORD_PATTERNS = Object.freeze([
|
|
796
|
+
"pin",
|
|
797
|
+
"otp",
|
|
798
|
+
"totp",
|
|
799
|
+
"iban",
|
|
800
|
+
"mfa",
|
|
801
|
+
"passphrase"
|
|
802
|
+
]);
|
|
803
|
+
function snakeLower(key) {
|
|
804
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
805
|
+
}
|
|
806
|
+
function matchesSensitiveKey(key, substringPatterns) {
|
|
807
|
+
const lower = key.toLowerCase();
|
|
808
|
+
const snake = snakeLower(key);
|
|
809
|
+
if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
|
|
810
|
+
return true;
|
|
811
|
+
}
|
|
812
|
+
const words = snake.split(/[^a-z0-9]+/);
|
|
813
|
+
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
814
|
+
}
|
|
780
815
|
|
|
781
816
|
// src/db/shared.ts
|
|
782
817
|
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
@@ -809,7 +844,19 @@ var DB_SENSITIVE_PATTERNS = Object.freeze([
|
|
|
809
844
|
"salt",
|
|
810
845
|
"digest",
|
|
811
846
|
"signature",
|
|
812
|
-
"hmac"
|
|
847
|
+
"hmac",
|
|
848
|
+
// 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
|
|
849
|
+
// the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
|
|
850
|
+
// `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
|
|
851
|
+
// SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
|
|
852
|
+
"credential",
|
|
853
|
+
"passwd",
|
|
854
|
+
"passcode",
|
|
855
|
+
"apikey",
|
|
856
|
+
"card_number",
|
|
857
|
+
"cardnumber",
|
|
858
|
+
"cvv",
|
|
859
|
+
"cvc"
|
|
813
860
|
]);
|
|
814
861
|
function emitDbEvent(input) {
|
|
815
862
|
try {
|
|
@@ -836,7 +883,8 @@ function emitDbEvent(input) {
|
|
|
836
883
|
correlation_id: current(),
|
|
837
884
|
resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
838
885
|
context,
|
|
839
|
-
duration_ms: null
|
|
886
|
+
duration_ms: null,
|
|
887
|
+
timestamp: input.timestamp ?? null
|
|
840
888
|
});
|
|
841
889
|
pushEvent(event);
|
|
842
890
|
} catch (error) {
|
|
@@ -894,8 +942,7 @@ function isMeaningfulAttribute(name) {
|
|
|
894
942
|
return true;
|
|
895
943
|
}
|
|
896
944
|
function isDbSensitive(name) {
|
|
897
|
-
|
|
898
|
-
return DB_SENSITIVE_PATTERNS.some((p) => lower.includes(p));
|
|
945
|
+
return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
|
|
899
946
|
}
|
|
900
947
|
function isScalar(value) {
|
|
901
948
|
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
|
|
@@ -967,43 +1014,51 @@ function describeError(error) {
|
|
|
967
1014
|
return String(error);
|
|
968
1015
|
}
|
|
969
1016
|
|
|
970
|
-
// src/triggers/audit-snapshot.ts
|
|
971
|
-
var storage3 = createScopedStorage(
|
|
972
|
-
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:audit-snapshot-storage")
|
|
973
|
-
);
|
|
974
|
-
function currentAuditSnapshot() {
|
|
975
|
-
return storage3.read();
|
|
976
|
-
}
|
|
977
|
-
function setAuditSnapshot(value) {
|
|
978
|
-
storage3.update(value);
|
|
979
|
-
}
|
|
980
|
-
function runWithAuditSnapshotScope(callback) {
|
|
981
|
-
return storage3.run(0n, callback);
|
|
982
|
-
}
|
|
983
|
-
|
|
984
1017
|
// src/triggers/emit-dedup.ts
|
|
985
|
-
var
|
|
1018
|
+
var storage3 = createScopedStorage(
|
|
986
1019
|
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:audit-emitted-ids-storage")
|
|
987
1020
|
);
|
|
988
1021
|
function currentEmittedAuditIds() {
|
|
989
|
-
return
|
|
1022
|
+
return storage3.read();
|
|
990
1023
|
}
|
|
991
1024
|
|
|
992
1025
|
// src/triggers/reader.ts
|
|
993
|
-
|
|
1026
|
+
var COLUMNS = "id, table_name, op, old_row, new_row, resource_id, correlation_id, txid, captured_at";
|
|
1027
|
+
var ORPHAN_AGE = "5 minutes";
|
|
1028
|
+
var PRUNE_AGE = "24 hours";
|
|
1029
|
+
var SWEEP_BATCH = 100;
|
|
1030
|
+
var PRUNE_BATCH = 2e3;
|
|
1031
|
+
var SWEEP_INTERVAL_MS = 1e4;
|
|
1032
|
+
var PRUNE_INTERVAL_MS = 10 * 6e4;
|
|
1033
|
+
var lastSweepAt = 0;
|
|
1034
|
+
var lastPruneAt = 0;
|
|
1035
|
+
var deleteDenied = false;
|
|
1036
|
+
var txCorrelation = /* @__PURE__ */ new Map();
|
|
1037
|
+
async function readAuditRows(exec, correlationId) {
|
|
994
1038
|
try {
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1039
|
+
if (!deleteDenied) {
|
|
1040
|
+
try {
|
|
1041
|
+
return sortById(
|
|
1042
|
+
extractRows(
|
|
1043
|
+
await exec(
|
|
1044
|
+
`DELETE FROM ezlogs_audit_log WHERE correlation_id = $1 RETURNING ${COLUMNS}`,
|
|
1045
|
+
[correlationId]
|
|
1046
|
+
)
|
|
1047
|
+
).map(normalizeAuditRow)
|
|
1048
|
+
);
|
|
1049
|
+
} catch (err) {
|
|
1050
|
+
if (!isPermissionDenied(err)) throw err;
|
|
1051
|
+
deleteDenied = true;
|
|
1052
|
+
logger.warn(
|
|
1053
|
+
"the database role used by the agent cannot DELETE from ezlogs_audit_log. Rows will not be cleaned up after they are sent; grant DELETE or schedule `DELETE FROM ezlogs_audit_log WHERE captured_at < now() - interval '24 hours'`."
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
const result = await exec(
|
|
1058
|
+
`SELECT ${COLUMNS} FROM ezlogs_audit_log WHERE correlation_id = $1 ORDER BY id ASC`,
|
|
1059
|
+
[correlationId]
|
|
1060
|
+
);
|
|
1061
|
+
return extractRows(result).map(normalizeAuditRow);
|
|
1007
1062
|
} catch (err) {
|
|
1008
1063
|
logger.debug(
|
|
1009
1064
|
`readAuditRows failed: ${err instanceof Error ? err.message : String(err)}`
|
|
@@ -1011,21 +1066,57 @@ async function readAuditRows(exec, correlationId, snapshotMaxId = null) {
|
|
|
1011
1066
|
return [];
|
|
1012
1067
|
}
|
|
1013
1068
|
}
|
|
1014
|
-
async function
|
|
1069
|
+
async function sweepAuditLog(exec) {
|
|
1070
|
+
if (deleteDenied) return 0;
|
|
1015
1071
|
try {
|
|
1016
|
-
const
|
|
1017
|
-
|
|
1072
|
+
const rows = sortById(
|
|
1073
|
+
extractRows(
|
|
1074
|
+
await exec(
|
|
1075
|
+
`DELETE FROM ezlogs_audit_log
|
|
1076
|
+
WHERE id IN (
|
|
1077
|
+
SELECT id FROM ezlogs_audit_log
|
|
1078
|
+
WHERE (correlation_id IS NULL
|
|
1079
|
+
OR captured_at < now() - interval '${ORPHAN_AGE}')
|
|
1080
|
+
-- Never resurface history: past the retention cap a
|
|
1081
|
+
-- row is pruned, not sent as new activity.
|
|
1082
|
+
AND captured_at > now() - interval '${PRUNE_AGE}'
|
|
1083
|
+
ORDER BY id
|
|
1084
|
+
LIMIT ${SWEEP_BATCH}
|
|
1085
|
+
FOR UPDATE SKIP LOCKED)
|
|
1086
|
+
RETURNING ${COLUMNS}`,
|
|
1087
|
+
[]
|
|
1088
|
+
)
|
|
1089
|
+
).map(normalizeAuditRow)
|
|
1090
|
+
);
|
|
1091
|
+
for (const row of rows) {
|
|
1092
|
+
const correlationId = row.correlation_id ?? correlationForTx(row.txid);
|
|
1093
|
+
runWithCorrelation(
|
|
1094
|
+
correlationId,
|
|
1095
|
+
() => runWithActorScope(() => emitOne(row))
|
|
1096
|
+
);
|
|
1097
|
+
}
|
|
1098
|
+
return rows.length;
|
|
1099
|
+
} catch (err) {
|
|
1100
|
+
logger.debug(
|
|
1101
|
+
`sweepAuditLog failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1102
|
+
);
|
|
1103
|
+
return 0;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
async function pruneAuditLog(exec) {
|
|
1107
|
+
if (deleteDenied) return;
|
|
1108
|
+
try {
|
|
1109
|
+
await exec(
|
|
1110
|
+
`DELETE FROM ezlogs_audit_log
|
|
1111
|
+
WHERE id IN (
|
|
1112
|
+
SELECT id FROM ezlogs_audit_log
|
|
1113
|
+
WHERE captured_at < now() - interval '${PRUNE_AGE}'
|
|
1114
|
+
LIMIT ${PRUNE_BATCH})`,
|
|
1018
1115
|
[]
|
|
1019
1116
|
);
|
|
1020
|
-
const rows = extractRows(result);
|
|
1021
|
-
if (rows.length === 0) return;
|
|
1022
|
-
const raw = rows[0] ?? {};
|
|
1023
|
-
const value = typeof raw.max_id === "bigint" ? raw.max_id : typeof raw.max_id === "number" ? BigInt(raw.max_id) : typeof raw.max_id === "string" && /^\d+$/.test(raw.max_id) ? BigInt(raw.max_id) : null;
|
|
1024
|
-
if (value === null) return;
|
|
1025
|
-
setAuditSnapshot(value);
|
|
1026
1117
|
} catch (err) {
|
|
1027
1118
|
logger.debug(
|
|
1028
|
-
`
|
|
1119
|
+
`pruneAuditLog failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1029
1120
|
);
|
|
1030
1121
|
}
|
|
1031
1122
|
}
|
|
@@ -1063,8 +1154,19 @@ async function flushAuditLog(exec) {
|
|
|
1063
1154
|
if (!exec) return;
|
|
1064
1155
|
const correlationId = current();
|
|
1065
1156
|
if (!correlationId) return;
|
|
1066
|
-
const
|
|
1067
|
-
|
|
1157
|
+
const rows = await readAuditRows(exec, correlationId);
|
|
1158
|
+
emitFresh(rows);
|
|
1159
|
+
const now = Date.now();
|
|
1160
|
+
if (now - lastPruneAt >= PRUNE_INTERVAL_MS) {
|
|
1161
|
+
lastPruneAt = now;
|
|
1162
|
+
await pruneAuditLog(exec);
|
|
1163
|
+
}
|
|
1164
|
+
if (now - lastSweepAt >= SWEEP_INTERVAL_MS) {
|
|
1165
|
+
lastSweepAt = now;
|
|
1166
|
+
await sweepAuditLog(exec);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
function emitFresh(rows) {
|
|
1068
1170
|
if (rows.length === 0) return;
|
|
1069
1171
|
const emitted = currentEmittedAuditIds();
|
|
1070
1172
|
if (emitted === null) {
|
|
@@ -1077,9 +1179,26 @@ async function flushAuditLog(exec) {
|
|
|
1077
1179
|
emitted.add(row.id);
|
|
1078
1180
|
fresh.push(row);
|
|
1079
1181
|
}
|
|
1080
|
-
if (fresh.length === 0) return;
|
|
1081
1182
|
emitAuditRows(fresh);
|
|
1082
1183
|
}
|
|
1184
|
+
function correlationForTx(txid) {
|
|
1185
|
+
if (!txid) return generate();
|
|
1186
|
+
let id = txCorrelation.get(txid);
|
|
1187
|
+
if (!id) {
|
|
1188
|
+
if (txCorrelation.size >= 1e3) txCorrelation.clear();
|
|
1189
|
+
id = generate();
|
|
1190
|
+
txCorrelation.set(txid, id);
|
|
1191
|
+
}
|
|
1192
|
+
return id;
|
|
1193
|
+
}
|
|
1194
|
+
function isPermissionDenied(err) {
|
|
1195
|
+
return err?.code === "42501";
|
|
1196
|
+
}
|
|
1197
|
+
function sortById(rows) {
|
|
1198
|
+
return rows.sort(
|
|
1199
|
+
(a, b) => a.id.length !== b.id.length ? a.id.length - b.id.length : a.id < b.id ? -1 : a.id > b.id ? 1 : 0
|
|
1200
|
+
);
|
|
1201
|
+
}
|
|
1083
1202
|
function emitOne(row) {
|
|
1084
1203
|
const op = mapOp(row.op);
|
|
1085
1204
|
if (!op) return;
|
|
@@ -1093,6 +1212,7 @@ function emitOne(row) {
|
|
|
1093
1212
|
initialAttributes: newRow,
|
|
1094
1213
|
modelInstance: newRow,
|
|
1095
1214
|
displayName: null,
|
|
1215
|
+
timestamp: row.captured_at ?? null,
|
|
1096
1216
|
_fromTrigger: true
|
|
1097
1217
|
});
|
|
1098
1218
|
return;
|
|
@@ -1111,6 +1231,7 @@ function emitOne(row) {
|
|
|
1111
1231
|
// user just told the system to call this row.
|
|
1112
1232
|
modelInstance: newRow,
|
|
1113
1233
|
displayName: null,
|
|
1234
|
+
timestamp: row.captured_at ?? null,
|
|
1114
1235
|
_fromTrigger: true
|
|
1115
1236
|
});
|
|
1116
1237
|
return;
|
|
@@ -1123,6 +1244,7 @@ function emitOne(row) {
|
|
|
1123
1244
|
initialAttributes: oldRow,
|
|
1124
1245
|
modelInstance: oldRow,
|
|
1125
1246
|
displayName: null,
|
|
1247
|
+
timestamp: row.captured_at ?? null,
|
|
1126
1248
|
_fromTrigger: true
|
|
1127
1249
|
});
|
|
1128
1250
|
return;
|
|
@@ -1171,6 +1293,11 @@ function extractRows(result) {
|
|
|
1171
1293
|
}
|
|
1172
1294
|
return [];
|
|
1173
1295
|
}
|
|
1296
|
+
function toDate(value) {
|
|
1297
|
+
if (value == null) return null;
|
|
1298
|
+
const d = value instanceof Date ? value : new Date(String(value));
|
|
1299
|
+
return Number.isNaN(d.getTime()) ? null : d;
|
|
1300
|
+
}
|
|
1174
1301
|
function normalizeAuditRow(raw) {
|
|
1175
1302
|
const r = raw ?? {};
|
|
1176
1303
|
return {
|
|
@@ -1179,7 +1306,10 @@ function normalizeAuditRow(raw) {
|
|
|
1179
1306
|
op: r.op,
|
|
1180
1307
|
old_row: r.old_row ?? null,
|
|
1181
1308
|
new_row: r.new_row ?? null,
|
|
1182
|
-
resource_id: r.resource_id == null ? null : String(r.resource_id)
|
|
1309
|
+
resource_id: r.resource_id == null ? null : String(r.resource_id),
|
|
1310
|
+
correlation_id: r.correlation_id == null ? null : String(r.correlation_id),
|
|
1311
|
+
txid: r.txid == null ? null : String(r.txid),
|
|
1312
|
+
captured_at: toDate(r.captured_at)
|
|
1183
1313
|
};
|
|
1184
1314
|
}
|
|
1185
1315
|
function coerceJsonb(value) {
|
|
@@ -1374,7 +1504,6 @@ function leadingVerb(text) {
|
|
|
1374
1504
|
|
|
1375
1505
|
exports.applyDatabaseContext = applyDatabaseContext;
|
|
1376
1506
|
exports.buildTrackingSql = buildTrackingSql;
|
|
1377
|
-
exports.currentAuditSnapshot = currentAuditSnapshot;
|
|
1378
1507
|
exports.detectEngine = detectEngine;
|
|
1379
1508
|
exports.detectTriggerTrackedTables = detectTriggerTrackedTables;
|
|
1380
1509
|
exports.emitAuditRows = emitAuditRows;
|
|
@@ -1382,8 +1511,8 @@ exports.flushAuditLog = flushAuditLog;
|
|
|
1382
1511
|
exports.getDatabaseContext = getDatabaseContext;
|
|
1383
1512
|
exports.parseArgs = parseArgs;
|
|
1384
1513
|
exports.patchPgClient = patchPgClient;
|
|
1514
|
+
exports.pruneAuditLog = pruneAuditLog;
|
|
1385
1515
|
exports.readAuditRows = readAuditRows;
|
|
1386
1516
|
exports.runInstall = runInstall;
|
|
1387
|
-
exports.
|
|
1388
|
-
exports.takeAuditSnapshot = takeAuditSnapshot;
|
|
1517
|
+
exports.sweepAuditLog = sweepAuditLog;
|
|
1389
1518
|
exports.withDatabaseContext = withDatabaseContext;
|