Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep the pasted number reader cheap on ASCII lists
Yudhi Armyndharis0c46156ed7f9
diff --git a/dashboard/src/pages/MessageTester.test.ts b/dashboard/src/pages/MessageTester.test.tsindex da6e37ace..aa1bd832a 100644--- a/dashboard/src/pages/MessageTester.test.ts+++ b/dashboard/src/pages/MessageTester.test.ts@@ -355,6 +355,36 @@ test('a 502 from a reverse proxy stops the run, since the gateway behind it is dassert.deepEqual(gateway.textSends, ['[email protected]']);});+// The list is read from a textarea that holds up to 2 MiB, so it is parsed when the list changes, not on every render.+test('the recipient list is parsed when it changes, not on every other keystroke', async () => {+ stubGroupGateway([]);+ window.sessionStorage.setItem('openwa_user_role', 'admin');+ const client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 1_000 } } });+ const { container } = rtl.render(+ createElement(QueryClientProvider, { client }, createElement(RoleProvider, null, createElement(MessageTester))),+ );+ await rtl.screen.findByRole('option', { name: /Main/ });+ rtl.fireEvent.click(rtl.screen.getByRole('button', { name: 'Bulk' }));++ // Count the line splits of the list. Swapped by hand: the test runner does not restore a symbol-keyed method.+ const list = '628111222333\n628222333444';+ let parses = 0;+ const split = RegExp.prototype[Symbol.split];+ RegExp.prototype[Symbol.split] = function (this: RegExp, text: string, limit?: number) {+ if (text === list) parses++;+ return split.call(this, text, limit);+ };+ try {+ type(container, '#mt-11', list);+ const afterList = parses;+ assert.ok(afterList >= 1, 'the list was not parsed');+ for (const text of ['a', 'ab', 'abc']) type(container, '#mt-12', text);+ assert.equal(parses, afterList);+ } finally {+ RegExp.prototype[Symbol.split] = split;+ }+});+test('an empty message or a media type with no file or URL keeps Send disabled', async () => {stubGroupGateway([{ id: '[email protected]', name: 'Family' },diff --git a/dashboard/src/pages/MessageTester.tsx b/dashboard/src/pages/MessageTester.tsxindex 58c3d8c7e..36c017a02 100644--- a/dashboard/src/pages/MessageTester.tsx+++ b/dashboard/src/pages/MessageTester.tsx@@ -1,4 +1,4 @@-import { Fragment, useState, useEffect, useRef, type ChangeEvent } from 'react';+import { Fragment, useState, useEffect, useMemo, useRef, type ChangeEvent } from 'react';import { useTranslation } from 'react-i18next';import { Send, CheckCircle, XCircle, Loader2, Upload, X, Plus, AlertCircle } from 'lucide-react';import {@@ -331,7 +331,7 @@ export function MessageTester() {};const isMediaMessageType = mediaMessageTypes.includes(messageType);- const bulkRecipientList = parseBulkRecipients(bulkRecipients);+ const bulkRecipientList = useMemo(() => parseBulkRecipients(bulkRecipients), [bulkRecipients]);const pollOptionsFilled = pollOptions.map(o => o.trim()).filter(o => o.length > 0);const lat = parseFloat(latitude);const lng = parseFloat(longitude);diff --git a/dashboard/src/utils/phone.test.ts b/dashboard/src/utils/phone.test.tsindex a43895d76..6baa8e7f9 100644--- a/dashboard/src/utils/phone.test.ts+++ b/dashboard/src/utils/phone.test.ts@@ -30,3 +30,20 @@ test('ASCII text, letters and punctuation come back unchanged', () => {assert.equal(normalizePhoneText('+62 (812) 345-6789, abc\n'), '+62 (812) 345-6789, abc\n');assert.equal(normalizePhoneText(''), '');});++// Nearly every number is plain ASCII, so it must cost no per-character work: mapping every digit made a 2 MiB+// recipient list about ten times slower to parse, and the recipient list is parsed while typing.+test('ASCII text is returned without any per-character mapping', t => {+ let calls = 0;+ const original = String.prototype.codePointAt;+ t.mock.method(String.prototype, 'codePointAt', function (this: string, index: number) {+ calls++;+ return original.call(this, index);+ });+ const text = '6281234567890\n+62 (812) 345-6789, abc\n'.repeat(50);+ assert.equal(normalizePhoneText(text), text);+ assert.equal(calls, 0);+ // Control: a native-script digit does go through the mapper.+ assert.equal(normalizePhoneText(char(0x0662)), '2');+ assert.ok(calls > 0);+});diff --git a/dashboard/src/utils/phone.ts b/dashboard/src/utils/phone.tsindex fc8f4bd80..403fc01fb 100644--- a/dashboard/src/utils/phone.ts+++ b/dashboard/src/utils/phone.ts@@ -14,12 +14,8 @@ function asciiDigit(digit: string): string {// script's decimal digits as ASCII ones. Only that block is folded: a general compatibility fold would also turn// circled and superscript numbers and fractions, which are list bullets and footnote marks, into digits.export function normalizePhoneText(text: string): string {- return [...text]- .map(ch => {- const code = ch.codePointAt(0)!;- return code >= 0xff01 && code <= 0xff5e ? String.fromCharCode(code - 0xfee0) : ch;- })- .join('')+ return text+ .replace(/[\uFF01-\uFF5E]/g, ch => String.fromCharCode(ch.charCodeAt(0) - 0xfee0)).replace(/\p{Cf}/gu, '')- .replace(/\p{Nd}/gu, asciiDigit);+ .replace(/(?![0-9])\p{Nd}/gu, asciiDigit);}