ezlogs-nextjs 0.2.0 → 0.3.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.
@@ -812,6 +812,37 @@ function matchesSensitiveKey(key, substringPatterns) {
812
812
  const words = snake.split(/[^a-z0-9]+/);
813
813
  return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
814
814
  }
815
+ function passesLuhn(digits) {
816
+ let sum = 0;
817
+ for (let i = 0; i < digits.length; i += 1) {
818
+ let d = Number(digits[digits.length - 1 - i]);
819
+ if (i % 2 === 1) {
820
+ d *= 2;
821
+ if (d > 9) d -= 9;
822
+ }
823
+ sum += d;
824
+ }
825
+ return sum % 10 === 0;
826
+ }
827
+ var SECRET_TEXT_PATTERNS = [
828
+ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
829
+ /\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
830
+ /\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
831
+ /\bsk-[A-Za-z0-9_-]{16,}/g,
832
+ /\bgh[pousr]_[A-Za-z0-9]{20,}/g,
833
+ /\bxox[abposr]-[A-Za-z0-9-]+/g,
834
+ /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
835
+ ];
836
+ var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
837
+ function scrubSecretsInText(text, options = {}) {
838
+ let out = text;
839
+ for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
840
+ out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
841
+ const digits = match.replace(/[ -]/g, "");
842
+ return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
843
+ });
844
+ return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
845
+ }
815
846
 
816
847
  // src/db/shared.ts
817
848
  var IGNORED_ATTRIBUTES = Object.freeze([
@@ -820,8 +851,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
820
851
  "created_at",
821
852
  "updatedAt",
822
853
  "updated_at",
823
- "deletedAt",
824
- "deleted_at",
825
854
  // Auth.js / NextAuth standard columns
826
855
  "emailVerified",
827
856
  "expires"
@@ -865,14 +894,17 @@ function emitDbEvent(input) {
865
894
  if (config.allExcludedTables().includes(input.modelClass)) return;
866
895
  if (config.databaseReader && !input._fromTrigger) {
867
896
  const tracked = config.triggerTrackedTables;
868
- if (tracked === null || tracked.has(input.modelClass)) return;
897
+ if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
869
898
  }
870
899
  if (input.operation === "update") {
871
900
  const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
872
901
  if (meaningful.length === 0) return;
873
902
  }
874
903
  const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
875
- const context = buildContext(input);
904
+ const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
905
+ const context = buildContext(
906
+ related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
907
+ );
876
908
  const event = build({
877
909
  source_type: "database_callback",
878
910
  source_data: {
@@ -881,7 +913,7 @@ function emitDbEvent(input) {
881
913
  },
882
914
  outcome: "success",
883
915
  correlation_id: current(),
884
- resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
916
+ resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
885
917
  context,
886
918
  duration_ms: null,
887
919
  timestamp: input.timestamp ?? null
@@ -928,7 +960,7 @@ function filterMeaningfulAttributes(attrs) {
928
960
  return Object.keys(out).length === 0 ? null : out;
929
961
  }
930
962
  function filterMeaningfulChanges(changes) {
931
- return changes.filter((c) => {
963
+ return flattenNestedChanges(changes).filter((c) => {
932
964
  if (!isMeaningfulAttribute(c.attribute)) return false;
933
965
  if (!isScalar(c.to)) return false;
934
966
  if (c.from === void 0) return true;
@@ -936,6 +968,60 @@ function filterMeaningfulChanges(changes) {
936
968
  return valuesActuallyChanged(c.from, c.to);
937
969
  });
938
970
  }
971
+ var MAX_NESTED_DEPTH = 3;
972
+ var MAX_NESTED_LEAVES = 20;
973
+ var MAX_UNKNOWN_FROM_LEAVES = 5;
974
+ function flattenNestedChanges(changes) {
975
+ if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
976
+ const out = [];
977
+ for (const c of changes) {
978
+ if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
979
+ out.push(c);
980
+ continue;
981
+ }
982
+ if (!isMeaningfulAttribute(c.attribute)) continue;
983
+ const leaves = [];
984
+ collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
985
+ const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
986
+ out.push(...leaves.slice(0, cap).map(scrubLeaf));
987
+ }
988
+ return out;
989
+ }
990
+ function collectLeafChanges(path, from, to, depth, out) {
991
+ if (out.length > MAX_NESTED_LEAVES) return;
992
+ const fromObj = isPlainRecord(from);
993
+ const toObj = isPlainRecord(to);
994
+ if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
995
+ const f = fromObj ? from : {};
996
+ const t = toObj ? to : {};
997
+ for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
998
+ const before = from === void 0 ? void 0 : f[key] ?? null;
999
+ collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
1000
+ }
1001
+ return;
1002
+ }
1003
+ if (from !== void 0 && sameJson(from, to)) return;
1004
+ out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
1005
+ }
1006
+ function scrubLeaf(change) {
1007
+ const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
1008
+ const out = { attribute: change.attribute, to: scrub(change.to) };
1009
+ if (change.from !== void 0) out.from = scrub(change.from);
1010
+ return out;
1011
+ }
1012
+ function isPlainRecord(value) {
1013
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
1014
+ const proto = Object.getPrototypeOf(value);
1015
+ return proto === Object.prototype || proto === null;
1016
+ }
1017
+ function sameJson(a, b) {
1018
+ if (a === b) return true;
1019
+ try {
1020
+ return JSON.stringify(a) === JSON.stringify(b);
1021
+ } catch {
1022
+ return false;
1023
+ }
1024
+ }
939
1025
  function isMeaningfulAttribute(name) {
940
1026
  if (IGNORED_ATTRIBUTES.includes(name)) return false;
941
1027
  if (isDbSensitive(name)) return false;
@@ -973,22 +1059,44 @@ function isPrismaDecimal(value) {
973
1059
  const obj = value;
974
1060
  return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
975
1061
  }
1062
+ var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
1063
+ var MAX_DISPLAY_NAME_LENGTH = 120;
976
1064
  function resolveDisplayName(modelClass, instance) {
977
1065
  try {
978
1066
  const config = configuration();
979
1067
  const resolver = config.displayNameFor[modelClass];
980
1068
  const resolved = applyResolver(resolver, instance);
981
1069
  if (resolved !== null) return resolved;
982
- for (const field of ["name", "title", "number"]) {
983
- const value = instance[field];
984
- if (value !== null && value !== void 0 && String(value).length > 0) {
985
- return String(value);
1070
+ const columns = /* @__PURE__ */ new Map();
1071
+ for (const key of Object.keys(instance)) {
1072
+ if (isDbSensitive(key)) continue;
1073
+ const norm = key.toLowerCase().replace(/_/g, "");
1074
+ if (!columns.has(norm)) columns.set(norm, instance[key]);
1075
+ }
1076
+ const text = (norm) => {
1077
+ const value = columns.get(norm);
1078
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
1079
+ return null;
986
1080
  }
1081
+ const str = String(value).trim();
1082
+ return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
1083
+ };
1084
+ for (const column of DISPLAY_NAME_COLUMNS) {
1085
+ const value = text(column);
1086
+ if (value !== null) return value;
987
1087
  }
1088
+ return text(`${singularNorm(modelClass)}number`);
988
1089
  } catch {
989
1090
  }
990
1091
  return null;
991
1092
  }
1093
+ function singularNorm(modelClass) {
1094
+ const norm = modelClass.toLowerCase().replace(/_/g, "");
1095
+ if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
1096
+ if (/(ss|us|is)$/.test(norm)) return norm;
1097
+ if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
1098
+ return norm.endsWith("s") ? norm.slice(0, -1) : norm;
1099
+ }
992
1100
  function applyResolver(resolver, instance) {
993
1101
  if (typeof resolver === "string") {
994
1102
  const value = instance[resolver];
@@ -1034,15 +1142,17 @@ var lastSweepAt = 0;
1034
1142
  var lastPruneAt = 0;
1035
1143
  var deleteDenied = false;
1036
1144
  var txCorrelation = /* @__PURE__ */ new Map();
1037
- async function readAuditRows(exec, correlationId) {
1145
+ async function readAuditRows(exec, correlationId, since = null) {
1146
+ const where = since ? "correlation_id = $1 AND captured_at >= $2" : "correlation_id = $1";
1147
+ const params = since ? [correlationId, since.toISOString()] : [correlationId];
1038
1148
  try {
1039
1149
  if (!deleteDenied) {
1040
1150
  try {
1041
1151
  return sortById(
1042
1152
  extractRows(
1043
1153
  await exec(
1044
- `DELETE FROM ezlogs_audit_log WHERE correlation_id = $1 RETURNING ${COLUMNS}`,
1045
- [correlationId]
1154
+ `DELETE FROM ezlogs_audit_log WHERE ${where} RETURNING ${COLUMNS}`,
1155
+ params
1046
1156
  )
1047
1157
  ).map(normalizeAuditRow)
1048
1158
  );
@@ -1055,8 +1165,8 @@ async function readAuditRows(exec, correlationId) {
1055
1165
  }
1056
1166
  }
1057
1167
  const result = await exec(
1058
- `SELECT ${COLUMNS} FROM ezlogs_audit_log WHERE correlation_id = $1 ORDER BY id ASC`,
1059
- [correlationId]
1168
+ `SELECT ${COLUMNS} FROM ezlogs_audit_log WHERE ${where} ORDER BY id ASC`,
1169
+ params
1060
1170
  );
1061
1171
  return extractRows(result).map(normalizeAuditRow);
1062
1172
  } catch (err) {
@@ -1088,6 +1198,7 @@ async function sweepAuditLog(exec) {
1088
1198
  )
1089
1199
  ).map(normalizeAuditRow)
1090
1200
  );
1201
+ await loadTableMeta(exec, rows);
1091
1202
  for (const row of rows) {
1092
1203
  const correlationId = row.correlation_id ?? correlationForTx(row.txid);
1093
1204
  runWithCorrelation(
@@ -1150,11 +1261,12 @@ function emitAuditRows(rows) {
1150
1261
  emitOne(row);
1151
1262
  }
1152
1263
  }
1153
- async function flushAuditLog(exec) {
1264
+ async function flushAuditLog(exec, options = {}) {
1154
1265
  if (!exec) return;
1155
1266
  const correlationId = current();
1156
1267
  if (!correlationId) return;
1157
- const rows = await readAuditRows(exec, correlationId);
1268
+ const rows = await readAuditRows(exec, correlationId, options.since ?? null);
1269
+ await loadTableMeta(exec, rows);
1158
1270
  emitFresh(rows);
1159
1271
  const now = Date.now();
1160
1272
  if (now - lastPruneAt >= PRUNE_INTERVAL_MS) {
@@ -1166,6 +1278,79 @@ async function flushAuditLog(exec) {
1166
1278
  await sweepAuditLog(exec);
1167
1279
  }
1168
1280
  }
1281
+ var tableMeta = /* @__PURE__ */ new Map();
1282
+ var META_RETRY_MS = 6e4;
1283
+ var metaRetryAt = 0;
1284
+ async function loadTableMeta(exec, rows) {
1285
+ if (Date.now() < metaRetryAt) return;
1286
+ const tables = [
1287
+ ...new Set(rows.map((r) => r.table_name).filter((t) => t !== "" && !t.includes(",") && !tableMeta.has(t)))
1288
+ ];
1289
+ if (tables.length === 0) return;
1290
+ const list = tables.join(",");
1291
+ try {
1292
+ const fkRows = extractRows(
1293
+ await exec(
1294
+ `SELECT cl.relname AS table_name, a.attname AS column_name, rf.relname AS ref_table
1295
+ FROM pg_constraint c
1296
+ JOIN pg_class cl ON cl.oid = c.conrelid
1297
+ JOIN pg_namespace n ON n.oid = cl.relnamespace
1298
+ JOIN pg_class rf ON rf.oid = c.confrelid
1299
+ JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1]
1300
+ WHERE c.contype = 'f'
1301
+ AND n.nspname = 'public'
1302
+ AND array_length(c.conkey, 1) = 1
1303
+ AND cl.relname = ANY(string_to_array($1, ','))
1304
+ ORDER BY cl.relname, a.attnum`,
1305
+ [list]
1306
+ )
1307
+ );
1308
+ const tsRows = extractRows(
1309
+ await exec(
1310
+ `SELECT cl.relname AS table_name, a.attname AS column_name
1311
+ FROM pg_attribute a
1312
+ JOIN pg_class cl ON cl.oid = a.attrelid
1313
+ JOIN pg_namespace n ON n.oid = cl.relnamespace
1314
+ WHERE n.nspname = 'public'
1315
+ AND cl.relname = ANY(string_to_array($1, ','))
1316
+ AND a.attnum > 0
1317
+ AND NOT a.attisdropped
1318
+ AND a.atttypid = 'timestamp'::regtype`,
1319
+ [list]
1320
+ )
1321
+ );
1322
+ const fks = /* @__PURE__ */ new Map();
1323
+ for (const raw of fkRows) {
1324
+ const r = raw ?? {};
1325
+ if (typeof r.table_name !== "string" || typeof r.column_name !== "string" || typeof r.ref_table !== "string") {
1326
+ continue;
1327
+ }
1328
+ fks.set(r.table_name, [...fks.get(r.table_name) ?? [], { column: r.column_name, refTable: r.ref_table }]);
1329
+ }
1330
+ const timestamps = /* @__PURE__ */ new Map();
1331
+ for (const raw of tsRows) {
1332
+ const r = raw ?? {};
1333
+ if (typeof r.table_name !== "string" || typeof r.column_name !== "string") continue;
1334
+ timestamps.set(r.table_name, (timestamps.get(r.table_name) ?? /* @__PURE__ */ new Set()).add(r.column_name));
1335
+ }
1336
+ for (const table of tables) {
1337
+ tableMeta.set(table, { fks: fks.get(table) ?? [], naiveTimestamps: timestamps.get(table) ?? /* @__PURE__ */ new Set() });
1338
+ }
1339
+ } catch (err) {
1340
+ metaRetryAt = Date.now() + META_RETRY_MS;
1341
+ logger.debug(`table metadata lookup failed: ${err instanceof Error ? err.message : String(err)}`);
1342
+ }
1343
+ }
1344
+ function relatedResourceIds(table, row) {
1345
+ const out = [];
1346
+ for (const fk of tableMeta.get(table)?.fks ?? []) {
1347
+ const value = row[fk.column];
1348
+ if (typeof value === "string" || typeof value === "number") {
1349
+ out.push({ resource_type: fk.refTable, resource_id: String(value) });
1350
+ }
1351
+ }
1352
+ return out;
1353
+ }
1169
1354
  function emitFresh(rows) {
1170
1355
  if (rows.length === 0) return;
1171
1356
  const emitted = currentEmittedAuditIds();
@@ -1202,8 +1387,10 @@ function sortById(rows) {
1202
1387
  function emitOne(row) {
1203
1388
  const op = mapOp(row.op);
1204
1389
  if (!op) return;
1205
- const oldRow = coerceJsonb(row.old_row);
1206
- const newRow = coerceJsonb(row.new_row);
1390
+ const naive = tableMeta.get(row.table_name)?.naiveTimestamps ?? null;
1391
+ const oldRow = withUtcTimestamps(coerceJsonb(row.old_row), naive);
1392
+ const newRow = withUtcTimestamps(coerceJsonb(row.new_row), naive);
1393
+ const related = row.resource_id === null ? relatedResourceIds(row.table_name, newRow ?? oldRow ?? {}) : null;
1207
1394
  if (op === "create" && newRow) {
1208
1395
  emitDbEvent({
1209
1396
  modelClass: row.table_name,
@@ -1213,6 +1400,7 @@ function emitOne(row) {
1213
1400
  modelInstance: newRow,
1214
1401
  displayName: null,
1215
1402
  timestamp: row.captured_at ?? null,
1403
+ relatedResourceIds: related,
1216
1404
  _fromTrigger: true
1217
1405
  });
1218
1406
  return;
@@ -1232,6 +1420,7 @@ function emitOne(row) {
1232
1420
  modelInstance: newRow,
1233
1421
  displayName: null,
1234
1422
  timestamp: row.captured_at ?? null,
1423
+ relatedResourceIds: related,
1235
1424
  _fromTrigger: true
1236
1425
  });
1237
1426
  return;
@@ -1245,11 +1434,22 @@ function emitOne(row) {
1245
1434
  modelInstance: oldRow,
1246
1435
  displayName: null,
1247
1436
  timestamp: row.captured_at ?? null,
1437
+ relatedResourceIds: related,
1248
1438
  _fromTrigger: true
1249
1439
  });
1250
1440
  return;
1251
1441
  }
1252
1442
  }
1443
+ var NAIVE_TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?$/;
1444
+ function withUtcTimestamps(row, naiveColumns) {
1445
+ if (!row) return row;
1446
+ const out = {};
1447
+ for (const [key, value] of Object.entries(row)) {
1448
+ const isNaiveColumn = naiveColumns ? naiveColumns.has(key) : /(_at|At)$/.test(key);
1449
+ out[key] = isNaiveColumn && typeof value === "string" && NAIVE_TIMESTAMP.test(value) ? `${value}Z` : value;
1450
+ }
1451
+ return out;
1452
+ }
1253
1453
  function mapOp(op) {
1254
1454
  if (op === "INSERT") return "create";
1255
1455
  if (op === "UPDATE") return "update";
@@ -148,10 +148,13 @@ interface AuditRow {
148
148
  * If the reader role may not DELETE, falls back to a plain SELECT
149
149
  * (rows then stay until the customer cleans them up) and warns once.
150
150
  *
151
+ * `since` limits the claim to rows written at or after that moment
152
+ * (a job claims only what it wrote, never the enqueuing request's rows).
153
+ *
151
154
  * Errors are caught and returned as `[]` — a failed audit read must
152
155
  * never break the host request.
153
156
  */
154
- declare function readAuditRows(exec: SqlExecutor, correlationId: string): Promise<AuditRow[]>;
157
+ declare function readAuditRows(exec: SqlExecutor, correlationId: string, since?: Date | null): Promise<AuditRow[]>;
155
158
  /**
156
159
  * Drain rows no request will claim: untagged rows, and tagged rows
157
160
  * older than ORPHAN_AGE. Each is emitted under its stored correlation
@@ -199,7 +202,9 @@ declare function emitAuditRows(rows: ReadonlyArray<AuditRow>): void;
199
202
  * before they push the HTTP event itself, so DB events arrive
200
203
  * grouped under the same correlation as the HTTP card.
201
204
  */
202
- declare function flushAuditLog(exec: SqlExecutor | null | undefined): Promise<void>;
205
+ declare function flushAuditLog(exec: SqlExecutor | null | undefined, options?: {
206
+ since?: Date | null;
207
+ }): Promise<void>;
203
208
 
204
209
  interface PgQueryConfig {
205
210
  text?: string;
@@ -148,10 +148,13 @@ interface AuditRow {
148
148
  * If the reader role may not DELETE, falls back to a plain SELECT
149
149
  * (rows then stay until the customer cleans them up) and warns once.
150
150
  *
151
+ * `since` limits the claim to rows written at or after that moment
152
+ * (a job claims only what it wrote, never the enqueuing request's rows).
153
+ *
151
154
  * Errors are caught and returned as `[]` — a failed audit read must
152
155
  * never break the host request.
153
156
  */
154
- declare function readAuditRows(exec: SqlExecutor, correlationId: string): Promise<AuditRow[]>;
157
+ declare function readAuditRows(exec: SqlExecutor, correlationId: string, since?: Date | null): Promise<AuditRow[]>;
155
158
  /**
156
159
  * Drain rows no request will claim: untagged rows, and tagged rows
157
160
  * older than ORPHAN_AGE. Each is emitted under its stored correlation
@@ -199,7 +202,9 @@ declare function emitAuditRows(rows: ReadonlyArray<AuditRow>): void;
199
202
  * before they push the HTTP event itself, so DB events arrive
200
203
  * grouped under the same correlation as the HTTP card.
201
204
  */
202
- declare function flushAuditLog(exec: SqlExecutor | null | undefined): Promise<void>;
205
+ declare function flushAuditLog(exec: SqlExecutor | null | undefined, options?: {
206
+ since?: Date | null;
207
+ }): Promise<void>;
203
208
 
204
209
  interface PgQueryConfig {
205
210
  text?: string;