Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): parse a body of code blocks in linear time
Yudhi Armyndharis14f8807bee4e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f212dbfca..c14b18ea3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -150,7 +150,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Show an API key's name as text in its delete and revoke confirmation, where a name with a styled tag took the dashboard to its error screen.- Keep the dashboard's QR and pairing dialog open when its status read fails transiently, instead of closing it mid-scan.- Stop a dashboard backup export or media download answered after sign-out from saving its file.-- Parse a dashboard message made of many code spans in linear time and render a body over 4096 characters without links, so one long message no longer freezes the tab for seconds.+- Parse a dashboard message made of many code spans or code blocks in linear time and render a body over 4096 characters without links, so one long message no longer freezes the tab for seconds.- Python SDK (next SDK release after 0.5.1): an error with an empty body names the response's status text instead of `None`, a `timeout` of 0 or infinity means no timeout, and a negative, NaN or non-number raises `ValueError`.- Python SDK (next SDK release after 0.5.1): `WebhookFilterCondition` and `CustomLinkPreview` require the keys the gateway requires.- JavaScript and PHP SDKs (next SDK release after 0.5.1): printing or dumping a client no longer shows its API key.diff --git a/dashboard/src/utils/messageFormatter.test.ts b/dashboard/src/utils/messageFormatter.test.tsindex cc09bf519..a785baf0f 100644--- a/dashboard/src/utils/messageFormatter.test.ts+++ b/dashboard/src/utils/messageFormatter.test.ts@@ -128,6 +128,20 @@ test('hostile input: code spans with no triple backtick after them do not rescanassert.ok(elapsed < 1000, `parsing ${evil.length} chars took ${Math.round(elapsed)} ms`);assert.ok(nodes.length > 0);}+ // Nor do code blocks: each block used to be followed by a search for a single backtick that ran past+ // every later block to the end of the body, which is quadratic in the number of blocks.+ for (const evil of [+ '```a``` '.repeat(8_000),+ '`'.repeat(64_000),+ '``` '.repeat(16_000),+ '```a``` `b` '.repeat(5_000),+ ]) {+ const started = performance.now();+ const nodes = parseMessageBody(evil);+ const elapsed = performance.now() - started;+ assert.ok(elapsed < 250, `parsing ${evil.length} chars took ${Math.round(elapsed)} ms`);+ assert.ok(nodes.length > 0);+ }// A triple backtick further on is still found once the spans in front of it are consumed.assert.deepEqual(parseMessageBody('`a` b ```c``` `d`'), [{ type: 'code', value: 'a' },diff --git a/dashboard/src/utils/messageFormatter.ts b/dashboard/src/utils/messageFormatter.tsindex 677e166e7..9e1f04ec2 100644--- a/dashboard/src/utils/messageFormatter.ts+++ b/dashboard/src/utils/messageFormatter.ts@@ -72,7 +72,9 @@ export function parseMessageBody(input: string): MessageNode[] {while (cursor < input.length) {// Look for next ``` first (longer marker wins).if (tripleStart !== -1 && tripleStart < cursor) tripleStart = input.indexOf('```', cursor);- const singleStart = findSingleBacktick(input, cursor);+ // A single backtick only wins when it comes before the triple, so the search stops there: running+ // on to the end of the body at every turn is quadratic in a message made of code blocks.+ const singleStart = findSingleBacktick(input, cursor, tripleStart === -1 ? input.length : tripleStart);let nextCode: 'triple' | 'single' | null = null;let nextIdx = Infinity;@@ -120,14 +122,14 @@ export function parseMessageBody(input: string): MessageNode[] {}/**- * Find next single-backtick that is NOT part of a triple-backtick.+ * Find next single-backtick, before `limit`, that is NOT part of a triple-backtick.* Returns -1 if none.*/-function findSingleBacktick(s: string, from: number): number {+function findSingleBacktick(s: string, from: number, limit: number): number {let i = from;- while (i < s.length) {+ while (i < limit) {const idx = s.indexOf('`', i);- if (idx === -1) return -1;+ if (idx === -1 || idx >= limit) return -1;// Skip if part of a triple-backtick sequence.if (s.slice(idx, idx + 3) === '```') {i = idx + 3;