Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): stop a cold-start pairing request throwing Boom 428
m7fz7f7572ef49877
diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.tsindex 150f488d0..49f384611 100644--- a/src/engine/adapters/baileys-lifecycle.ts+++ b/src/engine/adapters/baileys-lifecycle.ts@@ -762,9 +762,19 @@ export class BaileysLifecycle {return this.qrCode;}+ /**+ * Gated on QR_READY, not on the socket merely existing — the same guard, for the same reason, as+ * the whatsapp-web.js engine's requestPairingCode. `this.sock` is assigned the moment+ * makeWASocket returns, seconds before the WebSocket finishes connecting, and Baileys' sendNode+ * throws a raw Boom 428 ('Connection Closed') whenever `ws.isOpen` is false — which surfaced as a+ * 500 with a stack trace on every cold-start pairing request. QR_READY is exactly the window this+ * can work in: it is set from the `connection.update { qr }` event, which WhatsApp only sends+ * once the noise handshake is done, and every transient close drops the status back to+ * INITIALIZING for the reconnect, so it also covers a socket that died after a QR was published.+ */async requestPairingCode(phoneNumber: string): Promise<string> {- if (!this.sock) {- throw new EngineNotReadyError('Cannot request a pairing code before the engine is initialized.');+ if (!this.sock || this.status !== EngineStatus.QR_READY) {+ throw new EngineNotReadyError('Session is not waiting to be linked. Start it and wait for the QR stage.');}return this.sock.requestPairingCode(phoneNumber);}diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex ee4227506..a041926a2 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -569,9 +569,26 @@ describe('BaileysAdapter lifecycle & status', () => {await expect(adapter.requestPairingCode('628999')).rejects.toBeInstanceOf(EngineNotReadyError);});- it('requestPairingCode delegates to the socket', async () => {+ // The socket object exists from the moment makeWASocket returns, but its WebSocket is still+ // connecting: sending then makes Baileys throw a raw Boom 428 that surfaces as a 500.+ it('requestPairingCode throws EngineNotReadyError while the socket is still connecting', async () => {const adapter = newAdapter();await adapter.initialize(noopCallbacks({}));+ await expect(adapter.requestPairingCode('628999')).rejects.toBeInstanceOf(EngineNotReadyError);+ expect(fakeSock.requestPairingCode).not.toHaveBeenCalled();+ });++ it('requestPairingCode delegates to the socket once a QR has been published', async () => {+ let resolveQr!: () => void;+ const qrPublished = new Promise<void>(resolve => {+ resolveQr = resolve;+ });+ const onQRCode = jest.fn(() => resolveQr());+ const adapter = newAdapter();+ await adapter.initialize(noopCallbacks({ onQRCode }));+ fakeSock.fire('connection.update', { qr: 'QR-STRING' });+ await qrPublished;+await expect(adapter.requestPairingCode('628999')).resolves.toBe('ABCD-EFGH');expect(fakeSock.requestPairingCode).toHaveBeenCalledWith('628999');});