Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): answer 409 when a Baileys send loses its socket in flight
Yudhi Armyndharis6b0d62c9915c
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.tsindex d113c6771..0a94cafb8 100644--- a/src/engine/adapters/baileys-messaging.ts+++ b/src/engine/adapters/baileys-messaging.ts@@ -45,6 +45,8 @@ export interface BaileysMessagingHost {ensureReady(): void;/** Post-ensureReady socket handle — call host.ensureReady() first. */getSocket(): WASocket;+ /** The live socket, or null once a stop or logout has torn it down. */+ getSocketOrNull(): WASocket | null;readonly logger: ReturnType<typeof createLogger>;toNeutralJid(jid: string): string;toEngineJid(jid: string): string;@@ -790,15 +792,23 @@ export class BaileysMessaging {* same tag WhatsApp uses to replay what the account typed on its phone while the gateway was* down, and the id is the only thing that tells the two apart (see handleMessagesUpsert). The* record is synchronous on the send's own continuation, ahead of the library's buffered echo.+ *+ * A send that fails after a stop or logout has torn its socket down is not ready (409), the same as+ * one interrupted before it reached the socket; a failure on a socket still in place propagates.*/private async send(jid: string,content: Parameters<WASocket['sendMessage']>[1],options?: Parameters<WASocket['sendMessage']>[2],): Promise<WAMessage | undefined> {- const sent = options- ? await this.sock().sendMessage(jid, content, options)- : await this.sock().sendMessage(jid, content);+ const sock = this.sock();+ let sent: WAMessage | undefined;+ try {+ sent = options ? await sock.sendMessage(jid, content, options) : await sock.sendMessage(jid, content);+ } catch (error) {+ if (this.host.getSocketOrNull() !== sock) throw new EngineNotReadyError();+ throw error;+ }this.host.rememberOwnSend(sent?.key?.id);return sent;}diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 02fb9f352..d3732384b 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -1821,6 +1821,26 @@ describe('BaileysAdapter messaging', () => {expect(res).toEqual({ id: 'OUT1', timestamp: 1700000001 });});+ // The socket a send is handed to can be torn down while the library is still writing to it: a+ // stop or logout then answers 409 like any other interrupted send, not a raw Connection Closed 500.+ it('a send whose socket is torn down while it is in flight reads as not ready', async () => {+ const adapter = await readyAdapter();+ fakeSock.sendMessage.mockImplementation(() => {+ (adapter as unknown as { sock: unknown }).sock = null;+ return Promise.reject(new Error('Connection Closed'));+ });+ await expect(adapter.sendTextMessage('[email protected]', 'hello')).rejects.toBeInstanceOf(+ EngineNotReadyError,+ );+ });++ it('a send that fails on a socket still in place rethrows the failure as is', async () => {+ const adapter = await readyAdapter();+ const failure = new Error('not-acceptable');+ fakeSock.sendMessage.mockRejectedValue(failure);+ await expect(adapter.sendTextMessage('[email protected]', 'hello')).rejects.toBe(failure);+ });+/** An adapter for a session started behind a proxy, which every fetch it makes must leave through. */const proxiedAdapter = async (): Promise<BaileysAdapter> => {const adapter = new BaileysAdapter({