@loggerjs/processors 0.5.1 → 0.5.2

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.
@@ -0,0 +1,7 @@
1
+ declare const NATIVE_ERROR_FIELDS: readonly ["cause", "errors"];
2
+ type NativeErrorField = (typeof NATIVE_ERROR_FIELDS)[number];
3
+ export declare function hiddenErrorFields(value: Error): NativeErrorField[];
4
+ export declare function defineHidden(target: Record<string, unknown>, field: string, value: unknown): void;
5
+ export declare function dotPath(path: string, field: string): string;
6
+ export {};
7
+ //# sourceMappingURL=error-fields.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-fields.d.ts","sourceRoot":"","sources":["../src/error-fields.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,mBAAmB,8BAA+B,CAAC;AAEzD,KAAK,gBAAgB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE7D,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,KAAK,GAAG,gBAAgB,EAAE,CAOlE;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAOjG;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE3D"}
package/dist/index.cjs CHANGED
@@ -1,5 +1,27 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  let _loggerjs_core = require("@loggerjs/core");
3
+ //#region src/error-fields.ts
4
+ const NATIVE_ERROR_FIELDS = ["cause", "errors"];
5
+ function hiddenErrorFields(value) {
6
+ const fields = [];
7
+ for (const field of NATIVE_ERROR_FIELDS) {
8
+ const descriptor = Object.getOwnPropertyDescriptor(value, field);
9
+ if (descriptor && !descriptor.enumerable) fields.push(field);
10
+ }
11
+ return fields;
12
+ }
13
+ function defineHidden(target, field, value) {
14
+ Object.defineProperty(target, field, {
15
+ value,
16
+ writable: true,
17
+ enumerable: false,
18
+ configurable: true
19
+ });
20
+ }
21
+ function dotPath(path, field) {
22
+ return path ? `${path}.${field}` : field;
23
+ }
24
+ //#endregion
3
25
  //#region src/redact.ts
4
26
  function matchesKey(key, path, value, matchers) {
5
27
  return matchers.some((matcher) => {
@@ -16,10 +38,13 @@ function matchesKey(key, path, value, matchers) {
16
38
  function pathMatches(path, paths) {
17
39
  return paths.includes(path);
18
40
  }
41
+ function shouldRedact(key, path, value, options) {
42
+ return matchesKey(key, path, value, options.keys ?? []) || pathMatches(path, options.paths ?? []);
43
+ }
19
44
  function redactValue(value, options, path = "", depth = 0, seen = /* @__PURE__ */ new WeakMap()) {
20
45
  if (value === null || value === void 0) return value;
21
46
  if (typeof value !== "object") return value;
22
- if (depth >= options.maxDepth) return value;
47
+ if (depth >= options.maxDepth) return options.replacement;
23
48
  if (seen.has(value)) return seen.get(value);
24
49
  if (Array.isArray(value)) {
25
50
  const out = [];
@@ -27,14 +52,68 @@ function redactValue(value, options, path = "", depth = 0, seen = /* @__PURE__ *
27
52
  for (let index = 0; index < value.length; index += 1) out[index] = redactValue(value[index], options, `${path}[${index}]`, depth + 1, seen);
28
53
  return out;
29
54
  }
30
- if (value instanceof Error) return value;
31
55
  if (value instanceof Date) return value;
56
+ if (value instanceof Error) {
57
+ const entries = Object.entries(value);
58
+ const hiddenFields = hiddenErrorFields(value);
59
+ if (entries.length === 0 && hiddenFields.length === 0) return value;
60
+ const errorOut = Object.create(Object.getPrototypeOf(value));
61
+ seen.set(value, errorOut);
62
+ defineHidden(errorOut, "message", value.message);
63
+ defineHidden(errorOut, "stack", value.stack);
64
+ for (const field of hiddenFields) {
65
+ const child = value[field];
66
+ const childPath = dotPath(path, field);
67
+ if (shouldRedact(field, childPath, child, options)) {
68
+ if (options.remove) continue;
69
+ defineHidden(errorOut, field, options.replacement);
70
+ continue;
71
+ }
72
+ defineHidden(errorOut, field, redactValue(child, options, childPath, depth + 1, seen));
73
+ }
74
+ for (const [key, child] of entries) {
75
+ const childPath = dotPath(path, key);
76
+ if (shouldRedact(key, childPath, child, options)) {
77
+ if (options.remove) continue;
78
+ errorOut[key] = options.replacement;
79
+ } else errorOut[key] = redactValue(child, options, childPath, depth + 1, seen);
80
+ }
81
+ return errorOut;
82
+ }
83
+ if (value instanceof Map) {
84
+ const mapOut = /* @__PURE__ */ new Map();
85
+ seen.set(value, mapOut);
86
+ for (const [key, child] of value) {
87
+ if (typeof key === "string") {
88
+ const childPath = dotPath(path, key);
89
+ if (shouldRedact(key, childPath, child, options)) {
90
+ if (options.remove) continue;
91
+ mapOut.set(key, options.replacement);
92
+ continue;
93
+ }
94
+ mapOut.set(key, redactValue(child, options, childPath, depth + 1, seen));
95
+ continue;
96
+ }
97
+ mapOut.set(key, redactValue(child, options, path, depth + 1, seen));
98
+ }
99
+ return mapOut;
100
+ }
101
+ if (value instanceof Set) {
102
+ const setOut = /* @__PURE__ */ new Set();
103
+ seen.set(value, setOut);
104
+ let index = 0;
105
+ for (const child of value) {
106
+ setOut.add(redactValue(child, options, `${path}[${index}]`, depth + 1, seen));
107
+ index += 1;
108
+ }
109
+ return setOut;
110
+ }
32
111
  const input = value;
33
112
  const out = {};
34
113
  seen.set(value, out);
35
114
  for (const [key, child] of Object.entries(input)) {
36
- const childPath = path ? `${path}.${key}` : key;
37
- if (matchesKey(key, childPath, child, options.keys ?? []) || pathMatches(childPath, options.paths ?? [])) {
115
+ const childPath = dotPath(path, key);
116
+ if (shouldRedact(key, childPath, child, options)) {
38
117
  if (options.remove) continue;
39
118
  out[key] = options.replacement;
40
119
  } else out[key] = redactValue(child, options, childPath, depth + 1, seen);
@@ -1135,6 +1214,12 @@ function guardString(input, path, options) {
1135
1214
  changed: changed || value !== input
1136
1215
  };
1137
1216
  }
1217
+ function guardErrorString(input, path, options) {
1218
+ return typeof input === "string" ? guardString(input, path, options) : {
1219
+ value: input,
1220
+ changed: false
1221
+ };
1222
+ }
1138
1223
  function guardValue(value, path, key, depth, options, seen) {
1139
1224
  if (matchesAny(options.denyKeys, key, path, value) && !matchesAny(options.allowKeys, key, path, value)) return {
1140
1225
  value: redact(path, "deny-key", options),
@@ -1153,10 +1238,72 @@ function guardValue(value, path, key, depth, options, seen) {
1153
1238
  value: seen.get(value),
1154
1239
  changed: true
1155
1240
  };
1156
- if (value instanceof Date || value instanceof Error) return {
1241
+ if (value instanceof Date) return {
1157
1242
  value,
1158
1243
  changed: false
1159
1244
  };
1245
+ if (value instanceof Error) {
1246
+ const message = guardErrorString(value.message, dotPath(path, "message"), options);
1247
+ const stack = guardErrorString(value.stack, dotPath(path, "stack"), options);
1248
+ const fieldChanged = message.changed || stack.changed;
1249
+ const entries = Object.entries(value);
1250
+ const hiddenFields = hiddenErrorFields(value);
1251
+ if (entries.length === 0 && hiddenFields.length === 0 && !fieldChanged) return {
1252
+ value,
1253
+ changed: false
1254
+ };
1255
+ const errorOut = Object.create(Object.getPrototypeOf(value));
1256
+ seen.set(value, errorOut);
1257
+ defineHidden(errorOut, "message", message.value);
1258
+ defineHidden(errorOut, "stack", stack.value);
1259
+ let changed = fieldChanged;
1260
+ for (const field of hiddenFields) {
1261
+ const childPath = dotPath(path, field);
1262
+ const child = guardValue(value[field], childPath, field, depth + 1, options, seen);
1263
+ defineHidden(errorOut, field, child.value);
1264
+ changed ||= child.changed;
1265
+ }
1266
+ for (const [childKey, childValue] of entries) {
1267
+ const child = guardValue(childValue, dotPath(path, childKey), childKey, depth + 1, options, seen);
1268
+ errorOut[childKey] = child.value;
1269
+ changed ||= child.changed;
1270
+ }
1271
+ return {
1272
+ value: changed ? errorOut : value,
1273
+ changed
1274
+ };
1275
+ }
1276
+ if (value instanceof Map) {
1277
+ const mapOut = /* @__PURE__ */ new Map();
1278
+ seen.set(value, mapOut);
1279
+ let changed = false;
1280
+ for (const [mapKey, mapValue] of value) {
1281
+ const isStringKey = typeof mapKey === "string";
1282
+ const child = guardValue(mapValue, isStringKey ? dotPath(path, mapKey) : path, isStringKey ? mapKey : key, depth + 1, options, seen);
1283
+ mapOut.set(mapKey, child.value);
1284
+ changed ||= child.changed;
1285
+ }
1286
+ return {
1287
+ value: changed ? mapOut : value,
1288
+ changed
1289
+ };
1290
+ }
1291
+ if (value instanceof Set) {
1292
+ const setOut = /* @__PURE__ */ new Set();
1293
+ seen.set(value, setOut);
1294
+ let changed = false;
1295
+ let index = 0;
1296
+ for (const item of value) {
1297
+ const child = guardValue(item, `${path}[${index}]`, String(index), depth + 1, options, seen);
1298
+ setOut.add(child.value);
1299
+ changed ||= child.changed;
1300
+ index += 1;
1301
+ }
1302
+ return {
1303
+ value: changed ? setOut : value,
1304
+ changed
1305
+ };
1306
+ }
1160
1307
  if (Array.isArray(value)) {
1161
1308
  const out = [];
1162
1309
  seen.set(value, out);
@@ -1176,7 +1323,7 @@ function guardValue(value, path, key, depth, options, seen) {
1176
1323
  seen.set(value, out);
1177
1324
  let changed = false;
1178
1325
  for (const [childKey, childValue] of Object.entries(input)) {
1179
- const child = guardValue(childValue, path ? `${path}.${childKey}` : childKey, childKey, depth + 1, options, seen);
1326
+ const child = guardValue(childValue, dotPath(path, childKey), childKey, depth + 1, options, seen);
1180
1327
  out[childKey] = child.value;
1181
1328
  changed ||= child.changed;
1182
1329
  }