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
|
@@ -117,17 +117,15 @@ interface PgLikeClient {
|
|
|
117
117
|
declare function withDatabaseContext<T>(client: PgLikeClient, callback: (client: PgLikeClient) => Promise<T>): Promise<T>;
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
|
-
* Shape of one row returned by the audit-log
|
|
121
|
-
* column list in `00_install.sql
|
|
120
|
+
* Shape of one row returned by the audit-log query. Matches the
|
|
121
|
+
* column list in `00_install.sql`. Drivers vary on how they
|
|
122
122
|
* deserialize jsonb (`pg` returns plain objects; some serverless
|
|
123
123
|
* drivers return strings) — `coerceJsonb` below handles both.
|
|
124
124
|
*/
|
|
125
125
|
interface AuditRow {
|
|
126
126
|
/**
|
|
127
127
|
* The audit log row's primary key, stringified. Used only for
|
|
128
|
-
* within-request emit dedup — never reaches the wire.
|
|
129
|
-
* here so drivers that return bigint, number, or string all live in
|
|
130
|
-
* one Set comparable type.
|
|
128
|
+
* within-request emit dedup — never reaches the wire.
|
|
131
129
|
*/
|
|
132
130
|
id: string;
|
|
133
131
|
table_name: string;
|
|
@@ -135,41 +133,37 @@ interface AuditRow {
|
|
|
135
133
|
old_row: Record<string, unknown> | string | null;
|
|
136
134
|
new_row: Record<string, unknown> | string | null;
|
|
137
135
|
resource_id: string | null;
|
|
136
|
+
/** Stored correlation id (null when the write couldn't be tagged). */
|
|
137
|
+
correlation_id?: string | null;
|
|
138
|
+
/** Postgres transaction id — groups untagged rows into one action. */
|
|
139
|
+
txid?: string | null;
|
|
140
|
+
/** When the trigger fired — becomes the event timestamp. */
|
|
141
|
+
captured_at?: Date | null;
|
|
138
142
|
}
|
|
139
143
|
/**
|
|
140
|
-
*
|
|
144
|
+
* Claim (delete + return) the audit rows tagged with `correlationId`,
|
|
145
|
+
* oldest first. A row can only be claimed once, so concurrent flushes
|
|
146
|
+
* never emit the same row twice.
|
|
141
147
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
148
|
+
* If the reader role may not DELETE, falls back to a plain SELECT
|
|
149
|
+
* (rows then stay until the customer cleans them up) and warns once.
|
|
144
150
|
*
|
|
145
151
|
* Errors are caught and returned as `[]` — a failed audit read must
|
|
146
|
-
* never break the host request.
|
|
152
|
+
* never break the host request.
|
|
147
153
|
*/
|
|
148
|
-
declare function readAuditRows(exec: SqlExecutor, correlationId: string
|
|
154
|
+
declare function readAuditRows(exec: SqlExecutor, correlationId: string): Promise<AuditRow[]>;
|
|
149
155
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
156
|
+
* Drain rows no request will claim: untagged rows, and tagged rows
|
|
157
|
+
* older than ORPHAN_AGE. Each is emitted under its stored correlation
|
|
158
|
+
* id, or — when it has none — under one fresh id per database
|
|
159
|
+
* transaction, always with no actor. Never attributes a row to the
|
|
160
|
+
* request that happens to run the sweep.
|
|
155
161
|
*
|
|
156
|
-
*
|
|
157
|
-
* (Phase 5 primary path).
|
|
162
|
+
* Returns the number of rows emitted. Errors are swallowed.
|
|
158
163
|
*/
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
|
|
162
|
-
* store it on the per-request scope. Called at the start of each HTTP
|
|
163
|
-
* request when `databaseReader` is configured. The reader's time-window
|
|
164
|
-
* fallback uses this as the lower bound for catching supabase-js writes
|
|
165
|
-
* (rows whose `correlation_id` is NULL because PostgREST didn't run
|
|
166
|
-
* inside our explicit transaction).
|
|
167
|
-
*
|
|
168
|
-
* Errors are swallowed — a failed snapshot just means the time-window
|
|
169
|
-
* fallback degrades to "match correlation_id only" for this request.
|
|
170
|
-
* Never break the host's request over a bookkeeping query.
|
|
171
|
-
*/
|
|
172
|
-
declare function takeAuditSnapshot(exec: SqlExecutor): Promise<void>;
|
|
164
|
+
declare function sweepAuditLog(exec: SqlExecutor): Promise<number>;
|
|
165
|
+
/** Delete rows older than PRUNE_AGE without emitting them. Errors swallowed. */
|
|
166
|
+
declare function pruneAuditLog(exec: SqlExecutor): Promise<void>;
|
|
173
167
|
/**
|
|
174
168
|
* Detect which tables have the `ezlogs_audit` trigger attached. Used
|
|
175
169
|
* by the per-ORM adapters to decide whether their per-mutation emit
|
|
@@ -196,9 +190,10 @@ declare function detectTriggerTrackedTables(exec: SqlExecutor): Promise<Set<stri
|
|
|
196
190
|
*/
|
|
197
191
|
declare function emitAuditRows(rows: ReadonlyArray<AuditRow>): void;
|
|
198
192
|
/**
|
|
199
|
-
* End-of-request entry point.
|
|
200
|
-
* id and emits each one
|
|
201
|
-
*
|
|
193
|
+
* End-of-request entry point. Claims rows for the current correlation
|
|
194
|
+
* id and emits each one, then runs the throttled sweep (untagged /
|
|
195
|
+
* orphaned rows, as their own actions) and prune. Skips when no
|
|
196
|
+
* correlation is in scope or no exec is supplied.
|
|
202
197
|
*
|
|
203
198
|
* The HTTP capturers call this after the user's handler returns,
|
|
204
199
|
* before they push the HTTP event itself, so DB events arrive
|
|
@@ -206,11 +201,6 @@ declare function emitAuditRows(rows: ReadonlyArray<AuditRow>): void;
|
|
|
206
201
|
*/
|
|
207
202
|
declare function flushAuditLog(exec: SqlExecutor | null | undefined): Promise<void>;
|
|
208
203
|
|
|
209
|
-
/** Read the current request's audit snapshot, if any. */
|
|
210
|
-
declare function currentAuditSnapshot(): bigint | null;
|
|
211
|
-
/** Run `callback` inside a fresh audit-snapshot scope. */
|
|
212
|
-
declare function runWithAuditSnapshotScope<R>(callback: () => R): R;
|
|
213
|
-
|
|
214
204
|
interface PgQueryConfig {
|
|
215
205
|
text?: string;
|
|
216
206
|
name?: string;
|
|
@@ -247,4 +237,4 @@ interface PgModule {
|
|
|
247
237
|
*/
|
|
248
238
|
declare function patchPgClient(pgModule: PgModule): void;
|
|
249
239
|
|
|
250
|
-
export { type AuditRow, type DatabaseContextSnapshot, type Detected, type PgLikeClient, type SqlExecutor, applyDatabaseContext, buildTrackingSql,
|
|
240
|
+
export { type AuditRow, type DatabaseContextSnapshot, type Detected, type PgLikeClient, type SqlExecutor, applyDatabaseContext, buildTrackingSql, detectEngine, detectTriggerTrackedTables, emitAuditRows, flushAuditLog, getDatabaseContext, parseArgs, patchPgClient, pruneAuditLog, readAuditRows, runInstall, sweepAuditLog, withDatabaseContext };
|
package/dist/triggers/index.d.ts
CHANGED
|
@@ -117,17 +117,15 @@ interface PgLikeClient {
|
|
|
117
117
|
declare function withDatabaseContext<T>(client: PgLikeClient, callback: (client: PgLikeClient) => Promise<T>): Promise<T>;
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
|
-
* Shape of one row returned by the audit-log
|
|
121
|
-
* column list in `00_install.sql
|
|
120
|
+
* Shape of one row returned by the audit-log query. Matches the
|
|
121
|
+
* column list in `00_install.sql`. Drivers vary on how they
|
|
122
122
|
* deserialize jsonb (`pg` returns plain objects; some serverless
|
|
123
123
|
* drivers return strings) — `coerceJsonb` below handles both.
|
|
124
124
|
*/
|
|
125
125
|
interface AuditRow {
|
|
126
126
|
/**
|
|
127
127
|
* The audit log row's primary key, stringified. Used only for
|
|
128
|
-
* within-request emit dedup — never reaches the wire.
|
|
129
|
-
* here so drivers that return bigint, number, or string all live in
|
|
130
|
-
* one Set comparable type.
|
|
128
|
+
* within-request emit dedup — never reaches the wire.
|
|
131
129
|
*/
|
|
132
130
|
id: string;
|
|
133
131
|
table_name: string;
|
|
@@ -135,41 +133,37 @@ interface AuditRow {
|
|
|
135
133
|
old_row: Record<string, unknown> | string | null;
|
|
136
134
|
new_row: Record<string, unknown> | string | null;
|
|
137
135
|
resource_id: string | null;
|
|
136
|
+
/** Stored correlation id (null when the write couldn't be tagged). */
|
|
137
|
+
correlation_id?: string | null;
|
|
138
|
+
/** Postgres transaction id — groups untagged rows into one action. */
|
|
139
|
+
txid?: string | null;
|
|
140
|
+
/** When the trigger fired — becomes the event timestamp. */
|
|
141
|
+
captured_at?: Date | null;
|
|
138
142
|
}
|
|
139
143
|
/**
|
|
140
|
-
*
|
|
144
|
+
* Claim (delete + return) the audit rows tagged with `correlationId`,
|
|
145
|
+
* oldest first. A row can only be claimed once, so concurrent flushes
|
|
146
|
+
* never emit the same row twice.
|
|
141
147
|
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
148
|
+
* If the reader role may not DELETE, falls back to a plain SELECT
|
|
149
|
+
* (rows then stay until the customer cleans them up) and warns once.
|
|
144
150
|
*
|
|
145
151
|
* Errors are caught and returned as `[]` — a failed audit read must
|
|
146
|
-
* never break the host request.
|
|
152
|
+
* never break the host request.
|
|
147
153
|
*/
|
|
148
|
-
declare function readAuditRows(exec: SqlExecutor, correlationId: string
|
|
154
|
+
declare function readAuditRows(exec: SqlExecutor, correlationId: string): Promise<AuditRow[]>;
|
|
149
155
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
156
|
+
* Drain rows no request will claim: untagged rows, and tagged rows
|
|
157
|
+
* older than ORPHAN_AGE. Each is emitted under its stored correlation
|
|
158
|
+
* id, or — when it has none — under one fresh id per database
|
|
159
|
+
* transaction, always with no actor. Never attributes a row to the
|
|
160
|
+
* request that happens to run the sweep.
|
|
155
161
|
*
|
|
156
|
-
*
|
|
157
|
-
* (Phase 5 primary path).
|
|
162
|
+
* Returns the number of rows emitted. Errors are swallowed.
|
|
158
163
|
*/
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
|
|
162
|
-
* store it on the per-request scope. Called at the start of each HTTP
|
|
163
|
-
* request when `databaseReader` is configured. The reader's time-window
|
|
164
|
-
* fallback uses this as the lower bound for catching supabase-js writes
|
|
165
|
-
* (rows whose `correlation_id` is NULL because PostgREST didn't run
|
|
166
|
-
* inside our explicit transaction).
|
|
167
|
-
*
|
|
168
|
-
* Errors are swallowed — a failed snapshot just means the time-window
|
|
169
|
-
* fallback degrades to "match correlation_id only" for this request.
|
|
170
|
-
* Never break the host's request over a bookkeeping query.
|
|
171
|
-
*/
|
|
172
|
-
declare function takeAuditSnapshot(exec: SqlExecutor): Promise<void>;
|
|
164
|
+
declare function sweepAuditLog(exec: SqlExecutor): Promise<number>;
|
|
165
|
+
/** Delete rows older than PRUNE_AGE without emitting them. Errors swallowed. */
|
|
166
|
+
declare function pruneAuditLog(exec: SqlExecutor): Promise<void>;
|
|
173
167
|
/**
|
|
174
168
|
* Detect which tables have the `ezlogs_audit` trigger attached. Used
|
|
175
169
|
* by the per-ORM adapters to decide whether their per-mutation emit
|
|
@@ -196,9 +190,10 @@ declare function detectTriggerTrackedTables(exec: SqlExecutor): Promise<Set<stri
|
|
|
196
190
|
*/
|
|
197
191
|
declare function emitAuditRows(rows: ReadonlyArray<AuditRow>): void;
|
|
198
192
|
/**
|
|
199
|
-
* End-of-request entry point.
|
|
200
|
-
* id and emits each one
|
|
201
|
-
*
|
|
193
|
+
* End-of-request entry point. Claims rows for the current correlation
|
|
194
|
+
* id and emits each one, then runs the throttled sweep (untagged /
|
|
195
|
+
* orphaned rows, as their own actions) and prune. Skips when no
|
|
196
|
+
* correlation is in scope or no exec is supplied.
|
|
202
197
|
*
|
|
203
198
|
* The HTTP capturers call this after the user's handler returns,
|
|
204
199
|
* before they push the HTTP event itself, so DB events arrive
|
|
@@ -206,11 +201,6 @@ declare function emitAuditRows(rows: ReadonlyArray<AuditRow>): void;
|
|
|
206
201
|
*/
|
|
207
202
|
declare function flushAuditLog(exec: SqlExecutor | null | undefined): Promise<void>;
|
|
208
203
|
|
|
209
|
-
/** Read the current request's audit snapshot, if any. */
|
|
210
|
-
declare function currentAuditSnapshot(): bigint | null;
|
|
211
|
-
/** Run `callback` inside a fresh audit-snapshot scope. */
|
|
212
|
-
declare function runWithAuditSnapshotScope<R>(callback: () => R): R;
|
|
213
|
-
|
|
214
204
|
interface PgQueryConfig {
|
|
215
205
|
text?: string;
|
|
216
206
|
name?: string;
|
|
@@ -247,4 +237,4 @@ interface PgModule {
|
|
|
247
237
|
*/
|
|
248
238
|
declare function patchPgClient(pgModule: PgModule): void;
|
|
249
239
|
|
|
250
|
-
export { type AuditRow, type DatabaseContextSnapshot, type Detected, type PgLikeClient, type SqlExecutor, applyDatabaseContext, buildTrackingSql,
|
|
240
|
+
export { type AuditRow, type DatabaseContextSnapshot, type Detected, type PgLikeClient, type SqlExecutor, applyDatabaseContext, buildTrackingSql, detectEngine, detectTriggerTrackedTables, emitAuditRows, flushAuditLog, getDatabaseContext, parseArgs, patchPgClient, pruneAuditLog, readAuditRows, runInstall, sweepAuditLog, withDatabaseContext };
|
package/dist/triggers/index.js
CHANGED
|
@@ -113,7 +113,6 @@ var Configuration = class {
|
|
|
113
113
|
displayNameFor = {};
|
|
114
114
|
isServerActionError = null;
|
|
115
115
|
databaseReader = null;
|
|
116
|
-
databaseSnapshotMode = false;
|
|
117
116
|
databaseUrl = null;
|
|
118
117
|
/**
|
|
119
118
|
* Tables that have the `ezlogs_audit` trigger attached. Detected once
|
|
@@ -165,9 +164,6 @@ var Configuration = class {
|
|
|
165
164
|
if (options.databaseReader !== void 0) {
|
|
166
165
|
this.databaseReader = options.databaseReader;
|
|
167
166
|
}
|
|
168
|
-
if (options.databaseSnapshotMode !== void 0) {
|
|
169
|
-
this.databaseSnapshotMode = options.databaseSnapshotMode;
|
|
170
|
-
}
|
|
171
167
|
if (options.databaseUrl !== void 0) {
|
|
172
168
|
this.databaseUrl = options.databaseUrl;
|
|
173
169
|
}
|
|
@@ -356,11 +352,11 @@ async function runInstall(args, options = {}) {
|
|
|
356
352
|
"\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."
|
|
357
353
|
);
|
|
358
354
|
} else {
|
|
359
|
-
const
|
|
360
|
-
const
|
|
361
|
-
${
|
|
355
|
+
const trackingSql = buildTrackingSql(args.tables, "postgres");
|
|
356
|
+
const fullSql = trackingSql ? `${installSql}
|
|
357
|
+
${trackingSql}
|
|
362
358
|
` : installSql;
|
|
363
|
-
out(
|
|
359
|
+
out(fullSql);
|
|
364
360
|
}
|
|
365
361
|
return 0;
|
|
366
362
|
}
|
|
@@ -401,12 +397,11 @@ ${trackingSql2}
|
|
|
401
397
|
return 1;
|
|
402
398
|
}
|
|
403
399
|
}
|
|
404
|
-
const trackingSql = buildTrackingSql(tablesToTrack, "postgres");
|
|
405
|
-
const fullSql = trackingSql ? `${installSql}
|
|
406
|
-
${trackingSql}
|
|
407
|
-
` : installSql;
|
|
408
400
|
try {
|
|
409
|
-
await options.exec(
|
|
401
|
+
await options.exec(installSql);
|
|
402
|
+
for (const table of tablesToTrack) {
|
|
403
|
+
await options.exec(buildTrackingSql([table], "postgres"));
|
|
404
|
+
}
|
|
410
405
|
if (tablesToTrack.length === 0) {
|
|
411
406
|
out(
|
|
412
407
|
"[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."
|
|
@@ -507,14 +502,34 @@ var storage = createScopedStorage(
|
|
|
507
502
|
function getCurrentActor() {
|
|
508
503
|
return storage.read();
|
|
509
504
|
}
|
|
505
|
+
function runWithActorScope(callback) {
|
|
506
|
+
return storage.run(null, callback);
|
|
507
|
+
}
|
|
510
508
|
|
|
511
509
|
// src/correlation.ts
|
|
512
510
|
var storage2 = createScopedStorage(
|
|
513
511
|
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:correlation-storage")
|
|
514
512
|
);
|
|
513
|
+
function generate() {
|
|
514
|
+
const ts = Date.now();
|
|
515
|
+
const hex = randomHexBytes(4);
|
|
516
|
+
return `ezl_${ts}_${hex}`;
|
|
517
|
+
}
|
|
518
|
+
function randomHexBytes(byteCount) {
|
|
519
|
+
const buf = new Uint8Array(byteCount);
|
|
520
|
+
globalThis.crypto.getRandomValues(buf);
|
|
521
|
+
let out = "";
|
|
522
|
+
for (let i = 0; i < buf.length; i++) {
|
|
523
|
+
out += buf[i].toString(16).padStart(2, "0");
|
|
524
|
+
}
|
|
525
|
+
return out;
|
|
526
|
+
}
|
|
515
527
|
function current() {
|
|
516
528
|
return storage2.read();
|
|
517
529
|
}
|
|
530
|
+
function runWithCorrelation(correlationId, callback) {
|
|
531
|
+
return storage2.run(correlationId, callback);
|
|
532
|
+
}
|
|
518
533
|
|
|
519
534
|
// src/triggers/context.ts
|
|
520
535
|
function getDatabaseContext() {
|
|
@@ -774,6 +789,26 @@ function describe2(error) {
|
|
|
774
789
|
if (error instanceof Error) return `${error.name}: ${error.message}`;
|
|
775
790
|
return String(error);
|
|
776
791
|
}
|
|
792
|
+
var SENSITIVE_WORD_PATTERNS = Object.freeze([
|
|
793
|
+
"pin",
|
|
794
|
+
"otp",
|
|
795
|
+
"totp",
|
|
796
|
+
"iban",
|
|
797
|
+
"mfa",
|
|
798
|
+
"passphrase"
|
|
799
|
+
]);
|
|
800
|
+
function snakeLower(key) {
|
|
801
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
802
|
+
}
|
|
803
|
+
function matchesSensitiveKey(key, substringPatterns) {
|
|
804
|
+
const lower = key.toLowerCase();
|
|
805
|
+
const snake = snakeLower(key);
|
|
806
|
+
if (substringPatterns.some((p) => lower.includes(p) || snake.includes(p))) {
|
|
807
|
+
return true;
|
|
808
|
+
}
|
|
809
|
+
const words = snake.split(/[^a-z0-9]+/);
|
|
810
|
+
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
811
|
+
}
|
|
777
812
|
|
|
778
813
|
// src/db/shared.ts
|
|
779
814
|
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
@@ -806,7 +841,19 @@ var DB_SENSITIVE_PATTERNS = Object.freeze([
|
|
|
806
841
|
"salt",
|
|
807
842
|
"digest",
|
|
808
843
|
"signature",
|
|
809
|
-
"hmac"
|
|
844
|
+
"hmac",
|
|
845
|
+
// 0.2.0 additions. `credential` / `passwd` / `apikey` were already in
|
|
846
|
+
// the HTTP list; `passcode` / `card_number` / `cardnumber` / `cvv` /
|
|
847
|
+
// `cvc` are new. Whole-word `pin` / `otp` / `totp` / `iban` come from
|
|
848
|
+
// SENSITIVE_WORD_PATTERNS. The SQL trigger mirrors this list.
|
|
849
|
+
"credential",
|
|
850
|
+
"passwd",
|
|
851
|
+
"passcode",
|
|
852
|
+
"apikey",
|
|
853
|
+
"card_number",
|
|
854
|
+
"cardnumber",
|
|
855
|
+
"cvv",
|
|
856
|
+
"cvc"
|
|
810
857
|
]);
|
|
811
858
|
function emitDbEvent(input) {
|
|
812
859
|
try {
|
|
@@ -833,7 +880,8 @@ function emitDbEvent(input) {
|
|
|
833
880
|
correlation_id: current(),
|
|
834
881
|
resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
835
882
|
context,
|
|
836
|
-
duration_ms: null
|
|
883
|
+
duration_ms: null,
|
|
884
|
+
timestamp: input.timestamp ?? null
|
|
837
885
|
});
|
|
838
886
|
pushEvent(event);
|
|
839
887
|
} catch (error) {
|
|
@@ -891,8 +939,7 @@ function isMeaningfulAttribute(name) {
|
|
|
891
939
|
return true;
|
|
892
940
|
}
|
|
893
941
|
function isDbSensitive(name) {
|
|
894
|
-
|
|
895
|
-
return DB_SENSITIVE_PATTERNS.some((p) => lower.includes(p));
|
|
942
|
+
return matchesSensitiveKey(name, DB_SENSITIVE_PATTERNS);
|
|
896
943
|
}
|
|
897
944
|
function isScalar(value) {
|
|
898
945
|
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || value instanceof Date) {
|
|
@@ -964,43 +1011,51 @@ function describeError(error) {
|
|
|
964
1011
|
return String(error);
|
|
965
1012
|
}
|
|
966
1013
|
|
|
967
|
-
// src/triggers/audit-snapshot.ts
|
|
968
|
-
var storage3 = createScopedStorage(
|
|
969
|
-
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:audit-snapshot-storage")
|
|
970
|
-
);
|
|
971
|
-
function currentAuditSnapshot() {
|
|
972
|
-
return storage3.read();
|
|
973
|
-
}
|
|
974
|
-
function setAuditSnapshot(value) {
|
|
975
|
-
storage3.update(value);
|
|
976
|
-
}
|
|
977
|
-
function runWithAuditSnapshotScope(callback) {
|
|
978
|
-
return storage3.run(0n, callback);
|
|
979
|
-
}
|
|
980
|
-
|
|
981
1014
|
// src/triggers/emit-dedup.ts
|
|
982
|
-
var
|
|
1015
|
+
var storage3 = createScopedStorage(
|
|
983
1016
|
/* @__PURE__ */ Symbol.for("ezlogs-nextjs:audit-emitted-ids-storage")
|
|
984
1017
|
);
|
|
985
1018
|
function currentEmittedAuditIds() {
|
|
986
|
-
return
|
|
1019
|
+
return storage3.read();
|
|
987
1020
|
}
|
|
988
1021
|
|
|
989
1022
|
// src/triggers/reader.ts
|
|
990
|
-
|
|
1023
|
+
var COLUMNS = "id, table_name, op, old_row, new_row, resource_id, correlation_id, txid, captured_at";
|
|
1024
|
+
var ORPHAN_AGE = "5 minutes";
|
|
1025
|
+
var PRUNE_AGE = "24 hours";
|
|
1026
|
+
var SWEEP_BATCH = 100;
|
|
1027
|
+
var PRUNE_BATCH = 2e3;
|
|
1028
|
+
var SWEEP_INTERVAL_MS = 1e4;
|
|
1029
|
+
var PRUNE_INTERVAL_MS = 10 * 6e4;
|
|
1030
|
+
var lastSweepAt = 0;
|
|
1031
|
+
var lastPruneAt = 0;
|
|
1032
|
+
var deleteDenied = false;
|
|
1033
|
+
var txCorrelation = /* @__PURE__ */ new Map();
|
|
1034
|
+
async function readAuditRows(exec, correlationId) {
|
|
991
1035
|
try {
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1036
|
+
if (!deleteDenied) {
|
|
1037
|
+
try {
|
|
1038
|
+
return sortById(
|
|
1039
|
+
extractRows(
|
|
1040
|
+
await exec(
|
|
1041
|
+
`DELETE FROM ezlogs_audit_log WHERE correlation_id = $1 RETURNING ${COLUMNS}`,
|
|
1042
|
+
[correlationId]
|
|
1043
|
+
)
|
|
1044
|
+
).map(normalizeAuditRow)
|
|
1045
|
+
);
|
|
1046
|
+
} catch (err) {
|
|
1047
|
+
if (!isPermissionDenied(err)) throw err;
|
|
1048
|
+
deleteDenied = true;
|
|
1049
|
+
logger.warn(
|
|
1050
|
+
"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'`."
|
|
1051
|
+
);
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
const result = await exec(
|
|
1055
|
+
`SELECT ${COLUMNS} FROM ezlogs_audit_log WHERE correlation_id = $1 ORDER BY id ASC`,
|
|
1056
|
+
[correlationId]
|
|
1057
|
+
);
|
|
1058
|
+
return extractRows(result).map(normalizeAuditRow);
|
|
1004
1059
|
} catch (err) {
|
|
1005
1060
|
logger.debug(
|
|
1006
1061
|
`readAuditRows failed: ${err instanceof Error ? err.message : String(err)}`
|
|
@@ -1008,21 +1063,57 @@ async function readAuditRows(exec, correlationId, snapshotMaxId = null) {
|
|
|
1008
1063
|
return [];
|
|
1009
1064
|
}
|
|
1010
1065
|
}
|
|
1011
|
-
async function
|
|
1066
|
+
async function sweepAuditLog(exec) {
|
|
1067
|
+
if (deleteDenied) return 0;
|
|
1012
1068
|
try {
|
|
1013
|
-
const
|
|
1014
|
-
|
|
1069
|
+
const rows = sortById(
|
|
1070
|
+
extractRows(
|
|
1071
|
+
await exec(
|
|
1072
|
+
`DELETE FROM ezlogs_audit_log
|
|
1073
|
+
WHERE id IN (
|
|
1074
|
+
SELECT id FROM ezlogs_audit_log
|
|
1075
|
+
WHERE (correlation_id IS NULL
|
|
1076
|
+
OR captured_at < now() - interval '${ORPHAN_AGE}')
|
|
1077
|
+
-- Never resurface history: past the retention cap a
|
|
1078
|
+
-- row is pruned, not sent as new activity.
|
|
1079
|
+
AND captured_at > now() - interval '${PRUNE_AGE}'
|
|
1080
|
+
ORDER BY id
|
|
1081
|
+
LIMIT ${SWEEP_BATCH}
|
|
1082
|
+
FOR UPDATE SKIP LOCKED)
|
|
1083
|
+
RETURNING ${COLUMNS}`,
|
|
1084
|
+
[]
|
|
1085
|
+
)
|
|
1086
|
+
).map(normalizeAuditRow)
|
|
1087
|
+
);
|
|
1088
|
+
for (const row of rows) {
|
|
1089
|
+
const correlationId = row.correlation_id ?? correlationForTx(row.txid);
|
|
1090
|
+
runWithCorrelation(
|
|
1091
|
+
correlationId,
|
|
1092
|
+
() => runWithActorScope(() => emitOne(row))
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
return rows.length;
|
|
1096
|
+
} catch (err) {
|
|
1097
|
+
logger.debug(
|
|
1098
|
+
`sweepAuditLog failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1099
|
+
);
|
|
1100
|
+
return 0;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
async function pruneAuditLog(exec) {
|
|
1104
|
+
if (deleteDenied) return;
|
|
1105
|
+
try {
|
|
1106
|
+
await exec(
|
|
1107
|
+
`DELETE FROM ezlogs_audit_log
|
|
1108
|
+
WHERE id IN (
|
|
1109
|
+
SELECT id FROM ezlogs_audit_log
|
|
1110
|
+
WHERE captured_at < now() - interval '${PRUNE_AGE}'
|
|
1111
|
+
LIMIT ${PRUNE_BATCH})`,
|
|
1015
1112
|
[]
|
|
1016
1113
|
);
|
|
1017
|
-
const rows = extractRows(result);
|
|
1018
|
-
if (rows.length === 0) return;
|
|
1019
|
-
const raw = rows[0] ?? {};
|
|
1020
|
-
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;
|
|
1021
|
-
if (value === null) return;
|
|
1022
|
-
setAuditSnapshot(value);
|
|
1023
1114
|
} catch (err) {
|
|
1024
1115
|
logger.debug(
|
|
1025
|
-
`
|
|
1116
|
+
`pruneAuditLog failed: ${err instanceof Error ? err.message : String(err)}`
|
|
1026
1117
|
);
|
|
1027
1118
|
}
|
|
1028
1119
|
}
|
|
@@ -1060,8 +1151,19 @@ async function flushAuditLog(exec) {
|
|
|
1060
1151
|
if (!exec) return;
|
|
1061
1152
|
const correlationId = current();
|
|
1062
1153
|
if (!correlationId) return;
|
|
1063
|
-
const
|
|
1064
|
-
|
|
1154
|
+
const rows = await readAuditRows(exec, correlationId);
|
|
1155
|
+
emitFresh(rows);
|
|
1156
|
+
const now = Date.now();
|
|
1157
|
+
if (now - lastPruneAt >= PRUNE_INTERVAL_MS) {
|
|
1158
|
+
lastPruneAt = now;
|
|
1159
|
+
await pruneAuditLog(exec);
|
|
1160
|
+
}
|
|
1161
|
+
if (now - lastSweepAt >= SWEEP_INTERVAL_MS) {
|
|
1162
|
+
lastSweepAt = now;
|
|
1163
|
+
await sweepAuditLog(exec);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
function emitFresh(rows) {
|
|
1065
1167
|
if (rows.length === 0) return;
|
|
1066
1168
|
const emitted = currentEmittedAuditIds();
|
|
1067
1169
|
if (emitted === null) {
|
|
@@ -1074,9 +1176,26 @@ async function flushAuditLog(exec) {
|
|
|
1074
1176
|
emitted.add(row.id);
|
|
1075
1177
|
fresh.push(row);
|
|
1076
1178
|
}
|
|
1077
|
-
if (fresh.length === 0) return;
|
|
1078
1179
|
emitAuditRows(fresh);
|
|
1079
1180
|
}
|
|
1181
|
+
function correlationForTx(txid) {
|
|
1182
|
+
if (!txid) return generate();
|
|
1183
|
+
let id = txCorrelation.get(txid);
|
|
1184
|
+
if (!id) {
|
|
1185
|
+
if (txCorrelation.size >= 1e3) txCorrelation.clear();
|
|
1186
|
+
id = generate();
|
|
1187
|
+
txCorrelation.set(txid, id);
|
|
1188
|
+
}
|
|
1189
|
+
return id;
|
|
1190
|
+
}
|
|
1191
|
+
function isPermissionDenied(err) {
|
|
1192
|
+
return err?.code === "42501";
|
|
1193
|
+
}
|
|
1194
|
+
function sortById(rows) {
|
|
1195
|
+
return rows.sort(
|
|
1196
|
+
(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
|
|
1197
|
+
);
|
|
1198
|
+
}
|
|
1080
1199
|
function emitOne(row) {
|
|
1081
1200
|
const op = mapOp(row.op);
|
|
1082
1201
|
if (!op) return;
|
|
@@ -1090,6 +1209,7 @@ function emitOne(row) {
|
|
|
1090
1209
|
initialAttributes: newRow,
|
|
1091
1210
|
modelInstance: newRow,
|
|
1092
1211
|
displayName: null,
|
|
1212
|
+
timestamp: row.captured_at ?? null,
|
|
1093
1213
|
_fromTrigger: true
|
|
1094
1214
|
});
|
|
1095
1215
|
return;
|
|
@@ -1108,6 +1228,7 @@ function emitOne(row) {
|
|
|
1108
1228
|
// user just told the system to call this row.
|
|
1109
1229
|
modelInstance: newRow,
|
|
1110
1230
|
displayName: null,
|
|
1231
|
+
timestamp: row.captured_at ?? null,
|
|
1111
1232
|
_fromTrigger: true
|
|
1112
1233
|
});
|
|
1113
1234
|
return;
|
|
@@ -1120,6 +1241,7 @@ function emitOne(row) {
|
|
|
1120
1241
|
initialAttributes: oldRow,
|
|
1121
1242
|
modelInstance: oldRow,
|
|
1122
1243
|
displayName: null,
|
|
1244
|
+
timestamp: row.captured_at ?? null,
|
|
1123
1245
|
_fromTrigger: true
|
|
1124
1246
|
});
|
|
1125
1247
|
return;
|
|
@@ -1168,6 +1290,11 @@ function extractRows(result) {
|
|
|
1168
1290
|
}
|
|
1169
1291
|
return [];
|
|
1170
1292
|
}
|
|
1293
|
+
function toDate(value) {
|
|
1294
|
+
if (value == null) return null;
|
|
1295
|
+
const d = value instanceof Date ? value : new Date(String(value));
|
|
1296
|
+
return Number.isNaN(d.getTime()) ? null : d;
|
|
1297
|
+
}
|
|
1171
1298
|
function normalizeAuditRow(raw) {
|
|
1172
1299
|
const r = raw ?? {};
|
|
1173
1300
|
return {
|
|
@@ -1176,7 +1303,10 @@ function normalizeAuditRow(raw) {
|
|
|
1176
1303
|
op: r.op,
|
|
1177
1304
|
old_row: r.old_row ?? null,
|
|
1178
1305
|
new_row: r.new_row ?? null,
|
|
1179
|
-
resource_id: r.resource_id == null ? null : String(r.resource_id)
|
|
1306
|
+
resource_id: r.resource_id == null ? null : String(r.resource_id),
|
|
1307
|
+
correlation_id: r.correlation_id == null ? null : String(r.correlation_id),
|
|
1308
|
+
txid: r.txid == null ? null : String(r.txid),
|
|
1309
|
+
captured_at: toDate(r.captured_at)
|
|
1180
1310
|
};
|
|
1181
1311
|
}
|
|
1182
1312
|
function coerceJsonb(value) {
|
|
@@ -1369,4 +1499,4 @@ function leadingVerb(text) {
|
|
|
1369
1499
|
return m ? m[0].toUpperCase() : "";
|
|
1370
1500
|
}
|
|
1371
1501
|
|
|
1372
|
-
export { applyDatabaseContext, buildTrackingSql,
|
|
1502
|
+
export { applyDatabaseContext, buildTrackingSql, detectEngine, detectTriggerTrackedTables, emitAuditRows, flushAuditLog, getDatabaseContext, parseArgs, patchPgClient, pruneAuditLog, readAuditRows, runInstall, sweepAuditLog, withDatabaseContext };
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ezlogs-nextjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "EZLogs agent for Next.js — capture HTTP, database, and background-job events as plain-English Action cards.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "EZLogs",
|
|
7
|
-
"homepage": "https://
|
|
7
|
+
"homepage": "https://ezlogs.io/nextjs/",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/dezsirazvan/ez_logs_agent_nextjs.git"
|
|
@@ -138,9 +138,11 @@
|
|
|
138
138
|
}
|
|
139
139
|
},
|
|
140
140
|
"devDependencies": {
|
|
141
|
+
"@prisma/client": "^5.22.0",
|
|
141
142
|
"@types/node": "^20.11.0",
|
|
142
143
|
"@types/pg": "^8.20.0",
|
|
143
144
|
"pg": "^8.20.0",
|
|
145
|
+
"prisma": "^5.22.0",
|
|
144
146
|
"sucrase": "^3.35.0",
|
|
145
147
|
"tsup": "^8.0.2",
|
|
146
148
|
"typescript": "^5.4.0",
|