@logtape/redaction 2.1.19 → 2.1.21
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/dist/field.cjs +81 -82
- package/dist/field.d.cts.map +1 -1
- package/dist/field.d.ts.map +1 -1
- package/dist/field.js +81 -82
- package/dist/field.js.map +1 -1
- package/dist/pattern.cjs +13 -3
- package/dist/pattern.d.cts.map +1 -1
- package/dist/pattern.d.ts.map +1 -1
- package/dist/pattern.js +14 -3
- package/dist/pattern.js.map +1 -1
- package/dist/structured.cjs +125 -0
- package/dist/structured.js +122 -0
- package/dist/structured.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
//#region src/structured.ts
|
|
2
|
+
const mapForEach = Map.prototype.forEach;
|
|
3
|
+
const setForEach = Set.prototype.forEach;
|
|
4
|
+
const errorIsError = Error.isError;
|
|
5
|
+
const errorCopies = /* @__PURE__ */ new WeakSet();
|
|
6
|
+
const errorPrototypes = [
|
|
7
|
+
AggregateError.prototype,
|
|
8
|
+
EvalError.prototype,
|
|
9
|
+
RangeError.prototype,
|
|
10
|
+
ReferenceError.prototype,
|
|
11
|
+
SyntaxError.prototype,
|
|
12
|
+
TypeError.prototype,
|
|
13
|
+
URIError.prototype,
|
|
14
|
+
Error.prototype
|
|
15
|
+
];
|
|
16
|
+
const omit = Symbol();
|
|
17
|
+
function defineField(target, key, value, enumerable = true) {
|
|
18
|
+
Object.defineProperty(target, key, {
|
|
19
|
+
value,
|
|
20
|
+
enumerable,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function errorPrototype(value) {
|
|
26
|
+
if (errorIsError != null && !errorIsError(value) && !errorCopies.has(value)) return void 0;
|
|
27
|
+
return errorPrototypes.find((p) => Object.prototype.isPrototypeOf.call(p, value)) ?? (errorIsError == null ? void 0 : Error.prototype);
|
|
28
|
+
}
|
|
29
|
+
const diagnostics = [
|
|
30
|
+
"name",
|
|
31
|
+
"message",
|
|
32
|
+
"cause",
|
|
33
|
+
"errors"
|
|
34
|
+
];
|
|
35
|
+
function* errorFields(value) {
|
|
36
|
+
for (const key of new Set([...diagnostics, ...Object.keys(value)])) {
|
|
37
|
+
if (key === "stack" || key === "toJSON" || key === "toString" || key === "valueOf") continue;
|
|
38
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
39
|
+
if (descriptor != null && "value" in descriptor && typeof descriptor.value !== "function") yield [key, descriptor.value];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function defineDiagnostic(copy, key, result) {
|
|
43
|
+
if (result === omit) return;
|
|
44
|
+
if ((key === "name" || key === "message") && typeof result !== "string") return;
|
|
45
|
+
defineField(copy, key, result, !diagnostics.includes(key));
|
|
46
|
+
}
|
|
47
|
+
function copyStructured(value, visited, transform, transformField, transformMapKey) {
|
|
48
|
+
const prototype = errorPrototype(value);
|
|
49
|
+
if (prototype != null) {
|
|
50
|
+
const copy = Object.create(prototype);
|
|
51
|
+
errorCopies.add(copy);
|
|
52
|
+
visited.set(value, copy);
|
|
53
|
+
for (const [key, member] of errorFields(value)) {
|
|
54
|
+
let result;
|
|
55
|
+
try {
|
|
56
|
+
result = transformField(key, member);
|
|
57
|
+
} catch {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
defineDiagnostic(copy, key, result);
|
|
61
|
+
}
|
|
62
|
+
return copy;
|
|
63
|
+
}
|
|
64
|
+
if (value instanceof Map) {
|
|
65
|
+
const copy = /* @__PURE__ */ new Map();
|
|
66
|
+
visited.set(value, copy);
|
|
67
|
+
mapForEach.call(value, (member, key) => {
|
|
68
|
+
const result = typeof key === "string" ? transformField(key, member) : transform(member);
|
|
69
|
+
if (result !== omit) {
|
|
70
|
+
const outputKey = !transformMapKey && typeof key === "string" ? key : transform(key);
|
|
71
|
+
copy.set(outputKey, result);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return copy;
|
|
75
|
+
}
|
|
76
|
+
if (value instanceof Set) {
|
|
77
|
+
const copy = /* @__PURE__ */ new Set();
|
|
78
|
+
visited.set(value, copy);
|
|
79
|
+
setForEach.call(value, (member) => copy.add(transform(member)));
|
|
80
|
+
return copy;
|
|
81
|
+
}
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
function copyStructuredAsync(value, visited, transform, transformField) {
|
|
85
|
+
const prototype = errorPrototype(value);
|
|
86
|
+
if (prototype != null) {
|
|
87
|
+
const copy = Object.create(prototype);
|
|
88
|
+
errorCopies.add(copy);
|
|
89
|
+
visited.set(value, copy);
|
|
90
|
+
return Promise.all(Array.from(errorFields(value), async ([key, member]) => [key, await transformField(key, member)])).then((fields) => {
|
|
91
|
+
for (const [key, member] of fields) defineDiagnostic(copy, key, member);
|
|
92
|
+
return copy;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (value instanceof Map) {
|
|
96
|
+
const copy = /* @__PURE__ */ new Map();
|
|
97
|
+
visited.set(value, copy);
|
|
98
|
+
const entries = [];
|
|
99
|
+
mapForEach.call(value, (member, key) => {
|
|
100
|
+
entries.push(Promise.all(typeof key === "string" ? [key, transformField(key, member)] : [transform(key), transform(member)]));
|
|
101
|
+
});
|
|
102
|
+
return Promise.all(entries).then((resolved) => {
|
|
103
|
+
for (const [key, member] of resolved) copy.set(key, member);
|
|
104
|
+
return copy;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (value instanceof Set) {
|
|
108
|
+
const copy = /* @__PURE__ */ new Set();
|
|
109
|
+
visited.set(value, copy);
|
|
110
|
+
const members = [];
|
|
111
|
+
setForEach.call(value, (member) => members.push(transform(member)));
|
|
112
|
+
return Promise.all(members).then((resolved) => {
|
|
113
|
+
for (const member of resolved) copy.add(member);
|
|
114
|
+
return copy;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return void 0;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
//#endregion
|
|
121
|
+
export { copyStructured, copyStructuredAsync, defineField, omit };
|
|
122
|
+
//# sourceMappingURL=structured.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structured.js","names":["target: object","key: string","value: unknown","value: object","copy: Error","result: unknown","visited: Map<object, object>","transform: (value: unknown) => unknown","transformField: TransformField","transformMapKey: boolean","transform: (value: unknown) => Promise<unknown>","transformField: (key: string, value: unknown) => Promise<unknown>","entries: Promise<[unknown, unknown]>[]","members: Promise<unknown>[]"],"sources":["../src/structured.ts"],"sourcesContent":["const mapForEach = Map.prototype.forEach;\nconst setForEach = Set.prototype.forEach;\nconst errorIsError = (Error as ErrorConstructor & {\n isError?: (value: unknown) => boolean;\n}).isError;\nconst errorCopies = new WeakSet<object>();\nconst errorPrototypes = [\n AggregateError.prototype,\n EvalError.prototype,\n RangeError.prototype,\n ReferenceError.prototype,\n SyntaxError.prototype,\n TypeError.prototype,\n URIError.prototype,\n Error.prototype,\n];\n\nexport const omit = Symbol();\n\nexport type TransformField = (\n key: string,\n value: unknown,\n) => unknown;\n\nexport function defineField(\n target: object,\n key: string,\n value: unknown,\n enumerable = true,\n): void {\n Object.defineProperty(target, key, {\n value,\n enumerable,\n configurable: true,\n writable: true,\n });\n}\n\nfunction errorPrototype(value: object): Error | undefined {\n if (errorIsError != null && !errorIsError(value) && !errorCopies.has(value)) {\n return undefined;\n }\n return errorPrototypes.find((p) =>\n Object.prototype.isPrototypeOf.call(p, value)\n ) ??\n (errorIsError == null ? undefined : Error.prototype);\n}\n\nconst diagnostics = [\"name\", \"message\", \"cause\", \"errors\"];\n\nfunction* errorFields(value: object): Iterable<[string, unknown]> {\n // Do not request the stack descriptor: some runtimes may format it lazily.\n for (const key of new Set([...diagnostics, ...Object.keys(value)])) {\n if (\n key === \"stack\" || key === \"toJSON\" || key === \"toString\" ||\n key === \"valueOf\"\n ) continue;\n const descriptor = Object.getOwnPropertyDescriptor(value, key);\n if (\n descriptor != null && \"value\" in descriptor &&\n typeof descriptor.value !== \"function\"\n ) yield [key, descriptor.value];\n }\n}\n\nfunction defineDiagnostic(copy: Error, key: string, result: unknown): void {\n if (result === omit) return;\n // Native renderers may coerce these diagnostics to strings.\n if ((key === \"name\" || key === \"message\") && typeof result !== \"string\") {\n return;\n }\n defineField(copy, key, result, !diagnostics.includes(key));\n}\n\nexport function copyStructured(\n value: object,\n visited: Map<object, object>,\n transform: (value: unknown) => unknown,\n transformField: TransformField,\n transformMapKey: boolean,\n): object | undefined {\n const prototype = errorPrototype(value);\n if (prototype != null) {\n // Do not construct a native Error here. Inspecting or removing its\n // generated stack can execute application code on some runtimes.\n const copy: Error = Object.create(prototype);\n errorCopies.add(copy);\n visited.set(value, copy);\n for (const [key, member] of errorFields(value)) {\n let result: unknown;\n try {\n result = transformField(key, member);\n } catch {\n // Do not pass the original diagnostic or the failure to a sink.\n continue;\n }\n defineDiagnostic(copy, key, result);\n }\n return copy;\n }\n if (value instanceof Map) {\n const copy = new Map();\n visited.set(value, copy);\n mapForEach.call(value, (member, key) => {\n const result = typeof key === \"string\"\n ? transformField(key, member)\n : transform(member);\n if (result !== omit) {\n const outputKey = !transformMapKey && typeof key === \"string\"\n ? key\n : transform(key);\n copy.set(outputKey, result);\n }\n });\n return copy;\n }\n if (value instanceof Set) {\n const copy = new Set();\n visited.set(value, copy);\n setForEach.call(value, (member) => copy.add(transform(member)));\n return copy;\n }\n return undefined;\n}\n\nexport function copyStructuredAsync(\n value: object,\n visited: Map<object, object>,\n transform: (value: unknown) => Promise<unknown>,\n transformField: (key: string, value: unknown) => Promise<unknown>,\n): Promise<object> | undefined {\n const prototype = errorPrototype(value);\n if (prototype != null) {\n const copy: Error = Object.create(prototype);\n errorCopies.add(copy);\n visited.set(value, copy);\n return Promise.all(\n Array.from(\n errorFields(value),\n async ([key, member]) =>\n [key, await transformField(key, member)] as const,\n ),\n ).then((fields) => {\n for (const [key, member] of fields) defineDiagnostic(copy, key, member);\n return copy;\n });\n }\n if (value instanceof Map) {\n const copy = new Map();\n visited.set(value, copy);\n const entries: Promise<[unknown, unknown]>[] = [];\n mapForEach.call(value, (member, key) => {\n entries.push(Promise.all(\n typeof key === \"string\"\n ? [key, transformField(key, member)]\n : [transform(key), transform(member)],\n ));\n });\n return Promise.all(entries).then((resolved) => {\n for (const [key, member] of resolved) copy.set(key, member);\n return copy;\n });\n }\n if (value instanceof Set) {\n const copy = new Set();\n visited.set(value, copy);\n const members: Promise<unknown>[] = [];\n setForEach.call(value, (member) => members.push(transform(member)));\n return Promise.all(members).then((resolved) => {\n for (const member of resolved) copy.add(member);\n return copy;\n });\n }\n return undefined;\n}\n"],"mappings":";AAAA,MAAM,aAAa,IAAI,UAAU;AACjC,MAAM,aAAa,IAAI,UAAU;AACjC,MAAM,eAAgB,MAEnB;AACH,MAAM,8BAAc,IAAI;AACxB,MAAM,kBAAkB;CACtB,eAAe;CACf,UAAU;CACV,WAAW;CACX,eAAe;CACf,YAAY;CACZ,UAAU;CACV,SAAS;CACT,MAAM;AACP;AAED,MAAa,OAAO,QAAQ;AAO5B,SAAgB,YACdA,QACAC,KACAC,OACA,aAAa,MACP;AACN,QAAO,eAAe,QAAQ,KAAK;EACjC;EACA;EACA,cAAc;EACd,UAAU;CACX,EAAC;AACH;AAED,SAAS,eAAeC,OAAkC;AACxD,KAAI,gBAAgB,SAAS,aAAa,MAAM,KAAK,YAAY,IAAI,MAAM,CACzE;AAEF,QAAO,gBAAgB,KAAK,CAAC,MAC3B,OAAO,UAAU,cAAc,KAAK,GAAG,MAAM,CAC9C,KACE,gBAAgB,gBAAmB,MAAM;AAC7C;AAED,MAAM,cAAc;CAAC;CAAQ;CAAW;CAAS;AAAS;AAE1D,UAAU,YAAYA,OAA4C;AAEhE,MAAK,MAAM,OAAO,IAAI,IAAI,CAAC,GAAG,aAAa,GAAG,OAAO,KAAK,MAAM,AAAC,IAAG;AAClE,MACE,QAAQ,WAAW,QAAQ,YAAY,QAAQ,cAC/C,QAAQ,UACR;EACF,MAAM,a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ACH;AACD;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logtape/redaction",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.21",
|
|
4
4
|
"description": "Redact sensitive data from log messages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"logging",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"dist/"
|
|
50
50
|
],
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"@logtape/logtape": "^2.1.
|
|
52
|
+
"@logtape/logtape": "^2.1.21"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@alinea/suite": "^0.6.3",
|