xmd-baileys 1.0.15 → 1.0.17

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.
@@ -40,18 +40,33 @@ const WABinary_1 = require("../WABinary");
40
40
  const sender_key_name_1 = require("./Group/sender-key-name");
41
41
  const sender_key_record_1 = require("./Group/sender-key-record");
42
42
  const Group_1 = require("./Group");
43
+
44
+ // Session cache - keeps working sessions in memory for fast reuse
45
+ const _sessionCache = new Map();
46
+ const _groupCache = new Map();
47
+ const CACHE_TTL = 300000; // 5 minutes
48
+
43
49
  function makeLibSignalRepository(auth) {
44
50
  const storage = signalStorage(auth);
45
51
  return {
46
52
  async decryptGroupMessage({ group, authorJid, msg }) {
47
53
  const senderName = jidToSignalSenderKeyName(group, authorJid);
48
- const cipher = new Group_1.GroupCipher(storage, senderName);
54
+ const cacheKey = senderName.toString();
55
+
56
+ // Use cached cipher if available
57
+ let cipher = _groupCache.get(cacheKey);
58
+ if (!cipher) {
59
+ cipher = new Group_1.GroupCipher(storage, senderName);
60
+ _groupCache.set(cacheKey, cipher);
61
+ setTimeout(() => _groupCache.delete(cacheKey), CACHE_TTL);
62
+ }
63
+
49
64
  try {
50
65
  return await cipher.decrypt(msg);
51
66
  } catch (err) {
67
+ _groupCache.delete(cacheKey); // Clear bad cache
52
68
  if (err.message?.includes('Bad MAC') || err.message?.includes('No valid sessions')) {
53
- const senderNameStr = senderName.toString();
54
- await auth.keys.set({ 'sender-key': { [senderNameStr]: null } });
69
+ await auth.keys.set({ 'sender-key': { [cacheKey]: null } });
55
70
  const e = new Error('SESSION_CIPHER_FAIL');
56
71
  e.needsRetry = true;
57
72
  throw e;
@@ -75,7 +90,16 @@ function makeLibSignalRepository(auth) {
75
90
  },
76
91
  async decryptMessage({ jid, type, ciphertext }) {
77
92
  const addr = jidToSignalProtocolAddress(jid);
78
- const session = new libsignal.SessionCipher(storage, addr);
93
+ const cacheKey = addr.toString();
94
+
95
+ // Use cached session cipher if available
96
+ let session = _sessionCache.get(cacheKey);
97
+ if (!session) {
98
+ session = new libsignal.SessionCipher(storage, addr);
99
+ _sessionCache.set(cacheKey, session);
100
+ setTimeout(() => _sessionCache.delete(cacheKey), CACHE_TTL);
101
+ }
102
+
79
103
  let result;
80
104
  try {
81
105
  switch (type) {
@@ -89,9 +113,9 @@ function makeLibSignalRepository(auth) {
89
113
  throw new Error(`Unknown message type: ${type}`);
90
114
  }
91
115
  } catch (err) {
116
+ _sessionCache.delete(cacheKey); // Clear bad cache
92
117
  if (err.message?.includes('Bad MAC') || err.message?.includes('No valid sessions')) {
93
- const sessionId = addr.toString();
94
- await auth.keys.set({ session: { [sessionId]: null } });
118
+ await auth.keys.set({ session: { [cacheKey]: null } });
95
119
  const e = new Error('SESSION_CIPHER_FAIL');
96
120
  e.needsRetry = true;
97
121
  throw e;
package/lib/index.js CHANGED
@@ -3,20 +3,12 @@
3
3
  const chalk = require("chalk");
4
4
 
5
5
  const _ol = console.log, _ow = console.warn, _oi = console.info, _oe = console.error;
6
- let _rc = 0, _lt = 0;
7
6
  const _sf = (m) => {
8
7
  if (typeof m === 'object' && m?._chains) return true;
9
8
  if (typeof m !== 'string') return false;
10
- if (m.includes('Bad MAC') || m.includes('SESSION_CIPHER_FAIL') || m.includes('No valid sessions')) {
11
- _rc++;
12
- const now = Date.now();
13
- if (now - _lt > 30000 && _rc > 0) {
14
- _ol.call(console, chalk.yellow(`[XMD] `) + chalk.gray(`Session auto-repair: ${_rc} key(s) refreshed`));
15
- _rc = 0; _lt = now;
16
- }
17
- return true;
18
- }
19
- return m.includes('Closing') || m.includes('SessionEntry') || m.includes('decrypt') && m.includes('fail');
9
+ if (m.includes('Bad MAC') || m.includes('SESSION_CIPHER_FAIL') || m.includes('No valid sessions') ||
10
+ m.includes('decrypt') && m.includes('fail') || m.includes('Closing') || m.includes('SessionEntry')) return true;
11
+ return false;
20
12
  };
21
13
  console.log = (...a) => { if (!_sf(a[0])) _ol.apply(console, a); };
22
14
  console.warn = (...a) => { if (!_sf(a[0])) _ow.apply(console, a); };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xmd-baileys",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "A lightweight, full-featured WhatsApp Web API library for Node.js",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",