aunubaileys 1.0.9 → 1.0.10

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.
package/README.md CHANGED
@@ -64,6 +64,10 @@ Everything else is pure upstream. When upstream moves, we rebase the patch layer
64
64
 
65
65
 
66
66
 
67
+ ### 🆕 What's new in 1.0.10 / Baru di 1.0.10
68
+
69
+ - 🛠️ **Reliability hardening against classic upstream weaknesses** — (1) **Retry receipts now get answered even without a custom `getMessage` store:** a built-in per-socket sent-message cache (last 256 relayed messages, whatsmeow-style) backs up your store, ending the silent "message not available" failures behind stuck unreadable/undelivered messages. Disable with `sentMessagesCacheSize: 0`. (2) **Pairing is now single-flight:** `requestPairingCode` refuses to double-fire (double requests invalidate the first code) and refuses outright on already-registered sessions, with clear errors instead of silent corruption. (3) **Handshake timeout raised:** default `connectTimeoutMs` goes 20s → 60s so pairing no longer times out on slow networks.
70
+
67
71
  ### 🆕 What's new in 1.0.9 / Baru di 1.0.9
68
72
 
69
73
  - 🪪 **LID identity layer** — WhatsApp is migrating identities from phone numbers to LIDs, and incoming stanzas already carry both forms. AunuBaileys now learns the mapping from live traffic: every socket exposes `sock.aunuLid`, a bidirectional PN <-> LID store with local `getLIDForPN()` / `getPNForLID()` lookups (no extra server queries), LRU capping, and optional JSON persistence. Prefer a standalone store? `makeLIDStore()` is exported from the package root, fully typed.
@@ -30,7 +30,7 @@ export const DEFAULT_CONNECTION_CONFIG = {
30
30
  version: version,
31
31
  browser: Browsers.ubuntu('Chrome'),
32
32
  waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
33
- connectTimeoutMs: 20000,
33
+ connectTimeoutMs: 60000,
34
34
  keepAliveIntervalMs: 30000,
35
35
  logger: logger.child({ class: 'baileys' }),
36
36
  emitOwnEvents: true,
@@ -7,6 +7,7 @@ import { DEFAULT_CACHE_TTLS, KEY_BUNDLE_TYPE, MIN_PREKEY_COUNT } from '../Defaul
7
7
  import { WAMessageStatus, WAMessageStubType } from '../Types/index.js';
8
8
  import { aesDecryptCTR, aesEncryptGCM, cleanMessage, Curve, decodeMediaRetryNode, decodeMessageNode, decryptMessageNode, delay, derivePairingCodeKey, encodeBigEndian, encodeSignedDeviceIdentity, getCallStatusFromNode, getHistoryMsg, getNextPreKeys, getStatusFromReceiptType, hkdf, MISSING_KEYS_ERROR_TEXT, NACK_REASONS, NO_MESSAGE_FOUND_ERROR_TEXT, unixTimestampSeconds, xmppPreKey, xmppSignedPreKey } from '../Utils/index.js';
9
9
  import { makeMutex } from '../Utils/make-mutex.js';
10
+ import { sentMessageCacheFor } from '../Utils/sent-message-cache.js';
10
11
  import { areJidsSameUser, getAllBinaryNodeChildren, getBinaryNodeChild, getBinaryNodeChildBuffer, getBinaryNodeChildren, getBinaryNodeChildString, isJidGroup, isJidStatusBroadcast, isJidUser, isLidUser, jidDecode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary/index.js';
11
12
  import { extractGroupMetadata } from './groups.js';
12
13
  import { makeMessagesSocket } from './messages-send.js';
@@ -465,7 +466,7 @@ export const makeMessagesRecvSocket = (config) => {
465
466
  };
466
467
  const sendMessagesAgain = async (key, ids, retryNode) => {
467
468
  // todo: implement a cache to store the last 256 sent messages (copy whatsmeow)
468
- const msgs = await Promise.all(ids.map(id => getMessage({ ...key, id })));
469
+ const msgs = await Promise.all(ids.map(async (id) => (await getMessage({ ...key, id })) ?? sentMessageCacheFor(config).get(key.remoteJid, id)));
469
470
  const remoteJid = key.remoteJid;
470
471
  const participant = key.participant || remoteJid;
471
472
  // if it's the primary jid sending the request
@@ -3,6 +3,7 @@ import { Boom } from '@hapi/boom';
3
3
  import { proto } from '../../WAProto/index.js';
4
4
  import { BIZ_BOT_SUPPORT_PAYLOAD, DEFAULT_CACHE_TTLS, WA_DEFAULT_EPHEMERAL } from '../Defaults/index.js';
5
5
  import { aggregateMessageKeysNotFromMe, assertMediaContent, bindWaitForEvent, decryptMediaRetryData, encodeNewsletterMessage, encodeSignedDeviceIdentity, encodeWAMessage, encryptMediaRetryRequest, delay, extractDeviceJids, generateMessageIDV2, generateWAMessage, getStatusCodeForMediaRetry, getUrlFromDirectPath, getWAUploadToServer, normalizeMessageContent, parseAndInjectE2ESessions, unixTimestampSeconds } from '../Utils/index.js';
6
+ import { sentMessageCacheFor } from '../Utils/sent-message-cache.js';
6
7
  import { getUrlInfo } from '../Utils/link-preview.js';
7
8
  import { areJidsSameUser, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, isJidNewsletter, isJidUser, isLidUser, jidDecode, jidEncode, jidNormalizedUser, S_WHATSAPP_NET } from '../WABinary/index.js';
8
9
  import { USyncQuery, USyncUser } from '../WAUSync/index.js';
@@ -311,6 +312,7 @@ export const makeMessagesSocket = (config) => {
311
312
  };
312
313
  logger.debug({ msgId }, `sending newsletter message to ${jid}`);
313
314
  await sendNode(stanza);
315
+ sentMessageCacheFor(config).add(jid, msgId, message);
314
316
  return;
315
317
  }
316
318
  if (normalizeMessageContent(message)?.pinInChatMessage) {
@@ -486,6 +488,7 @@ export const makeMessagesSocket = (config) => {
486
488
  logger.debug({ msgId }, `sending message to ${participants.length} devices`);
487
489
  await sendNode(stanza);
488
490
  });
491
+ sentMessageCacheFor(config).add(jid, msgId, message);
489
492
  return msgId;
490
493
  };
491
494
  const getMessageType = (message) => {
@@ -370,11 +370,20 @@ export const makeSocket = (config) => {
370
370
  }
371
371
  end(new Boom(msg || 'Intentional Logout', { statusCode: DisconnectReason.loggedOut }));
372
372
  };
373
+ let pairingCodeRequestInFlight = false;
373
374
  const requestPairingCode = async (phoneNumber, customPairingCode) => {
375
+ if (authState.creds.registered) {
376
+ throw new Error('requestPairingCode: this session is already registered — pairing codes are only for new logins');
377
+ }
378
+ if (pairingCodeRequestInFlight) {
379
+ throw new Error('requestPairingCode: a pairing code request is already in flight — wait for it to finish instead of firing another one (double requests invalidate the first code)');
380
+ }
374
381
  const pairingCode = customPairingCode ?? bytesToCrockford(randomBytes(5));
375
382
  if (customPairingCode && customPairingCode?.length !== 8) {
376
383
  throw new Error('Custom pairing code must be exactly 8 chars');
377
384
  }
385
+ pairingCodeRequestInFlight = true;
386
+ try {
378
387
  authState.creds.pairingCode = pairingCode;
379
388
  authState.creds.me = {
380
389
  id: jidEncode(phoneNumber, 's.whatsapp.net'),
@@ -428,6 +437,10 @@ export const makeSocket = (config) => {
428
437
  ]
429
438
  });
430
439
  return authState.creds.pairingCode;
440
+ }
441
+ finally {
442
+ pairingCodeRequestInFlight = false;
443
+ }
431
444
  };
432
445
  async function generatePairingKey() {
433
446
  const salt = randomBytes(32);
@@ -75,6 +75,8 @@ export type SocketConfig = {
75
75
  * map to store the retry counts for failed messages;
76
76
  * used to determine whether to retry a message or not */
77
77
  msgRetryCounterCache?: CacheStore;
78
+ /** AunuBaileys: capacity of the built-in sent-message cache used to answer retry receipts when `getMessage` has no copy. Default 256; set 0 to disable. */
79
+ sentMessagesCacheSize?: number;
78
80
  /** provide a cache to store a user's device list */
79
81
  userDevicesCache?: CacheStore;
80
82
  /** cache to store call offers */
@@ -0,0 +1,15 @@
1
+ import type { WAMessageContent } from '../Types/index.js';
2
+ export interface SentMessageCache {
3
+ add(remoteJid: string, id: string, message: WAMessageContent): void;
4
+ get(remoteJid: string, id: string): WAMessageContent | undefined;
5
+ readonly size: number;
6
+ clear(): void;
7
+ }
8
+ /**
9
+ * Create a bounded FIFO cache of recently relayed message contents,
10
+ * used to answer retry receipts when the app's `getMessage` store
11
+ * has no copy. Default capacity 256.
12
+ */
13
+ export declare const makeSentMessageCache: (maxEntries?: number) => SentMessageCache;
14
+ /** Returns the per-socket cache bound to a socket config object (internal plumbing, also handy in tests). */
15
+ export declare const sentMessageCacheFor: (config: object) => SentMessageCache;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * AunuBaileys sent-message cache for retry receipts.
3
+ *
4
+ * When a peer cannot decrypt a message we sent, WhatsApp asks for a
5
+ * resend via a retry receipt. Upstream Baileys can only answer when
6
+ * the app supplied a `getMessage` store; with the default
7
+ * `getMessage: async () => undefined` every retry dies with
8
+ * "message not available" and the peer never receives the message
9
+ * (the classic "read but never delivered / stuck unreadable" bug).
10
+ *
11
+ * This cache keeps the last N relayed message contents per socket
12
+ * (whatsmeow-style, default 256) so `sendMessagesAgain` can fall
13
+ * back to it when the app's own `getMessage` has nothing. The app's
14
+ * store always wins; this is only a fallback. Set
15
+ * `sentMessagesCacheSize: 0` in the socket config to disable.
16
+ */
17
+ export const makeSentMessageCache = (maxEntries = 256) => {
18
+ const cache = new Map();
19
+ const keyOf = (remoteJid, id) => `${remoteJid}:${id}`;
20
+ return {
21
+ add: (remoteJid, id, message) => {
22
+ if (!remoteJid || !id || !message || maxEntries <= 0) {
23
+ return;
24
+ }
25
+ const key = keyOf(remoteJid, id);
26
+ cache.delete(key);
27
+ cache.set(key, message);
28
+ while (cache.size > maxEntries) {
29
+ cache.delete(cache.keys().next().value);
30
+ }
31
+ },
32
+ get: (remoteJid, id) => {
33
+ if (!remoteJid || !id) {
34
+ return undefined;
35
+ }
36
+ return cache.get(keyOf(remoteJid, id));
37
+ },
38
+ get size() {
39
+ return cache.size;
40
+ },
41
+ clear: () => cache.clear()
42
+ };
43
+ };
44
+ const cachesByConfig = new WeakMap();
45
+ /**
46
+ * Returns the per-socket sent-message cache bound to a socket
47
+ * config object. Send and receive layers of the same socket share
48
+ * one config object, so a WeakMap keyed by it gives every socket
49
+ * its own isolated cache without any extra plumbing.
50
+ */
51
+ export const sentMessageCacheFor = (config) => {
52
+ let cache = cachesByConfig.get(config);
53
+ if (!cache) {
54
+ const size = typeof config?.sentMessagesCacheSize === 'number' ? config.sentMessagesCacheSize : 256;
55
+ cache = makeSentMessageCache(size);
56
+ cachesByConfig.set(config, cache);
57
+ }
58
+ return cache;
59
+ };
package/lib/index.d.ts CHANGED
@@ -17,3 +17,4 @@ export * from './Utils/aunu-handler';
17
17
  export * from './Utils/builders';
18
18
  export * from './Utils/antiban';
19
19
  export * from './Utils/lid-store.js';
20
+ export * from './Utils/sent-message-cache.js';
package/lib/index.js CHANGED
@@ -16,3 +16,4 @@ export * from './Utils/aunu-handler.js';
16
16
  export * from './Utils/builders.js';
17
17
  export * from './Utils/antiban.js';
18
18
  export * from './Utils/lid-store.js';
19
+ export * from './Utils/sent-message-cache.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "aunubaileys",
3
3
  "type": "module",
4
- "version": "1.0.9",
4
+ "version": "1.0.10",
5
5
  "description": "AunuBaileys \u2014 a WhatsApp Web API for bots that never sleep. Full upstream power with the beloved in-memory store restored and pairing that just works. Forged by AunuCore.",
6
6
  "keywords": [
7
7
  "whatsapp",