Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): keep an unconfirmed Baileys call link out of 503
Yudhi Armyndharis8a4c223912dd
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.tsindex 0a94cafb8..1fc8fdc7d 100644--- a/src/engine/adapters/baileys-messaging.ts+++ b/src/engine/adapters/baileys-messaging.ts@@ -435,13 +435,19 @@ export class BaileysMessaging {// link is that token behind one of the library's two exported prefixes. Note the audio prefix// WhatsApp itself uses is `/voice/`, which is also what whatsapp-web.js calls the same thing.//- // No timeoutMs is passed: given one, baileys' query() arms its own timer before ours and rejects- // with a raw 408 Boom, which answers 500. Without it the library's wait resolves nothing after its- // own 60 s, so the wrap below is the deadline that answers, with a 503.- const token = await this.confirmed(- this.sock().createCallLink(type, { startTime: Math.floor(startTime / 1000) }),- 'the call link',- );+ // No timeoutMs is passed: without it the library's wait resolves nothing after its own 60 s, which+ // would read as "no link". The wrap below is the deadline, but its 503 is rethrown as a 500:+ // minting is non-idempotent and the deadline does not cancel the query, so a client replaying a+ // 503 could create a second link. Same rule as createGroup and the whatsapp-web.js call link.+ let token: string | undefined;+ try {+ token = await this.confirmed(+ this.sock().createCallLink(type, { startTime: Math.floor(startTime / 1000) }),+ 'the call link',+ );+ } catch (err) {+ throw err instanceof EngineTransportError ? new Error(err.message) : err;+ }if (!token) {// A prefix with nothing after it is a dead link that looks like a real one — the caller would// hand it to a user and only find out then.diff --git a/src/engine/adapters/create-call-link.spec.ts b/src/engine/adapters/create-call-link.spec.tsindex 71667251e..5a6a11af8 100644--- a/src/engine/adapters/create-call-link.spec.ts+++ b/src/engine/adapters/create-call-link.spec.ts@@ -83,27 +83,14 @@ describe('BaileysMessaging.createCallLink', () => {);});- it('reports an unanswered query rather than hanging on a silent socket', async () => {+ // Minting a link is non-idempotent: the deadline abandons the query without cancelling it, so a+ // 503, which the Go SDK replays for POST, could mint a second link. An unanswered query must still+ // fail in time, but as a 500.+ it('reports an unanswered query in time, as a non-retryable failure', async () => {const createCallLink = jest.fn(() => new Promise<never>(() => undefined));- await expect(makeMessaging({ createCallLink }, 15).createCallLink('video', START_MS)).rejects.toBeInstanceOf(- EngineTransportError,- );- });-- it('answers 503 when the library would time the query out on its own', async () => {- // Baileys' query() given a timeoutMs arms its own timer first and rejects with a raw 408 Boom,- // which is not an HTTP error; without one, its wait resolves nothing and the deadline answers.- const createCallLink = jest.fn(- (_type: string, _event: unknown, timeoutMs?: number) =>- new Promise<never>((_resolve, reject) => {- if (timeoutMs) {- setTimeout(() => reject(Object.assign(new Error('Timed Out'), { output: { statusCode: 408 } })), timeoutMs);- }- }),- );- await expect(makeMessaging({ createCallLink }, 15).createCallLink('video', START_MS)).rejects.toBeInstanceOf(- EngineTransportError,- );+ const failure = makeMessaging({ createCallLink }, 15).createCallLink('video', START_MS);+ await expect(failure).rejects.toThrow(/did not confirm the call link in time/);+ await expect(failure).rejects.not.toBeInstanceOf(EngineTransportError);});});diff --git a/src/modules/call/call.controller.spec.ts b/src/modules/call/call.controller.spec.tsindex bb9aa9418..06a5831e1 100644--- a/src/modules/call/call.controller.spec.ts+++ b/src/modules/call/call.controller.spec.ts@@ -31,14 +31,14 @@ describe('CallController', () => {);});- // Baileys answers an unconfirmed link_create with EngineTransportError (503). The mint is not- // idempotent, so the contract must say a 503 can follow a link that was in fact created.- it('POST link declares the Baileys 503', () => {+ // The mint is not idempotent, so an unconfirmed link_create answers 500 on both engines: a+ // retryable 503 would invite a replay that creates a second link.+ it('POST link declares no retryable 503', () => {const responses = (Reflect.getMetadata('swagger/apiResponse',Object.getOwnPropertyDescriptor(CallController.prototype, 'createLink')?.value as object,) ?? {}) as Record<string, unknown>;- expect(Object.keys(responses)).toContain('503');+ expect(Object.keys(responses)).not.toContain('503');});it('POST :callId/reject returns the success envelope', async () => {diff --git a/src/modules/call/call.controller.ts b/src/modules/call/call.controller.tsindex a5aa2ae0c..e3050e995 100644--- a/src/modules/call/call.controller.ts+++ b/src/modules/call/call.controller.ts@@ -21,12 +21,6 @@ export class CallController {@ApiResponse({ status: 200, description: 'The generated link', type: CallLinkResponseDto })@ApiResponse({ status: 400, description: 'Session is not started, or an invalid type / startTime' })@ApiResponse({ status: 403, description: 'WhatsApp generated no link for this request' })- @ApiResponse({- status: 503,- description:- 'Baileys only: WhatsApp did not answer within the request budget. A link may or may not have been ' +- 'minted, so a retry can create a second one.',- })@ApiResponse({ status: 409, description: ENGINE_NOT_READY_409 })async createLink(@Param('sessionId') sessionId: string,