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.
@@ -809,6 +809,37 @@ function matchesSensitiveKey(key, substringPatterns) {
809
809
  const words = snake.split(/[^a-z0-9]+/);
810
810
  return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
811
811
  }
812
+ function passesLuhn(digits) {
813
+ let sum = 0;
814
+ for (let i = 0; i < digits.length; i += 1) {
815
+ let d = Number(digits[digits.length - 1 - i]);
816
+ if (i % 2 === 1) {
817
+ d *= 2;
818
+ if (d > 9) d -= 9;
819
+ }
820
+ sum += d;
821
+ }
822
+ return sum % 10 === 0;
823
+ }
824
+ var SECRET_TEXT_PATTERNS = [
825
+ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
826
+ /\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
827
+ /\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
828
+ /\bsk-[A-Za-z0-9_-]{16,}/g,
829
+ /\bgh[pousr]_[A-Za-z0-9]{20,}/g,
830
+ /\bxox[abposr]-[A-Za-z0-9-]+/g,
831
+ /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
832
+ ];
833
+ var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
834
+ function scrubSecretsInText(text, options = {}) {
835
+ let out = text;
836
+ for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
837
+ out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
838
+ const digits = match.replace(/[ -]/g, "");
839
+ return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
840
+ });
841
+ return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
842
+ }
812
843
 
813
844
  // src/db/shared.ts
814
845
  var IGNORED_ATTRIBUTES = Object.freeze([
@@ -817,8 +848,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
817
848
  "created_at",
818
849
  "updatedAt",
819
850
  "updated_at",
820
- "deletedAt",
821
- "deleted_at",
822
851
  // Auth.js / NextAuth standard columns
823
852
  "emailVerified",
824
853
  "expires"
@@ -862,14 +891,17 @@ function emitDbEvent(input) {
862
891
  if (config.allExcludedTables().includes(input.modelClass)) return;
863
892
  if (config.databaseReader && !input._fromTrigger) {
864
893
  const tracked = config.triggerTrackedTables;
865
- if (tracked === null || tracked.has(input.modelClass)) return;
894
+ if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
866
895
  }
867
896
  if (input.operation === "update") {
868
897
  const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
869
898
  if (meaningful.length === 0) return;
870
899
  }
871
900
  const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
872
- const context = buildContext(input);
901
+ const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
902
+ const context = buildContext(
903
+ related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
904
+ );
873
905
  const event = build({
874
906
  source_type: "database_callback",
875
907
  source_data: {
@@ -878,7 +910,7 @@ function emitDbEvent(input) {
878
910
  },
879
911
  outcome: "success",
880
912
  correlation_id: current(),
881
- resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
913
+ resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
882
914
  context,
883
915
  duration_ms: null,
884
916
  timestamp: input.timestamp ?? null
@@ -925,7 +957,7 @@ function filterMeaningfulAttributes(attrs) {
925
957
  return Object.keys(out).length === 0 ? null : out;
926
958
  }
927
959
  function filterMeaningfulChanges(changes) {
928
- return changes.filter((c) => {
960
+ return flattenNestedChanges(changes).filter((c) => {
929
961
  if (!isMeaningfulAttribute(c.attribute)) return false;
930
962
  if (!isScalar(c.to)) return false;
931
963
  if (c.from === void 0) return true;
@@ -933,6 +965,60 @@ function filterMeaningfulChanges(changes) {
933
965
  return valuesActuallyChanged(c.from, c.to);
934
966
  });
935
967
  }
968
+ var MAX_NESTED_DEPTH = 3;
969
+ var MAX_NESTED_LEAVES = 20;
970
+ var MAX_UNKNOWN_FROM_LEAVES = 5;
971
+ function flattenNestedChanges(changes) {
972
+ if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
973
+ const out = [];
974
+ for (const c of changes) {
975
+ if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
976
+ out.push(c);
977
+ continue;
978
+ }
979
+ if (!isMeaningfulAttribute(c.attribute)) continue;
980
+ const leaves = [];
981
+ collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
982
+ const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
983
+ out.push(...leaves.slice(0, cap).map(scrubLeaf));
984
+ }
985
+ return out;
986
+ }
987
+ function collectLeafChanges(path, from, to, depth, out) {
988
+ if (out.length > MAX_NESTED_LEAVES) return;
989
+ const fromObj = isPlainRecord(from);
990
+ const toObj = isPlainRecord(to);
991
+ if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
992
+ const f = fromObj ? from : {};
993
+ const t = toObj ? to : {};
994
+ for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
995
+ const before = from === void 0 ? void 0 : f[key] ?? null;
996
+ collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
997
+ }
998
+ return;
999
+ }
1000
+ if (from !== void 0 && sameJson(from, to)) return;
1001
+ out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
1002
+ }
1003
+ function scrubLeaf(change) {
1004
+ const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
1005
+ const out = { attribute: change.attribute, to: scrub(change.to) };
1006
+ if (change.from !== void 0) out.from = scrub(change.from);
1007
+ return out;
1008
+ }
1009
+ function isPlainRecord(value) {
1010
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
1011
+ const proto = Object.getPrototypeOf(value);
1012
+ return proto === Object.prototype || proto === null;
1013
+ }
1014
+ function sameJson(a, b) {
1015
+ if (a === b) return true;
1016
+ try {
1017
+ return JSON.stringify(a) === JSON.stringify(b);
1018
+ } catch {
1019
+ return false;
1020
+ }
1021
+ }
936
1022
  function isMeaningfulAttribute(name) {
937
1023
  if (IGNORED_ATTRIBUTES.includes(name)) return false;
938
1024
  if (isDbSensitive(name)) return false;
@@ -970,22 +1056,44 @@ function isPrismaDecimal(value) {
970
1056
  const obj = value;
971
1057
  return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
972
1058
  }
1059
+ var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
1060
+ var MAX_DISPLAY_NAME_LENGTH = 120;
973
1061
  function resolveDisplayName(modelClass, instance) {
974
1062
  try {
975
1063
  const config = configuration();
976
1064
  const resolver = config.displayNameFor[modelClass];
977
1065
  const resolved = applyResolver(resolver, instance);
978
1066
  if (resolved !== null) return resolved;
979
- for (const field of ["name", "title", "number"]) {
980
- const value = instance[field];
981
- if (value !== null && value !== void 0 && String(value).length > 0) {
982
- return String(value);
1067
+ const columns = /* @__PURE__ */ new Map();
1068
+ for (const key of Object.keys(instance)) {
1069
+ if (isDbSensitive(key)) continue;
1070
+ const norm = key.toLowerCase().replace(/_/g, "");
1071
+ if (!columns.has(norm)) columns.set(norm, instance[key]);
1072
+ }
1073
+ const text = (norm) => {
1074
+ const value = columns.get(norm);
1075
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
1076
+ return null;
983
1077
  }
1078
+ const str = String(value).trim();
1079
+ return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
1080
+ };
1081
+ for (const column of DISPLAY_NAME_COLUMNS) {
1082
+ const value = text(column);
1083
+ if (value !== null) return value;
984
1084
  }
1085
+ return text(`${singularNorm(modelClass)}number`);
985
1086
  } catch {
986
1087
  }
987
1088
  return null;
988
1089
  }
1090
+ function singularNorm(modelClass) {
1091
+ const norm = modelClass.toLowerCase().replace(/_/g, "");
1092
+ if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
1093
+ if (/(ss|us|is)$/.test(norm)) return norm;
1094
+ if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
1095
+ return norm.endsWith("s") ? norm.slice(0, -1) : norm;
1096
+ }
989
1097
  function applyResolver(resolver, instance) {
990
1098
  if (typeof resolver === "string") {
991
1099
  const value = instance[resolver];
@@ -1031,15 +1139,17 @@ var lastSweepAt = 0;
1031
1139
  var lastPruneAt = 0;
1032
1140
  var deleteDenied = false;
1033
1141
  var txCorrelation = /* @__PURE__ */ new Map();
1034
- async function readAuditRows(exec, correlationId) {
1142
+ async function readAuditRows(exec, correlationId, since = null) {
1143
+ const where = since ? "correlation_id = $1 AND captured_at >= $2" : "correlation_id = $1";
1144
+ const params = since ? [correlationId, since.toISOString()] : [correlationId];
1035
1145
  try {
1036
1146
  if (!deleteDenied) {
1037
1147
  try {
1038
1148
  return sortById(
1039
1149
  extractRows(
1040
1150
  await exec(
1041
- `DELETE FROM ezlogs_audit_log WHERE correlation_id = $1 RETURNING ${COLUMNS}`,
1042
- [correlationId]
1151
+ `DELETE FROM ezlogs_audit_log WHERE ${where} RETURNING ${COLUMNS}`,
1152
+ params
1043
1153
  )
1044
1154
  ).map(normalizeAuditRow)
1045
1155
  );
@@ -1052,8 +1162,8 @@ async function readAuditRows(exec, correlationId) {
1052
1162
  }
1053
1163
  }
1054
1164
  const result = await exec(
1055
- `SELECT ${COLUMNS} FROM ezlogs_audit_log WHERE correlation_id = $1 ORDER BY id ASC`,
1056
- [correlationId]
1165
+ `SELECT ${COLUMNS} FROM ezlogs_audit_log WHERE ${where} ORDER BY id ASC`,
1166
+ params
1057
1167
  );
1058
1168
  return extractRows(result).map(normalizeAuditRow);
1059
1169
  } catch (err) {
@@ -1085,6 +1195,7 @@ async function sweepAuditLog(exec) {
1085
1195
  )
1086
1196
  ).map(normalizeAuditRow)
1087
1197
  );
1198
+ await loadTableMeta(exec, rows);
1088
1199
  for (const row of rows) {
1089
1200
  const correlationId = row.correlation_id ?? correlationForTx(row.txid);
1090
1201
  runWithCorrelation(
@@ -1147,11 +1258,12 @@ function emitAuditRows(rows) {
1147
1258
  emitOne(row);
1148
1259
  }
1149
1260
  }
1150
- async function flushAuditLog(exec) {
1261
+ async function flushAuditLog(exec, options = {}) {
1151
1262
  if (!exec) return;
1152
1263
  const correlationId = current();
1153
1264
  if (!correlationId) return;
1154
- const rows = await readAuditRows(exec, correlationId);
1265
+ const rows = await readAuditRows(exec, correlationId, options.since ?? null);
1266
+ await loadTableMeta(exec, rows);
1155
1267
  emitFresh(rows);
1156
1268
  const now = Date.now();
1157
1269
  if (now - lastPruneAt >= PRUNE_INTERVAL_MS) {
@@ -1163,6 +1275,79 @@ async function flushAuditLog(exec) {
1163
1275
  await sweepAuditLog(exec);
1164
1276
  }
1165
1277
  }
1278
+ var tableMeta = /* @__PURE__ */ new Map();
1279
+ var META_RETRY_MS = 6e4;
1280
+ var metaRetryAt = 0;
1281
+ async function loadTableMeta(exec, rows) {
1282
+ if (Date.now() < metaRetryAt) return;
1283
+ const tables = [
1284
+ ...new Set(rows.map((r) => r.table_name).filter((t) => t !== "" && !t.includes(",") && !tableMeta.has(t)))
1285
+ ];
1286
+ if (tables.length === 0) return;
1287
+ const list = tables.join(",");
1288
+ try {
1289
+ const fkRows = extractRows(
1290
+ await exec(
1291
+ `SELECT cl.relname AS table_name, a.attname AS column_name, rf.relname AS ref_table
1292
+ FROM pg_constraint c
1293
+ JOIN pg_class cl ON cl.oid = c.conrelid
1294
+ JOIN pg_namespace n ON n.oid = cl.relnamespace
1295
+ JOIN pg_class rf ON rf.oid = c.confrelid
1296
+ JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = c.conkey[1]
1297
+ WHERE c.contype = 'f'
1298
+ AND n.nspname = 'public'
1299
+ AND array_length(c.conkey, 1) = 1
1300
+ AND cl.relname = ANY(string_to_array($1, ','))
1301
+ ORDER BY cl.relname, a.attnum`,
1302
+ [list]
1303
+ )
1304
+ );
1305
+ const tsRows = extractRows(
1306
+ await exec(
1307
+ `SELECT cl.relname AS table_name, a.attname AS column_name
1308
+ FROM pg_attribute a
1309
+ JOIN pg_class cl ON cl.oid = a.attrelid
1310
+ JOIN pg_namespace n ON n.oid = cl.relnamespace
1311
+ WHERE n.nspname = 'public'
1312
+ AND cl.relname = ANY(string_to_array($1, ','))
1313
+ AND a.attnum > 0
1314
+ AND NOT a.attisdropped
1315
+ AND a.atttypid = 'timestamp'::regtype`,
1316
+ [list]
1317
+ )
1318
+ );
1319
+ const fks = /* @__PURE__ */ new Map();
1320
+ for (const raw of fkRows) {
1321
+ const r = raw ?? {};
1322
+ if (typeof r.table_name !== "string" || typeof r.column_name !== "string" || typeof r.ref_table !== "string") {
1323
+ continue;
1324
+ }
1325
+ fks.set(r.table_name, [...fks.get(r.table_name) ?? [], { column: r.column_name, refTable: r.ref_table }]);
1326
+ }
1327
+ const timestamps = /* @__PURE__ */ new Map();
1328
+ for (const raw of tsRows) {
1329
+ const r = raw ?? {};
1330
+ if (typeof r.table_name !== "string" || typeof r.column_name !== "string") continue;
1331
+ timestamps.set(r.table_name, (timestamps.get(r.table_name) ?? /* @__PURE__ */ new Set()).add(r.column_name));
1332
+ }
1333
+ for (const table of tables) {
1334
+ tableMeta.set(table, { fks: fks.get(table) ?? [], naiveTimestamps: timestamps.get(table) ?? /* @__PURE__ */ new Set() });
1335
+ }
1336
+ } catch (err) {
1337
+ metaRetryAt = Date.now() + META_RETRY_MS;
1338
+ logger.debug(`table metadata lookup failed: ${err instanceof Error ? err.message : String(err)}`);
1339
+ }
1340
+ }
1341
+ function relatedResourceIds(table, row) {
1342
+ const out = [];
1343
+ for (const fk of tableMeta.get(table)?.fks ?? []) {
1344
+ const value = row[fk.column];
1345
+ if (typeof value === "string" || typeof value === "number") {
1346
+ out.push({ resource_type: fk.refTable, resource_id: String(value) });
1347
+ }
1348
+ }
1349
+ return out;
1350
+ }
1166
1351
  function emitFresh(rows) {
1167
1352
  if (rows.length === 0) return;
1168
1353
  const emitted = currentEmittedAuditIds();
@@ -1199,8 +1384,10 @@ function sortById(rows) {
1199
1384
  function emitOne(row) {
1200
1385
  const op = mapOp(row.op);
1201
1386
  if (!op) return;
1202
- const oldRow = coerceJsonb(row.old_row);
1203
- const newRow = coerceJsonb(row.new_row);
1387
+ const naive = tableMeta.get(row.table_name)?.naiveTimestamps ?? null;
1388
+ const oldRow = withUtcTimestamps(coerceJsonb(row.old_row), naive);
1389
+ const newRow = withUtcTimestamps(coerceJsonb(row.new_row), naive);
1390
+ const related = row.resource_id === null ? relatedResourceIds(row.table_name, newRow ?? oldRow ?? {}) : null;
1204
1391
  if (op === "create" && newRow) {
1205
1392
  emitDbEvent({
1206
1393
  modelClass: row.table_name,
@@ -1210,6 +1397,7 @@ function emitOne(row) {
1210
1397
  modelInstance: newRow,
1211
1398
  displayName: null,
1212
1399
  timestamp: row.captured_at ?? null,
1400
+ relatedResourceIds: related,
1213
1401
  _fromTrigger: true
1214
1402
  });
1215
1403
  return;
@@ -1229,6 +1417,7 @@ function emitOne(row) {
1229
1417
  modelInstance: newRow,
1230
1418
  displayName: null,
1231
1419
  timestamp: row.captured_at ?? null,
1420
+ relatedResourceIds: related,
1232
1421
  _fromTrigger: true
1233
1422
  });
1234
1423
  return;
@@ -1242,11 +1431,22 @@ function emitOne(row) {
1242
1431
  modelInstance: oldRow,
1243
1432
  displayName: null,
1244
1433
  timestamp: row.captured_at ?? null,
1434
+ relatedResourceIds: related,
1245
1435
  _fromTrigger: true
1246
1436
  });
1247
1437
  return;
1248
1438
  }
1249
1439
  }
1440
+ var NAIVE_TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?$/;
1441
+ function withUtcTimestamps(row, naiveColumns) {
1442
+ if (!row) return row;
1443
+ const out = {};
1444
+ for (const [key, value] of Object.entries(row)) {
1445
+ const isNaiveColumn = naiveColumns ? naiveColumns.has(key) : /(_at|At)$/.test(key);
1446
+ out[key] = isNaiveColumn && typeof value === "string" && NAIVE_TIMESTAMP.test(value) ? `${value}Z` : value;
1447
+ }
1448
+ return out;
1449
+ }
1250
1450
  function mapOp(op) {
1251
1451
  if (op === "INSERT") return "create";
1252
1452
  if (op === "UPDATE") return "update";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ezlogs-nextjs",
3
- "version": "0.2.0",
3
+ "version": "0.3.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",
@@ -24,6 +24,7 @@
24
24
  "README.md"
25
25
  ],
26
26
  "bin": {
27
+ "ezlogs-nextjs": "./bin/install-triggers.cjs",
27
28
  "install-triggers": "./bin/install-triggers.cjs"
28
29
  },
29
30
  "exports": {
@@ -141,6 +142,7 @@
141
142
  "@prisma/client": "^5.22.0",
142
143
  "@types/node": "^20.11.0",
143
144
  "@types/pg": "^8.20.0",
145
+ "bullmq": "^5.76.10",
144
146
  "pg": "^8.20.0",
145
147
  "prisma": "^5.22.0",
146
148
  "sucrase": "^3.35.0",