Skip to content

Commit d803c9f

Browse files
committed
fix(baileys): name the 515 reconnect grace + tighten stream-error types
Addresses sourcery-ai review feedback on the previous commit: - Extract the 30 000ms reconnect grace window into a named class constant STREAM_515_RECONNECT_GRACE_MS so future tuning is self-documenting rather than a literal scattered through the close handler. - Extract the magic '515' string into STREAM_ERROR_CODE_RECONNECT. - Replace the loose 'node: any' on the 'CB:stream:error' handler with a minimal structural type ({ attrs?: { code?: string | number } }) so the payload shape is documented and type-checked. - Compare the code via String(...) so a numeric 515 from the underlying socket library still triggers the grace window — the original literal '515' check would have silently broken on a type change.
1 parent b76325d commit d803c9f

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ export class BaileysStartupService extends ChannelStartupService {
257257
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
258258
private readonly UPDATE_CACHE_TTL_SECONDS = 30 * 60; // 30 minutes - avoid duplicate status updates
259259

260+
// Reconnect behaviour for the Baileys "stream:error 515" sequence.
261+
// After WhatsApp emits 515 it usually closes with `loggedOut`; that close is *not* a real logout
262+
// and we should reconnect. We treat any close arriving within this grace window as 515-driven.
263+
private static readonly STREAM_515_RECONNECT_GRACE_MS = 30_000;
264+
// The numeric WhatsApp stream-error code that triggers the grace-period reconnect above.
265+
private static readonly STREAM_ERROR_CODE_RECONNECT = '515';
266+
260267
public stateConnection: wa.StateConnection = { state: 'close' };
261268

262269
public phoneNumber: string;
@@ -427,7 +434,8 @@ export class BaileysStartupService extends ChannelStartupService {
427434
if (connection === 'close') {
428435
const statusCode = (lastDisconnect?.error as Boom)?.output?.statusCode;
429436
const codesToNotReconnect = [DisconnectReason.loggedOut, DisconnectReason.forbidden, 402, 406];
430-
const recentStream515 = Date.now() - this._lastStream515At < 30_000;
437+
const recentStream515 =
438+
Date.now() - this._lastStream515At < BaileysStartupService.STREAM_515_RECONNECT_GRACE_MS;
431439
const shouldReconnect =
432440
!codesToNotReconnect.includes(statusCode) ||
433441
(statusCode === DisconnectReason.loggedOut && recentStream515);
@@ -719,8 +727,8 @@ export class BaileysStartupService extends ChannelStartupService {
719727
this.sendDataWebhook(Events.CALL, payload, true, ['websocket']);
720728
});
721729

722-
this.client.ws.on('CB:stream:error', (node: any) => {
723-
if (node?.attrs?.code === '515') {
730+
this.client.ws.on('CB:stream:error', (node: { attrs?: { code?: string | number } }) => {
731+
if (String(node?.attrs?.code) === BaileysStartupService.STREAM_ERROR_CODE_RECONNECT) {
724732
this._lastStream515At = Date.now();
725733
}
726734
});

0 commit comments

Comments
 (0)