Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

docs(plugins): say what the relay's size count includes

Yudhi Armyndharis10e9fa99ad74
diff --git a/src/core/plugins/plugin-sandbox-bridge.ts b/src/core/plugins/plugin-sandbox-bridge.ts
index c787402d3..6216d53d0 100644
--- a/src/core/plugins/plugin-sandbox-bridge.ts
+++ b/src/core/plugins/plugin-sandbox-bridge.ts
@@ -590,9 +590,10 @@ export class PluginSandboxBridge {
let boundedMeta = meta;
if (meta !== undefined) {
const reason = typeof meta?.error === 'string' ? { error: truncate(meta.error) } : undefined;
- // More values than the size limit write a longer text, so the text is only built, to learn its length, for a
- // meta with fewer: building it for any other could take seconds (a shared reference is written out at every
- // use) or exhaust the heap (a typed array lists a key per element).
+ // A count past the size limit (values, string characters and property name characters) marks a meta that is
+ // replaced without being written out. The text is only built, to learn its length, for a meta under the
+ // limit: building it for any other could take seconds (a shared reference is written out at every use) or
+ // exhaust the heap (a typed array lists a key per element).
if (countMetaValues(meta, SANDBOX_LOG_MAX_MESSAGE_LENGTH) > SANDBOX_LOG_MAX_MESSAGE_LENGTH) {
boundedMeta = { ...reason, metaTruncated: true };
} else {
diff --git a/src/core/plugins/sandbox-log-meta.ts b/src/core/plugins/sandbox-log-meta.ts
index 9d0072b4a..344917435 100644
--- a/src/core/plugins/sandbox-log-meta.ts
+++ b/src/core/plugins/sandbox-log-meta.ts
@@ -1,13 +1,17 @@
/**
- * Counts the values JSON.stringify would write for a worker's log metadata, and stops once the count passes `limit`.
+ * Counts what JSON.stringify would write for a worker's log metadata, and stops once the count passes `limit`: one
+ * for every value, one per character of a string, a String wrapper or a typed array element, and one per character
+ * of a property name.
*
* JSON.stringify cannot be stopped part way: it lists every key of an object or typed array before it calls a
* replacer for the first one, and it writes a shared reference out again at every use. So the size of the text has
- * to be bounded before it runs. A shared reference is counted at every use, a string, a typed array and a String
- * wrapper by their length, and a property name by its length (it is written at every use of its object too). The
- * count errs toward more: a property that JSON.stringify skips (undefined, a function, a symbol) and the entries of
- * a Map or Set (written as {}) are counted too. Every value that is written costs at least one character, so a
- * count past `limit` means a text longer than `limit`.
+ * to be bounded before it runs. A shared reference is counted at every use, and so is a property name, which is
+ * written at every use of its object.
+ *
+ * The count can be higher than the length of the text. A property that JSON.stringify skips (undefined, a function,
+ * a symbol) and the entries of a Map or Set (written as {}) are counted as if they were written. A metadata whose
+ * count passes `limit` is replaced by the size marker whatever its text would measure; for data that is plain JSON
+ * the count never exceeds the length of the text, so a count past `limit` means a longer text.
*/
export function countMetaValues(meta: unknown, limit: number): number {
let count = 0;