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.
- package/README.md +40 -16
- package/bin/install-triggers.cjs +45 -11
- package/dist/db/drizzle.cjs +118 -10
- package/dist/db/drizzle.js +118 -10
- package/dist/db/prisma.cjs +143 -29
- package/dist/db/prisma.d.cts +3 -1
- package/dist/db/prisma.d.ts +3 -1
- package/dist/db/prisma.js +143 -29
- package/dist/db/supabase.cjs +118 -10
- package/dist/db/supabase.js +118 -10
- package/dist/index.cjs +295 -42
- package/dist/index.js +295 -42
- package/dist/jobs/bullmq.cjs +973 -12
- package/dist/jobs/bullmq.d.cts +8 -3
- package/dist/jobs/bullmq.d.ts +8 -3
- package/dist/jobs/bullmq.js +973 -12
- package/dist/jobs/inngest.cjs +880 -10
- package/dist/jobs/inngest.d.cts +7 -5
- package/dist/jobs/inngest.d.ts +7 -5
- package/dist/jobs/inngest.js +880 -10
- package/dist/jobs/supabase-queues.cjs +878 -12
- package/dist/jobs/supabase-queues.js +878 -12
- package/dist/jobs/trigger.cjs +914 -38
- package/dist/jobs/trigger.js +914 -38
- package/dist/jobs/upstash-workflow.cjs +914 -10
- package/dist/jobs/upstash-workflow.d.cts +16 -2
- package/dist/jobs/upstash-workflow.d.ts +16 -2
- package/dist/jobs/upstash-workflow.js +914 -11
- package/dist/triggers/index.cjs +219 -19
- package/dist/triggers/index.d.cts +7 -2
- package/dist/triggers/index.d.ts +7 -2
- package/dist/triggers/index.js +219 -19
- package/package.json +3 -1
package/dist/db/prisma.cjs
CHANGED
|
@@ -519,6 +519,37 @@ function matchesSensitiveKey(key, substringPatterns) {
|
|
|
519
519
|
const words = snake.split(/[^a-z0-9]+/);
|
|
520
520
|
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
521
521
|
}
|
|
522
|
+
function passesLuhn(digits) {
|
|
523
|
+
let sum = 0;
|
|
524
|
+
for (let i = 0; i < digits.length; i += 1) {
|
|
525
|
+
let d = Number(digits[digits.length - 1 - i]);
|
|
526
|
+
if (i % 2 === 1) {
|
|
527
|
+
d *= 2;
|
|
528
|
+
if (d > 9) d -= 9;
|
|
529
|
+
}
|
|
530
|
+
sum += d;
|
|
531
|
+
}
|
|
532
|
+
return sum % 10 === 0;
|
|
533
|
+
}
|
|
534
|
+
var SECRET_TEXT_PATTERNS = [
|
|
535
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
|
|
536
|
+
/\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
|
|
537
|
+
/\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
|
|
538
|
+
/\bsk-[A-Za-z0-9_-]{16,}/g,
|
|
539
|
+
/\bgh[pousr]_[A-Za-z0-9]{20,}/g,
|
|
540
|
+
/\bxox[abposr]-[A-Za-z0-9-]+/g,
|
|
541
|
+
/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
|
|
542
|
+
];
|
|
543
|
+
var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
|
|
544
|
+
function scrubSecretsInText(text, options = {}) {
|
|
545
|
+
let out = text;
|
|
546
|
+
for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
|
|
547
|
+
out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
|
|
548
|
+
const digits = match.replace(/[ -]/g, "");
|
|
549
|
+
return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
|
|
550
|
+
});
|
|
551
|
+
return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
|
|
552
|
+
}
|
|
522
553
|
|
|
523
554
|
// src/db/shared.ts
|
|
524
555
|
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
@@ -527,8 +558,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
|
|
|
527
558
|
"created_at",
|
|
528
559
|
"updatedAt",
|
|
529
560
|
"updated_at",
|
|
530
|
-
"deletedAt",
|
|
531
|
-
"deleted_at",
|
|
532
561
|
// Auth.js / NextAuth standard columns
|
|
533
562
|
"emailVerified",
|
|
534
563
|
"expires"
|
|
@@ -572,14 +601,17 @@ function emitDbEvent(input) {
|
|
|
572
601
|
if (config.allExcludedTables().includes(input.modelClass)) return;
|
|
573
602
|
if (config.databaseReader && !input._fromTrigger) {
|
|
574
603
|
const tracked = config.triggerTrackedTables;
|
|
575
|
-
if (tracked === null || tracked.has(input.modelClass)) return;
|
|
604
|
+
if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
|
|
576
605
|
}
|
|
577
606
|
if (input.operation === "update") {
|
|
578
607
|
const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
|
|
579
608
|
if (meaningful.length === 0) return;
|
|
580
609
|
}
|
|
581
610
|
const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
|
|
582
|
-
const
|
|
611
|
+
const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
|
|
612
|
+
const context = buildContext(
|
|
613
|
+
related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
|
|
614
|
+
);
|
|
583
615
|
const event = build({
|
|
584
616
|
source_type: "database_callback",
|
|
585
617
|
source_data: {
|
|
@@ -588,7 +620,7 @@ function emitDbEvent(input) {
|
|
|
588
620
|
},
|
|
589
621
|
outcome: "success",
|
|
590
622
|
correlation_id: current(),
|
|
591
|
-
resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
623
|
+
resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
592
624
|
context,
|
|
593
625
|
duration_ms: null,
|
|
594
626
|
timestamp: input.timestamp ?? null
|
|
@@ -635,7 +667,7 @@ function filterMeaningfulAttributes(attrs) {
|
|
|
635
667
|
return Object.keys(out).length === 0 ? null : out;
|
|
636
668
|
}
|
|
637
669
|
function filterMeaningfulChanges(changes) {
|
|
638
|
-
return changes.filter((c) => {
|
|
670
|
+
return flattenNestedChanges(changes).filter((c) => {
|
|
639
671
|
if (!isMeaningfulAttribute(c.attribute)) return false;
|
|
640
672
|
if (!isScalar(c.to)) return false;
|
|
641
673
|
if (c.from === void 0) return true;
|
|
@@ -643,6 +675,60 @@ function filterMeaningfulChanges(changes) {
|
|
|
643
675
|
return valuesActuallyChanged(c.from, c.to);
|
|
644
676
|
});
|
|
645
677
|
}
|
|
678
|
+
var MAX_NESTED_DEPTH = 3;
|
|
679
|
+
var MAX_NESTED_LEAVES = 20;
|
|
680
|
+
var MAX_UNKNOWN_FROM_LEAVES = 5;
|
|
681
|
+
function flattenNestedChanges(changes) {
|
|
682
|
+
if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
|
|
683
|
+
const out = [];
|
|
684
|
+
for (const c of changes) {
|
|
685
|
+
if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
|
|
686
|
+
out.push(c);
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
if (!isMeaningfulAttribute(c.attribute)) continue;
|
|
690
|
+
const leaves = [];
|
|
691
|
+
collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
|
|
692
|
+
const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
|
|
693
|
+
out.push(...leaves.slice(0, cap).map(scrubLeaf));
|
|
694
|
+
}
|
|
695
|
+
return out;
|
|
696
|
+
}
|
|
697
|
+
function collectLeafChanges(path, from, to, depth, out) {
|
|
698
|
+
if (out.length > MAX_NESTED_LEAVES) return;
|
|
699
|
+
const fromObj = isPlainRecord(from);
|
|
700
|
+
const toObj = isPlainRecord(to);
|
|
701
|
+
if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
|
|
702
|
+
const f = fromObj ? from : {};
|
|
703
|
+
const t = toObj ? to : {};
|
|
704
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
|
|
705
|
+
const before = from === void 0 ? void 0 : f[key] ?? null;
|
|
706
|
+
collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
|
|
707
|
+
}
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
if (from !== void 0 && sameJson(from, to)) return;
|
|
711
|
+
out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
|
|
712
|
+
}
|
|
713
|
+
function scrubLeaf(change) {
|
|
714
|
+
const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
|
|
715
|
+
const out = { attribute: change.attribute, to: scrub(change.to) };
|
|
716
|
+
if (change.from !== void 0) out.from = scrub(change.from);
|
|
717
|
+
return out;
|
|
718
|
+
}
|
|
719
|
+
function isPlainRecord(value) {
|
|
720
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
721
|
+
const proto = Object.getPrototypeOf(value);
|
|
722
|
+
return proto === Object.prototype || proto === null;
|
|
723
|
+
}
|
|
724
|
+
function sameJson(a, b) {
|
|
725
|
+
if (a === b) return true;
|
|
726
|
+
try {
|
|
727
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
728
|
+
} catch {
|
|
729
|
+
return false;
|
|
730
|
+
}
|
|
731
|
+
}
|
|
646
732
|
function isMeaningfulAttribute(name) {
|
|
647
733
|
if (IGNORED_ATTRIBUTES.includes(name)) return false;
|
|
648
734
|
if (isDbSensitive(name)) return false;
|
|
@@ -680,22 +766,44 @@ function isPrismaDecimal(value) {
|
|
|
680
766
|
const obj = value;
|
|
681
767
|
return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
|
|
682
768
|
}
|
|
769
|
+
var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
|
|
770
|
+
var MAX_DISPLAY_NAME_LENGTH = 120;
|
|
683
771
|
function resolveDisplayName(modelClass, instance) {
|
|
684
772
|
try {
|
|
685
773
|
const config = configuration();
|
|
686
774
|
const resolver = config.displayNameFor[modelClass];
|
|
687
775
|
const resolved = applyResolver(resolver, instance);
|
|
688
776
|
if (resolved !== null) return resolved;
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
if (
|
|
692
|
-
|
|
777
|
+
const columns = /* @__PURE__ */ new Map();
|
|
778
|
+
for (const key of Object.keys(instance)) {
|
|
779
|
+
if (isDbSensitive(key)) continue;
|
|
780
|
+
const norm = key.toLowerCase().replace(/_/g, "");
|
|
781
|
+
if (!columns.has(norm)) columns.set(norm, instance[key]);
|
|
782
|
+
}
|
|
783
|
+
const text = (norm) => {
|
|
784
|
+
const value = columns.get(norm);
|
|
785
|
+
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
|
|
786
|
+
return null;
|
|
693
787
|
}
|
|
788
|
+
const str = String(value).trim();
|
|
789
|
+
return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
|
|
790
|
+
};
|
|
791
|
+
for (const column of DISPLAY_NAME_COLUMNS) {
|
|
792
|
+
const value = text(column);
|
|
793
|
+
if (value !== null) return value;
|
|
694
794
|
}
|
|
795
|
+
return text(`${singularNorm(modelClass)}number`);
|
|
695
796
|
} catch {
|
|
696
797
|
}
|
|
697
798
|
return null;
|
|
698
799
|
}
|
|
800
|
+
function singularNorm(modelClass) {
|
|
801
|
+
const norm = modelClass.toLowerCase().replace(/_/g, "");
|
|
802
|
+
if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
|
|
803
|
+
if (/(ss|us|is)$/.test(norm)) return norm;
|
|
804
|
+
if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
|
|
805
|
+
return norm.endsWith("s") ? norm.slice(0, -1) : norm;
|
|
806
|
+
}
|
|
699
807
|
function applyResolver(resolver, instance) {
|
|
700
808
|
if (typeof resolver === "string") {
|
|
701
809
|
const value = instance[resolver];
|
|
@@ -745,7 +853,7 @@ var OPERATION_TO_DB_OPERATION = {
|
|
|
745
853
|
function ezlogsPrisma() {
|
|
746
854
|
return (client) => client.$extends(buildPrismaExtension(client));
|
|
747
855
|
}
|
|
748
|
-
function buildPrismaExtension(client) {
|
|
856
|
+
function buildPrismaExtension(client, schemaClient = client) {
|
|
749
857
|
return {
|
|
750
858
|
name: "ezlogs",
|
|
751
859
|
query: {
|
|
@@ -756,7 +864,7 @@ function buildPrismaExtension(client) {
|
|
|
756
864
|
}
|
|
757
865
|
const result = await runTagged(client, ctx);
|
|
758
866
|
try {
|
|
759
|
-
recordPrismaOperation(ctx, result);
|
|
867
|
+
recordPrismaOperation(ctx, result, tableNameFor(schemaClient, ctx.model));
|
|
760
868
|
} catch (error) {
|
|
761
869
|
logger.debug(`prisma capture failed: ${describeError2(error)}`);
|
|
762
870
|
}
|
|
@@ -816,12 +924,21 @@ function isTransactionUnsupported(error) {
|
|
|
816
924
|
const message = error instanceof Error ? error.message : String(error);
|
|
817
925
|
return /transaction/i.test(message) && /not (?:be )?supported|unsupported|not available/i.test(message);
|
|
818
926
|
}
|
|
819
|
-
function
|
|
927
|
+
function tableNameFor(schemaClient, model) {
|
|
928
|
+
try {
|
|
929
|
+
const dbName = schemaClient?._runtimeDataModel?.models?.[model]?.dbName;
|
|
930
|
+
return typeof dbName === "string" && dbName.length > 0 ? dbName : model;
|
|
931
|
+
} catch {
|
|
932
|
+
return model;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
function recordPrismaOperation(ctx, result, table) {
|
|
820
936
|
const dbOperation = OPERATION_TO_DB_OPERATION[ctx.operation];
|
|
821
937
|
if (!dbOperation) return;
|
|
822
938
|
if (isBatchResult(result)) {
|
|
823
939
|
emitDbEvent({
|
|
824
940
|
modelClass: ctx.model,
|
|
941
|
+
table,
|
|
825
942
|
operation: dbOperation,
|
|
826
943
|
resourceId: null,
|
|
827
944
|
// For batch updates we still capture the `args.data` shape so
|
|
@@ -838,16 +955,17 @@ function recordPrismaOperation(ctx, result) {
|
|
|
838
955
|
}
|
|
839
956
|
if (Array.isArray(result)) {
|
|
840
957
|
for (const row of result) {
|
|
841
|
-
emitSingleRowEvent(ctx, dbOperation, row);
|
|
958
|
+
emitSingleRowEvent(ctx, dbOperation, row, table);
|
|
842
959
|
}
|
|
843
960
|
return;
|
|
844
961
|
}
|
|
845
|
-
emitSingleRowEvent(ctx, dbOperation, result);
|
|
962
|
+
emitSingleRowEvent(ctx, dbOperation, result, table);
|
|
846
963
|
}
|
|
847
|
-
function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
964
|
+
function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
848
965
|
if (!isPlainObject2(row)) {
|
|
849
966
|
emitDbEvent({
|
|
850
967
|
modelClass: ctx.model,
|
|
968
|
+
table,
|
|
851
969
|
operation: dbOperation,
|
|
852
970
|
resourceId: null
|
|
853
971
|
});
|
|
@@ -859,9 +977,13 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
859
977
|
if (dbOperation === "create") {
|
|
860
978
|
emitDbEvent({
|
|
861
979
|
modelClass: ctx.model,
|
|
980
|
+
table,
|
|
862
981
|
operation: "create",
|
|
863
982
|
resourceId,
|
|
864
|
-
initialAttributes:
|
|
983
|
+
initialAttributes: {
|
|
984
|
+
...isPlainObject2(ctx.args.data) ? ctx.args.data : {},
|
|
985
|
+
...rowObj
|
|
986
|
+
},
|
|
865
987
|
displayName,
|
|
866
988
|
modelInstance: rowObj
|
|
867
989
|
});
|
|
@@ -870,6 +992,7 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
870
992
|
if (dbOperation === "update") {
|
|
871
993
|
emitDbEvent({
|
|
872
994
|
modelClass: ctx.model,
|
|
995
|
+
table,
|
|
873
996
|
operation: "update",
|
|
874
997
|
resourceId,
|
|
875
998
|
changes: extractUpdateChangesFromArgs(ctx, rowObj),
|
|
@@ -880,34 +1003,25 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
880
1003
|
}
|
|
881
1004
|
emitDbEvent({
|
|
882
1005
|
modelClass: ctx.model,
|
|
1006
|
+
table,
|
|
883
1007
|
operation: "destroy",
|
|
884
1008
|
resourceId,
|
|
1009
|
+
initialAttributes: rowObj,
|
|
885
1010
|
displayName,
|
|
886
1011
|
modelInstance: rowObj
|
|
887
1012
|
});
|
|
888
1013
|
}
|
|
889
|
-
function extractCreateAttributes(ctx, resultRow) {
|
|
890
|
-
if (isPlainObject2(ctx.args.data)) {
|
|
891
|
-
return ctx.args.data;
|
|
892
|
-
}
|
|
893
|
-
if (ctx.operation === "upsert" && isPlainObject2(ctx.args.create)) {
|
|
894
|
-
return ctx.args.create;
|
|
895
|
-
}
|
|
896
|
-
return resultRow;
|
|
897
|
-
}
|
|
898
1014
|
function extractUpdateChangesFromArgs(ctx, resultRow) {
|
|
899
1015
|
const data = isPlainObject2(ctx.args.data) ? ctx.args.data : ctx.operation === "upsert" && isPlainObject2(ctx.args.update) ? ctx.args.update : null;
|
|
900
1016
|
if (!data) return [];
|
|
901
1017
|
return Object.keys(data).map((attribute) => ({
|
|
902
1018
|
attribute,
|
|
903
|
-
from: null,
|
|
904
1019
|
to: resultRow[attribute] ?? data[attribute]
|
|
905
1020
|
}));
|
|
906
1021
|
}
|
|
907
1022
|
function extractChangesFromUpdateData(data) {
|
|
908
1023
|
return Object.keys(data).map((attribute) => ({
|
|
909
1024
|
attribute,
|
|
910
|
-
from: null,
|
|
911
1025
|
to: data[attribute]
|
|
912
1026
|
}));
|
|
913
1027
|
}
|
|
@@ -1004,7 +1118,7 @@ function applyEzlogsExtension(prisma) {
|
|
|
1004
1118
|
return prisma;
|
|
1005
1119
|
}
|
|
1006
1120
|
try {
|
|
1007
|
-
return client.$extends(buildPrismaExtension(null));
|
|
1121
|
+
return client.$extends(buildPrismaExtension(null, prisma));
|
|
1008
1122
|
} catch (error) {
|
|
1009
1123
|
logger.debug(`patchPrismaClient: $extends failed: ${describeError2(error)}`);
|
|
1010
1124
|
return prisma;
|
package/dist/db/prisma.d.cts
CHANGED
|
@@ -30,9 +30,11 @@ declare function ezlogsPrisma(): unknown;
|
|
|
30
30
|
/**
|
|
31
31
|
* The extension descriptor. `client` null = capture only, no tagging
|
|
32
32
|
* (the adapter-pg path tags via the patched `pg` driver instead).
|
|
33
|
+
* `schemaClient` (defaults to `client`) is read for each model's table
|
|
34
|
+
* name (`@@map`).
|
|
33
35
|
* @internal exported for tests.
|
|
34
36
|
*/
|
|
35
|
-
declare function buildPrismaExtension(client: TaggingClient | null): unknown;
|
|
37
|
+
declare function buildPrismaExtension(client: TaggingClient | null, schemaClient?: object | null): unknown;
|
|
36
38
|
/** @internal test-only */
|
|
37
39
|
declare function _resetPrismaTaggingForTests(): void;
|
|
38
40
|
/**
|
package/dist/db/prisma.d.ts
CHANGED
|
@@ -30,9 +30,11 @@ declare function ezlogsPrisma(): unknown;
|
|
|
30
30
|
/**
|
|
31
31
|
* The extension descriptor. `client` null = capture only, no tagging
|
|
32
32
|
* (the adapter-pg path tags via the patched `pg` driver instead).
|
|
33
|
+
* `schemaClient` (defaults to `client`) is read for each model's table
|
|
34
|
+
* name (`@@map`).
|
|
33
35
|
* @internal exported for tests.
|
|
34
36
|
*/
|
|
35
|
-
declare function buildPrismaExtension(client: TaggingClient | null): unknown;
|
|
37
|
+
declare function buildPrismaExtension(client: TaggingClient | null, schemaClient?: object | null): unknown;
|
|
36
38
|
/** @internal test-only */
|
|
37
39
|
declare function _resetPrismaTaggingForTests(): void;
|
|
38
40
|
/**
|
package/dist/db/prisma.js
CHANGED
|
@@ -517,6 +517,37 @@ function matchesSensitiveKey(key, substringPatterns) {
|
|
|
517
517
|
const words = snake.split(/[^a-z0-9]+/);
|
|
518
518
|
return SENSITIVE_WORD_PATTERNS.some((w) => words.includes(w));
|
|
519
519
|
}
|
|
520
|
+
function passesLuhn(digits) {
|
|
521
|
+
let sum = 0;
|
|
522
|
+
for (let i = 0; i < digits.length; i += 1) {
|
|
523
|
+
let d = Number(digits[digits.length - 1 - i]);
|
|
524
|
+
if (i % 2 === 1) {
|
|
525
|
+
d *= 2;
|
|
526
|
+
if (d > 9) d -= 9;
|
|
527
|
+
}
|
|
528
|
+
sum += d;
|
|
529
|
+
}
|
|
530
|
+
return sum % 10 === 0;
|
|
531
|
+
}
|
|
532
|
+
var SECRET_TEXT_PATTERNS = [
|
|
533
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)/g,
|
|
534
|
+
/\beyJ[\w-]+\.[\w-]+\.[\w-]+/g,
|
|
535
|
+
/\b(?:sk|pk|rk)_(?:live|test)_[A-Za-z0-9]+/g,
|
|
536
|
+
/\bsk-[A-Za-z0-9_-]{16,}/g,
|
|
537
|
+
/\bgh[pousr]_[A-Za-z0-9]{20,}/g,
|
|
538
|
+
/\bxox[abposr]-[A-Za-z0-9-]+/g,
|
|
539
|
+
/\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g
|
|
540
|
+
];
|
|
541
|
+
var EMAIL = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g;
|
|
542
|
+
function scrubSecretsInText(text, options = {}) {
|
|
543
|
+
let out = text;
|
|
544
|
+
for (const pattern of SECRET_TEXT_PATTERNS) out = out.replace(pattern, "[FILTERED]");
|
|
545
|
+
out = out.replace(/\b(Bearer|Basic)\s+[\w.~+/=-]+/gi, "$1 [FILTERED]").replace(/\b\d(?:[ -]?\d){12,18}\b/g, (match) => {
|
|
546
|
+
const digits = match.replace(/[ -]/g, "");
|
|
547
|
+
return (digits.length === 15 || digits.length === 16) && passesLuhn(digits) ? "[FILTERED]" : match;
|
|
548
|
+
});
|
|
549
|
+
return options.emails ? out.replace(EMAIL, "[FILTERED]") : out;
|
|
550
|
+
}
|
|
520
551
|
|
|
521
552
|
// src/db/shared.ts
|
|
522
553
|
var IGNORED_ATTRIBUTES = Object.freeze([
|
|
@@ -525,8 +556,6 @@ var IGNORED_ATTRIBUTES = Object.freeze([
|
|
|
525
556
|
"created_at",
|
|
526
557
|
"updatedAt",
|
|
527
558
|
"updated_at",
|
|
528
|
-
"deletedAt",
|
|
529
|
-
"deleted_at",
|
|
530
559
|
// Auth.js / NextAuth standard columns
|
|
531
560
|
"emailVerified",
|
|
532
561
|
"expires"
|
|
@@ -570,14 +599,17 @@ function emitDbEvent(input) {
|
|
|
570
599
|
if (config.allExcludedTables().includes(input.modelClass)) return;
|
|
571
600
|
if (config.databaseReader && !input._fromTrigger) {
|
|
572
601
|
const tracked = config.triggerTrackedTables;
|
|
573
|
-
if (tracked === null || tracked.has(input.modelClass)) return;
|
|
602
|
+
if (tracked === null || tracked.has(input.table ?? input.modelClass)) return;
|
|
574
603
|
}
|
|
575
604
|
if (input.operation === "update") {
|
|
576
605
|
const meaningful = input.changes ? filterMeaningfulChanges(input.changes) : [];
|
|
577
606
|
if (meaningful.length === 0) return;
|
|
578
607
|
}
|
|
579
608
|
const resourceIdStr = input.resourceId === null || input.resourceId === void 0 ? null : String(input.resourceId);
|
|
580
|
-
const
|
|
609
|
+
const related = resourceIdStr === null ? input.relatedResourceIds ?? [] : [];
|
|
610
|
+
const context = buildContext(
|
|
611
|
+
related.length > 0 ? { ...input, displayName: null, modelInstance: null } : input
|
|
612
|
+
);
|
|
581
613
|
const event = build({
|
|
582
614
|
source_type: "database_callback",
|
|
583
615
|
source_data: {
|
|
@@ -586,7 +618,7 @@ function emitDbEvent(input) {
|
|
|
586
618
|
},
|
|
587
619
|
outcome: "success",
|
|
588
620
|
correlation_id: current(),
|
|
589
|
-
resource_ids: resourceIdStr === null ? [] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
621
|
+
resource_ids: resourceIdStr === null ? [...related] : [{ resource_type: input.modelClass, resource_id: resourceIdStr }],
|
|
590
622
|
context,
|
|
591
623
|
duration_ms: null,
|
|
592
624
|
timestamp: input.timestamp ?? null
|
|
@@ -633,7 +665,7 @@ function filterMeaningfulAttributes(attrs) {
|
|
|
633
665
|
return Object.keys(out).length === 0 ? null : out;
|
|
634
666
|
}
|
|
635
667
|
function filterMeaningfulChanges(changes) {
|
|
636
|
-
return changes.filter((c) => {
|
|
668
|
+
return flattenNestedChanges(changes).filter((c) => {
|
|
637
669
|
if (!isMeaningfulAttribute(c.attribute)) return false;
|
|
638
670
|
if (!isScalar(c.to)) return false;
|
|
639
671
|
if (c.from === void 0) return true;
|
|
@@ -641,6 +673,60 @@ function filterMeaningfulChanges(changes) {
|
|
|
641
673
|
return valuesActuallyChanged(c.from, c.to);
|
|
642
674
|
});
|
|
643
675
|
}
|
|
676
|
+
var MAX_NESTED_DEPTH = 3;
|
|
677
|
+
var MAX_NESTED_LEAVES = 20;
|
|
678
|
+
var MAX_UNKNOWN_FROM_LEAVES = 5;
|
|
679
|
+
function flattenNestedChanges(changes) {
|
|
680
|
+
if (!changes.some((c) => isPlainRecord(c.to) || isPlainRecord(c.from))) return changes;
|
|
681
|
+
const out = [];
|
|
682
|
+
for (const c of changes) {
|
|
683
|
+
if (!isPlainRecord(c.to) && !isPlainRecord(c.from)) {
|
|
684
|
+
out.push(c);
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
if (!isMeaningfulAttribute(c.attribute)) continue;
|
|
688
|
+
const leaves = [];
|
|
689
|
+
collectLeafChanges(c.attribute, c.from, c.to, 1, leaves);
|
|
690
|
+
const cap = c.from === void 0 ? MAX_UNKNOWN_FROM_LEAVES : MAX_NESTED_LEAVES;
|
|
691
|
+
out.push(...leaves.slice(0, cap).map(scrubLeaf));
|
|
692
|
+
}
|
|
693
|
+
return out;
|
|
694
|
+
}
|
|
695
|
+
function collectLeafChanges(path, from, to, depth, out) {
|
|
696
|
+
if (out.length > MAX_NESTED_LEAVES) return;
|
|
697
|
+
const fromObj = isPlainRecord(from);
|
|
698
|
+
const toObj = isPlainRecord(to);
|
|
699
|
+
if ((fromObj || toObj) && depth <= MAX_NESTED_DEPTH && (fromObj || from == null) && (toObj || to == null)) {
|
|
700
|
+
const f = fromObj ? from : {};
|
|
701
|
+
const t = toObj ? to : {};
|
|
702
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(f), ...Object.keys(t)])) {
|
|
703
|
+
const before = from === void 0 ? void 0 : f[key] ?? null;
|
|
704
|
+
collectLeafChanges(`${path}.${key}`, before, t[key] ?? null, depth + 1, out);
|
|
705
|
+
}
|
|
706
|
+
return;
|
|
707
|
+
}
|
|
708
|
+
if (from !== void 0 && sameJson(from, to)) return;
|
|
709
|
+
out.push(from === void 0 ? { attribute: path, to } : { attribute: path, from, to });
|
|
710
|
+
}
|
|
711
|
+
function scrubLeaf(change) {
|
|
712
|
+
const scrub = (v) => typeof v === "string" ? scrubSecretsInText(v) : v;
|
|
713
|
+
const out = { attribute: change.attribute, to: scrub(change.to) };
|
|
714
|
+
if (change.from !== void 0) out.from = scrub(change.from);
|
|
715
|
+
return out;
|
|
716
|
+
}
|
|
717
|
+
function isPlainRecord(value) {
|
|
718
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return false;
|
|
719
|
+
const proto = Object.getPrototypeOf(value);
|
|
720
|
+
return proto === Object.prototype || proto === null;
|
|
721
|
+
}
|
|
722
|
+
function sameJson(a, b) {
|
|
723
|
+
if (a === b) return true;
|
|
724
|
+
try {
|
|
725
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
726
|
+
} catch {
|
|
727
|
+
return false;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
644
730
|
function isMeaningfulAttribute(name) {
|
|
645
731
|
if (IGNORED_ATTRIBUTES.includes(name)) return false;
|
|
646
732
|
if (isDbSensitive(name)) return false;
|
|
@@ -678,22 +764,44 @@ function isPrismaDecimal(value) {
|
|
|
678
764
|
const obj = value;
|
|
679
765
|
return typeof obj.toString === "function" && typeof obj.toFixed === "function" && "d" in obj && "e" in obj && "s" in obj;
|
|
680
766
|
}
|
|
767
|
+
var DISPLAY_NAME_COLUMNS = ["name", "title", "number", "label"];
|
|
768
|
+
var MAX_DISPLAY_NAME_LENGTH = 120;
|
|
681
769
|
function resolveDisplayName(modelClass, instance) {
|
|
682
770
|
try {
|
|
683
771
|
const config = configuration();
|
|
684
772
|
const resolver = config.displayNameFor[modelClass];
|
|
685
773
|
const resolved = applyResolver(resolver, instance);
|
|
686
774
|
if (resolved !== null) return resolved;
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
if (
|
|
690
|
-
|
|
775
|
+
const columns = /* @__PURE__ */ new Map();
|
|
776
|
+
for (const key of Object.keys(instance)) {
|
|
777
|
+
if (isDbSensitive(key)) continue;
|
|
778
|
+
const norm = key.toLowerCase().replace(/_/g, "");
|
|
779
|
+
if (!columns.has(norm)) columns.set(norm, instance[key]);
|
|
780
|
+
}
|
|
781
|
+
const text = (norm) => {
|
|
782
|
+
const value = columns.get(norm);
|
|
783
|
+
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "bigint") {
|
|
784
|
+
return null;
|
|
691
785
|
}
|
|
786
|
+
const str = String(value).trim();
|
|
787
|
+
return str.length > 0 ? str.slice(0, MAX_DISPLAY_NAME_LENGTH) : null;
|
|
788
|
+
};
|
|
789
|
+
for (const column of DISPLAY_NAME_COLUMNS) {
|
|
790
|
+
const value = text(column);
|
|
791
|
+
if (value !== null) return value;
|
|
692
792
|
}
|
|
793
|
+
return text(`${singularNorm(modelClass)}number`);
|
|
693
794
|
} catch {
|
|
694
795
|
}
|
|
695
796
|
return null;
|
|
696
797
|
}
|
|
798
|
+
function singularNorm(modelClass) {
|
|
799
|
+
const norm = modelClass.toLowerCase().replace(/_/g, "");
|
|
800
|
+
if (norm.endsWith("ies")) return norm.slice(0, -3) + "y";
|
|
801
|
+
if (/(ss|us|is)$/.test(norm)) return norm;
|
|
802
|
+
if (/(xes|ches|shes|sses)$/.test(norm)) return norm.slice(0, -2);
|
|
803
|
+
return norm.endsWith("s") ? norm.slice(0, -1) : norm;
|
|
804
|
+
}
|
|
697
805
|
function applyResolver(resolver, instance) {
|
|
698
806
|
if (typeof resolver === "string") {
|
|
699
807
|
const value = instance[resolver];
|
|
@@ -743,7 +851,7 @@ var OPERATION_TO_DB_OPERATION = {
|
|
|
743
851
|
function ezlogsPrisma() {
|
|
744
852
|
return (client) => client.$extends(buildPrismaExtension(client));
|
|
745
853
|
}
|
|
746
|
-
function buildPrismaExtension(client) {
|
|
854
|
+
function buildPrismaExtension(client, schemaClient = client) {
|
|
747
855
|
return {
|
|
748
856
|
name: "ezlogs",
|
|
749
857
|
query: {
|
|
@@ -754,7 +862,7 @@ function buildPrismaExtension(client) {
|
|
|
754
862
|
}
|
|
755
863
|
const result = await runTagged(client, ctx);
|
|
756
864
|
try {
|
|
757
|
-
recordPrismaOperation(ctx, result);
|
|
865
|
+
recordPrismaOperation(ctx, result, tableNameFor(schemaClient, ctx.model));
|
|
758
866
|
} catch (error) {
|
|
759
867
|
logger.debug(`prisma capture failed: ${describeError2(error)}`);
|
|
760
868
|
}
|
|
@@ -814,12 +922,21 @@ function isTransactionUnsupported(error) {
|
|
|
814
922
|
const message = error instanceof Error ? error.message : String(error);
|
|
815
923
|
return /transaction/i.test(message) && /not (?:be )?supported|unsupported|not available/i.test(message);
|
|
816
924
|
}
|
|
817
|
-
function
|
|
925
|
+
function tableNameFor(schemaClient, model) {
|
|
926
|
+
try {
|
|
927
|
+
const dbName = schemaClient?._runtimeDataModel?.models?.[model]?.dbName;
|
|
928
|
+
return typeof dbName === "string" && dbName.length > 0 ? dbName : model;
|
|
929
|
+
} catch {
|
|
930
|
+
return model;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
function recordPrismaOperation(ctx, result, table) {
|
|
818
934
|
const dbOperation = OPERATION_TO_DB_OPERATION[ctx.operation];
|
|
819
935
|
if (!dbOperation) return;
|
|
820
936
|
if (isBatchResult(result)) {
|
|
821
937
|
emitDbEvent({
|
|
822
938
|
modelClass: ctx.model,
|
|
939
|
+
table,
|
|
823
940
|
operation: dbOperation,
|
|
824
941
|
resourceId: null,
|
|
825
942
|
// For batch updates we still capture the `args.data` shape so
|
|
@@ -836,16 +953,17 @@ function recordPrismaOperation(ctx, result) {
|
|
|
836
953
|
}
|
|
837
954
|
if (Array.isArray(result)) {
|
|
838
955
|
for (const row of result) {
|
|
839
|
-
emitSingleRowEvent(ctx, dbOperation, row);
|
|
956
|
+
emitSingleRowEvent(ctx, dbOperation, row, table);
|
|
840
957
|
}
|
|
841
958
|
return;
|
|
842
959
|
}
|
|
843
|
-
emitSingleRowEvent(ctx, dbOperation, result);
|
|
960
|
+
emitSingleRowEvent(ctx, dbOperation, result, table);
|
|
844
961
|
}
|
|
845
|
-
function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
962
|
+
function emitSingleRowEvent(ctx, dbOperation, row, table) {
|
|
846
963
|
if (!isPlainObject2(row)) {
|
|
847
964
|
emitDbEvent({
|
|
848
965
|
modelClass: ctx.model,
|
|
966
|
+
table,
|
|
849
967
|
operation: dbOperation,
|
|
850
968
|
resourceId: null
|
|
851
969
|
});
|
|
@@ -857,9 +975,13 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
857
975
|
if (dbOperation === "create") {
|
|
858
976
|
emitDbEvent({
|
|
859
977
|
modelClass: ctx.model,
|
|
978
|
+
table,
|
|
860
979
|
operation: "create",
|
|
861
980
|
resourceId,
|
|
862
|
-
initialAttributes:
|
|
981
|
+
initialAttributes: {
|
|
982
|
+
...isPlainObject2(ctx.args.data) ? ctx.args.data : {},
|
|
983
|
+
...rowObj
|
|
984
|
+
},
|
|
863
985
|
displayName,
|
|
864
986
|
modelInstance: rowObj
|
|
865
987
|
});
|
|
@@ -868,6 +990,7 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
868
990
|
if (dbOperation === "update") {
|
|
869
991
|
emitDbEvent({
|
|
870
992
|
modelClass: ctx.model,
|
|
993
|
+
table,
|
|
871
994
|
operation: "update",
|
|
872
995
|
resourceId,
|
|
873
996
|
changes: extractUpdateChangesFromArgs(ctx, rowObj),
|
|
@@ -878,34 +1001,25 @@ function emitSingleRowEvent(ctx, dbOperation, row) {
|
|
|
878
1001
|
}
|
|
879
1002
|
emitDbEvent({
|
|
880
1003
|
modelClass: ctx.model,
|
|
1004
|
+
table,
|
|
881
1005
|
operation: "destroy",
|
|
882
1006
|
resourceId,
|
|
1007
|
+
initialAttributes: rowObj,
|
|
883
1008
|
displayName,
|
|
884
1009
|
modelInstance: rowObj
|
|
885
1010
|
});
|
|
886
1011
|
}
|
|
887
|
-
function extractCreateAttributes(ctx, resultRow) {
|
|
888
|
-
if (isPlainObject2(ctx.args.data)) {
|
|
889
|
-
return ctx.args.data;
|
|
890
|
-
}
|
|
891
|
-
if (ctx.operation === "upsert" && isPlainObject2(ctx.args.create)) {
|
|
892
|
-
return ctx.args.create;
|
|
893
|
-
}
|
|
894
|
-
return resultRow;
|
|
895
|
-
}
|
|
896
1012
|
function extractUpdateChangesFromArgs(ctx, resultRow) {
|
|
897
1013
|
const data = isPlainObject2(ctx.args.data) ? ctx.args.data : ctx.operation === "upsert" && isPlainObject2(ctx.args.update) ? ctx.args.update : null;
|
|
898
1014
|
if (!data) return [];
|
|
899
1015
|
return Object.keys(data).map((attribute) => ({
|
|
900
1016
|
attribute,
|
|
901
|
-
from: null,
|
|
902
1017
|
to: resultRow[attribute] ?? data[attribute]
|
|
903
1018
|
}));
|
|
904
1019
|
}
|
|
905
1020
|
function extractChangesFromUpdateData(data) {
|
|
906
1021
|
return Object.keys(data).map((attribute) => ({
|
|
907
1022
|
attribute,
|
|
908
|
-
from: null,
|
|
909
1023
|
to: data[attribute]
|
|
910
1024
|
}));
|
|
911
1025
|
}
|
|
@@ -1002,7 +1116,7 @@ function applyEzlogsExtension(prisma) {
|
|
|
1002
1116
|
return prisma;
|
|
1003
1117
|
}
|
|
1004
1118
|
try {
|
|
1005
|
-
return client.$extends(buildPrismaExtension(null));
|
|
1119
|
+
return client.$extends(buildPrismaExtension(null, prisma));
|
|
1006
1120
|
} catch (error) {
|
|
1007
1121
|
logger.debug(`patchPrismaClient: $extends failed: ${describeError2(error)}`);
|
|
1008
1122
|
return prisma;
|