Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(session): read a null reconnect setting as unset, not zero
Yudhi Armyndharis3c865bae2ec6
diff --git a/src/modules/session/reconnect-config.spec.ts b/src/modules/session/reconnect-config.spec.tsindex 1efc419f1..53e480728 100644--- a/src/modules/session/reconnect-config.spec.ts+++ b/src/modules/session/reconnect-config.spec.ts@@ -20,6 +20,30 @@ describe('resolveReconnectConfig', () => {});});+ it('treats null, undefined and blank values as unset, the way GET /config reports them', () => {+ // GET /config reports the unlimited default as maxReconnectAttempts: null, so a client copying it into a+ // create body sends that null; Number(null) is 0, which would disable reconnect and floor the delay.+ expect(resolveReconnectConfig({ maxReconnectAttempts: null, reconnectBaseDelay: null })).toEqual({+ baseDelay: 5000,+ maxAttempts: Number.POSITIVE_INFINITY,+ });+ expect(resolveReconnectConfig({ maxReconnectAttempts: undefined, reconnectBaseDelay: undefined })).toEqual({+ baseDelay: 5000,+ maxAttempts: Number.POSITIVE_INFINITY,+ });+ expect(resolveReconnectConfig({ maxReconnectAttempts: '', reconnectBaseDelay: ' ' })).toEqual({+ baseDelay: 5000,+ maxAttempts: Number.POSITIVE_INFINITY,+ });+ });++ it('keeps coercing numeric strings a create body may have stored', () => {+ expect(resolveReconnectConfig({ maxReconnectAttempts: '5', reconnectBaseDelay: '8000' })).toEqual({+ baseDelay: 8000,+ maxAttempts: 5,+ });+ });+it('clamps a huge baseDelay down to the 5-minute max (no infinite-timer wedge)', () => {expect(resolveReconnectConfig({ reconnectBaseDelay: 1e15 }).baseDelay).toBe(300_000);});diff --git a/src/modules/session/session-engine-lifecycle.service.ts b/src/modules/session/session-engine-lifecycle.service.tsindex 596cd9c26..e7feafd5a 100644--- a/src/modules/session/session-engine-lifecycle.service.ts+++ b/src/modules/session/session-engine-lifecycle.service.ts@@ -68,17 +68,20 @@ const RECONNECT_MAX_ATTEMPTS_CAP = 20;/** Coerce + clamp the untyped session.config reconnect knobs to finite, bounded values. Defaults are* a 5000ms base delay and UNLIMITED attempts (`Infinity`): a long-lived session must keep retrying* (the backoff parks at the 5-minute cap) instead of dying permanently after ~2.5 minutes. An EXPLICIT- * `maxReconnectAttempts: 0` (disable) is preserved, and 1..20 clamps as before. */+ * `maxReconnectAttempts: 0` (disable) is preserved, and 1..20 clamps as before; null means unset. */export function resolveReconnectConfig(config: { maxReconnectAttempts?: unknown; reconnectBaseDelay?: unknown } | null,): { maxAttempts: number; baseDelay: number } {- const baseRaw = Number(config?.reconnectBaseDelay);+ // null, undefined and a blank string mean "unset" (GET /config reports the unlimited default as null), not+ // the 0 that Number() makes of them; a numeric string a create body stored still coerces.+ const toNumber = (v: unknown): number => (v == null || (typeof v === 'string' && v.trim() === '') ? NaN : Number(v));+ const baseRaw = toNumber(config?.reconnectBaseDelay);const baseDelay = clampNumber(Number.isFinite(baseRaw) ? baseRaw : 5000,RECONNECT_BASE_DELAY_MIN_MS,RECONNECT_BASE_DELAY_MAX_MS,);- const attemptsRaw = Number(config?.maxReconnectAttempts);+ const attemptsRaw = toNumber(config?.maxReconnectAttempts);const maxAttempts = Number.isFinite(attemptsRaw)? Math.floor(clampNumber(attemptsRaw, 0, RECONNECT_MAX_ATTEMPTS_CAP)): Number.POSITIVE_INFINITY;diff --git a/src/modules/session/session.service.spec.ts b/src/modules/session/session.service.spec.tsindex 599b6eeea..5957311b3 100644--- a/src/modules/session/session.service.spec.ts+++ b/src/modules/session/session.service.spec.ts@@ -7761,8 +7761,8 @@ describe('SessionService', () => {const result = await service.updateConfig('sess-uuid-1', { maxReconnectAttempts: null });- // Deleted outright rather than written as null: resolveReconnectConfig reads Number(null) as 0,- // so a stored null would mean "never reconnect" — the exact opposite of the default it restores.+ // Deleted outright rather than written as null, so the stored row reads as the default it restores+ // and never depends on how a reader interprets a stored null.expect(writtenConfig()).not.toHaveProperty('maxReconnectAttempts');expect(writtenConfig()).toEqual({ autoRejectCalls: true });expect(result.maxReconnectAttempts).toBeNull();