kage-baileys 25.4.7 → 25.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -718,7 +718,38 @@ export const makeSocket = (config) => {
718
718
  return authState.creds.pairingCode;
719
719
  }
720
720
  catch (error) {
721
+ // Always roll back the provisional identity/code when WhatsApp rejects
722
+ // the request. In particular, never leave a locally generated code
723
+ // looking usable after a server-side rate limit.
721
724
  clearPendingPairing({ restore: true });
725
+
726
+ const isRateLimited = error?.output?.statusCode === 429
727
+ || error?.data?.isRateLimited === true
728
+ || /rate[-_\s]?overlimit/i.test(String(error?.message || ''));
729
+ if (isRateLimited) {
730
+ const retryAfterMs = Number(error?.data?.retryAfterMs) > 0
731
+ ? Math.ceil(Number(error.data.retryAfterMs))
732
+ : undefined;
733
+ const retryHint = retryAfterMs
734
+ ? ` The server indicated that you should wait at least ${Math.ceil(retryAfterMs / 1000)} seconds before trying again.`
735
+ : ' WhatsApp did not provide a retry time; stop requesting new codes and wait until the service allows another attempt.';
736
+ const rateLimitError = new Boom(
737
+ `WhatsApp rejected the pairing request (rate-overlimit, 429). No pairing code was issued.${retryHint} Kage does not retry this request automatically.`,
738
+ {
739
+ statusCode: 429,
740
+ data: {
741
+ serverCode: error?.data?.serverCode || '429',
742
+ serverText: error?.data?.serverText || error.message,
743
+ isRateLimited: true,
744
+ retryable: false,
745
+ ...(retryAfterMs ? { retryAfterMs } : {})
746
+ }
747
+ }
748
+ );
749
+ logger.warn({ retryAfterMs }, 'WhatsApp rate-limited a pairing-code request; provisional credentials were restored and no retry was scheduled');
750
+ throw rateLimitError;
751
+ }
752
+
722
753
  throw error;
723
754
  }
724
755
  };
@@ -12,3 +12,15 @@ export declare function buildPairingCompanionHelloNode(options: {
12
12
  platformDisplay: string;
13
13
  }): { tag: string; attrs: Record<string, string>; content: Array<Record<string, unknown>> };
14
14
  export declare function extractPairingReference(response: unknown): Buffer | null;
15
+
16
+ export declare function classifyWhatsAppNodeError(attrs?: Record<string, unknown>): {
17
+ message: string;
18
+ statusCode: number;
19
+ data: {
20
+ serverCode: string | null;
21
+ serverText: string;
22
+ isRateLimited: boolean;
23
+ retryable: false;
24
+ retryAfterMs?: number;
25
+ };
26
+ };
@@ -87,3 +87,40 @@ export function isValidPairingRef(creds, receivedRef, now = Date.now(), maxAgeMs
87
87
  return false;
88
88
  }
89
89
  }
90
+
91
+
92
+ /**
93
+ * Classify an error returned inside a WhatsApp binary-node response.
94
+ * A rate-overlimit response is an authoritative server-side rejection; it must
95
+ * be surfaced as HTTP 429 and never retried automatically.
96
+ */
97
+ export function classifyWhatsAppNodeError(attrs = {}) {
98
+ const serverText = typeof attrs.text === 'string' && attrs.text.trim()
99
+ ? attrs.text.trim()
100
+ : 'Unknown WhatsApp server error';
101
+ const rawCode = attrs.code == null ? null : String(attrs.code);
102
+ const numericCode = rawCode == null ? NaN : Number(rawCode);
103
+ const isRateLimited = numericCode === 429 || /rate[-_\s]?overlimit/i.test(serverText);
104
+
105
+ let retryAfterMs;
106
+ const retryAfterMsAttr = Number(attrs.retry_after_ms);
107
+ const retryAfterSecondsAttr = Number(attrs.retry_after);
108
+ if (Number.isFinite(retryAfterMsAttr) && retryAfterMsAttr > 0) {
109
+ retryAfterMs = Math.ceil(retryAfterMsAttr);
110
+ }
111
+ else if (Number.isFinite(retryAfterSecondsAttr) && retryAfterSecondsAttr > 0) {
112
+ retryAfterMs = Math.ceil(retryAfterSecondsAttr * 1000);
113
+ }
114
+
115
+ return {
116
+ message: serverText,
117
+ statusCode: isRateLimited ? 429 : 500,
118
+ data: {
119
+ serverCode: rawCode,
120
+ serverText,
121
+ isRateLimited,
122
+ retryable: false,
123
+ ...(retryAfterMs ? { retryAfterMs } : {})
124
+ }
125
+ };
126
+ }
@@ -2,6 +2,7 @@ import { Boom } from '@hapi/boom';
2
2
  import { proto } from '../../WAProto/index.js';
3
3
  import { randomBytes } from 'node:crypto';
4
4
  import {} from './types.js';
5
+ import { classifyWhatsAppNodeError } from '../Utils/pairing-code.js';
5
6
  // some extra useful utilities
6
7
  const indexCache = new WeakMap();
7
8
  export const getBinaryNodeChildren = (node, childTag) => {
@@ -55,7 +56,11 @@ export const getBinaryNodeChildUInt = (node, childTag, length) => {
55
56
  export const assertNodeErrorFree = (node) => {
56
57
  const errNode = getBinaryNodeChild(node, 'error');
57
58
  if (errNode) {
58
- throw new Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
59
+ const details = classifyWhatsAppNodeError(errNode.attrs || {});
60
+ throw new Boom(details.message, {
61
+ statusCode: details.statusCode,
62
+ data: details.data
63
+ });
59
64
  }
60
65
  };
61
66
  export const reduceBinaryNodeToDictionary = (node, tag) => {
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "kage-baileys",
3
3
  "type": "module",
4
- "version": "25.4.7",
5
- "description": "Kage Baileys with pairing logger ReferenceError fix, validated WhatsApp pairing-code flow, Native Flow review_and_pay relay fixes, expanded WAProto schema and guarded media diagnostics",
4
+ "version": "25.4.8",
5
+ "description": "Kage Baileys with explicit WhatsApp pairing rate-limit handling, validated pairing-code flow, Native Flow review_and_pay relay fixes, expanded WAProto schema and guarded media diagnostics",
6
6
  "keywords": [
7
7
  "whatsapp",
8
8
  "automation"
@@ -46,7 +46,7 @@
46
46
  "release": "release-it",
47
47
  "test": "node --test",
48
48
  "test:viewonce": "node --test test/media-message-guards.test.js",
49
- "test:offline": "node --test test/cross-fork-helpers.test.js test/media-message-guards.test.js test/newsletter-utils.test.js test/offline-node-processor.test.js test/pairing-code.test.mjs test/proto-schema.test.js"
49
+ "test:offline": "node --test test/cross-fork-helpers.test.js test/media-message-guards.test.js test/newsletter-utils.test.js test/offline-node-processor.test.js test/pairing-code.test.mjs test/rate-limit-handling.test.mjs test/proto-schema.test.js"
50
50
  },
51
51
  "dependencies": {
52
52
  "@adiwajshing/keyed-db": "^0.2.4",
@@ -0,0 +1,44 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { readFile } from 'node:fs/promises';
4
+ import { classifyWhatsAppNodeError } from '../lib/Utils/pairing-code.js';
5
+
6
+ const genericUtils = await readFile(new URL('../lib/WABinary/generic-utils.js', import.meta.url), 'utf8');
7
+ const socketSource = await readFile(new URL('../lib/Socket/socket.js', import.meta.url), 'utf8');
8
+
9
+ test('rate-overlimit is classified as HTTP 429, not a generic 500', () => {
10
+ const result = classifyWhatsAppNodeError({ code: '429', text: 'rate-overlimit' });
11
+ assert.equal(result.statusCode, 429);
12
+ assert.equal(result.data.isRateLimited, true);
13
+ assert.equal(result.data.retryable, false);
14
+ assert.equal(result.data.retryAfterMs, undefined);
15
+ });
16
+
17
+ test('an explicit server retry_after is preserved without inventing a wait time', () => {
18
+ const result = classifyWhatsAppNodeError({ code: '429', text: 'rate-overlimit', retry_after: '90' });
19
+ assert.equal(result.data.retryAfterMs, 90_000);
20
+ });
21
+
22
+ test('retry_after_ms takes precedence over retry_after seconds', () => {
23
+ const result = classifyWhatsAppNodeError({ code: '429', text: 'rate-overlimit', retry_after: '90', retry_after_ms: '1200' });
24
+ assert.equal(result.data.retryAfterMs, 1_200);
25
+ });
26
+
27
+ test('non-rate-limit IQ errors retain their server message and generic status', () => {
28
+ const result = classifyWhatsAppNodeError({ code: '400', text: 'bad-request' });
29
+ assert.equal(result.statusCode, 500);
30
+ assert.equal(result.message, 'bad-request');
31
+ assert.equal(result.data.isRateLimited, false);
32
+ });
33
+
34
+ test('generic binary-node errors use the new WhatsApp error classifier', () => {
35
+ assert.match(genericUtils, /classifyWhatsAppNodeError/);
36
+ assert.match(genericUtils, /statusCode:\s*details\.statusCode/);
37
+ });
38
+
39
+ test('pairing rolls back provisional credentials and never auto-retries rate limits', () => {
40
+ assert.match(socketSource, /clearPendingPairing\(\{ restore: true \}\);[\s\S]{0,500}const isRateLimited/);
41
+ assert.match(socketSource, /statusCode: 429/);
42
+ assert.match(socketSource, /Kage does not retry this request automatically/);
43
+ assert.match(socketSource, /no retry was scheduled/);
44
+ });