Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): stop a wwebjs send leaving for a closing page
Yudhi Armyndharis407699ebc869
diff --git a/src/engine/adapters/wwebjs-client-teardown.spec.ts b/src/engine/adapters/wwebjs-client-teardown.spec.tsindex c554b1b15..d9081f43c 100644--- a/src/engine/adapters/wwebjs-client-teardown.spec.ts+++ b/src/engine/adapters/wwebjs-client-teardown.spec.ts@@ -1,6 +1,7 @@import { WhatsAppWebJsAdapter } from './whatsapp-web-js.adapter';import { EngineStatus } from '../interfaces/whatsapp-engine.interface';import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error';+import { RecipientUnreachableError } from '../../common/errors/recipient-unreachable.error';import { countsTowardSendBreaker, sentNothing } from '../../modules/message/send-pacing.service';import * as loadRemoteMediaModule from '../../common/media/load-remote-media';@@ -14,6 +15,7 @@ import * as loadRemoteMediaModule from '../../common/media/load-remote-media';*/const CHAT = '[email protected]';+const LID = '159442138038327@lid';const MEDIA_URL = 'https://files.example.com/a.png';interface Gate {@@ -171,6 +173,48 @@ const CASES: Case[] = [},run: adapter => adapter.getGroupInfo('[email protected]'),},+ {+ // The reply and forward legs send through the quoted Message's own client, never through the host accessor.+ name: 'a reply',+ build: parked => {+ const reply = jest.fn();+ const chat = { fetchMessages: () => Promise.resolve([{ id: { _serialized: 'Q1' }, reply }]) };+ return {+ client: { getNumberId: () => parked.park().then(() => found(CHAT)), getChatById: () => Promise.resolve(chat) },+ unreached: reply,+ };+ },+ run: adapter => adapter.replyToMessage(CHAT, 'Q1', 'hi'),+ },+ {+ name: 'a forward parked in the destination lookup',+ build: parked => {+ const forward = jest.fn();+ const chat = { fetchMessages: () => Promise.resolve([{ id: { _serialized: 'M1' }, forward }]) };+ return {+ client: { getNumberId: () => parked.park().then(() => found(CHAT)), getChatById: () => Promise.resolve(chat) },+ unreached: forward,+ };+ },+ run: adapter => adapter.forwardMessage('[email protected]', CHAT, 'M1'),+ },+ {+ name: 'a forward parked in the read of the destination chat',+ build: parked => {+ const forward = jest.fn();+ const source = { fetchMessages: () => Promise.resolve([{ id: { _serialized: 'M1' }, forward }]) };+ const destination = { fetchMessages: () => Promise.resolve([]) };+ return {+ client: {+ getNumberId: () => Promise.resolve(found(CHAT)),+ getChatById: (id: string) =>+ id === '[email protected]' ? Promise.resolve(source) : parked.park().then(() => destination),+ },+ unreached: forward,+ };+ },+ run: adapter => adapter.forwardMessage('[email protected]', CHAT, 'M1'),+ },];describe('whatsapp-web.js operations that race a session teardown', () => {@@ -197,4 +241,78 @@ describe('whatsapp-web.js operations that race a session teardown', () => {expect(sentNothing(error)).toBe(true);});});++ describe('the send retry after "No LID for user"', () => {+ it.each(PHASES)('answers the not-ready 409, not an unreachable recipient, %s', async phase => {+ // The first send fails on a stale id; the re-resolve parks, and the lookup then fails on the closing page.+ const parked = gate();+ const sendMessage = jest.fn().mockRejectedValue(new Error('No LID for user'));+ const getNumberId = jest+ .fn()+ .mockResolvedValueOnce(found(CHAT))+ .mockImplementationOnce(() =>+ parked.park().then(() => Promise.reject(new Error('Protocol error: Target closed'))),+ );+ const { adapter, tearDown, finish } = readyAdapter({ getNumberId, sendMessage });++ const outcome = outcomeOf(adapter.sendTextMessage(CHAT, 'hi'));+ await settle();+ expect(parked.parkedCalls).toBe(1);+ await tearDown(phase);+ parked.open();+ const result = await outcome;+ await finish();++ expect(result).toEqual({ error: expect.any(EngineNotReadyError) as unknown });+ expect((result as { error: unknown }).error).not.toBeInstanceOf(RecipientUnreachableError);+ expect(sendMessage).toHaveBeenCalledTimes(1);+ });++ it.each(PHASES)('does not retry a reply once the session is going away, %s', async phase => {+ // The re-resolve names a different id, which would normally be retried, here on the Message's own client.+ const parked = gate();+ const reply = jest.fn().mockRejectedValue(new Error('No LID for user'));+ const chat = { fetchMessages: () => Promise.resolve([{ id: { _serialized: 'Q1' }, reply }]) };+ const getNumberId = jest+ .fn()+ .mockResolvedValueOnce(found(LID))+ .mockImplementationOnce(() => parked.park().then(() => found(CHAT)));+ const { adapter, tearDown, finish } = readyAdapter({ getNumberId, getChatById: () => Promise.resolve(chat) });++ const outcome = outcomeOf(adapter.replyToMessage(CHAT, 'Q1', 'hi'));+ await settle();+ expect(parked.parkedCalls).toBe(1);+ await tearDown(phase);+ parked.open();+ const result = await outcome;+ await finish();++ expect(reply).toHaveBeenCalledTimes(1);+ expect(result).toEqual({ error: expect.any(EngineNotReadyError) as unknown });+ });+ });++ it('keeps a send that was already on the wire an outcome-unknown failure', async () => {+ const onWire = gate();+ const sendMessage = jest.fn(() =>+ onWire.park().then(() => Promise.reject(new Error('Protocol error (Runtime.callFunctionOn): Target closed.'))),+ );+ const { adapter, tearDown, finish } = readyAdapter({+ getNumberId: () => Promise.resolve(found(CHAT)),+ sendMessage,+ });++ const outcome = outcomeOf(adapter.sendTextMessage(CHAT, 'hi'));+ await settle();+ expect(onWire.parkedCalls).toBe(1);+ await tearDown('while destroy() runs');+ onWire.open();+ const { error } = (await outcome) as { error: unknown };+ await finish();++ expect(error).toBeInstanceOf(Error);+ expect(error).not.toBeInstanceOf(EngineNotReadyError);+ expect(countsTowardSendBreaker(error)).toBe(true);+ expect(sentNothing(error)).toBe(false);+ });});diff --git a/src/engine/adapters/wwebjs-messaging.ts b/src/engine/adapters/wwebjs-messaging.tsindex 4eaa2c9c9..22e9b0f25 100644--- a/src/engine/adapters/wwebjs-messaging.ts+++ b/src/engine/adapters/wwebjs-messaging.ts@@ -465,6 +465,10 @@ export class WwebjsMessaging {): Promise<T> {if (quotedMessageId) ensureSendable(chatId, 'reply');const to = await this.resolveSendId(chatId);+ // The lookup is an await a teardown can land in, and resolveSendId answers a failed lookup with the+ // unresolved id. The reply and forward legs send through the Message's own client, which the host's+ // readiness check on getClient never sees, so a send must not leave for a closing page from here.+ this.host.ensureReady();try {return await send(to);} catch (err) {@@ -481,6 +485,9 @@ export class WwebjsMessaging {}this.resolvedSendIds.delete(chatId);const fresh = await this.resolveSendId(chatId);+ // Same gap as above, and before the comparison: a lookup that failed because the session went away+ // answers the unresolved id, which would read as an unreachable recipient.+ this.host.ensureReady();if (fresh === to) {throw new RecipientUnreachableError(chatId);}@@ -777,6 +784,8 @@ export class WwebjsMessaging {} catch (error) {this.host.logger.warn(`Could not read the destination chat before forwarding: ${String(error)}`);}+ // The read above is a page round trip a teardown can land in, and its failure is only logged.+ this.host.ensureReady();return msgToForward.forward(to);});