Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

docs(logger): state that a Map takes three entries a pair

Yudhi Armyndharis8f4001c597b5
diff --git a/docs/10-devops-infrastructure.md b/docs/10-devops-infrastructure.md
index 11ab90dab..b929ded08 100644
--- a/docs/10-devops-infrastructure.md
+++ b/docs/10-devops-infrastructure.md
@@ -1107,8 +1107,10 @@ replaced with `[REDACTED]` before the line is written, at any depth. A log call
metadata: an object that contains itself is written with `[Circular]` in its place, and below the fifth
level at most 1000 more objects and arrays are copied, the rest written as `[Truncated]`. So is an array,
Map, Set or object with more entries than that allowance has left (a list of 1000 numbers at the sixth
-level), and, at any level, an array, Map or Set of more than a million entries. At every level one call
-copies at most two million entries in all (the keys of its objects plus the lengths of its arrays) and writes at
+level), and, at any level, an array, Map or Set of more than a million entries. A Map is counted as the arrays
+it is written as: its list of pairs, and each pair an array of two, which below the fifth level is one more of
+those 1000. At every level one call copies at most two million entries in all (the keys of its objects plus the
+lengths of its arrays, so three to a Map's pair: one of more than 666,666 pairs loses its last pairs) and writes at
most 2^26 (about 67 million) characters of string values, key names and BigInt text (sixteen to the digit); a
value referenced again is counted again at each use, and what no longer fits is written as `[Truncated]`.
A value that keeps its data outside its own properties is written readably, at any depth: an Error as its
diff --git a/src/common/services/logger.service.spec.ts b/src/common/services/logger.service.spec.ts
index d49946abd..e0593f368 100644
--- a/src/common/services/logger.service.spec.ts
+++ b/src/common/services/logger.service.spec.ts
@@ -1093,9 +1093,13 @@ describe('redactSecrets', () => {
} finally {
read.mockRestore();
}
- expect(
- (redactSecrets(fourth(entries(999))) as { a: { b: { c: { d: { list: unknown[] } } } } }).a.b.c.d.list,
- ).toHaveLength(999);
+ // Below the fifth level each pair array is one more node, and needs room for its two entries in what is left:
+ // the Map of 999 that fits takes 999 more nodes, so its last two pairs are written as [Truncated].
+ const pairs = Array.from({ length: 997 }, (_, key) => [key, 1]);
+ expect(redactSecrets(fourth(entries(999)))).toEqual(fourth([...pairs, '[Truncated]', '[Truncated]']));
+ expect(redactSecrets(fourth(new Set(Array.from({ length: 999 }, (_, key) => key))))).toEqual(
+ fourth(Array.from({ length: 999 }, (_, key) => key)),
+ );
expect(redactSecrets(fourth(new Set(Array.from({ length: 1000 }, (_, key) => key))))).toEqual(
fourth('[Truncated]'),
);
@@ -1411,6 +1415,18 @@ describe('redactSecrets', () => {
expect(shown(over.b)).toBe('[Truncated]');
});
+ // A Map is written as the array of its pairs: its list takes one entry a pair and each pair array two more.
+ it('charges a Map three entries a pair and writes a pair that does not fit as [Truncated]', () => {
+ const pairs = (count: number): number[][] => Array.from({ length: count }, (_, key) => [key, key]);
+ const m = new Map(pairs(10) as [number, number][]);
+ // The top object has 3 keys and `a` takes a million, so `b` leaves `entries` for the Map.
+ const withRoom = (entries: number): unknown =>
+ (redactSecrets({ a: sparse(1_000_000), b: sparse(999_997 - entries), m }) as { m: unknown }).m;
+
+ expect(withRoom(30)).toEqual(pairs(10));
+ expect(withRoom(29)).toEqual([...pairs(9), '[Truncated]']);
+ });
+
it('still copies an empty object and an empty array once the entries are spent, and not an object with a key', () => {
const spent = redactSecrets({ a: zeros(1_000_000), b: zeros(999_995), c: {}, d: [], e: { id: 1 } }) as Record<
string,
diff --git a/src/common/services/logger.service.ts b/src/common/services/logger.service.ts
index fc72c0a30..22507ad01 100644
--- a/src/common/services/logger.service.ts
+++ b/src/common/services/logger.service.ts
@@ -42,8 +42,10 @@ const REDACT_DEEP_NODES = 1000;
const REDACT_MAX_ARRAY_LENGTH = 1_000_000;
// One copy takes at most this many entries at every level: the keys of the objects and the length of the arrays it
// copies, a shared one charged again at every use, because every use is copied and written out again. It is twice the
-// array limit, so a structure of that size is written whole; an object or array that does not fit in what is left of
-// it is written as [Truncated], which is what keeps a few shared references from expanding into an unbounded copy.
+// array limit, so an array, Set or object of that size is written whole; a Map is charged as the arrays it is written
+// as, its list and a pair of two each, so three entries a pair, and one of more than 666,666 pairs is not. An object or
+// array that does not fit in what is left of it is written as [Truncated], which is what keeps a few shared references
+// from expanding into an unbounded copy.
const REDACT_MAX_ENTRIES = 2_000_000;
// A string is written whole and is not copied, so a long one referenced many times, or a long key name of a shared
// object, expands past what the entries count. One copy writes at most this many characters of string values, BigInt