Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
refactor(engine): word unconfirmed Baileys writes in one place
Yudhi Armyndharisdc345cc2b06c
diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.tsindex 45f685c05..fbf609434 100644--- a/src/engine/adapters/baileys-contacts.ts+++ b/src/engine/adapters/baileys-contacts.ts@@ -2,7 +2,7 @@ import type { WAMessage, WAMessageKey, WASocket } from '@whiskeysockets/baileys'import { ChatSummary, Contact, MediaInput } from '../interfaces/whatsapp-engine.interface';import { assertReadableImage, resolveMediaBuffer } from './baileys-messaging';import { type createLogger } from '../../common/services/logger.service';-import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';+import { BAILEYS_QUERY_BUDGET_MS, confirmedWrite, withQueryDeadline } from './baileys-query-deadline';import { EngineTransportError } from '../../common/errors/engine-transport.error';import { RecipientUnreachableError } from '../../common/errors/recipient-unreachable.error';import { LidNotMappedError } from '../../common/errors/lid-not-mapped.error';@@ -61,7 +61,7 @@ export class BaileysContacts {/** Bound a write whose confirmation the library discards; see baileys-query-deadline.ts. */private confirmed<T>(work: Promise<T>, operation: string): Promise<T> {- return withQueryDeadline(work, this.queryBudgetMs, `WhatsApp did not confirm ${operation} in time`);+ return confirmedWrite(work, this.queryBudgetMs, operation);}/** Post-ensureReady socket handle. */diff --git a/src/engine/adapters/baileys-groups.ts b/src/engine/adapters/baileys-groups.tsindex bd4ec4140..b7e05ca59 100644--- a/src/engine/adapters/baileys-groups.ts+++ b/src/engine/adapters/baileys-groups.ts@@ -17,7 +17,7 @@ import { EngineTransportError } from '../../common/errors/engine-transport.errorimport { EngineThrottledError } from '../../common/errors/engine-throttled.error';import { InvalidInviteCodeError } from '../../common/errors/invalid-invite-code.error';import { type createLogger } from '../../common/services/logger.service';-import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';+import { BAILEYS_QUERY_BUDGET_MS, confirmedWrite, withQueryDeadline } from './baileys-query-deadline';import { toParticipantWid } from '../identity/wa-id';/**@@ -162,7 +162,7 @@ export class BaileysGroups {/** Bound a write whose confirmation the library discards; see baileys-query-deadline.ts. */private confirmed<T>(work: Promise<T>, operation: string): Promise<T> {- return withQueryDeadline(work, this.queryBudgetMs, `WhatsApp did not confirm ${operation} in time`);+ return confirmedWrite(work, this.queryBudgetMs, operation);}/** Post-ensureReady socket handle. */diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.tsindex 0e3ae8c34..e0d677f4d 100644--- a/src/engine/adapters/baileys-messaging.ts+++ b/src/engine/adapters/baileys-messaging.ts@@ -43,7 +43,7 @@ import { EngineTransportError } from '../../common/errors/engine-transport.errorimport { EngineThrottledError } from '../../common/errors/engine-throttled.error';import { EngineNotSentError } from '../../common/errors/engine-not-sent.error';import { parseWaId, userPart } from '../identity/wa-id';-import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';+import { BAILEYS_QUERY_BUDGET_MS, confirmedWrite, withQueryDeadline } from './baileys-query-deadline';import { ConcurrencyLimiter } from '../../common/utils/concurrency-limiter';import { describeAudio } from './baileys-audio-probe';@@ -293,7 +293,7 @@ export class BaileysMessaging {/** Bound a write whose confirmation the library discards; see baileys-query-deadline.ts. */private confirmed<T>(work: Promise<T>, operation: string): Promise<T> {- return withQueryDeadline(work, this.queryBudgetMs, `WhatsApp did not confirm ${operation} in time`);+ return confirmedWrite(work, this.queryBudgetMs, operation);}/** Post-ensureReady socket handle. */diff --git a/src/engine/adapters/baileys-query-deadline.ts b/src/engine/adapters/baileys-query-deadline.tsindex d181de668..f3ee978d0 100644--- a/src/engine/adapters/baileys-query-deadline.ts+++ b/src/engine/adapters/baileys-query-deadline.ts@@ -42,6 +42,11 @@ export async function withQueryDeadline<T>(work: Promise<T>, timeoutMs: number,}}+/** {@link withQueryDeadline} for a write whose confirmation the library discards; the error names the operation. */+export function confirmedWrite<T>(work: Promise<T>, timeoutMs: number, operation: string): Promise<T> {+ return withQueryDeadline(work, timeoutMs, `WhatsApp did not confirm ${operation} in time`);+}+/*** Budget for a single bounded Baileys call, read or write. Anchored to MEDIA_DOWNLOAD_TIMEOUT_MS (inbound-media-cap.ts),* this repo's existing figure for one bounded network round trip over WhatsApp. It must stay underdiff --git a/src/engine/adapters/baileys-write-deadline.spec.ts b/src/engine/adapters/baileys-write-deadline.spec.tsindex eba9cae6f..80bdc10d2 100644--- a/src/engine/adapters/baileys-write-deadline.spec.ts+++ b/src/engine/adapters/baileys-write-deadline.spec.ts@@ -82,6 +82,12 @@ describe('group writes report an unconfirmed write rather than success', () => {await expect(groups({ groupLeave }, 500).leaveGroup('[email protected]')).resolves.toBeUndefined();expect(groupLeave).toHaveBeenCalledWith('[email protected]');});++ it('names the operation that was not confirmed', async () => {+ await expect(groups({ groupLeave: stub() }, 15).leaveGroup('[email protected]')).rejects.toThrow(+ 'WhatsApp did not confirm leaving the group in time',+ );+ });});describe('profile writes report an unconfirmed write rather than success', () => {