@logtape/redaction 2.1.20 → 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
package/dist/field.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_structured = require('./structured.cjs');
|
|
2
3
|
const __logtape_logtape = require_rolldown_runtime.__toESM(require("@logtape/logtape"));
|
|
3
4
|
|
|
4
5
|
//#region src/field.ts
|
|
@@ -56,15 +57,17 @@ const DEFAULT_REDACT_FIELDS = [
|
|
|
56
57
|
function redactByField(sink, options = DEFAULT_REDACT_FIELDS) {
|
|
57
58
|
const opts = Array.isArray(options) ? { fieldPatterns: options } : options;
|
|
58
59
|
const wrapped = (record) => {
|
|
59
|
-
const
|
|
60
|
+
const visited = /* @__PURE__ */ new Map();
|
|
61
|
+
const redactedProperties = redactProperties(record.properties, opts, visited);
|
|
62
|
+
const redactArgument = (value) => redactValue(value, opts, visited);
|
|
60
63
|
let redactedMessage = record.message;
|
|
61
64
|
if (typeof record.rawMessage === "string") {
|
|
62
65
|
const placeholders = extractPlaceholderNames(record.rawMessage);
|
|
63
66
|
const { redactedIndices, wildcardIndices } = getRedactedPlaceholderIndices(placeholders, opts.fieldPatterns);
|
|
64
|
-
redactedMessage = redactMessageArray(record.message, placeholders, redactedIndices, wildcardIndices, redactedProperties, opts.action);
|
|
67
|
+
redactedMessage = redactMessageArray(record.message, placeholders, redactedIndices, wildcardIndices, redactedProperties, opts.action, redactArgument);
|
|
65
68
|
} else {
|
|
66
69
|
const redactedValues = getRedactedValues(record.properties, redactedProperties);
|
|
67
|
-
|
|
70
|
+
redactedMessage = redactMessageByValues(record.message, redactedValues, redactArgument);
|
|
68
71
|
}
|
|
69
72
|
sink({
|
|
70
73
|
...record,
|
|
@@ -148,20 +151,20 @@ function redactByFieldAsync(sink, options) {
|
|
|
148
151
|
return wrapped;
|
|
149
152
|
}
|
|
150
153
|
async function redactLogRecordAsync(record, options) {
|
|
151
|
-
const
|
|
154
|
+
const visited = /* @__PURE__ */ new Map();
|
|
155
|
+
const redactedProperties = await redactPropertiesAsync(record.properties, options, visited);
|
|
156
|
+
const redactArgument = (value) => redactValueAsync(value, options, visited);
|
|
152
157
|
if (typeof record.rawMessage === "string") {
|
|
153
158
|
const placeholders = extractPlaceholderNames(record.rawMessage);
|
|
154
159
|
const { redactedIndices, wildcardIndices } = getRedactedPlaceholderIndices(placeholders, options.fieldPatterns);
|
|
155
160
|
return {
|
|
156
|
-
message: await redactMessageArrayAsync(record.message, placeholders, redactedIndices, wildcardIndices, redactedProperties, options.action),
|
|
161
|
+
message: await redactMessageArrayAsync(record.message, placeholders, redactedIndices, wildcardIndices, redactedProperties, options.action, redactArgument),
|
|
157
162
|
properties: redactedProperties
|
|
158
163
|
};
|
|
159
164
|
}
|
|
160
|
-
let redactedMessage = record.message;
|
|
161
165
|
const redactedValues = getRedactedValues(record.properties, redactedProperties);
|
|
162
|
-
if (redactedValues.size > 0) redactedMessage = redactMessageByValues(record.message, redactedValues);
|
|
163
166
|
return {
|
|
164
|
-
message:
|
|
167
|
+
message: await Promise.all(redactMessageByValues(record.message, redactedValues, redactArgument)),
|
|
165
168
|
properties: redactedProperties
|
|
166
169
|
};
|
|
167
170
|
}
|
|
@@ -225,14 +228,10 @@ function redactProperties(properties, options, visited = /* @__PURE__ */ new Map
|
|
|
225
228
|
visited.set(properties, copy);
|
|
226
229
|
for (const field of Object.keys(properties)) {
|
|
227
230
|
if (shouldFieldRedacted(field, options.fieldPatterns)) {
|
|
228
|
-
if (typeof options.action === "function")
|
|
231
|
+
if (typeof options.action === "function") require_structured.defineField(copy, field, options.action(properties[field]));
|
|
229
232
|
continue;
|
|
230
233
|
}
|
|
231
|
-
|
|
232
|
-
if (Array.isArray(value)) setProperty(copy, field, redactArray(value, options, visited));
|
|
233
|
-
else if (typeof value === "object" && value !== null) if (isBuiltInObject(value)) setProperty(copy, field, value);
|
|
234
|
-
else setProperty(copy, field, redactProperties(value, options, visited));
|
|
235
|
-
else setProperty(copy, field, value);
|
|
234
|
+
require_structured.defineField(copy, field, redactValue(properties[field], options, visited));
|
|
236
235
|
}
|
|
237
236
|
return copy;
|
|
238
237
|
}
|
|
@@ -256,67 +255,57 @@ async function redactPropertiesAsync(properties, options, visited = /* @__PURE__
|
|
|
256
255
|
values.push(Promise.resolve(options.action(properties[field])));
|
|
257
256
|
continue;
|
|
258
257
|
}
|
|
259
|
-
|
|
260
|
-
if (Array.isArray(value)) values.push(redactArrayAsync(value, options, visited));
|
|
261
|
-
else if (typeof value === "object" && value !== null) if (isBuiltInObject(value)) values.push(Promise.resolve(value));
|
|
262
|
-
else values.push(redactPropertiesAsync(value, options, visited));
|
|
263
|
-
else values.push(Promise.resolve(value));
|
|
258
|
+
values.push(redactValueAsync(properties[field], options, visited));
|
|
264
259
|
}
|
|
265
260
|
const redactedValues = await Promise.all(values);
|
|
266
|
-
for (let i = 0; i < fields.length; i++)
|
|
261
|
+
for (let i = 0; i < fields.length; i++) require_structured.defineField(copy, fields[i], redactedValues[i]);
|
|
267
262
|
return copy;
|
|
268
263
|
}
|
|
269
|
-
function
|
|
270
|
-
if (
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
* @param array The array to process.
|
|
281
|
-
* @param options The redaction options.
|
|
282
|
-
* @param visited Map of visited objects to prevent circular reference issues.
|
|
283
|
-
* @returns A new array with redacted values.
|
|
284
|
-
*/
|
|
285
|
-
function redactArray(array, options, visited) {
|
|
286
|
-
if (visited.has(array)) return visited.get(array);
|
|
287
|
-
const copy = [];
|
|
288
|
-
copy.length = array.length;
|
|
289
|
-
visited.set(array, copy);
|
|
290
|
-
for (let i = 0; i < array.length; i++) {
|
|
291
|
-
if (!(i in array)) continue;
|
|
292
|
-
const item = array[i];
|
|
293
|
-
if (Array.isArray(item)) copy[i] = redactArray(item, options, visited);
|
|
294
|
-
else if (typeof item === "object" && item !== null) if (isBuiltInObject(item)) copy[i] = item;
|
|
295
|
-
else copy[i] = redactProperties(item, options, visited);
|
|
296
|
-
else copy[i] = item;
|
|
264
|
+
function redactValue(value, options, visited) {
|
|
265
|
+
if (typeof value !== "object" || value == null) return value;
|
|
266
|
+
if (visited.has(value)) return visited.get(value);
|
|
267
|
+
const transform = (item) => redactValue(item, options, visited);
|
|
268
|
+
if (Array.isArray(value)) {
|
|
269
|
+
const copy = new Array(value.length);
|
|
270
|
+
visited.set(value, copy);
|
|
271
|
+
Array.prototype.forEach.call(value, (item, index) => {
|
|
272
|
+
copy[index] = transform(item);
|
|
273
|
+
});
|
|
274
|
+
return copy;
|
|
297
275
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const itemPromises = [];
|
|
306
|
-
for (let i = 0; i < array.length; i++) {
|
|
307
|
-
if (!(i in array)) {
|
|
308
|
-
itemPromises.push(Promise.resolve(void 0));
|
|
309
|
-
continue;
|
|
276
|
+
const structured = require_structured.copyStructured(value, visited, transform, (key, item) => {
|
|
277
|
+
if (!shouldFieldRedacted(key, options.fieldPatterns)) return transform(item);
|
|
278
|
+
if (typeof options.action !== "function") return require_structured.omit;
|
|
279
|
+
try {
|
|
280
|
+
return options.action(item);
|
|
281
|
+
} catch {
|
|
282
|
+
return require_structured.omit;
|
|
310
283
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
284
|
+
}, false);
|
|
285
|
+
if (structured != null) return structured;
|
|
286
|
+
if (isBuiltInObject(value)) return value;
|
|
287
|
+
return redactProperties(value, options, visited);
|
|
288
|
+
}
|
|
289
|
+
async function redactValueAsync(value, options, visited) {
|
|
290
|
+
if (typeof value !== "object" || value == null) return value;
|
|
291
|
+
if (visited.has(value)) return visited.get(value);
|
|
292
|
+
const transform = (item) => redactValueAsync(item, options, visited);
|
|
293
|
+
if (Array.isArray(value)) {
|
|
294
|
+
const copy = new Array(value.length);
|
|
295
|
+
const pending = [];
|
|
296
|
+
visited.set(value, copy);
|
|
297
|
+
Array.prototype.forEach.call(value, (item, index) => {
|
|
298
|
+
pending.push(transform(item).then((member) => {
|
|
299
|
+
copy[index] = member;
|
|
300
|
+
}));
|
|
301
|
+
});
|
|
302
|
+
await Promise.all(pending);
|
|
303
|
+
return copy;
|
|
316
304
|
}
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
return
|
|
305
|
+
const structured = require_structured.copyStructuredAsync(value, visited, transform, async (key, item) => shouldFieldRedacted(key, options.fieldPatterns) ? await options.action(item) : await transform(item));
|
|
306
|
+
if (structured != null) return structured;
|
|
307
|
+
if (isBuiltInObject(value)) return value;
|
|
308
|
+
return redactPropertiesAsync(value, options, visited);
|
|
320
309
|
}
|
|
321
310
|
/**
|
|
322
311
|
* Checks if a value is a built-in object that should not be recursively
|
|
@@ -325,7 +314,7 @@ async function redactArrayAsync(array, options, visited) {
|
|
|
325
314
|
* @returns `true` if the value is a built-in object, `false` otherwise.
|
|
326
315
|
*/
|
|
327
316
|
function isBuiltInObject(value) {
|
|
328
|
-
return value instanceof
|
|
317
|
+
return value instanceof Date || value instanceof RegExp || value instanceof WeakMap || value instanceof WeakSet || value instanceof Promise || value instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer || ArrayBuffer.isView(value);
|
|
329
318
|
}
|
|
330
319
|
/**
|
|
331
320
|
* Checks if a field should be redacted based on the provided field patterns.
|
|
@@ -464,9 +453,10 @@ function getRedactedPlaceholderIndices(placeholders, fieldPatterns) {
|
|
|
464
453
|
* @param wildcardIndices Set of wildcard placeholder indices.
|
|
465
454
|
* @param redactedProperties The redacted properties object.
|
|
466
455
|
* @param action The redaction action.
|
|
456
|
+
* @param redactArgument Transforms an argument without a matching field.
|
|
467
457
|
* @returns New message array with redacted values.
|
|
468
458
|
*/
|
|
469
|
-
function redactMessageArray(message, placeholders, redactedIndices, wildcardIndices, redactedProperties, action) {
|
|
459
|
+
function redactMessageArray(message, placeholders, redactedIndices, wildcardIndices, redactedProperties, action, redactArgument) {
|
|
470
460
|
const result = [];
|
|
471
461
|
let placeholderIndex = 0;
|
|
472
462
|
for (let i = 0; i < message.length; i++) if (i % 2 === 0) result.push(message[i]);
|
|
@@ -475,16 +465,16 @@ function redactMessageArray(message, placeholders, redactedIndices, wildcardIndi
|
|
|
475
465
|
else if (redactedIndices.has(placeholderIndex)) if (action == null || action === "delete") result.push("");
|
|
476
466
|
else result.push(action(message[i]));
|
|
477
467
|
else {
|
|
478
|
-
const placeholderName = placeholders[placeholderIndex];
|
|
468
|
+
const placeholderName = placeholders[placeholderIndex] ?? "";
|
|
479
469
|
const redactedValue = getPathValue(redactedProperties, placeholderName);
|
|
480
470
|
if (redactedValue.found) result.push(redactedValue.value);
|
|
481
|
-
else result.push(message[i]);
|
|
471
|
+
else result.push(redactArgument(message[i]));
|
|
482
472
|
}
|
|
483
473
|
placeholderIndex++;
|
|
484
474
|
}
|
|
485
475
|
return result;
|
|
486
476
|
}
|
|
487
|
-
async function redactMessageArrayAsync(message, placeholders, redactedIndices, wildcardIndices, redactedProperties, action) {
|
|
477
|
+
async function redactMessageArrayAsync(message, placeholders, redactedIndices, wildcardIndices, redactedProperties, action, redactArgument) {
|
|
488
478
|
const result = [];
|
|
489
479
|
const tasks = [];
|
|
490
480
|
let placeholderIndex = 0;
|
|
@@ -498,10 +488,16 @@ async function redactMessageArrayAsync(message, placeholders, redactedIndices, w
|
|
|
498
488
|
result[index] = redacted;
|
|
499
489
|
}));
|
|
500
490
|
} else {
|
|
501
|
-
const placeholderName = placeholders[placeholderIndex];
|
|
491
|
+
const placeholderName = placeholders[placeholderIndex] ?? "";
|
|
502
492
|
const redactedValue = getPathValue(redactedProperties, placeholderName);
|
|
503
493
|
if (redactedValue.found) result.push(redactedValue.value);
|
|
504
|
-
else
|
|
494
|
+
else {
|
|
495
|
+
const index = result.length;
|
|
496
|
+
result.push(void 0);
|
|
497
|
+
tasks.push(redactArgument(message[i]).then((redacted) => {
|
|
498
|
+
result[index] = redacted;
|
|
499
|
+
}));
|
|
500
|
+
}
|
|
505
501
|
}
|
|
506
502
|
placeholderIndex++;
|
|
507
503
|
}
|
|
@@ -527,13 +523,16 @@ function getPathValue(properties, path) {
|
|
|
527
523
|
* @param redacted The redacted properties.
|
|
528
524
|
* @param map The map to populate with original -> redacted value pairs.
|
|
529
525
|
*/
|
|
530
|
-
function collectRedactedValues(original, redacted, map) {
|
|
526
|
+
function collectRedactedValues(original, redacted, map, ancestors) {
|
|
527
|
+
if (ancestors.has(original)) return;
|
|
528
|
+
ancestors.add(original);
|
|
531
529
|
for (const key of Object.keys(original)) {
|
|
532
530
|
const origVal = original[key];
|
|
533
|
-
const redVal = redacted[key];
|
|
531
|
+
const redVal = Object.hasOwn(redacted, key) ? redacted[key] : void 0;
|
|
534
532
|
if (origVal !== redVal) map.set(origVal, redVal);
|
|
535
|
-
if (typeof origVal === "object" && origVal !== null && typeof redVal === "object" && redVal !== null && !Array.isArray(origVal)) collectRedactedValues(origVal, redVal, map);
|
|
533
|
+
if (typeof origVal === "object" && origVal !== null && typeof redVal === "object" && redVal !== null && !Array.isArray(origVal) && Object.getPrototypeOf(redVal) === Object.prototype) collectRedactedValues(origVal, redVal, map, ancestors);
|
|
536
534
|
}
|
|
535
|
+
ancestors.delete(original);
|
|
537
536
|
}
|
|
538
537
|
/**
|
|
539
538
|
* Gets a map of original values to their redacted replacements.
|
|
@@ -543,7 +542,7 @@ function collectRedactedValues(original, redacted, map) {
|
|
|
543
542
|
*/
|
|
544
543
|
function getRedactedValues(original, redacted) {
|
|
545
544
|
const map = /* @__PURE__ */ new Map();
|
|
546
|
-
collectRedactedValues(original, redacted, map);
|
|
545
|
+
collectRedactedValues(original, redacted, map, /* @__PURE__ */ new Set());
|
|
547
546
|
return map;
|
|
548
547
|
}
|
|
549
548
|
/**
|
|
@@ -551,16 +550,16 @@ function getRedactedValues(original, redacted) {
|
|
|
551
550
|
* Used for tagged template literals where placeholder names are not available.
|
|
552
551
|
* @param message The original message array.
|
|
553
552
|
* @param redactedValues Map of original -> redacted values.
|
|
553
|
+
* @param redactArgument Transforms an argument without a matching field.
|
|
554
554
|
* @returns New message array with redacted values.
|
|
555
555
|
*/
|
|
556
|
-
function redactMessageByValues(message, redactedValues) {
|
|
557
|
-
if (redactedValues.size === 0) return message;
|
|
556
|
+
function redactMessageByValues(message, redactedValues, redactArgument) {
|
|
558
557
|
const result = [];
|
|
559
558
|
for (let i = 0; i < message.length; i++) if (i % 2 === 0) result.push(message[i]);
|
|
560
559
|
else {
|
|
561
560
|
const val = message[i];
|
|
562
561
|
if (redactedValues.has(val)) result.push(redactedValues.get(val));
|
|
563
|
-
else result.push(val);
|
|
562
|
+
else result.push(redactArgument(val));
|
|
564
563
|
}
|
|
565
564
|
return result;
|
|
566
565
|
}
|
package/dist/field.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field.d.cts","names":[],"sources":["../src/field.ts"],"sourcesContent":[],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"field.d.cts","names":[],"sources":["../src/field.ts"],"sourcesContent":[],"mappings":";;;;;;AAmBA;AAOA;AAMA;AAMY,KAnBA,YAAA,GAmBA,MAAyB,GAnBD,MAmBC;AAQrC;AAWA;AAqBA;;;AAmBoB,KAvER,aAAA,GAAgB,YAuER,EAAA;AAAoB;AAQxC;;;AAWmB,KApFP,oBAAA,GAoFO,QAAA,GAAA,CAAA,CAAA,KAAA,EAAA,OAAA,EAAA,GAAA,OAAA,CAAA;AAAyB;AAO5C;;;AAIsC,KAzF1B,yBAAA,GAyF0B,CAAA,KAAA,EAAA,OAAA,EAAA,GAvFjC,WAuFiC,CAAA,OAAA,CAAA;;AAAuB;AAgD7D;;AACQ,KAlII,iBAAA,GAkIJ,CAAA,KAAA,EAAA,OAAA,EAAA,GAlI4C,OAkI5C,CAAA,MAAA,CAAA;;;;;;;AAEL,cAzHU,qBAyHV,EAzHiC,aAyHjC;;;;;AAAiD;AAoFpC,UAxLC,qBAAA,CAwLiB;EAAA;;;;;;EACuB,SAC9C,aAAA,EAnLe,aAmLf;EAA0B;;AACZ;AAqIzB;;;;;AAEU;;oBA/SU;;;;;;;UAQH,0BAAA;;;;;;0BAMS;;;;mBAKP;;;;;;UAOF,wBAAA;;;;yBAIQ,aAAa,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDpC,aAAA,OACR,OAAO,OAAO,aAAa,OAAO,2BAC/B,wBAAwB,gBAChC,OAAO,OAAO,aAAa,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoFrB,kBAAA,OACR,OAAO,OAAO,aAAa,OAAO,0BAC/B,6BACR,OAAO;;;;;;;;;;;;;iBAqIY,uBAAA,UACX,2BACR,QAAQ"}
|
package/dist/field.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"field.d.ts","names":[],"sources":["../src/field.ts"],"sourcesContent":[],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"field.d.ts","names":[],"sources":["../src/field.ts"],"sourcesContent":[],"mappings":";;;;;;AAmBA;AAOA;AAMA;AAMY,KAnBA,YAAA,GAmBA,MAAyB,GAnBD,MAmBC;AAQrC;AAWA;AAqBA;;;AAmBoB,KAvER,aAAA,GAAgB,YAuER,EAAA;AAAoB;AAQxC;;;AAWmB,KApFP,oBAAA,GAoFO,QAAA,GAAA,CAAA,CAAA,KAAA,EAAA,OAAA,EAAA,GAAA,OAAA,CAAA;AAAyB;AAO5C;;;AAIsC,KAzF1B,yBAAA,GAyF0B,CAAA,KAAA,EAAA,OAAA,EAAA,GAvFjC,WAuFiC,CAAA,OAAA,CAAA;;AAAuB;AAgD7D;;AACQ,KAlII,iBAAA,GAkIJ,CAAA,KAAA,EAAA,OAAA,EAAA,GAlI4C,OAkI5C,CAAA,MAAA,CAAA;;;;;;;AAEL,cAzHU,qBAyHV,EAzHiC,aAyHjC;;;;;AAAiD;AAoFpC,UAxLC,qBAAA,CAwLiB;EAAA;;;;;;EACuB,SAC9C,aAAA,EAnLe,aAmLf;EAA0B;;AACZ;AAqIzB;;;;;AAEU;;oBA/SU;;;;;;;UAQH,0BAAA;;;;;;0BAMS;;;;mBAKP;;;;;;UAOF,wBAAA;;;;yBAIQ,aAAa,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDpC,aAAA,OACR,OAAO,OAAO,aAAa,OAAO,2BAC/B,wBAAwB,gBAChC,OAAO,OAAO,aAAa,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoFrB,kBAAA,OACR,OAAO,OAAO,aAAa,OAAO,0BAC/B,6BACR,OAAO;;;;;;;;;;;;;iBAqIY,uBAAA,UACX,2BACR,QAAQ"}
|
package/dist/field.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { copyStructured, copyStructuredAsync, defineField, omit } from "./structured.js";
|
|
1
2
|
import { getLogger } from "@logtape/logtape";
|
|
2
3
|
|
|
3
4
|
//#region src/field.ts
|
|
@@ -55,15 +56,17 @@ const DEFAULT_REDACT_FIELDS = [
|
|
|
55
56
|
function redactByField(sink, options = DEFAULT_REDACT_FIELDS) {
|
|
56
57
|
const opts = Array.isArray(options) ? { fieldPatterns: options } : options;
|
|
57
58
|
const wrapped = (record) => {
|
|
58
|
-
const
|
|
59
|
+
const visited = /* @__PURE__ */ new Map();
|
|
60
|
+
const redactedProperties = redactProperties(record.properties, opts, visited);
|
|
61
|
+
const redactArgument = (value) => redactValue(value, opts, visited);
|
|
59
62
|
let redactedMessage = record.message;
|
|
60
63
|
if (typeof record.rawMessage === "string") {
|
|
61
64
|
const placeholders = extractPlaceholderNames(record.rawMessage);
|
|
62
65
|
const { redactedIndices, wildcardIndices } = getRedactedPlaceholderIndices(placeholders, opts.fieldPatterns);
|
|
63
|
-
redactedMessage = redactMessageArray(record.message, placeholders, redactedIndices, wildcardIndices, redactedProperties, opts.action);
|
|
66
|
+
redactedMessage = redactMessageArray(record.message, placeholders, redactedIndices, wildcardIndices, redactedProperties, opts.action, redactArgument);
|
|
64
67
|
} else {
|
|
65
68
|
const redactedValues = getRedactedValues(record.properties, redactedProperties);
|
|
66
|
-
|
|
69
|
+
redactedMessage = redactMessageByValues(record.message, redactedValues, redactArgument);
|
|
67
70
|
}
|
|
68
71
|
sink({
|
|
69
72
|
...record,
|
|
@@ -147,20 +150,20 @@ function redactByFieldAsync(sink, options) {
|
|
|
147
150
|
return wrapped;
|
|
148
151
|
}
|
|
149
152
|
async function redactLogRecordAsync(record, options) {
|
|
150
|
-
const
|
|
153
|
+
const visited = /* @__PURE__ */ new Map();
|
|
154
|
+
const redactedProperties = await redactPropertiesAsync(record.properties, options, visited);
|
|
155
|
+
const redactArgument = (value) => redactValueAsync(value, options, visited);
|
|
151
156
|
if (typeof record.rawMessage === "string") {
|
|
152
157
|
const placeholders = extractPlaceholderNames(record.rawMessage);
|
|
153
158
|
const { redactedIndices, wildcardIndices } = getRedactedPlaceholderIndices(placeholders, options.fieldPatterns);
|
|
154
159
|
return {
|
|
155
|
-
message: await redactMessageArrayAsync(record.message, placeholders, redactedIndices, wildcardIndices, redactedProperties, options.action),
|
|
160
|
+
message: await redactMessageArrayAsync(record.message, placeholders, redactedIndices, wildcardIndices, redactedProperties, options.action, redactArgument),
|
|
156
161
|
properties: redactedProperties
|
|
157
162
|
};
|
|
158
163
|
}
|
|
159
|
-
let redactedMessage = record.message;
|
|
160
164
|
const redactedValues = getRedactedValues(record.properties, redactedProperties);
|
|
161
|
-
if (redactedValues.size > 0) redactedMessage = redactMessageByValues(record.message, redactedValues);
|
|
162
165
|
return {
|
|
163
|
-
message:
|
|
166
|
+
message: await Promise.all(redactMessageByValues(record.message, redactedValues, redactArgument)),
|
|
164
167
|
properties: redactedProperties
|
|
165
168
|
};
|
|
166
169
|
}
|
|
@@ -224,14 +227,10 @@ function redactProperties(properties, options, visited = /* @__PURE__ */ new Map
|
|
|
224
227
|
visited.set(properties, copy);
|
|
225
228
|
for (const field of Object.keys(properties)) {
|
|
226
229
|
if (shouldFieldRedacted(field, options.fieldPatterns)) {
|
|
227
|
-
if (typeof options.action === "function")
|
|
230
|
+
if (typeof options.action === "function") defineField(copy, field, options.action(properties[field]));
|
|
228
231
|
continue;
|
|
229
232
|
}
|
|
230
|
-
|
|
231
|
-
if (Array.isArray(value)) setProperty(copy, field, redactArray(value, options, visited));
|
|
232
|
-
else if (typeof value === "object" && value !== null) if (isBuiltInObject(value)) setProperty(copy, field, value);
|
|
233
|
-
else setProperty(copy, field, redactProperties(value, options, visited));
|
|
234
|
-
else setProperty(copy, field, value);
|
|
233
|
+
defineField(copy, field, redactValue(properties[field], options, visited));
|
|
235
234
|
}
|
|
236
235
|
return copy;
|
|
237
236
|
}
|
|
@@ -255,67 +254,57 @@ async function redactPropertiesAsync(properties, options, visited = /* @__PURE__
|
|
|
255
254
|
values.push(Promise.resolve(options.action(properties[field])));
|
|
256
255
|
continue;
|
|
257
256
|
}
|
|
258
|
-
|
|
259
|
-
if (Array.isArray(value)) values.push(redactArrayAsync(value, options, visited));
|
|
260
|
-
else if (typeof value === "object" && value !== null) if (isBuiltInObject(value)) values.push(Promise.resolve(value));
|
|
261
|
-
else values.push(redactPropertiesAsync(value, options, visited));
|
|
262
|
-
else values.push(Promise.resolve(value));
|
|
257
|
+
values.push(redactValueAsync(properties[field], options, visited));
|
|
263
258
|
}
|
|
264
259
|
const redactedValues = await Promise.all(values);
|
|
265
|
-
for (let i = 0; i < fields.length; i++)
|
|
260
|
+
for (let i = 0; i < fields.length; i++) defineField(copy, fields[i], redactedValues[i]);
|
|
266
261
|
return copy;
|
|
267
262
|
}
|
|
268
|
-
function
|
|
269
|
-
if (
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
* @param array The array to process.
|
|
280
|
-
* @param options The redaction options.
|
|
281
|
-
* @param visited Map of visited objects to prevent circular reference issues.
|
|
282
|
-
* @returns A new array with redacted values.
|
|
283
|
-
*/
|
|
284
|
-
function redactArray(array, options, visited) {
|
|
285
|
-
if (visited.has(array)) return visited.get(array);
|
|
286
|
-
const copy = [];
|
|
287
|
-
copy.length = array.length;
|
|
288
|
-
visited.set(array, copy);
|
|
289
|
-
for (let i = 0; i < array.length; i++) {
|
|
290
|
-
if (!(i in array)) continue;
|
|
291
|
-
const item = array[i];
|
|
292
|
-
if (Array.isArray(item)) copy[i] = redactArray(item, options, visited);
|
|
293
|
-
else if (typeof item === "object" && item !== null) if (isBuiltInObject(item)) copy[i] = item;
|
|
294
|
-
else copy[i] = redactProperties(item, options, visited);
|
|
295
|
-
else copy[i] = item;
|
|
263
|
+
function redactValue(value, options, visited) {
|
|
264
|
+
if (typeof value !== "object" || value == null) return value;
|
|
265
|
+
if (visited.has(value)) return visited.get(value);
|
|
266
|
+
const transform = (item) => redactValue(item, options, visited);
|
|
267
|
+
if (Array.isArray(value)) {
|
|
268
|
+
const copy = new Array(value.length);
|
|
269
|
+
visited.set(value, copy);
|
|
270
|
+
Array.prototype.forEach.call(value, (item, index) => {
|
|
271
|
+
copy[index] = transform(item);
|
|
272
|
+
});
|
|
273
|
+
return copy;
|
|
296
274
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
const itemPromises = [];
|
|
305
|
-
for (let i = 0; i < array.length; i++) {
|
|
306
|
-
if (!(i in array)) {
|
|
307
|
-
itemPromises.push(Promise.resolve(void 0));
|
|
308
|
-
continue;
|
|
275
|
+
const structured = copyStructured(value, visited, transform, (key, item) => {
|
|
276
|
+
if (!shouldFieldRedacted(key, options.fieldPatterns)) return transform(item);
|
|
277
|
+
if (typeof options.action !== "function") return omit;
|
|
278
|
+
try {
|
|
279
|
+
return options.action(item);
|
|
280
|
+
} catch {
|
|
281
|
+
return omit;
|
|
309
282
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
283
|
+
}, false);
|
|
284
|
+
if (structured != null) return structured;
|
|
285
|
+
if (isBuiltInObject(value)) return value;
|
|
286
|
+
return redactProperties(value, options, visited);
|
|
287
|
+
}
|
|
288
|
+
async function redactValueAsync(value, options, visited) {
|
|
289
|
+
if (typeof value !== "object" || value == null) return value;
|
|
290
|
+
if (visited.has(value)) return visited.get(value);
|
|
291
|
+
const transform = (item) => redactValueAsync(item, options, visited);
|
|
292
|
+
if (Array.isArray(value)) {
|
|
293
|
+
const copy = new Array(value.length);
|
|
294
|
+
const pending = [];
|
|
295
|
+
visited.set(value, copy);
|
|
296
|
+
Array.prototype.forEach.call(value, (item, index) => {
|
|
297
|
+
pending.push(transform(item).then((member) => {
|
|
298
|
+
copy[index] = member;
|
|
299
|
+
}));
|
|
300
|
+
});
|
|
301
|
+
await Promise.all(pending);
|
|
302
|
+
return copy;
|
|
315
303
|
}
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
return
|
|
304
|
+
const structured = copyStructuredAsync(value, visited, transform, async (key, item) => shouldFieldRedacted(key, options.fieldPatterns) ? await options.action(item) : await transform(item));
|
|
305
|
+
if (structured != null) return structured;
|
|
306
|
+
if (isBuiltInObject(value)) return value;
|
|
307
|
+
return redactPropertiesAsync(value, options, visited);
|
|
319
308
|
}
|
|
320
309
|
/**
|
|
321
310
|
* Checks if a value is a built-in object that should not be recursively
|
|
@@ -324,7 +313,7 @@ async function redactArrayAsync(array, options, visited) {
|
|
|
324
313
|
* @returns `true` if the value is a built-in object, `false` otherwise.
|
|
325
314
|
*/
|
|
326
315
|
function isBuiltInObject(value) {
|
|
327
|
-
return value instanceof
|
|
316
|
+
return value instanceof Date || value instanceof RegExp || value instanceof WeakMap || value instanceof WeakSet || value instanceof Promise || value instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer || ArrayBuffer.isView(value);
|
|
328
317
|
}
|
|
329
318
|
/**
|
|
330
319
|
* Checks if a field should be redacted based on the provided field patterns.
|
|
@@ -463,9 +452,10 @@ function getRedactedPlaceholderIndices(placeholders, fieldPatterns) {
|
|
|
463
452
|
* @param wildcardIndices Set of wildcard placeholder indices.
|
|
464
453
|
* @param redactedProperties The redacted properties object.
|
|
465
454
|
* @param action The redaction action.
|
|
455
|
+
* @param redactArgument Transforms an argument without a matching field.
|
|
466
456
|
* @returns New message array with redacted values.
|
|
467
457
|
*/
|
|
468
|
-
function redactMessageArray(message, placeholders, redactedIndices, wildcardIndices, redactedProperties, action) {
|
|
458
|
+
function redactMessageArray(message, placeholders, redactedIndices, wildcardIndices, redactedProperties, action, redactArgument) {
|
|
469
459
|
const result = [];
|
|
470
460
|
let placeholderIndex = 0;
|
|
471
461
|
for (let i = 0; i < message.length; i++) if (i % 2 === 0) result.push(message[i]);
|
|
@@ -474,16 +464,16 @@ function redactMessageArray(message, placeholders, redactedIndices, wildcardIndi
|
|
|
474
464
|
else if (redactedIndices.has(placeholderIndex)) if (action == null || action === "delete") result.push("");
|
|
475
465
|
else result.push(action(message[i]));
|
|
476
466
|
else {
|
|
477
|
-
const placeholderName = placeholders[placeholderIndex];
|
|
467
|
+
const placeholderName = placeholders[placeholderIndex] ?? "";
|
|
478
468
|
const redactedValue = getPathValue(redactedProperties, placeholderName);
|
|
479
469
|
if (redactedValue.found) result.push(redactedValue.value);
|
|
480
|
-
else result.push(message[i]);
|
|
470
|
+
else result.push(redactArgument(message[i]));
|
|
481
471
|
}
|
|
482
472
|
placeholderIndex++;
|
|
483
473
|
}
|
|
484
474
|
return result;
|
|
485
475
|
}
|
|
486
|
-
async function redactMessageArrayAsync(message, placeholders, redactedIndices, wildcardIndices, redactedProperties, action) {
|
|
476
|
+
async function redactMessageArrayAsync(message, placeholders, redactedIndices, wildcardIndices, redactedProperties, action, redactArgument) {
|
|
487
477
|
const result = [];
|
|
488
478
|
const tasks = [];
|
|
489
479
|
let placeholderIndex = 0;
|
|
@@ -497,10 +487,16 @@ async function redactMessageArrayAsync(message, placeholders, redactedIndices, w
|
|
|
497
487
|
result[index] = redacted;
|
|
498
488
|
}));
|
|
499
489
|
} else {
|
|
500
|
-
const placeholderName = placeholders[placeholderIndex];
|
|
490
|
+
const placeholderName = placeholders[placeholderIndex] ?? "";
|
|
501
491
|
const redactedValue = getPathValue(redactedProperties, placeholderName);
|
|
502
492
|
if (redactedValue.found) result.push(redactedValue.value);
|
|
503
|
-
else
|
|
493
|
+
else {
|
|
494
|
+
const index = result.length;
|
|
495
|
+
result.push(void 0);
|
|
496
|
+
tasks.push(redactArgument(message[i]).then((redacted) => {
|
|
497
|
+
result[index] = redacted;
|
|
498
|
+
}));
|
|
499
|
+
}
|
|
504
500
|
}
|
|
505
501
|
placeholderIndex++;
|
|
506
502
|
}
|
|
@@ -526,13 +522,16 @@ function getPathValue(properties, path) {
|
|
|
526
522
|
* @param redacted The redacted properties.
|
|
527
523
|
* @param map The map to populate with original -> redacted value pairs.
|
|
528
524
|
*/
|
|
529
|
-
function collectRedactedValues(original, redacted, map) {
|
|
525
|
+
function collectRedactedValues(original, redacted, map, ancestors) {
|
|
526
|
+
if (ancestors.has(original)) return;
|
|
527
|
+
ancestors.add(original);
|
|
530
528
|
for (const key of Object.keys(original)) {
|
|
531
529
|
const origVal = original[key];
|
|
532
|
-
const redVal = redacted[key];
|
|
530
|
+
const redVal = Object.hasOwn(redacted, key) ? redacted[key] : void 0;
|
|
533
531
|
if (origVal !== redVal) map.set(origVal, redVal);
|
|
534
|
-
if (typeof origVal === "object" && origVal !== null && typeof redVal === "object" && redVal !== null && !Array.isArray(origVal)) collectRedactedValues(origVal, redVal, map);
|
|
532
|
+
if (typeof origVal === "object" && origVal !== null && typeof redVal === "object" && redVal !== null && !Array.isArray(origVal) && Object.getPrototypeOf(redVal) === Object.prototype) collectRedactedValues(origVal, redVal, map, ancestors);
|
|
535
533
|
}
|
|
534
|
+
ancestors.delete(original);
|
|
536
535
|
}
|
|
537
536
|
/**
|
|
538
537
|
* Gets a map of original values to their redacted replacements.
|
|
@@ -542,7 +541,7 @@ function collectRedactedValues(original, redacted, map) {
|
|
|
542
541
|
*/
|
|
543
542
|
function getRedactedValues(original, redacted) {
|
|
544
543
|
const map = /* @__PURE__ */ new Map();
|
|
545
|
-
collectRedactedValues(original, redacted, map);
|
|
544
|
+
collectRedactedValues(original, redacted, map, /* @__PURE__ */ new Set());
|
|
546
545
|
return map;
|
|
547
546
|
}
|
|
548
547
|
/**
|
|
@@ -550,16 +549,16 @@ function getRedactedValues(original, redacted) {
|
|
|
550
549
|
* Used for tagged template literals where placeholder names are not available.
|
|
551
550
|
* @param message The original message array.
|
|
552
551
|
* @param redactedValues Map of original -> redacted values.
|
|
552
|
+
* @param redactArgument Transforms an argument without a matching field.
|
|
553
553
|
* @returns New message array with redacted values.
|
|
554
554
|
*/
|
|
555
|
-
function redactMessageByValues(message, redactedValues) {
|
|
556
|
-
if (redactedValues.size === 0) return message;
|
|
555
|
+
function redactMessageByValues(message, redactedValues, redactArgument) {
|
|
557
556
|
const result = [];
|
|
558
557
|
for (let i = 0; i < message.length; i++) if (i % 2 === 0) result.push(message[i]);
|
|
559
558
|
else {
|
|
560
559
|
const val = message[i];
|
|
561
560
|
if (redactedValues.has(val)) result.push(redactedValues.get(val));
|
|
562
|
-
else result.push(val);
|
|
561
|
+
else result.push(redactArgument(val));
|
|
563
562
|
}
|
|
564
563
|
return result;
|
|
565
564
|
}
|