@badzz88/baileys 8.5.4 → 8.5.5

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 (107) hide show
  1. package/README.md +1 -1
  2. package/WAProto/WAProto.proto +75 -1256
  3. package/WAProto/index.d.ts +19729 -36122
  4. package/WAProto/index.js +132193 -178538
  5. package/engine-requirements.js +15 -7
  6. package/lib/Defaults/index.js +14 -22
  7. package/lib/Signal/Group/group_cipher.js +5 -5
  8. package/lib/Signal/Group/keyhelper.js +4 -4
  9. package/lib/Signal/Group/sender-chain-key.js +2 -2
  10. package/lib/Signal/Group/sender-key-distribution-message.js +1 -1
  11. package/lib/Signal/Group/sender-key-message.js +3 -3
  12. package/lib/Signal/Group/sender-key-record.js +1 -1
  13. package/lib/Signal/Group/sender-message-key.js +5 -5
  14. package/lib/Signal/libsignal.js +28 -39
  15. package/lib/Signal/lid-mapping.js +8 -28
  16. package/lib/Socket/aigroups.js +5 -5
  17. package/lib/Socket/business.js +11 -3
  18. package/lib/Socket/chats.js +319 -668
  19. package/lib/Socket/communities.js +89 -85
  20. package/lib/Socket/graphql.js +228 -435
  21. package/lib/Socket/groups.js +211 -132
  22. package/lib/Socket/index.js +7 -9
  23. package/lib/Socket/interactive-handler.js +60 -117
  24. package/lib/Socket/interop.js +175 -56
  25. package/lib/Socket/luxu.js +387 -0
  26. package/lib/Socket/managed-account.js +48 -79
  27. package/lib/Socket/messages-recv.js +340 -898
  28. package/lib/Socket/messages-send.js +160 -462
  29. package/lib/Socket/mex.js +53 -40
  30. package/lib/Socket/newsletter.js +295 -291
  31. package/lib/Socket/privacy.js +265 -77
  32. package/lib/Socket/registration.js +157 -164
  33. package/lib/Socket/socket.js +199 -181
  34. package/lib/Socket/text-router.js +83 -0
  35. package/lib/Socket/username.js +227 -144
  36. package/lib/Store/keyed-db.js +108 -0
  37. package/lib/Store/make-cache-manager-store.js +2 -2
  38. package/lib/Store/make-in-memory-store.js +141 -25
  39. package/lib/Types/Auth.js +3 -3
  40. package/lib/Types/Label.js +1 -1
  41. package/lib/Types/LabelAssociation.js +1 -1
  42. package/lib/Types/Mex.js +112 -0
  43. package/lib/Types/Newsletter.js +65 -96
  44. package/lib/Types/State.js +4 -4
  45. package/lib/Types/index.js +1 -1
  46. package/lib/Utils/auth-utils.js +57 -52
  47. package/lib/Utils/browser-utils.js +1 -1
  48. package/lib/Utils/business.js +6 -2
  49. package/lib/Utils/chat-utils.js +38 -102
  50. package/lib/Utils/command-loader.d.ts +31 -0
  51. package/lib/Utils/command-loader.js +100 -0
  52. package/lib/Utils/companion-reg-client-utils.js +42 -0
  53. package/lib/Utils/crypto.js +84 -54
  54. package/lib/Utils/curve25519-js.js +275 -0
  55. package/lib/Utils/decode-wa-message.js +83 -316
  56. package/lib/Utils/event-buffer.js +61 -37
  57. package/lib/Utils/generics.js +103 -157
  58. package/lib/Utils/group-history.js +12 -3
  59. package/lib/Utils/history.js +15 -15
  60. package/lib/Utils/index.js +5 -1
  61. package/lib/Utils/link-preview.js +8 -3
  62. package/lib/Utils/lt-hash.js +35 -2
  63. package/lib/Utils/make-mutex.js +1 -1
  64. package/lib/Utils/message-retry-manager.js +92 -27
  65. package/lib/Utils/messages-media.js +130 -159
  66. package/lib/Utils/messages.js +566 -144
  67. package/lib/Utils/meta-ai-msmsg.js +202 -62
  68. package/lib/Utils/native-bridge.js +82 -0
  69. package/lib/Utils/noise-handler.js +21 -19
  70. package/lib/Utils/offline-node-processor.js +8 -3
  71. package/lib/Utils/pre-key-manager.js +23 -13
  72. package/lib/Utils/process-message.js +106 -197
  73. package/lib/Utils/reporting-utils.js +2 -2
  74. package/lib/Utils/session-pool.d.ts +16 -0
  75. package/lib/Utils/session-pool.js +94 -0
  76. package/lib/Utils/signal.js +63 -28
  77. package/lib/Utils/stanza-ack.js +12 -39
  78. package/lib/Utils/sticker.d.ts +17 -0
  79. package/lib/Utils/sticker.js +145 -0
  80. package/lib/Utils/sync-action-utils.js +7 -4
  81. package/lib/Utils/tc-token-utils.js +25 -20
  82. package/lib/Utils/use-multi-file-auth-state.js +14 -30
  83. package/lib/Utils/validate-connection.js +19 -59
  84. package/lib/Utils/view-once-cache.d.ts +9 -0
  85. package/lib/Utils/view-once-cache.js +79 -0
  86. package/lib/WABinary/decode.js +16 -50
  87. package/lib/WABinary/encode.js +224 -54
  88. package/lib/WABinary/generic-utils.js +97 -7
  89. package/lib/WABinary/jid-utils.js +14 -27
  90. package/lib/WAM/encode.js +7 -7
  91. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +1 -58
  92. package/lib/WAUSync/Protocols/USyncContactProtocol.js +1 -7
  93. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +5 -13
  94. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +4 -1
  95. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +7 -20
  96. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +1 -1
  97. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +2 -2
  98. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +2 -7
  99. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +1 -1
  100. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +1 -2
  101. package/lib/WAUSync/Protocols/index.js +0 -1
  102. package/lib/WAUSync/USyncQuery.js +6 -77
  103. package/lib/WAUSync/USyncUser.js +8 -16
  104. package/lib/WAUSync/index.js +0 -1
  105. package/lib/antiban.js +854 -608
  106. package/lib/index.js +1 -1
  107. package/package.json +20 -16
@@ -26,18 +26,21 @@ const BUFFERABLE_EVENT = [
26
26
  'groups.update'
27
27
  ]
28
28
  const BUFFERABLE_EVENT_SET = new Set(BUFFERABLE_EVENT)
29
-
29
+ /**
30
+ * The event buffer logically consolidates different events into a single event
31
+ * making the data processing more efficient.
32
+ */
30
33
  const makeEventBuffer = logger => {
31
34
  const ev = new events_1.default()
32
35
  const historyCache = new Set()
33
36
  let data = makeBufferData()
34
37
  let isBuffering = false
35
38
  let bufferTimeout = null
36
- let flushPendingTimeout = null
39
+ let flushPendingTimeout = null // Add a specific timer for the debounced flush to prevent leak
37
40
  let bufferCount = 0
38
- const MAX_HISTORY_CACHE_SIZE = 10000
39
- const BUFFER_TIMEOUT_MS = 30000
40
-
41
+ const MAX_HISTORY_CACHE_SIZE = 10000 // Limit the history cache size to prevent memory bloat
42
+ const BUFFER_TIMEOUT_MS = 30000 // 30 seconds
43
+ // take the generic event and fire it as a baileys event
41
44
  ev.on('event', map => {
42
45
  for (const event in map) {
43
46
  ev.emit(event, map[event])
@@ -58,7 +61,7 @@ const makeEventBuffer = logger => {
58
61
  }
59
62
  }, BUFFER_TIMEOUT_MS)
60
63
  }
61
-
64
+ // Always increment count when requested
62
65
  bufferCount++
63
66
  }
64
67
  function flush() {
@@ -68,7 +71,7 @@ const makeEventBuffer = logger => {
68
71
  logger.debug({ bufferCount }, 'Flushing event buffer')
69
72
  isBuffering = false
70
73
  bufferCount = 0
71
-
74
+ // Clear timeout
72
75
  if (bufferTimeout) {
73
76
  clearTimeout(bufferTimeout)
74
77
  bufferTimeout = null
@@ -77,7 +80,7 @@ const makeEventBuffer = logger => {
77
80
  clearTimeout(flushPendingTimeout)
78
81
  flushPendingTimeout = null
79
82
  }
80
-
83
+ // Clear history cache if it exceeds the max size
81
84
  if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
82
85
  logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache')
83
86
  historyCache.clear()
@@ -111,8 +114,8 @@ const makeEventBuffer = logger => {
111
114
  }
112
115
  },
113
116
  emit(event, evData) {
114
-
115
-
117
+ // Check if this is a messages.upsert with a different type than what's buffered
118
+ // If so, flush the buffered messages first to avoid type overshadowing
116
119
  if (event === 'messages.upsert') {
117
120
  const { type } = evData
118
121
  const existingUpserts = Object.values(data.messageUpserts)
@@ -120,14 +123,14 @@ const makeEventBuffer = logger => {
120
123
  const bufferedType = existingUpserts[0].type
121
124
  if (bufferedType !== type) {
122
125
  logger.debug({ bufferedType, newType: type }, 'messages.upsert type mismatch, emitting buffered messages')
123
-
126
+ // Emit the buffered messages with their correct type
124
127
  ev.emit('event', {
125
128
  'messages.upsert': {
126
129
  messages: existingUpserts.map(m => m.message),
127
130
  type: bufferedType
128
131
  }
129
132
  })
130
-
133
+ // Clear the message upserts from the buffer
131
134
  data.messageUpserts = {}
132
135
  }
133
136
  }
@@ -148,13 +151,13 @@ const makeEventBuffer = logger => {
148
151
  buffer()
149
152
  try {
150
153
  const result = await work(...args)
151
-
154
+ // If this is the only buffer, flush after a small delay
152
155
  if (bufferCount === 1) {
153
156
  setTimeout(() => {
154
157
  if (isBuffering && bufferCount === 1) {
155
158
  flush()
156
159
  }
157
- }, 100)
160
+ }, 100) // Small delay to allow nested buffers
158
161
  }
159
162
  return result
160
163
  } catch (error) {
@@ -162,7 +165,7 @@ const makeEventBuffer = logger => {
162
165
  } finally {
163
166
  bufferCount = Math.max(0, bufferCount - 1)
164
167
  if (bufferCount === 0) {
165
-
168
+ // Only schedule ONE timeout, not 10,000
166
169
  if (!flushPendingTimeout) {
167
170
  flushPendingTimeout = setTimeout(flush, 100)
168
171
  }
@@ -172,7 +175,6 @@ const makeEventBuffer = logger => {
172
175
  },
173
176
  on: (...args) => ev.on(...args),
174
177
  off: (...args) => ev.off(...args),
175
- once: (...args) => ev.once(...args),
176
178
  removeAllListeners: (...args) => ev.removeAllListeners(...args)
177
179
  }
178
180
  }
@@ -203,7 +205,7 @@ function append(
203
205
  data,
204
206
  historyCache,
205
207
  event,
206
-
208
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
207
209
  eventData,
208
210
  logger
209
211
  ) {
@@ -244,6 +246,30 @@ function append(
244
246
  }
245
247
  data.historySets.empty = false
246
248
  data.historySets.syncType = eventData.syncType
249
+ if (eventData.pastParticipants?.length) {
250
+ const merged = new Map()
251
+ const sigOf = p => `${p.userJid || ''}:${p.leaveTs || ''}:${p.leaveReason || ''}`
252
+ const ingest = entry => {
253
+ const key = entry.groupJid ?? JSON.stringify(entry)
254
+ const existing = merged.get(key)
255
+ if (!existing) {
256
+ merged.set(key, { ...entry, pastParticipants: [...(entry.pastParticipants || [])] })
257
+ return
258
+ }
259
+ const seen = new Set((existing.pastParticipants || []).map(sigOf))
260
+ for (const p of entry.pastParticipants || []) {
261
+ const sig = sigOf(p)
262
+ if (!seen.has(sig)) {
263
+ existing.pastParticipants.push(p)
264
+ seen.add(sig)
265
+ }
266
+ }
267
+ }
268
+ for (const entry of data.historySets.pastParticipants || []) ingest(entry)
269
+ for (const entry of eventData.pastParticipants) ingest(entry)
270
+ data.historySets.pastParticipants = [...merged.values()]
271
+ }
272
+ data.historySets.chunkOrder = eventData.chunkOrder
247
273
  data.historySets.progress = eventData.progress
248
274
  data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId
249
275
  data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest
@@ -276,22 +302,22 @@ function append(
276
302
  const conditionMatches = update.conditional ? update.conditional(data) : true
277
303
  if (conditionMatches) {
278
304
  delete update.conditional
279
-
305
+ // if there is an existing upsert, merge the update into it
280
306
  const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
281
307
  if (upsert) {
282
308
  concatChats(upsert, update)
283
309
  } else {
284
-
310
+ // merge the update into the existing update
285
311
  const chatUpdate = data.chatUpdates[chatId] || {}
286
312
  data.chatUpdates[chatId] = concatChats(chatUpdate, update)
287
313
  }
288
314
  } else if (conditionMatches === undefined) {
289
-
315
+ // condition yet to be fulfilled
290
316
  data.chatUpdates[chatId] = update
291
317
  }
292
-
293
-
294
-
318
+ // otherwise -- condition not met, update is invalid
319
+ // if the chat has been updated
320
+ // ignore any existing chat delete
295
321
  if (data.chatDeletes.has(chatId)) {
296
322
  data.chatDeletes.delete(chatId)
297
323
  }
@@ -302,7 +328,7 @@ function append(
302
328
  if (!data.chatDeletes.has(chatId)) {
303
329
  data.chatDeletes.add(chatId)
304
330
  }
305
-
331
+ // remove any prior updates & upserts
306
332
  if (data.chatUpdates[chatId]) {
307
333
  delete data.chatUpdates[chatId]
308
334
  }
@@ -339,12 +365,12 @@ function append(
339
365
  const contactUpdates = eventData
340
366
  for (const update of contactUpdates) {
341
367
  const id = update.id
342
-
368
+ // merge into prior upsert
343
369
  const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
344
370
  if (upsert) {
345
371
  Object.assign(upsert, update)
346
372
  } else {
347
-
373
+ // merge into prior update
348
374
  const contactUpdate = data.contactUpdates[id] || {}
349
375
  data.contactUpdates[id] = Object.assign(contactUpdate, update)
350
376
  }
@@ -386,9 +412,9 @@ function append(
386
412
  const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message
387
413
  if (existing) {
388
414
  Object.assign(existing, update)
389
-
390
-
391
-
415
+ // if the message was received & read by us
416
+ // the chat counter must have been incremented
417
+ // so we need to decrement it
392
418
  if (update.status === Types_1.WAMessageStatus.READ && !key.fromMe) {
393
419
  decrementChatReadCounterIfMsgDidUnread(existing)
394
420
  }
@@ -416,11 +442,7 @@ function append(
416
442
  }
417
443
  }
418
444
  } else {
419
-
420
-
421
-
422
-
423
-
445
+ // TODO: add support
424
446
  }
425
447
  break
426
448
  case 'messages.reaction':
@@ -479,8 +501,8 @@ function append(
479
501
  }
480
502
  }
481
503
  function decrementChatReadCounterIfMsgDidUnread(message) {
482
-
483
-
504
+ // decrement chat unread counter
505
+ // if the message has already been marked read by us
484
506
  const chatId = message.key.remoteJid
485
507
  const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId]
486
508
  if (
@@ -504,9 +526,11 @@ function consolidateEvents(data) {
504
526
  chats: Object.values(data.historySets.chats),
505
527
  messages: Object.values(data.historySets.messages),
506
528
  contacts: Object.values(data.historySets.contacts),
529
+ pastParticipants: data.historySets.pastParticipants,
507
530
  syncType: data.historySets.syncType,
508
531
  progress: data.historySets.progress,
509
532
  isLatest: data.historySets.isLatest,
533
+ chunkOrder: data.historySets.chunkOrder,
510
534
  peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
511
535
  }
512
536
  }
@@ -566,7 +590,7 @@ function consolidateEvents(data) {
566
590
  }
567
591
  function concatChats(a, b) {
568
592
  if (
569
- b.unreadCount === null &&
593
+ b.unreadCount === null && // neutralize unread counter
570
594
  a.unreadCount < 0
571
595
  ) {
572
596
  a.unreadCount = undefined