jagproject 26.3.22 → 26.3.25

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 (206) hide show
  1. package/WAProto/GenerateStatics.sh +3 -4
  2. package/WAProto/WAProto.proto +1215 -511
  3. package/WAProto/fix-imports.js +73 -0
  4. package/WAProto/index.d.ts +14017 -0
  5. package/WAProto/index.js +64857 -145167
  6. package/engine-requirements.js +4 -7
  7. package/lib/Defaults/index.d.ts +74 -0
  8. package/lib/Defaults/index.js +51 -33
  9. package/lib/Defaults/phonenumber-mcc.json +223 -0
  10. package/lib/Defaults/wileys-version.json +2 -2
  11. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  12. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  13. package/lib/Signal/Group/group-session-builder.js +5 -3
  14. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  15. package/lib/Signal/Group/group_cipher.js +35 -46
  16. package/lib/Signal/Group/index.d.ts +12 -0
  17. package/lib/Signal/Group/index.js +21 -21
  18. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  19. package/lib/Signal/Group/keyhelper.js +2 -2
  20. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  21. package/lib/Signal/Group/sender-chain-key.js +5 -10
  22. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  23. package/lib/Signal/Group/sender-key-distribution-message.js +7 -7
  24. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  25. package/lib/Signal/Group/sender-key-message.js +8 -8
  26. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  27. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  28. package/lib/Signal/Group/sender-key-record.js +7 -16
  29. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  30. package/lib/Signal/Group/sender-key-state.js +25 -37
  31. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  32. package/lib/Signal/Group/sender-message-key.js +2 -2
  33. package/lib/Signal/libsignal.d.ts +5 -0
  34. package/lib/Signal/libsignal.js +358 -54
  35. package/lib/Signal/lid-mapping.d.ts +19 -0
  36. package/lib/Signal/lid-mapping.js +274 -0
  37. package/lib/Socket/Client/index.d.ts +3 -0
  38. package/lib/Socket/Client/index.js +2 -2
  39. package/lib/Socket/Client/types.d.ts +16 -0
  40. package/lib/Socket/Client/types.js +1 -0
  41. package/lib/Socket/Client/websocket.d.ts +13 -0
  42. package/lib/Socket/Client/websocket.js +18 -30
  43. package/lib/Socket/business.d.ts +202 -0
  44. package/lib/Socket/business.js +160 -38
  45. package/lib/Socket/chats.d.ts +111 -0
  46. package/lib/Socket/chats.js +497 -314
  47. package/lib/Socket/communities.d.ts +258 -0
  48. package/lib/Socket/communities.js +438 -0
  49. package/lib/Socket/community.js +333 -0
  50. package/lib/Socket/groups.d.ts +150 -0
  51. package/lib/Socket/groups.js +229 -91
  52. package/lib/Socket/index.d.ts +245 -0
  53. package/lib/Socket/index.js +9 -6
  54. package/lib/Socket/messages-recv.d.ts +187 -0
  55. package/lib/Socket/messages-recv.js +1105 -501
  56. package/lib/Socket/messages-send.d.ts +183 -0
  57. package/lib/Socket/messages-send.js +1184 -501
  58. package/lib/Socket/mex.d.ts +3 -0
  59. package/lib/Socket/mex.js +45 -0
  60. package/lib/Socket/newsletter.d.ts +160 -0
  61. package/lib/Socket/newsletter.js +227 -200
  62. package/lib/Socket/socket.d.ts +55 -0
  63. package/lib/Socket/socket.js +507 -206
  64. package/lib/Socket/usync.js +6 -6
  65. package/lib/Store/index.js +17 -5
  66. package/lib/Store/make-cache-manager-store.js +83 -0
  67. package/lib/Store/make-in-memory-store.js +48 -89
  68. package/lib/Store/make-ordered-dictionary.js +1 -1
  69. package/lib/Types/Auth.d.ts +116 -0
  70. package/lib/Types/Bussines.d.ts +25 -0
  71. package/lib/Types/Bussines.js +2 -0
  72. package/lib/Types/Call.d.ts +15 -0
  73. package/lib/Types/Chat.d.ts +123 -0
  74. package/lib/Types/Chat.js +7 -1
  75. package/lib/Types/Contact.d.ts +24 -0
  76. package/lib/Types/Events.d.ts +237 -0
  77. package/lib/Types/Events.js +1 -0
  78. package/lib/Types/GroupMetadata.d.ts +67 -0
  79. package/lib/Types/Label.d.ts +47 -0
  80. package/lib/Types/Label.js +1 -3
  81. package/lib/Types/LabelAssociation.d.ts +30 -0
  82. package/lib/Types/LabelAssociation.js +1 -3
  83. package/lib/Types/Message.d.ts +305 -0
  84. package/lib/Types/Message.js +9 -5
  85. package/lib/Types/MexUpdates.js +11 -0
  86. package/lib/Types/Newsletter.d.ts +135 -0
  87. package/lib/Types/Newsletter.js +36 -11
  88. package/lib/Types/Product.d.ts +79 -0
  89. package/lib/Types/Signal.d.ts +76 -0
  90. package/lib/Types/Signal.js +1 -0
  91. package/lib/Types/Socket.d.ts +133 -0
  92. package/lib/Types/Socket.js +1 -0
  93. package/lib/Types/State.d.ts +39 -0
  94. package/lib/Types/State.js +12 -0
  95. package/lib/Types/USync.d.ts +26 -0
  96. package/lib/Types/USync.js +1 -0
  97. package/lib/Types/index.d.ts +65 -0
  98. package/lib/Types/index.js +14 -14
  99. package/lib/Utils/audioToBuffer.js +31 -0
  100. package/lib/Utils/auth-utils.d.ts +19 -0
  101. package/lib/Utils/auth-utils.js +222 -123
  102. package/lib/Utils/baileys-event-stream.js +60 -0
  103. package/lib/Utils/browser-utils.d.ts +4 -0
  104. package/lib/Utils/browser-utils.js +38 -29
  105. package/lib/Utils/business.d.ts +23 -0
  106. package/lib/Utils/business.js +54 -48
  107. package/lib/Utils/chat-utils.d.ts +70 -0
  108. package/lib/Utils/chat-utils.js +284 -189
  109. package/lib/Utils/crypto.d.ts +37 -0
  110. package/lib/Utils/crypto.js +16 -41
  111. package/lib/Utils/decode-wa-message.d.ts +48 -0
  112. package/lib/Utils/decode-wa-message.js +128 -48
  113. package/lib/Utils/event-buffer.d.ts +34 -0
  114. package/lib/Utils/event-buffer.js +124 -62
  115. package/lib/Utils/generics.d.ts +91 -0
  116. package/lib/Utils/generics.js +154 -138
  117. package/lib/Utils/history.d.ts +22 -0
  118. package/lib/Utils/history.js +77 -34
  119. package/lib/Utils/identity-change-handler.d.ts +37 -0
  120. package/lib/Utils/identity-change-handler.js +54 -0
  121. package/lib/Utils/index.d.ts +22 -0
  122. package/lib/Utils/index.js +32 -19
  123. package/lib/Utils/link-preview.d.ts +21 -0
  124. package/lib/Utils/link-preview.js +12 -17
  125. package/lib/Utils/logger.d.ts +13 -0
  126. package/lib/Utils/lt-hash.d.ts +8 -0
  127. package/lib/Utils/lt-hash.js +2 -43
  128. package/lib/Utils/make-mutex.d.ts +9 -0
  129. package/lib/Utils/make-mutex.js +21 -27
  130. package/lib/Utils/message-retry-manager.d.ts +110 -0
  131. package/lib/Utils/message-retry-manager.js +143 -45
  132. package/lib/Utils/messages-media.d.ts +130 -0
  133. package/lib/Utils/messages-media.js +429 -502
  134. package/lib/Utils/messages-newsletter.d.ts +84 -0
  135. package/lib/Utils/messages-newsletter.js +295 -0
  136. package/lib/Utils/messages.d.ts +92 -0
  137. package/lib/Utils/messages.js +1099 -400
  138. package/lib/Utils/noise-handler.d.ts +20 -0
  139. package/lib/Utils/noise-handler.js +145 -91
  140. package/lib/Utils/pre-key-manager.d.ts +28 -0
  141. package/lib/Utils/pre-key-manager.js +112 -0
  142. package/lib/Utils/process-message.d.ts +60 -0
  143. package/lib/Utils/process-message.js +316 -184
  144. package/lib/Utils/reporting-utils.d.ts +11 -0
  145. package/lib/Utils/reporting-utils.js +262 -0
  146. package/lib/Utils/resolve-jid.d.ts +43 -0
  147. package/lib/Utils/resolve-jid.js +95 -0
  148. package/lib/Utils/serial-task-queue.js +29 -0
  149. package/lib/Utils/signal.d.ts +34 -0
  150. package/lib/Utils/signal.js +56 -39
  151. package/lib/Utils/streamToBuffer.js +17 -0
  152. package/lib/Utils/sync-action-utils.d.ts +19 -0
  153. package/lib/Utils/sync-action-utils.js +52 -0
  154. package/lib/Utils/tc-token-utils.d.ts +12 -0
  155. package/lib/Utils/tc-token-utils.js +20 -0
  156. package/lib/Utils/use-mongo-file-auth-state.js +71 -0
  157. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  158. package/lib/Utils/use-multi-file-auth-state.js +11 -12
  159. package/lib/Utils/use-single-file-auth-state.js +73 -0
  160. package/lib/Utils/validate-connection.d.ts +11 -0
  161. package/lib/Utils/validate-connection.js +59 -82
  162. package/lib/Utils/wileys-event-stream.js +1 -61
  163. package/lib/WABinary/constants.d.ts +28 -0
  164. package/lib/WABinary/decode.d.ts +7 -0
  165. package/lib/WABinary/decode.js +39 -4
  166. package/lib/WABinary/encode.d.ts +3 -0
  167. package/lib/WABinary/encode.js +17 -11
  168. package/lib/WABinary/generic-utils.d.ts +15 -0
  169. package/lib/WABinary/generic-utils.js +46 -18
  170. package/lib/WABinary/index.d.ts +6 -0
  171. package/lib/WABinary/index.js +9 -5
  172. package/lib/WABinary/jid-utils.d.ts +48 -0
  173. package/lib/WABinary/jid-utils.js +67 -37
  174. package/lib/WABinary/types.d.ts +19 -0
  175. package/lib/WABinary/types.js +34 -0
  176. package/lib/WAM/BinaryInfo.d.ts +9 -0
  177. package/lib/WAM/constants.d.ts +40 -0
  178. package/lib/WAM/constants.js +19183 -11678
  179. package/lib/WAM/encode.d.ts +3 -0
  180. package/lib/WAM/encode.js +15 -17
  181. package/lib/WAM/index.d.ts +4 -0
  182. package/lib/WAM/index.js +3 -3
  183. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  184. package/lib/WAUSync/Protocols/USyncContactProtocol.js +6 -6
  185. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  186. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +9 -9
  187. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  188. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +6 -6
  189. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  190. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +7 -8
  191. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  192. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +18 -17
  193. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  194. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +11 -3
  195. package/lib/WAUSync/Protocols/index.d.ts +5 -0
  196. package/lib/WAUSync/Protocols/index.js +6 -4
  197. package/lib/WAUSync/USyncQuery.d.ts +29 -0
  198. package/lib/WAUSync/USyncQuery.js +38 -30
  199. package/lib/WAUSync/USyncUser.d.ts +13 -0
  200. package/lib/WAUSync/index.d.ts +4 -0
  201. package/lib/WAUSync/index.js +3 -3
  202. package/lib/index.d.ts +12 -0
  203. package/lib/index.js +3 -5
  204. package/package.json +10 -6
  205. package/readme.md +97 -0
  206. package/LICENSE +0 -21
@@ -5,10 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.makeEventBuffer = void 0;
7
7
  const events_1 = __importDefault(require("events"));
8
- const Types_1 = require("../Types");
9
- const generics_1 = require("./generics");
10
- const messages_1 = require("./messages");
11
- const process_message_1 = require("./process-message");
8
+ const index_js_1 = require("../Types/index.js");
9
+ const generics_js_1 = require("./generics.js");
10
+ const messages_js_1 = require("./messages.js");
11
+ const process_message_js_1 = require("./process-message.js");
12
12
  const BUFFERABLE_EVENT = [
13
13
  'messaging-history.set',
14
14
  'chats.upsert',
@@ -21,45 +21,70 @@ const BUFFERABLE_EVENT = [
21
21
  'messages.delete',
22
22
  'messages.reaction',
23
23
  'message-receipt.update',
24
- 'groups.update',
24
+ 'groups.update'
25
25
  ];
26
26
  const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT);
27
27
  /**
28
28
  * The event buffer logically consolidates different events into a single event
29
29
  * making the data processing more efficient.
30
- * @param ev the wileys event emitter
31
30
  */
32
31
  const makeEventBuffer = (logger) => {
33
32
  const ev = new events_1.default();
34
33
  const historyCache = new Set();
35
34
  let data = makeBufferData();
36
- let buffersInProgress = 0;
37
- // take the generic event and fire it as a wileys event
35
+ let isBuffering = false;
36
+ let bufferTimeout = null;
37
+ let flushPendingTimeout = null; // Add a specific timer for the debounced flush to prevent leak
38
+ let bufferCount = 0;
39
+ const MAX_HISTORY_CACHE_SIZE = 10000; // Limit the history cache size to prevent memory bloat
40
+ const BUFFER_TIMEOUT_MS = 30000; // 30 seconds
41
+ // take the generic event and fire it as a baileys event
38
42
  ev.on('event', (map) => {
39
43
  for (const event in map) {
40
44
  ev.emit(event, map[event]);
41
45
  }
42
46
  });
43
47
  function buffer() {
44
- buffersInProgress += 1;
48
+ if (!isBuffering) {
49
+ logger.debug('Event buffer activated');
50
+ isBuffering = true;
51
+ bufferCount = 0;
52
+ if (bufferTimeout) {
53
+ clearTimeout(bufferTimeout);
54
+ }
55
+ bufferTimeout = setTimeout(() => {
56
+ if (isBuffering) {
57
+ logger.warn('Buffer timeout reached, auto-flushing');
58
+ flush();
59
+ }
60
+ }, BUFFER_TIMEOUT_MS);
61
+ }
62
+ // Always increment count when requested
63
+ bufferCount++;
45
64
  }
46
- function flush(force = false) {
47
- // no buffer going on
48
- if (!buffersInProgress) {
65
+ function flush() {
66
+ if (!isBuffering) {
49
67
  return false;
50
68
  }
51
- if (!force) {
52
- // reduce the number of buffers in progress
53
- buffersInProgress -= 1;
54
- // if there are still some buffers going on
55
- // then we don't flush now
56
- if (buffersInProgress) {
57
- return false;
58
- }
69
+ logger.debug({ bufferCount }, 'Flushing event buffer');
70
+ isBuffering = false;
71
+ bufferCount = 0;
72
+ // Clear timeout
73
+ if (bufferTimeout) {
74
+ clearTimeout(bufferTimeout);
75
+ bufferTimeout = null;
76
+ }
77
+ if (flushPendingTimeout) {
78
+ clearTimeout(flushPendingTimeout);
79
+ flushPendingTimeout = null;
80
+ }
81
+ // Clear history cache if it exceeds the max size
82
+ if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
83
+ logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache');
84
+ historyCache.clear();
59
85
  }
60
86
  const newData = makeBufferData();
61
87
  const chatUpdates = Object.values(data.chatUpdates);
62
- // gather the remaining conditional events so we re-queue them
63
88
  let conditionalChatUpdatesLeft = 0;
64
89
  for (const update of chatUpdates) {
65
90
  if (update.conditional) {
@@ -78,8 +103,8 @@ const makeEventBuffer = (logger) => {
78
103
  }
79
104
  return {
80
105
  process(handler) {
81
- const listener = (map) => {
82
- handler(map);
106
+ const listener = async (map) => {
107
+ await handler(map);
83
108
  };
84
109
  ev.on('event', listener);
85
110
  return () => {
@@ -87,14 +112,35 @@ const makeEventBuffer = (logger) => {
87
112
  };
88
113
  },
89
114
  emit(event, evData) {
90
- if (buffersInProgress && BUFFERABLE_EVENT_SET.has(event)) {
115
+ // Check if this is a messages.upsert with a different type than what's buffered
116
+ // If so, flush the buffered messages first to avoid type overshadowing
117
+ if (event === 'messages.upsert') {
118
+ const { type } = evData;
119
+ const existingUpserts = Object.values(data.messageUpserts);
120
+ if (existingUpserts.length > 0) {
121
+ const bufferedType = existingUpserts[0].type;
122
+ if (bufferedType !== type) {
123
+ logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages');
124
+ // Emit the buffered messages with their correct type
125
+ ev.emit('event', {
126
+ 'messages.upsert': {
127
+ messages: existingUpserts.map(m => m.message),
128
+ type: bufferedType
129
+ }
130
+ });
131
+ // Clear the message upserts from the buffer
132
+ data.messageUpserts = {};
133
+ }
134
+ }
135
+ }
136
+ if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
91
137
  append(data, historyCache, event, evData, logger);
92
138
  return true;
93
139
  }
94
140
  return ev.emit('event', { [event]: evData });
95
141
  },
96
142
  isBuffering() {
97
- return buffersInProgress > 0;
143
+ return isBuffering;
98
144
  },
99
145
  buffer,
100
146
  flush,
@@ -103,16 +149,33 @@ const makeEventBuffer = (logger) => {
103
149
  buffer();
104
150
  try {
105
151
  const result = await work(...args);
152
+ // If this is the only buffer, flush after a small delay
153
+ if (bufferCount === 1) {
154
+ setTimeout(() => {
155
+ if (isBuffering && bufferCount === 1) {
156
+ flush();
157
+ }
158
+ }, 100); // Small delay to allow nested buffers
159
+ }
106
160
  return result;
107
161
  }
162
+ catch (error) {
163
+ throw error;
164
+ }
108
165
  finally {
109
- flush();
166
+ bufferCount = Math.max(0, bufferCount - 1);
167
+ if (bufferCount === 0) {
168
+ // Only schedule ONE timeout, not 10,000
169
+ if (!flushPendingTimeout) {
170
+ flushPendingTimeout = setTimeout(flush, 100);
171
+ }
172
+ }
110
173
  }
111
174
  };
112
175
  },
113
176
  on: (...args) => ev.on(...args),
114
177
  off: (...args) => ev.off(...args),
115
- removeAllListeners: (...args) => ev.removeAllListeners(...args),
178
+ removeAllListeners: (...args) => ev.removeAllListeners(...args)
116
179
  };
117
180
  };
118
181
  exports.makeEventBuffer = makeEventBuffer;
@@ -138,25 +201,27 @@ const makeBufferData = () => {
138
201
  groupUpdates: {}
139
202
  };
140
203
  };
141
- function append(data, historyCache, event, eventData, logger) {
142
- var _a, _b, _c;
204
+ function append(data, historyCache, event,
205
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
206
+ eventData, logger) {
143
207
  switch (event) {
144
208
  case 'messaging-history.set':
145
209
  for (const chat of eventData.chats) {
146
- const existingChat = data.historySets.chats[chat.id];
210
+ const id = chat.id || '';
211
+ const existingChat = data.historySets.chats[id];
147
212
  if (existingChat) {
148
213
  existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType;
149
214
  }
150
- if (!existingChat && !historyCache.has(chat.id)) {
151
- data.historySets.chats[chat.id] = chat;
152
- historyCache.add(chat.id);
215
+ if (!existingChat && !historyCache.has(id)) {
216
+ data.historySets.chats[id] = chat;
217
+ historyCache.add(id);
153
218
  absorbingChatUpdate(chat);
154
219
  }
155
220
  }
156
221
  for (const contact of eventData.contacts) {
157
222
  const existingContact = data.historySets.contacts[contact.id];
158
223
  if (existingContact) {
159
- Object.assign(existingContact, (0, generics_1.trimUndefined)(contact));
224
+ Object.assign(existingContact, (0, generics_js_1.trimUndefined)(contact));
160
225
  }
161
226
  else {
162
227
  const historyContactId = `c:${contact.id}`;
@@ -183,11 +248,12 @@ function append(data, historyCache, event, eventData, logger) {
183
248
  break;
184
249
  case 'chats.upsert':
185
250
  for (const chat of eventData) {
186
- let upsert = data.chatUpserts[chat.id];
187
- if (!upsert) {
188
- upsert = data.historySets[chat.id];
251
+ const id = chat.id || '';
252
+ let upsert = data.chatUpserts[id];
253
+ if (id && !upsert) {
254
+ upsert = data.historySets.chats[id];
189
255
  if (upsert) {
190
- logger.debug({ chatId: chat.id }, 'absorbed chat upsert in chat set');
256
+ logger.debug({ chatId: id }, 'absorbed chat upsert in chat set');
191
257
  }
192
258
  }
193
259
  if (upsert) {
@@ -195,11 +261,11 @@ function append(data, historyCache, event, eventData, logger) {
195
261
  }
196
262
  else {
197
263
  upsert = chat;
198
- data.chatUpserts[chat.id] = upsert;
264
+ data.chatUpserts[id] = upsert;
199
265
  }
200
266
  absorbingChatUpdate(upsert);
201
- if (data.chatDeletes.has(chat.id)) {
202
- data.chatDeletes.delete(chat.id);
267
+ if (data.chatDeletes.has(id)) {
268
+ data.chatDeletes.delete(id);
203
269
  }
204
270
  }
205
271
  break;
@@ -259,14 +325,14 @@ function append(data, historyCache, event, eventData, logger) {
259
325
  }
260
326
  }
261
327
  if (upsert) {
262
- upsert = Object.assign(upsert, (0, generics_1.trimUndefined)(contact));
328
+ upsert = Object.assign(upsert, (0, generics_js_1.trimUndefined)(contact));
263
329
  }
264
330
  else {
265
331
  upsert = contact;
266
332
  data.contactUpserts[contact.id] = upsert;
267
333
  }
268
334
  if (data.contactUpdates[contact.id]) {
269
- upsert = Object.assign(data.contactUpdates[contact.id], (0, generics_1.trimUndefined)(contact));
335
+ upsert = Object.assign(data.contactUpdates[contact.id], (0, generics_js_1.trimUndefined)(contact));
270
336
  delete data.contactUpdates[contact.id];
271
337
  }
272
338
  }
@@ -291,7 +357,7 @@ function append(data, historyCache, event, eventData, logger) {
291
357
  const { messages, type } = eventData;
292
358
  for (const message of messages) {
293
359
  const key = stringifyMessageKey(message.key);
294
- let existing = (_a = data.messageUpserts[key]) === null || _a === void 0 ? void 0 : _a.message;
360
+ let existing = data.messageUpserts[key]?.message;
295
361
  if (!existing) {
296
362
  existing = data.historySets.messages[key];
297
363
  if (existing) {
@@ -312,9 +378,7 @@ function append(data, historyCache, event, eventData, logger) {
312
378
  else {
313
379
  data.messageUpserts[key] = {
314
380
  message,
315
- type: type === 'notify' || ((_b = data.messageUpserts[key]) === null || _b === void 0 ? void 0 : _b.type) === 'notify'
316
- ? 'notify'
317
- : type
381
+ type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
318
382
  };
319
383
  }
320
384
  }
@@ -323,13 +387,13 @@ function append(data, historyCache, event, eventData, logger) {
323
387
  const msgUpdates = eventData;
324
388
  for (const { key, update } of msgUpdates) {
325
389
  const keyStr = stringifyMessageKey(key);
326
- const existing = data.historySets.messages[keyStr] || ((_c = data.messageUpserts[keyStr]) === null || _c === void 0 ? void 0 : _c.message);
390
+ const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message;
327
391
  if (existing) {
328
392
  Object.assign(existing, update);
329
393
  // if the message was received & read by us
330
394
  // the chat counter must have been incremented
331
395
  // so we need to decrement it
332
- if (update.status === Types_1.WAMessageStatus.READ && !key.fromMe) {
396
+ if (update.status === index_js_1.WAMessageStatus.READ && !key.fromMe) {
333
397
  decrementChatReadCounterIfMsgDidUnread(existing);
334
398
  }
335
399
  }
@@ -367,12 +431,11 @@ function append(data, historyCache, event, eventData, logger) {
367
431
  const keyStr = stringifyMessageKey(key);
368
432
  const existing = data.messageUpserts[keyStr];
369
433
  if (existing) {
370
- (0, messages_1.updateMessageWithReaction)(existing.message, reaction);
434
+ (0, messages_js_1.updateMessageWithReaction)(existing.message, reaction);
371
435
  }
372
436
  else {
373
- data.messageReactions[keyStr] = data.messageReactions[keyStr]
374
- || { key, reactions: [] };
375
- (0, messages_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction);
437
+ data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] };
438
+ (0, messages_js_1.updateMessageWithReaction)(data.messageReactions[keyStr], reaction);
376
439
  }
377
440
  }
378
441
  break;
@@ -382,12 +445,11 @@ function append(data, historyCache, event, eventData, logger) {
382
445
  const keyStr = stringifyMessageKey(key);
383
446
  const existing = data.messageUpserts[keyStr];
384
447
  if (existing) {
385
- (0, messages_1.updateMessageWithReceipt)(existing.message, receipt);
448
+ (0, messages_js_1.updateMessageWithReceipt)(existing.message, receipt);
386
449
  }
387
450
  else {
388
- data.messageReceipts[keyStr] = data.messageReceipts[keyStr]
389
- || { key, userReceipt: [] };
390
- (0, messages_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt);
451
+ data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] };
452
+ (0, messages_js_1.updateMessageWithReceipt)(data.messageReceipts[keyStr], receipt);
391
453
  }
392
454
  }
393
455
  break;
@@ -405,7 +467,7 @@ function append(data, historyCache, event, eventData, logger) {
405
467
  throw new Error(`"${event}" cannot be buffered`);
406
468
  }
407
469
  function absorbingChatUpdate(existing) {
408
- const chatId = existing.id;
470
+ const chatId = existing.id || '';
409
471
  const update = data.chatUpdates[chatId];
410
472
  if (update) {
411
473
  const conditionMatches = update.conditional ? update.conditional(data) : true;
@@ -426,10 +488,10 @@ function append(data, historyCache, event, eventData, logger) {
426
488
  // if the message has already been marked read by us
427
489
  const chatId = message.key.remoteJid;
428
490
  const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId];
429
- if ((0, process_message_1.isRealMessage)(message, '')
430
- && (0, process_message_1.shouldIncrementChatUnread)(message)
431
- && typeof (chat === null || chat === void 0 ? void 0 : chat.unreadCount) === 'number'
432
- && chat.unreadCount > 0) {
491
+ if ((0, process_message_js_1.isRealMessage)(message) &&
492
+ (0, process_message_js_1.shouldIncrementChatUnread)(message) &&
493
+ typeof chat?.unreadCount === 'number' &&
494
+ chat.unreadCount > 0) {
433
495
  logger.debug({ chatId: chat.id }, 'decrementing chat counter');
434
496
  chat.unreadCount -= 1;
435
497
  if (chat.unreadCount === 0) {
@@ -0,0 +1,91 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import type { BaileysEventEmitter, BaileysEventMap, ConnectionState, WACallUpdateType, WAMessageKey, WAVersion } from '../Types/index.js';
3
+ import { type BinaryNode } from '../WABinary/index.js';
4
+ export declare const BufferJSON: {
5
+ replacer: (k: any, value: any) => any;
6
+ reviver: (_: any, value: any) => any;
7
+ };
8
+ export declare const getKeyAuthor: (key: WAMessageKey | undefined | null, meId?: string) => string;
9
+ export declare const isStringNullOrEmpty: (value: string | null | undefined) => value is null | undefined | "";
10
+ export declare const writeRandomPadMax16: (msg: Uint8Array) => Buffer<ArrayBuffer>;
11
+ export declare const unpadRandomMax16: (e: Uint8Array | Buffer) => Uint8Array<ArrayBuffer>;
12
+ export declare const generateParticipantHashV2: (participants: string[]) => string;
13
+ export declare const encodeWAMessage: (message: proto.IMessage) => Buffer<ArrayBuffer>;
14
+ export declare const generateRegistrationId: () => number;
15
+ export declare const encodeBigEndian: (e: number, t?: number) => Uint8Array<ArrayBuffer>;
16
+ export declare const toNumber: (t: Long | number | null | undefined) => number;
17
+ /** unix timestamp of a date in seconds */
18
+ export declare const unixTimestampSeconds: (date?: Date) => number;
19
+ export type DebouncedTimeout = ReturnType<typeof debouncedTimeout>;
20
+ export declare const debouncedTimeout: (intervalMs?: number, task?: () => void) => {
21
+ start: (newIntervalMs?: number, newTask?: () => void) => void;
22
+ cancel: () => void;
23
+ setTask: (newTask: () => void) => () => void;
24
+ setInterval: (newInterval: number) => number;
25
+ };
26
+ export declare const delay: (ms: number) => Promise<void>;
27
+ export declare const delayCancellable: (ms: number) => {
28
+ delay: Promise<void>;
29
+ cancel: () => void;
30
+ };
31
+ export declare function promiseTimeout<T>(ms: number | undefined, promise: (resolve: (v: T) => void, reject: (error: any) => void) => void): Promise<T>;
32
+ export declare const generateMessageIDV2: (userId?: string) => string;
33
+ export declare const generateMessageID: () => string;
34
+ export declare function bindWaitForEvent<T extends keyof BaileysEventMap>(ev: BaileysEventEmitter, event: T): (check: (u: BaileysEventMap[T]) => Promise<boolean | undefined>, timeoutMs?: number) => Promise<void>;
35
+ export declare const bindWaitForConnectionUpdate: (ev: BaileysEventEmitter) => (check: (u: Partial<ConnectionState>) => Promise<boolean | undefined>, timeoutMs?: number) => Promise<void>;
36
+ /**
37
+ * utility that fetches latest baileys version from the master branch.
38
+ * Use to ensure your WA connection is always on the latest version
39
+ */
40
+ export declare const fetchLatestBaileysVersion: (options?: RequestInit) => Promise<{
41
+ version: WAVersion;
42
+ isLatest: boolean;
43
+ error?: undefined;
44
+ } | {
45
+ version: WAVersion;
46
+ isLatest: boolean;
47
+ error: unknown;
48
+ }>;
49
+ /**
50
+ * A utility that fetches the latest web version of whatsapp.
51
+ * Use to ensure your WA connection is always on the latest version
52
+ */
53
+ export declare const fetchLatestWaWebVersion: (options?: RequestInit) => Promise<{
54
+ version: WAVersion;
55
+ isLatest: boolean;
56
+ error?: undefined;
57
+ } | {
58
+ version: WAVersion;
59
+ isLatest: boolean;
60
+ error: unknown;
61
+ }>;
62
+ /** unique message tag prefix for MD clients */
63
+ export declare const generateMdTagPrefix: () => string;
64
+ /**
65
+ * Given a type of receipt, returns what the new status of the message should be
66
+ * @param type type from receipt
67
+ */
68
+ export declare const getStatusFromReceiptType: (type: string | undefined) => proto.WebMessageInfo.Status | undefined;
69
+ /**
70
+ * Stream errors generally provide a reason, map that to a baileys DisconnectReason
71
+ * @param reason the string reason given, eg. "conflict"
72
+ */
73
+ export declare const getErrorCodeFromStreamError: (node: BinaryNode) => {
74
+ reason: string;
75
+ statusCode: number;
76
+ };
77
+ export declare const getCallStatusFromNode: ({ tag, attrs }: BinaryNode) => WACallUpdateType;
78
+ export declare const getCodeFromWSError: (error: Error) => number;
79
+ /**
80
+ * Is the given platform WA business
81
+ * @param platform AuthenticationCreds.platform
82
+ */
83
+ export declare const isWABusinessPlatform: (platform: string) => platform is "smba" | "smbi";
84
+ export declare function trimUndefined(obj: {
85
+ [_: string]: any;
86
+ }): {
87
+ [_: string]: any;
88
+ };
89
+ export declare function bytesToCrockford(buffer: Buffer): string;
90
+ export declare function encodeNewsletterMessage(message: proto.IMessage): Uint8Array;
91
+ //# sourceMappingURL=generics.d.ts.map