@vinzsocket/baileys 2.8.10 → 2.8.20

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.
Files changed (83) hide show
  1. package/lib/Defaults/index.js +75 -73
  2. package/lib/Signal/Group/ciphertext-message.js +9 -9
  3. package/lib/Signal/Group/group-session-builder.js +21 -21
  4. package/lib/Signal/Group/group_cipher.js +79 -81
  5. package/lib/Signal/Group/index.js +2 -2
  6. package/lib/Signal/Group/keyhelper.js +9 -9
  7. package/lib/Signal/Group/queue-job.js +43 -46
  8. package/lib/Signal/Group/sender-chain-key.js +24 -25
  9. package/lib/Signal/Group/sender-key-distribution-message.js +50 -58
  10. package/lib/Signal/Group/sender-key-message.js +55 -60
  11. package/lib/Signal/Group/sender-key-name.js +38 -39
  12. package/lib/Signal/Group/sender-key-record.js +39 -41
  13. package/lib/Signal/Group/sender-key-state.js +85 -89
  14. package/lib/Signal/Group/sender-message-key.js +22 -22
  15. package/lib/Signal/libsignal.js +387 -292
  16. package/lib/Signal/lid-mapping.js +98 -93
  17. package/lib/Socket/Client/types.js +6 -6
  18. package/lib/Socket/Client/websocket.js +44 -44
  19. package/lib/Socket/business.js +350 -365
  20. package/lib/Socket/chats.js +1019 -914
  21. package/lib/Socket/communities.js +454 -376
  22. package/lib/Socket/groups.js +378 -310
  23. package/lib/Socket/index.js +11 -11
  24. package/lib/Socket/messages-recv.js +1355 -1182
  25. package/lib/Socket/messages-send.js +1224 -1081
  26. package/lib/Socket/mex.js +40 -34
  27. package/lib/Socket/newsletter.js +212 -169
  28. package/lib/Socket/socket.js +896 -757
  29. package/lib/Store/index.js +1 -1
  30. package/lib/Store/make-cache-manager-store.js +75 -79
  31. package/lib/Store/make-in-memory-store.js +450 -389
  32. package/lib/Store/make-ordered-dictionary.js +73 -76
  33. package/lib/Store/object-repository.js +24 -22
  34. package/lib/Types/Chat.js +1 -7
  35. package/lib/Types/Label.js +20 -20
  36. package/lib/Types/LabelAssociation.js +2 -2
  37. package/lib/Types/Message.js +2 -2
  38. package/lib/Types/Newsletter.js +24 -24
  39. package/lib/Types/Socket.js +1 -1
  40. package/lib/Types/State.js +8 -8
  41. package/lib/Types/index.js +10 -10
  42. package/lib/Utils/auth-utils.js +408 -401
  43. package/lib/Utils/baileys-event-stream.js +44 -35
  44. package/lib/Utils/business.js +200 -197
  45. package/lib/Utils/chat-utils.js +775 -708
  46. package/lib/Utils/crypto.js +97 -85
  47. package/lib/Utils/decode-wa-message.js +240 -227
  48. package/lib/Utils/event-buffer.js +547 -518
  49. package/lib/Utils/extended-messages.js +250 -203
  50. package/lib/Utils/generics.js +300 -289
  51. package/lib/Utils/history.js +76 -69
  52. package/lib/Utils/lid-resolver.js +47 -42
  53. package/lib/Utils/link-preview.js +75 -65
  54. package/lib/Utils/logger.js +3 -1
  55. package/lib/Utils/lt-hash.js +38 -34
  56. package/lib/Utils/make-mutex.js +32 -34
  57. package/lib/Utils/mention.js +79 -86
  58. package/lib/Utils/message-retry-manager.js +148 -136
  59. package/lib/Utils/messages-media.js +652 -562
  60. package/lib/Utils/messages.js +753 -707
  61. package/lib/Utils/noise-handler.js +156 -135
  62. package/lib/Utils/process-message.js +404 -341
  63. package/lib/Utils/signal.js +180 -118
  64. package/lib/Utils/use-multi-file-auth-state.js +87 -91
  65. package/lib/Utils/validate-connection.js +167 -142
  66. package/lib/WABinary/constants.js +28 -1293
  67. package/lib/WABinary/decode.js +220 -218
  68. package/lib/WABinary/encode.js +199 -201
  69. package/lib/WABinary/generic-utils.js +80 -77
  70. package/lib/WABinary/jid-utils.js +41 -35
  71. package/lib/WAM/BinaryInfo.js +7 -7
  72. package/lib/WAM/constants.js +1 -15241
  73. package/lib/WAM/encode.js +124 -131
  74. package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -23
  75. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +51 -47
  76. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -22
  77. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -32
  78. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -19
  79. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -43
  80. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +17 -17
  81. package/lib/WAUSync/USyncQuery.js +82 -84
  82. package/lib/WAUSync/USyncUser.js +20 -20
  83. package/package.json +5 -2
@@ -11,38 +11,31 @@ const mentionCache = new Map();
11
11
  * @returns {string[]}
12
12
  */
13
13
  export function normalizeMentions(mentions) {
14
- const cacheKey = JSON.stringify(mentions);
15
- if (mentionCache.has(cacheKey)) {
16
- return mentionCache.get(cacheKey);
14
+ const cacheKey = JSON.stringify(mentions);
15
+ if (mentionCache.has(cacheKey)) {
16
+ return mentionCache.get(cacheKey);
17
+ }
18
+ if (!mentions) return [];
19
+ if (!Array.isArray(mentions)) mentions = [mentions];
20
+ const result = mentions.map(jid => {
21
+ if (typeof jid !== 'string') return null;
22
+ jid = jid.trim();
23
+ if (!jid) return null;
24
+ // Already has @s.whatsapp.net or @lid
25
+ if (jid.includes('@s.whatsapp.net') || jid.includes('@lid')) {
26
+ return jid;
17
27
  }
18
- if (!mentions)
19
- return [];
20
- if (!Array.isArray(mentions))
21
- mentions = [mentions];
22
- const result = mentions
23
- .map(jid => {
24
- if (typeof jid !== 'string')
25
- return null;
26
- jid = jid.trim();
27
- if (!jid)
28
- return null;
29
- // Already has @s.whatsapp.net or @lid
30
- if (jid.includes('@s.whatsapp.net') || jid.includes('@lid')) {
31
- return jid;
32
- }
33
- // Has @ but not complete
34
- if (jid.includes('@')) {
35
- return jid;
36
- }
37
- // Plain number
38
- return jid + '@s.whatsapp.net';
39
- })
40
- .filter(Boolean);
41
- mentionCache.set(cacheKey, result);
42
- // Simple cache size limit
43
- if (mentionCache.size > 1000)
44
- mentionCache.clear();
45
- return result;
28
+ // Has @ but not complete
29
+ if (jid.includes('@')) {
30
+ return jid;
31
+ }
32
+ // Plain number
33
+ return jid + '@s.whatsapp.net';
34
+ }).filter(Boolean);
35
+ mentionCache.set(cacheKey, result);
36
+ // Simple cache size limit
37
+ if (mentionCache.size > 1000) mentionCache.clear();
38
+ return result;
46
39
  }
47
40
  /**
48
41
  * Apply mentions into a message object (for use by custom content builders)
@@ -52,43 +45,39 @@ export function normalizeMentions(mentions) {
52
45
  * @returns {object} msg
53
46
  */
54
47
  export function applyMentions(msg, mentions, contextInfo = {}) {
55
- const normalized = normalizeMentions(mentions);
56
- if (normalized.length === 0)
57
- return msg;
58
- // messageContextInfo
59
- if (!msg.messageContextInfo)
60
- msg.messageContextInfo = {};
61
- if (!msg.messageContextInfo.mentionedJid)
62
- msg.messageContextInfo.mentionedJid = [];
63
- const existing = new Set(msg.messageContextInfo.mentionedJid);
48
+ const normalized = normalizeMentions(mentions);
49
+ if (normalized.length === 0) return msg;
50
+ // messageContextInfo
51
+ if (!msg.messageContextInfo) msg.messageContextInfo = {};
52
+ if (!msg.messageContextInfo.mentionedJid) msg.messageContextInfo.mentionedJid = [];
53
+ const existing = new Set(msg.messageContextInfo.mentionedJid);
54
+ for (const jid of normalized) {
55
+ if (!existing.has(jid)) {
56
+ msg.messageContextInfo.mentionedJid.push(jid);
57
+ existing.add(jid);
58
+ }
59
+ }
60
+ // contextInfo
61
+ if (contextInfo && typeof contextInfo === 'object') {
62
+ if (!contextInfo.mentionedJid) contextInfo.mentionedJid = [];
64
63
  for (const jid of normalized) {
65
- if (!existing.has(jid)) {
66
- msg.messageContextInfo.mentionedJid.push(jid);
67
- existing.add(jid);
68
- }
64
+ if (!contextInfo.mentionedJid.includes(jid)) {
65
+ contextInfo.mentionedJid.push(jid);
66
+ }
69
67
  }
70
- // contextInfo
71
- if (contextInfo && typeof contextInfo === 'object') {
72
- if (!contextInfo.mentionedJid)
73
- contextInfo.mentionedJid = [];
74
- for (const jid of normalized) {
75
- if (!contextInfo.mentionedJid.includes(jid)) {
76
- contextInfo.mentionedJid.push(jid);
77
- }
78
- }
68
+ }
69
+ // Extra for interactiveMessage
70
+ if (msg.interactiveMessage?.contextInfo) {
71
+ if (!msg.interactiveMessage.contextInfo.mentionedJid) {
72
+ msg.interactiveMessage.contextInfo.mentionedJid = [];
79
73
  }
80
- // Extra for interactiveMessage
81
- if (msg.interactiveMessage?.contextInfo) {
82
- if (!msg.interactiveMessage.contextInfo.mentionedJid) {
83
- msg.interactiveMessage.contextInfo.mentionedJid = [];
84
- }
85
- for (const jid of normalized) {
86
- if (!msg.interactiveMessage.contextInfo.mentionedJid.includes(jid)) {
87
- msg.interactiveMessage.contextInfo.mentionedJid.push(jid);
88
- }
89
- }
74
+ for (const jid of normalized) {
75
+ if (!msg.interactiveMessage.contextInfo.mentionedJid.includes(jid)) {
76
+ msg.interactiveMessage.contextInfo.mentionedJid.push(jid);
77
+ }
90
78
  }
91
- return msg;
79
+ }
80
+ return msg;
92
81
  }
93
82
  /**
94
83
  * Build a "tag all" text block + mentionedJid list from a group's participants,
@@ -109,26 +98,30 @@ export function applyMentions(msg, mentions, contextInfo = {}) {
109
98
  * @returns {{ text: string, mentions: string[] }}
110
99
  */
111
100
  export function buildTagAllText(participants, options = {}) {
112
- const { getDisplayName = lidResolver.getDisplayName, header = '', footer = '' } = options;
113
- const lines = [];
114
- const mentions = [];
115
- for (const p of participants || []) {
116
- // Prefer a real phone-number JID when WhatsApp actually disclosed one.
117
- const realJid = p.jid && p.jid.endsWith('@s.whatsapp.net')
118
- ? p.jid
119
- : (p.phoneNumber ? (p.phoneNumber.includes('@') ? p.phoneNumber : `${p.phoneNumber}@s.whatsapp.net`) : null);
120
- const mentionJid = realJid || p.id; // what we actually tag/ping (lid is fine for this)
121
- mentions.push(mentionJid);
122
- if (realJid) {
123
- // We know the real phone number — safe & useful to show it.
124
- lines.push(`@${realJid.split('@')[0]}`);
125
- }
126
- else {
127
- // LID-only participant: don't print fake-looking digits.
128
- const name = getDisplayName ? getDisplayName(p.id) : null;
129
- lines.push(name ? `@${name}` : '@Member');
130
- }
101
+ const {
102
+ getDisplayName = lidResolver.getDisplayName,
103
+ header = '',
104
+ footer = ''
105
+ } = options;
106
+ const lines = [];
107
+ const mentions = [];
108
+ for (const p of participants || []) {
109
+ // Prefer a real phone-number JID when WhatsApp actually disclosed one.
110
+ const realJid = p.jid && p.jid.endsWith('@s.whatsapp.net') ? p.jid : p.phoneNumber ? p.phoneNumber.includes('@') ? p.phoneNumber : `${p.phoneNumber}@s.whatsapp.net` : null;
111
+ const mentionJid = realJid || p.id; // what we actually tag/ping (lid is fine for this)
112
+ mentions.push(mentionJid);
113
+ if (realJid) {
114
+ // We know the real phone number — safe & useful to show it.
115
+ lines.push(`@${realJid.split('@')[0]}`);
116
+ } else {
117
+ // LID-only participant: don't print fake-looking digits.
118
+ const name = getDisplayName ? getDisplayName(p.id) : null;
119
+ lines.push(name ? `@${name}` : '@Member');
131
120
  }
132
- const body = [header, lines.join('\n'), footer].filter(Boolean).join('\n\n');
133
- return { text: body, mentions };
134
- }
121
+ }
122
+ const body = [header, lines.join('\n'), footer].filter(Boolean).join('\n\n');
123
+ return {
124
+ text: body,
125
+ mentions
126
+ };
127
+ }
@@ -5,145 +5,157 @@ const RECENT_MESSAGES_SIZE = 512;
5
5
  const RECREATE_SESSION_TIMEOUT = 60 * 60 * 1000; // 1 hour in milliseconds
6
6
  const PHONE_REQUEST_DELAY = 3000;
7
7
  export class MessageRetryManager {
8
- constructor(logger, maxMsgRetryCount) {
9
- this.logger = logger;
10
- this.recentMessagesMap = new LRUCache({
11
- max: RECENT_MESSAGES_SIZE
12
- });
13
- this.sessionRecreateHistory = new LRUCache({
14
- ttl: RECREATE_SESSION_TIMEOUT * 2,
15
- ttlAutopurge: true
16
- });
17
- this.retryCounters = new LRUCache({
18
- ttl: 15 * 60 * 1000,
19
- ttlAutopurge: true,
20
- updateAgeOnGet: true
21
- }); // 15 minutes TTL
22
- this.pendingPhoneRequests = {};
23
- this.maxMsgRetryCount = 5;
24
- this.statistics = {
25
- totalRetries: 0,
26
- successfulRetries: 0,
27
- failedRetries: 0,
28
- mediaRetries: 0,
29
- sessionRecreations: 0,
30
- phoneRequests: 0
31
- };
32
- this.maxMsgRetryCount = maxMsgRetryCount;
8
+ constructor(logger, maxMsgRetryCount) {
9
+ this.logger = logger;
10
+ this.recentMessagesMap = new LRUCache({
11
+ max: RECENT_MESSAGES_SIZE
12
+ });
13
+ this.sessionRecreateHistory = new LRUCache({
14
+ ttl: RECREATE_SESSION_TIMEOUT * 2,
15
+ ttlAutopurge: true
16
+ });
17
+ this.retryCounters = new LRUCache({
18
+ ttl: 15 * 60 * 1000,
19
+ ttlAutopurge: true,
20
+ updateAgeOnGet: true
21
+ }); // 15 minutes TTL
22
+ this.pendingPhoneRequests = {};
23
+ this.maxMsgRetryCount = 5;
24
+ this.statistics = {
25
+ totalRetries: 0,
26
+ successfulRetries: 0,
27
+ failedRetries: 0,
28
+ mediaRetries: 0,
29
+ sessionRecreations: 0,
30
+ phoneRequests: 0
31
+ };
32
+ this.maxMsgRetryCount = maxMsgRetryCount;
33
+ }
34
+ /**
35
+ * Add a recent message to the cache for retry handling
36
+ */
37
+ addRecentMessage(to, id, message) {
38
+ const key = {
39
+ to,
40
+ id
41
+ };
42
+ const keyStr = this.keyToString(key);
43
+ // Add new message
44
+ this.recentMessagesMap.set(keyStr, {
45
+ message,
46
+ timestamp: Date.now()
47
+ });
48
+ this.logger.debug(`Added message to retry cache: ${to}/${id}`);
49
+ }
50
+ /**
51
+ * Get a recent message from the cache
52
+ */
53
+ getRecentMessage(to, id) {
54
+ const key = {
55
+ to,
56
+ id
57
+ };
58
+ const keyStr = this.keyToString(key);
59
+ return this.recentMessagesMap.get(keyStr);
60
+ }
61
+ /**
62
+ * Check if a session should be recreated based on retry count and history
63
+ */
64
+ shouldRecreateSession(jid, retryCount, hasSession) {
65
+ // If we don't have a session, always recreate
66
+ if (!hasSession) {
67
+ this.sessionRecreateHistory.set(jid, Date.now());
68
+ this.statistics.sessionRecreations++;
69
+ return {
70
+ reason: "we don't have a Signal session with them",
71
+ recreate: true
72
+ };
33
73
  }
34
- /**
35
- * Add a recent message to the cache for retry handling
36
- */
37
- addRecentMessage(to, id, message) {
38
- const key = { to, id };
39
- const keyStr = this.keyToString(key);
40
- // Add new message
41
- this.recentMessagesMap.set(keyStr, {
42
- message,
43
- timestamp: Date.now()
44
- });
45
- this.logger.debug(`Added message to retry cache: ${to}/${id}`);
74
+ // Only consider recreation if retry count > 1
75
+ if (retryCount < 2) {
76
+ return {
77
+ reason: '',
78
+ recreate: false
79
+ };
46
80
  }
47
- /**
48
- * Get a recent message from the cache
49
- */
50
- getRecentMessage(to, id) {
51
- const key = { to, id };
52
- const keyStr = this.keyToString(key);
53
- return this.recentMessagesMap.get(keyStr);
81
+ const now = Date.now();
82
+ const prevTime = this.sessionRecreateHistory.get(jid);
83
+ // If no previous recreation or it's been more than an hour
84
+ if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
85
+ this.sessionRecreateHistory.set(jid, now);
86
+ this.statistics.sessionRecreations++;
87
+ return {
88
+ reason: 'retry count > 1 and over an hour since last recreation',
89
+ recreate: true
90
+ };
54
91
  }
55
- /**
56
- * Check if a session should be recreated based on retry count and history
57
- */
58
- shouldRecreateSession(jid, retryCount, hasSession) {
59
- // If we don't have a session, always recreate
60
- if (!hasSession) {
61
- this.sessionRecreateHistory.set(jid, Date.now());
62
- this.statistics.sessionRecreations++;
63
- return {
64
- reason: "we don't have a Signal session with them",
65
- recreate: true
66
- };
67
- }
68
- // Only consider recreation if retry count > 1
69
- if (retryCount < 2) {
70
- return { reason: '', recreate: false };
71
- }
72
- const now = Date.now();
73
- const prevTime = this.sessionRecreateHistory.get(jid);
74
- // If no previous recreation or it's been more than an hour
75
- if (!prevTime || now - prevTime > RECREATE_SESSION_TIMEOUT) {
76
- this.sessionRecreateHistory.set(jid, now);
77
- this.statistics.sessionRecreations++;
78
- return {
79
- reason: 'retry count > 1 and over an hour since last recreation',
80
- recreate: true
81
- };
82
- }
83
- return { reason: '', recreate: false };
84
- }
85
- /**
86
- * Increment retry counter for a message
87
- */
88
- incrementRetryCount(messageId) {
89
- this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
90
- this.statistics.totalRetries++;
91
- return this.retryCounters.get(messageId);
92
- }
93
- /**
94
- * Get retry count for a message
95
- */
96
- getRetryCount(messageId) {
97
- return this.retryCounters.get(messageId) || 0;
98
- }
99
- /**
100
- * Check if message has exceeded maximum retry attempts
101
- */
102
- hasExceededMaxRetries(messageId) {
103
- return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
104
- }
105
- /**
106
- * Mark retry as successful
107
- */
108
- markRetrySuccess(messageId) {
109
- this.statistics.successfulRetries++;
110
- // Clean up retry counter for successful message
111
- this.retryCounters.delete(messageId);
112
- this.cancelPendingPhoneRequest(messageId);
113
- }
114
- /**
115
- * Mark retry as failed
116
- */
117
- markRetryFailed(messageId) {
118
- this.statistics.failedRetries++;
119
- this.retryCounters.delete(messageId);
120
- }
121
- /**
122
- * Schedule a phone request with delay
123
- */
124
- schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
125
- // Cancel any existing request for this message
126
- this.cancelPendingPhoneRequest(messageId);
127
- this.pendingPhoneRequests[messageId] = setTimeout(() => {
128
- delete this.pendingPhoneRequests[messageId];
129
- this.statistics.phoneRequests++;
130
- callback();
131
- }, delay);
132
- this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
133
- }
134
- /**
135
- * Cancel pending phone request
136
- */
137
- cancelPendingPhoneRequest(messageId) {
138
- const timeout = this.pendingPhoneRequests[messageId];
139
- if (timeout) {
140
- clearTimeout(timeout);
141
- delete this.pendingPhoneRequests[messageId];
142
- this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
143
- }
144
- }
145
- keyToString(key) {
146
- return `${key.to}:${key.id}`;
92
+ return {
93
+ reason: '',
94
+ recreate: false
95
+ };
96
+ }
97
+ /**
98
+ * Increment retry counter for a message
99
+ */
100
+ incrementRetryCount(messageId) {
101
+ this.retryCounters.set(messageId, (this.retryCounters.get(messageId) || 0) + 1);
102
+ this.statistics.totalRetries++;
103
+ return this.retryCounters.get(messageId);
104
+ }
105
+ /**
106
+ * Get retry count for a message
107
+ */
108
+ getRetryCount(messageId) {
109
+ return this.retryCounters.get(messageId) || 0;
110
+ }
111
+ /**
112
+ * Check if message has exceeded maximum retry attempts
113
+ */
114
+ hasExceededMaxRetries(messageId) {
115
+ return this.getRetryCount(messageId) >= this.maxMsgRetryCount;
116
+ }
117
+ /**
118
+ * Mark retry as successful
119
+ */
120
+ markRetrySuccess(messageId) {
121
+ this.statistics.successfulRetries++;
122
+ // Clean up retry counter for successful message
123
+ this.retryCounters.delete(messageId);
124
+ this.cancelPendingPhoneRequest(messageId);
125
+ }
126
+ /**
127
+ * Mark retry as failed
128
+ */
129
+ markRetryFailed(messageId) {
130
+ this.statistics.failedRetries++;
131
+ this.retryCounters.delete(messageId);
132
+ }
133
+ /**
134
+ * Schedule a phone request with delay
135
+ */
136
+ schedulePhoneRequest(messageId, callback, delay = PHONE_REQUEST_DELAY) {
137
+ // Cancel any existing request for this message
138
+ this.cancelPendingPhoneRequest(messageId);
139
+ this.pendingPhoneRequests[messageId] = setTimeout(() => {
140
+ delete this.pendingPhoneRequests[messageId];
141
+ this.statistics.phoneRequests++;
142
+ callback();
143
+ }, delay);
144
+ this.logger.debug(`Scheduled phone request for message ${messageId} with ${delay}ms delay`);
145
+ }
146
+ /**
147
+ * Cancel pending phone request
148
+ */
149
+ cancelPendingPhoneRequest(messageId) {
150
+ const timeout = this.pendingPhoneRequests[messageId];
151
+ if (timeout) {
152
+ clearTimeout(timeout);
153
+ delete this.pendingPhoneRequests[messageId];
154
+ this.logger.debug(`Cancelled pending phone request for message ${messageId}`);
147
155
  }
156
+ }
157
+ keyToString(key) {
158
+ return `${key.to}:${key.id}`;
159
+ }
148
160
  }
149
161
  //# sourceMappingURL=message-retry-manager.js.map