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.
@@ -509,6 +509,37 @@ function matchesSensitiveKey(key, substringPatterns) {
509
509
  const words = snake.split(/[^a-z0-9]+/);
510
510
  return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
511
511
  }
512
+ function passesLuhn(digits) {
513
+ let sum = 0;
514
+ for (let i = 0; i < digits.length; i += 1) {
515
+ let d = Number(digits[digits.length - 1 - i]);
516
+ if (i % 2 === 1) {
517
+ d *= 2;
518
+ if (d > 9) d -= 9;
519
+ }
520
+ sum += d;
521
+ }
522
+ return sum % 10 === 0;
523
+ }
524
+ var SECRET_TEXT_PATTERNS = [
525
+ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
526
+ /\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
527
+ /\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
528
+ /\bsk-[A-Za-z0-9_-]{16,}/g,
529
+ /\bgh[pousr]_[A-Za-z0-9]{20,}/g,
530
+ /\bxox[abposr]-[A-Za-z0-9-]+/g,
531
+ /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
532
+ ];
533
+ var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
534
+ function scrubSecretsInText(text, options = {}) {
535
+ let out = text;
536
+ for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
537
+ out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
538
+ const digits = match.replace(/[ -]/g, "");
539
+ return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
540
+ });
541
+ return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
542
+ }
512
543
 
513
544
  // src/db/shared.ts
514
545
  var IGNORED_ATTRIBUTES = Object.freeze([
@@ -517,8 +548,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
517
548
  "created_at",
518
549
  "updatedAt",
519
550
  "updated_at",
520
- "deletedAt",
521
- "deleted_at",
522
551
  // Auth.js / NextAuth standard columns
523
552
  "emailVerified",
524
553
  "expires"
@@ -562,14 +591,17 @@ function emitDbEvent(input) {
562
591
  if (config.allExcludedTables().includes(input.modelClass)) return;
563
592
  if (config.databaseReader && !input._fromTrigger) {
564
593
  const tracked = config.triggerTrackedTables;
565
- if (tracked === null || tracked.has(input.modelClass)) return;
594
+ if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
566
595
  }
567
596
  if (input.operation === "update") {
568
597
  const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
569
598
  if (meaningful.length === 0) return;
570
599
  }
571
600
  const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
572
- const context = buildContext(input);
601
+ const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
602
+ const context = buildContext(
603
+ related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
604
+ );
573
605
  const event = build({
574
606
  source_type: "database_callback",
575
607
  source_data: {
@@ -578,7 +610,7 @@ function emitDbEvent(input) {
578
610
  },
579
611
  outcome: "success",
580
612
  correlation_id: current(),
581
- resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
613
+ resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
582
614
  context,
583
615
  duration_ms: null,
584
616
  timestamp: input.timestamp ?? null
@@ -625,7 +657,7 @@ function filterMeaningfulAttributes(attrs) {
625
657
  return Object.keys(out).length === 0 ? null : out;
626
658
  }
627
659
  function filterMeaningfulChanges(changes) {
628
- return changes.filter((c) => {
660
+ return flattenNestedChanges(changes).filter((c) => {
629
661
  if (!isMeaningfulAttribute(c.attribute)) return false;
630
662
  if (!isScalar(c.to)) return false;
631
663
  if (c.from === void 0) return true;
@@ -633,6 +665,60 @@ function filterMeaningfulChanges(changes) {
633
665
  return valuesActuallyChanged(c.from, c.to);
634
666
  });
635
667
  }
668
+ var MAX_NESTED_DEPTH = 3;
669
+ var MAX_NESTED_LEAVES = 20;
670
+ var MAX_UNKNOWN_FROM_LEAVES = 5;
671
+ function flattenNestedChanges(changes) {
672
+ if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
673
+ const out = [];
674
+ for (const c of changes) {
675
+ if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
676
+ out.push(c);
677
+ continue;
678
+ }
679
+ if (!isMeaningfulAttribute(c.attribute)) continue;
680
+ const leaves = [];
681
+ collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
682
+ const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
683
+ out.push(...leaves.slice(0, cap).map(scrubLeaf));
684
+ }
685
+ return out;
686
+ }
687
+ function collectLeafChanges(path, from, to, depth, out) {
688
+ if (out.length > MAX_NESTED_LEAVES) return;
689
+ const fromObj = isPlainRecord(from);
690
+ const toObj = isPlainRecord(to);
691
+ if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
692
+ const f = fromObj ? from : {};
693
+ const t = toObj ? to : {};
694
+ for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
695
+ const before = from === void 0 ? void 0 : f[key] ?? null;
696
+ collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
697
+ }
698
+ return;
699
+ }
700
+ if (from !== void 0 && sameJson(from, to)) return;
701
+ out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
702
+ }
703
+ function scrubLeaf(change) {
704
+ const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
705
+ const out = { attribute: change.attribute, to: scrub(change.to) };
706
+ if (change.from !== void 0) out.from = scrub(change.from);
707
+ return out;
708
+ }
709
+ function isPlainRecord(value) {
710
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
711
+ const proto = Object.getPrototypeOf(value);
712
+ return proto === Object.prototype || proto === null;
713
+ }
714
+ function sameJson(a, b) {
715
+ if (a === b) return true;
716
+ try {
717
+ return JSON.stringify(a) === JSON.stringify(b);
718
+ } catch {
719
+ return false;
720
+ }
721
+ }
636
722
  function isMeaningfulAttribute(name) {
637
723
  if (IGNORED_ATTRIBUTES.includes(name)) return false;
638
724
  if (isDbSensitive(name)) return false;
@@ -670,22 +756,44 @@ function isPrismaDecimal(value) {
670
756
  const obj = value;
671
757
  return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
672
758
  }
759
+ var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
760
+ var MAX_DISPLAY_NAME_LENGTH = 120;
673
761
  function resolveDisplayName(modelClass, instance) {
674
762
  try {
675
763
  const config = configuration();
676
764
  const resolver = config.displayNameFor[modelClass];
677
765
  const resolved = applyResolver(resolver, instance);
678
766
  if (resolved !== null) return resolved;
679
- for (const field of ["name", "title", "number"]) {
680
- const value = instance[field];
681
- if (value !== null && value !== void 0 && String(value).length > 0) {
682
- return String(value);
767
+ const columns = /* @__PURE__ */ new Map();
768
+ for (const key of Object.keys(instance)) {
769
+ if (isDbSensitive(key)) continue;
770
+ const norm = key.toLowerCase().replace(/_/g, "");
771
+ if (!columns.has(norm)) columns.set(norm, instance[key]);
772
+ }
773
+ const text = (norm) => {
774
+ const value = columns.get(norm);
775
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
776
+ return null;
683
777
  }
778
+ const str = String(value).trim();
779
+ return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
780
+ };
781
+ for (const column of DISPLAY_NAME_COLUMNS) {
782
+ const value = text(column);
783
+ if (value !== null) return value;
684
784
  }
785
+ return text(`${singularNorm(modelClass)}number`);
685
786
  } catch {
686
787
  }
687
788
  return null;
688
789
  }
790
+ function singularNorm(modelClass) {
791
+ const norm = modelClass.toLowerCase().replace(/_/g, "");
792
+ if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
793
+ if (/(ss|us|is)$/.test(norm)) return norm;
794
+ if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
795
+ return norm.endsWith("s") ? norm.slice(0, -1) : norm;
796
+ }
689
797
  function applyResolver(resolver, instance) {
690
798
  if (typeof resolver === "string") {
691
799
  const value = instance[resolver];
@@ -507,6 +507,37 @@ function matchesSensitiveKey(key, substringPatterns) {
507
507
  const words = snake.split(/[^a-z0-9]+/);
508
508
  return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
509
509
  }
510
+ function passesLuhn(digits) {
511
+ let sum = 0;
512
+ for (let i = 0; i < digits.length; i += 1) {
513
+ let d = Number(digits[digits.length - 1 - i]);
514
+ if (i % 2 === 1) {
515
+ d *= 2;
516
+ if (d > 9) d -= 9;
517
+ }
518
+ sum += d;
519
+ }
520
+ return sum % 10 === 0;
521
+ }
522
+ var SECRET_TEXT_PATTERNS = [
523
+ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
524
+ /\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
525
+ /\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
526
+ /\bsk-[A-Za-z0-9_-]{16,}/g,
527
+ /\bgh[pousr]_[A-Za-z0-9]{20,}/g,
528
+ /\bxox[abposr]-[A-Za-z0-9-]+/g,
529
+ /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
530
+ ];
531
+ var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
532
+ function scrubSecretsInText(text, options = {}) {
533
+ let out = text;
534
+ for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
535
+ out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
536
+ const digits = match.replace(/[ -]/g, "");
537
+ return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
538
+ });
539
+ return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
540
+ }
510
541
 
511
542
  // src/db/shared.ts
512
543
  var IGNORED_ATTRIBUTES = Object.freeze([
@@ -515,8 +546,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
515
546
  "created_at",
516
547
  "updatedAt",
517
548
  "updated_at",
518
- "deletedAt",
519
- "deleted_at",
520
549
  // Auth.js / NextAuth standard columns
521
550
  "emailVerified",
522
551
  "expires"
@@ -560,14 +589,17 @@ function emitDbEvent(input) {
560
589
  if (config.allExcludedTables().includes(input.modelClass)) return;
561
590
  if (config.databaseReader && !input._fromTrigger) {
562
591
  const tracked = config.triggerTrackedTables;
563
- if (tracked === null || tracked.has(input.modelClass)) return;
592
+ if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
564
593
  }
565
594
  if (input.operation === "update") {
566
595
  const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
567
596
  if (meaningful.length === 0) return;
568
597
  }
569
598
  const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
570
- const context = buildContext(input);
599
+ const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
600
+ const context = buildContext(
601
+ related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
602
+ );
571
603
  const event = build({
572
604
  source_type: "database_callback",
573
605
  source_data: {
@@ -576,7 +608,7 @@ function emitDbEvent(input) {
576
608
  },
577
609
  outcome: "success",
578
610
  correlation_id: current(),
579
- resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
611
+ resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
580
612
  context,
581
613
  duration_ms: null,
582
614
  timestamp: input.timestamp ?? null
@@ -623,7 +655,7 @@ function filterMeaningfulAttributes(attrs) {
623
655
  return Object.keys(out).length === 0 ? null : out;
624
656
  }
625
657
  function filterMeaningfulChanges(changes) {
626
- return changes.filter((c) => {
658
+ return flattenNestedChanges(changes).filter((c) => {
627
659
  if (!isMeaningfulAttribute(c.attribute)) return false;
628
660
  if (!isScalar(c.to)) return false;
629
661
  if (c.from === void 0) return true;
@@ -631,6 +663,60 @@ function filterMeaningfulChanges(changes) {
631
663
  return valuesActuallyChanged(c.from, c.to);
632
664
  });
633
665
  }
666
+ var MAX_NESTED_DEPTH = 3;
667
+ var MAX_NESTED_LEAVES = 20;
668
+ var MAX_UNKNOWN_FROM_LEAVES = 5;
669
+ function flattenNestedChanges(changes) {
670
+ if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
671
+ const out = [];
672
+ for (const c of changes) {
673
+ if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
674
+ out.push(c);
675
+ continue;
676
+ }
677
+ if (!isMeaningfulAttribute(c.attribute)) continue;
678
+ const leaves = [];
679
+ collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
680
+ const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
681
+ out.push(...leaves.slice(0, cap).map(scrubLeaf));
682
+ }
683
+ return out;
684
+ }
685
+ function collectLeafChanges(path, from, to, depth, out) {
686
+ if (out.length > MAX_NESTED_LEAVES) return;
687
+ const fromObj = isPlainRecord(from);
688
+ const toObj = isPlainRecord(to);
689
+ if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
690
+ const f = fromObj ? from : {};
691
+ const t = toObj ? to : {};
692
+ for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
693
+ const before = from === void 0 ? void 0 : f[key] ?? null;
694
+ collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
695
+ }
696
+ return;
697
+ }
698
+ if (from !== void 0 && sameJson(from, to)) return;
699
+ out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
700
+ }
701
+ function scrubLeaf(change) {
702
+ const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
703
+ const out = { attribute: change.attribute, to: scrub(change.to) };
704
+ if (change.from !== void 0) out.from = scrub(change.from);
705
+ return out;
706
+ }
707
+ function isPlainRecord(value) {
708
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
709
+ const proto = Object.getPrototypeOf(value);
710
+ return proto === Object.prototype || proto === null;
711
+ }
712
+ function sameJson(a, b) {
713
+ if (a === b) return true;
714
+ try {
715
+ return JSON.stringify(a) === JSON.stringify(b);
716
+ } catch {
717
+ return false;
718
+ }
719
+ }
634
720
  function isMeaningfulAttribute(name) {
635
721
  if (IGNORED_ATTRIBUTES.includes(name)) return false;
636
722
  if (isDbSensitive(name)) return false;
@@ -668,22 +754,44 @@ function isPrismaDecimal(value) {
668
754
  const obj = value;
669
755
  return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
670
756
  }
757
+ var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
758
+ var MAX_DISPLAY_NAME_LENGTH = 120;
671
759
  function resolveDisplayName(modelClass, instance) {
672
760
  try {
673
761
  const config = configuration();
674
762
  const resolver = config.displayNameFor[modelClass];
675
763
  const resolved = applyResolver(resolver, instance);
676
764
  if (resolved !== null) return resolved;
677
- for (const field of ["name", "title", "number"]) {
678
- const value = instance[field];
679
- if (value !== null && value !== void 0 && String(value).length > 0) {
680
- return String(value);
765
+ const columns = /* @__PURE__ */ new Map();
766
+ for (const key of Object.keys(instance)) {
767
+ if (isDbSensitive(key)) continue;
768
+ const norm = key.toLowerCase().replace(/_/g, "");
769
+ if (!columns.has(norm)) columns.set(norm, instance[key]);
770
+ }
771
+ const text = (norm) => {
772
+ const value = columns.get(norm);
773
+ if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
774
+ return null;
681
775
  }
776
+ const str = String(value).trim();
777
+ return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
778
+ };
779
+ for (const column of DISPLAY_NAME_COLUMNS) {
780
+ const value = text(column);
781
+ if (value !== null) return value;
682
782
  }
783
+ return text(`${singularNorm(modelClass)}number`);
683
784
  } catch {
684
785
  }
685
786
  return null;
686
787
  }
788
+ function singularNorm(modelClass) {
789
+ const norm = modelClass.toLowerCase().replace(/_/g, "");
790
+ if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
791
+ if (/(ss|us|is)$/.test(norm)) return norm;
792
+ if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
793
+ return norm.endsWith("s") ? norm.slice(0, -1) : norm;
794
+ }
687
795
  function applyResolver(resolver, instance) {
688
796
  if (typeof resolver === "string") {
689
797
  const value = instance[resolver];