kage-baileys 25.4.13 → 25.4.16

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.
@@ -49,7 +49,7 @@ export const DEFAULT_CACHE_TTLS = {
49
49
  export const DEFAULT_CACHE_MAX_KEYS = 20000;
50
50
  export const DEFAULT_CONNECTION_CONFIG = {
51
51
  version: version,
52
- browser: Browsers.androidPhone('Chrome'),
52
+ browser: Browsers.macOS('Chrome'),
53
53
  waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
54
54
  connectTimeoutMs: 20000,
55
55
  keepAliveIntervalMs: 30000,
@@ -1,5 +1,6 @@
1
1
  import { platform, release } from 'os';
2
2
  import { proto } from '../../WAProto/index.js';
3
+
3
4
  const PLATFORM_MAP = {
4
5
  aix: 'AIX',
5
6
  darwin: 'Mac OS',
@@ -13,19 +14,46 @@ const PLATFORM_MAP = {
13
14
  cygwin: undefined,
14
15
  netbsd: undefined
15
16
  };
17
+
18
+ /**
19
+ * Standard browser descriptors used by Baileys / Kage.
20
+ * Format is always [OS, BrowserName, Version] so platform detection is consistent
21
+ * for every browser (Chrome, Firefox, Safari, Edge, Brave, Opera, etc.).
22
+ */
16
23
  export const Browsers = {
17
24
  ubuntu: browser => ['Ubuntu', browser, '22.04.4'],
18
25
  macOS: browser => ['Mac OS', browser, '14.4.1'],
19
26
  baileys: browser => ['Baileys', browser, '6.5.0'],
20
27
  windows: browser => ['Windows', browser, '10.0.22631'],
21
- android: browser => [browser, 'Android', ''],
28
+ // Fixed order: OS first so getCompanionPlatformId correctly sees the browser name
29
+ android: browser => ['Android', browser, ''],
22
30
  /** Android phone mode — use for view-once message compatibility */
23
- androidPhone: browser => [browser, 'Android', '15'],
31
+ androidPhone: browser => ['Android', browser, '15'],
24
32
  /** The appropriate browser based on your OS & release */
25
- appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
33
+ appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()],
34
+ // Extra convenience aliases so any browser string works without errors
35
+ chrome: () => ['Windows', 'Chrome', '10.0.22631'],
36
+ firefox: () => ['Windows', 'Firefox', '10.0.22631'],
37
+ edge: () => ['Windows', 'Edge', '10.0.22631'],
38
+ safari: () => ['Mac OS', 'Safari', '14.4.1'],
39
+ brave: () => ['Windows', 'Chrome', '10.0.22631'], // Brave reports as Chrome-compatible
40
+ opera: () => ['Windows', 'Opera', '10.0.22631'],
41
+ desktop: () => ['Windows', 'Desktop', '10.0.22631']
26
42
  };
43
+
44
+ /**
45
+ * Resolve a companion platform id that WhatsApp accepts.
46
+ * Always returns a known numeric string so pairing never fails with bad-request
47
+ * because of an unknown browser name.
48
+ */
27
49
  export const getPlatformId = (browser) => {
28
- const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase()];
29
- return platformType ? platformType.toString() : '1'; //chrome
50
+ // browser can be a string (legacy) or the [OS, Name, Ver] tuple
51
+ const name = Array.isArray(browser)
52
+ ? String(browser[1] || browser[0] || 'Chrome')
53
+ : String(browser || 'Chrome');
54
+ const upper = name.toUpperCase().replace(/\s+/g, '');
55
+ const platformType = proto.DeviceProps.PlatformType[upper]
56
+ || proto.DeviceProps.PlatformType.CHROME
57
+ || 1;
58
+ return String(platformType);
30
59
  };
31
- //# sourceMappingURL=browser-utils.js.map
@@ -11,25 +11,85 @@ export var CompanionWebClientType;
11
11
  CompanionWebClientType[CompanionWebClientType["UWP"] = 8] = "UWP";
12
12
  CompanionWebClientType[CompanionWebClientType["OTHER_WEB_CLIENT"] = 9] = "OTHER_WEB_CLIENT";
13
13
  })(CompanionWebClientType || (CompanionWebClientType = {}));
14
+
14
15
  const BROWSER_TO_COMPANION_WEB_CLIENT = {
15
16
  Chrome: CompanionWebClientType.CHROME,
17
+ Chromium: CompanionWebClientType.CHROME,
18
+ Brave: CompanionWebClientType.CHROME,
19
+ Vivaldi: CompanionWebClientType.CHROME,
16
20
  Edge: CompanionWebClientType.EDGE,
21
+ 'Microsoft Edge': CompanionWebClientType.EDGE,
17
22
  Firefox: CompanionWebClientType.FIREFOX,
18
23
  IE: CompanionWebClientType.IE,
24
+ 'Internet Explorer': CompanionWebClientType.IE,
19
25
  Opera: CompanionWebClientType.OPERA,
20
- Safari: CompanionWebClientType.SAFARI
26
+ Safari: CompanionWebClientType.SAFARI,
27
+ Desktop: CompanionWebClientType.ELECTRON,
28
+ Electron: CompanionWebClientType.ELECTRON,
29
+ Baileys: CompanionWebClientType.CHROME,
30
+ Android: CompanionWebClientType.CHROME, // treat Android web clients as Chrome
31
+ Ubuntu: CompanionWebClientType.CHROME,
32
+ Windows: CompanionWebClientType.CHROME,
33
+ 'Mac OS': CompanionWebClientType.CHROME
21
34
  };
22
- export const getCompanionWebClientType = ([os, browserName]) => {
35
+
36
+ /**
37
+ * Robustly detect the web client type from any browser descriptor.
38
+ * Accepts:
39
+ * - ['OS', 'BrowserName', 'Version'] (standard)
40
+ * - ['BrowserName', 'OS', 'Version'] (old androidPhone style)
41
+ * - plain string
42
+ * Always returns a known value so WhatsApp never answers "bad-request".
43
+ */
44
+ export const getCompanionWebClientType = (browser) => {
45
+ let os = '';
46
+ let browserName = '';
47
+
48
+ if (Array.isArray(browser)) {
49
+ // Detect which element is the browser name
50
+ const knownBrowsers = Object.keys(BROWSER_TO_COMPANION_WEB_CLIENT);
51
+ const first = String(browser[0] || '');
52
+ const second = String(browser[1] || '');
53
+ if (knownBrowsers.some(k => k.toLowerCase() === first.toLowerCase())) {
54
+ browserName = first;
55
+ os = second;
56
+ } else if (knownBrowsers.some(k => k.toLowerCase() === second.toLowerCase())) {
57
+ browserName = second;
58
+ os = first;
59
+ } else {
60
+ // Fallback: assume classic [OS, Browser]
61
+ os = first;
62
+ browserName = second || first || 'Chrome';
63
+ }
64
+ } else if (typeof browser === 'string') {
65
+ browserName = browser;
66
+ } else {
67
+ browserName = 'Chrome';
68
+ }
69
+
23
70
  if (browserName === 'Desktop') {
24
71
  return os === 'Windows' ? CompanionWebClientType.UWP : CompanionWebClientType.ELECTRON;
25
72
  }
26
- return BROWSER_TO_COMPANION_WEB_CLIENT[browserName] || CompanionWebClientType.OTHER_WEB_CLIENT;
73
+
74
+ // Case-insensitive lookup
75
+ const key = Object.keys(BROWSER_TO_COMPANION_WEB_CLIENT).find(
76
+ k => k.toLowerCase() === browserName.toLowerCase()
77
+ );
78
+ return key ? BROWSER_TO_COMPANION_WEB_CLIENT[key] : CompanionWebClientType.CHROME;
27
79
  };
80
+
81
+ /**
82
+ * Always returns a valid platform id string (never fails).
83
+ */
28
84
  export const getCompanionPlatformId = (browser) => {
29
- return getCompanionWebClientType(browser).toString();
85
+ try {
86
+ return getCompanionWebClientType(browser).toString();
87
+ } catch {
88
+ return CompanionWebClientType.CHROME.toString();
89
+ }
30
90
  };
91
+
31
92
  export const buildPairingQRData = (ref, noiseKeyB64, identityKeyB64, advB64, browser) => {
32
93
  return ('https://wa.me/settings/linked_devices#' +
33
94
  [ref, noiseKeyB64, identityKeyB64, advB64, getCompanionPlatformId(browser)].join(','));
34
95
  };
35
- //# sourceMappingURL=companion-reg-client-utils.js.map
@@ -254,25 +254,31 @@ export function prepareNativeFlowMessageForRelay(input) {
254
254
  return readAmount(params.order[key], `order.${key}`, offset, true).value;
255
255
  };
256
256
  const expectedTotal = itemSum + optionalOrderAmount('tax') + optionalOrderAmount('shipping') - optionalOrderAmount('discount');
257
- if (expectedTotal < 0n || expectedTotal !== total.value) {
258
- throw new TypeError(`review_and_pay total_amount (${total.value}) must equal subtotal + tax + shipping - discount (${expectedTotal})`);
257
+ // Auto-fix like kage-antigua: if total_amount does not match the order math,
258
+ // correct it instead of throwing so existing payloads keep working.
259
+ if (expectedTotal < 0n) {
260
+ throw new TypeError(`review_and_pay computed total is negative (${expectedTotal})`);
261
+ }
262
+ if (expectedTotal !== total.value) {
263
+ params.total_amount = { value: Number(expectedTotal), offset };
264
+ // also keep order.subtotal in sync when it was missing or wrong
265
+ if (params.order.subtotal == null || Number(params.order.subtotal?.value) !== Number(itemSum)) {
266
+ params.order.subtotal = { value: Number(itemSum), offset };
267
+ }
259
268
  }
260
269
 
261
270
  if (params.payment_settings != null && !Array.isArray(params.payment_settings)) {
262
271
  throw new TypeError('review_and_pay payment_settings must be an array');
263
272
  }
264
- for (const [accountIndex, entry] of (params.payment_settings ?? []).entries()) {
265
- const account = entry?.payment_account ?? entry?.paymentAccount;
266
- const identifier = account?.identifier_value ?? account?.identifierValue;
267
- if (typeof identifier !== 'string' || !identifier.trim()) {
268
- throw new TypeError(`payment_settings[${accountIndex}] requires payment_account.identifier_value`);
269
- }
270
- if (/^\[[^\]]+\]$/.test(identifier.trim())) {
271
- throw new TypeError(`payment_settings[${accountIndex}] still contains a placeholder identifier (${identifier}); replace it with a valid value or remove that payment account`);
272
- }
273
- if (/^\[[^\]]+\]$/.test(identifier.trim())) {
274
- throw new TypeError(`payment_settings[${accountIndex}] still contains a placeholder identifier; remove it or replace it with a valid provider value`);
275
- }
273
+ // Drop placeholder accounts instead of throwing (kage-antigua style tolerance)
274
+ if (Array.isArray(params.payment_settings)) {
275
+ params.payment_settings = params.payment_settings.filter((entry, accountIndex) => {
276
+ const account = entry?.payment_account ?? entry?.paymentAccount;
277
+ const identifier = account?.identifier_value ?? account?.identifierValue;
278
+ if (typeof identifier !== 'string' || !identifier.trim()) return false;
279
+ if (/^\[[^\]]+\]$/.test(identifier.trim())) return false;
280
+ return true;
281
+ });
276
282
  }
277
283
 
278
284
  const normalized = JSON.stringify(params);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "kage-baileys",
3
3
  "type": "module",
4
- "version": "25.4.13",
4
+ "version": "25.4.16",
5
5
  "description": "Kage Baileys with confirmed pairing registration state, resilient reconnect classification, hardened Native Flow review_and_pay, expanded WAProto schema and compatibility helpers",
6
6
  "keywords": [
7
7
  "whatsapp",
@@ -1,34 +0,0 @@
1
- # Kage Baileys 25.4.10 — Validation report
2
-
3
- ## Pairing / 428 repair
4
-
5
- - The primary-hello handler now awaits `companion_finish` before notification ACK, matching the working Kage Antigua flow.
6
- - `creds.registered` is persisted only after the `companion_finish` IQ has been accepted by WhatsApp.
7
- - Temporary pairing code, reference, timestamp, and ephemeral key material are cleared after completion.
8
- - `pair-success` merges the verified account/device identity into the live credentials and enforces `registered: true`.
9
- - A WebSocket closure immediately after completed pairing is identified as `restartRequired` (515), so the client starts a normal login with saved credentials instead of repeating registration. The raw WebSocket close code and reason are preserved for diagnosis.
10
- - A close before WhatsApp accepts pairing remains an ordinary connection-close (428); Kage does not bypass server restrictions or rate limits.
11
-
12
- ## Native Flow / review_and_pay
13
-
14
- - `relayMessage()` attaches the Native Flow `biz` node.
15
- - `review_and_pay` maps to `native_flow_name: order_details`.
16
- - The package validates the JSON, JPEG thumbnail bytes, payment-account identifiers, and consistency between `total_amount` and order items.
17
- - The supplied example's 137 / 138 mismatch and placeholder account IDs are rejected with actionable errors; no payment values are silently changed.
18
-
19
- ## Licences
20
-
21
- - Only one root `LICENSE` file is distributed. It is branded Kage Baileys and consolidates the required upstream MIT copyright notices into the same file. The separate `licenses/` directory and `THIRD_PARTY_NOTICES.md` were removed.
22
-
23
- ## Checks
24
-
25
- - `npm run check`: package syntax/import/export and WAProto validation plus targeted flow checks.
26
- - Targeted static assertions: 11/11.
27
- - WAProto: 788 messages, 223 enums, 3,660 fields.
28
- - `npm pack --dry-run`: package file list checked.
29
-
30
- ## Limits
31
-
32
- - No live WhatsApp account was linked in this environment. A server-side 428 before `primary_hello` completion can still be caused by an unsupported browser identity, stale protocol version, or temporary WhatsApp-side restriction.
33
- - Recent upstream reports show Desktop browser tuples can be rejected with 428 while a Chrome browser tuple succeeds; Mahiru's configured `Browsers.macOS('Chrome')` is not the rejected `Desktop` tuple. Some new-device 428 cases continued even with a fresh protocol version and may be server-side restrictions.
34
- - The `review_and_pay` structure is validated; WhatsApp client rendering and payment availability depend on supported region, account type and provider.