Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): count a string by its length in the relay
Yudhi Armyndharis8a9af75d4dc1
diff --git a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts b/src/core/plugins/plugin-loader-sandbox-routing.spec.tsindex 7ced95141..1600e7794 100644--- a/src/core/plugins/plugin-loader-sandbox-routing.spec.ts+++ b/src/core/plugins/plugin-loader-sandbox-routing.spec.ts@@ -734,11 +734,7 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {loader.capturedOnLog!('log', 'big', { data: 'x'.repeat(20000) });loader.capturedOnLog!('log', 'small', { n: 1 });- expect(logSpy).toHaveBeenNthCalledWith(1, '[p1] big', {- metaTruncated: true,- metaLength: 20011,- pluginId: 'p1',- });+ expect(logSpy).toHaveBeenNthCalledWith(1, '[p1] big', { metaTruncated: true, pluginId: 'p1' });expect(logSpy).toHaveBeenNthCalledWith(2, '[p1] small', { n: 1, pluginId: 'p1' });});@@ -759,7 +755,6 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {expect(errorSpy).toHaveBeenNthCalledWith(1, '[p1] big', undefined, {error: 'boom',metaTruncated: true,- metaLength: 20026,pluginId: 'p1',});expect(errorSpy).toHaveBeenNthCalledWith(2, '[p1] bigint', undefined, { error: 'boom', pluginId: 'p1' });@@ -859,6 +854,20 @@ describe('PluginLoaderService — sandbox log relay bounds', () => {expect(logSpy).toHaveBeenCalledWith('[p1] boxed', { metaTruncated: true, pluginId: 'p1' });});+ it('replaces one long string shared by 2000 entries by the size marker at once', async () => {+ const loader = makeLoader();+ seed(loader, { builtIn: false, instance: null });+ await loader.enablePlugin('p1');+ const logSpy = jest.spyOn(loggerOf(loader), 'log').mockImplementation(() => undefined);+ const holder = { s: 'x'.repeat(500_000) };++ const started = Date.now();+ loader.capturedOnLog!('log', 'shared-string', { list: Array.from({ length: 2000 }, () => holder) });++ expect(Date.now() - started).toBeLessThan(1000);+ expect(logSpy).toHaveBeenCalledWith('[p1] shared-string', { metaTruncated: true, pluginId: 'p1' });+ });+it('counts a Map or a Set by its size', async () => {const loader = makeLoader();seed(loader, { builtIn: false, instance: null });diff --git a/src/core/plugins/sandbox-log-meta.spec.ts b/src/core/plugins/sandbox-log-meta.spec.tsindex 072fda694..81968cebe 100644--- a/src/core/plugins/sandbox-log-meta.spec.ts+++ b/src/core/plugins/sandbox-log-meta.spec.ts@@ -11,9 +11,25 @@ describe('countMetaValues', () => {};it('counts every value, containers included', () => {- expect(countMetaValues('text', NO_LIMIT)).toBe(1);expect(countMetaValues(null, NO_LIMIT)).toBe(1);- expect(countMetaValues({ a: 1, b: [1, 2], c: { d: 'x' } }, NO_LIMIT)).toBe(1 + 1 + 1 + 2 + 1 + 1);+ expect(countMetaValues({ a: 1, b: [1, 2], c: { d: true } }, NO_LIMIT)).toBe(1 + 1 + 1 + 2 + 1 + 1);+ });++ // Each character of a string is written, and a string shared by several references is written at every use.+ it('counts a string by its length, a shared one at every use', () => {+ expect(countMetaValues('text', NO_LIMIT)).toBe(1 + 4);+ expect(countMetaValues('', NO_LIMIT)).toBe(1);+ expect(countMetaValues({ d: 'x'.repeat(100) }, NO_LIMIT)).toBe(1 + 1 + 100);+ expect(countMetaValues(['abc', 'abc'], NO_LIMIT)).toBe(1 + 2 * (1 + 3));+ const small = { s: 'x'.repeat(10) };+ expect(countMetaValues({ list: [small, small] }, NO_LIMIT)).toBe(1 + 1 + 2 * (1 + 1 + 10));++ const holder = { s: 'x'.repeat(500_000) };+ const started = Date.now();+ const count = countMetaValues({ list: Array.from({ length: 2000 }, () => holder) }, 8192);++ expect(Date.now() - started).toBeLessThan(1000);+ expect(count).toBeGreaterThan(8192);});it('counts a shared reference at every use', () => {diff --git a/src/core/plugins/sandbox-log-meta.ts b/src/core/plugins/sandbox-log-meta.tsindex 67d51f56e..cb1a2dad1 100644--- a/src/core/plugins/sandbox-log-meta.ts+++ b/src/core/plugins/sandbox-log-meta.ts@@ -3,8 +3,8 @@** 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 typed array and a String wrapper by- * their length. The count errs toward more: a property that JSON.stringify skips (undefined, a function, a symbol)+ * 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. 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`.*/@@ -14,6 +14,7 @@ export function countMetaValues(meta: unknown, limit: number): number {const frames: Array<{ node: object; items: ArrayLike<unknown>; next: number }> = [];const visit = (value: unknown): void => {count++;+ if (typeof value === 'string') count += value.length;if (typeof value !== 'object' || value === null) return;if (ArrayBuffer.isView(value)) {count += (value as { length?: number }).length ?? 0;