@realvare/based 2.7.62 → 2.7.71

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 (57) hide show
  1. package/README.MD +1062 -282
  2. package/WAProto/WAProto.proto +1073 -244
  3. package/WAProto/index.d.ts +16282 -8183
  4. package/WAProto/index.js +76605 -50628
  5. package/engine-requirements.js +10 -10
  6. package/lib/Defaults/baileys-version.json +1 -1
  7. package/lib/Defaults/index.d.ts +4 -2
  8. package/lib/Defaults/index.js +8 -6
  9. package/lib/Signal/Group/ciphertext-message.d.ts +1 -1
  10. package/lib/Signal/Group/ciphertext-message.js +1 -1
  11. package/lib/Signal/Group/sender-message-key.d.ts +1 -1
  12. package/lib/Signal/Group/sender-message-key.js +1 -1
  13. package/lib/Signal/libsignal.d.ts +1 -1
  14. package/lib/Socket/business.d.ts +1 -1
  15. package/lib/Socket/business.js +1 -1
  16. package/lib/Socket/chats.d.ts +4 -1
  17. package/lib/Socket/chats.js +213 -36
  18. package/lib/Socket/groups.js +87 -15
  19. package/lib/Socket/index.js +9 -0
  20. package/lib/Socket/messages-interactive.js +259 -0
  21. package/lib/Socket/messages-recv.js +1473 -1228
  22. package/lib/Socket/messages-send.js +437 -469
  23. package/lib/Socket/socket.js +143 -26
  24. package/lib/Socket/usync.js +57 -4
  25. package/lib/Store/make-in-memory-store.js +28 -15
  26. package/lib/Types/Auth.d.ts +4 -0
  27. package/lib/Types/Message.d.ts +316 -6
  28. package/lib/Types/Message.js +1 -1
  29. package/lib/Types/Socket.d.ts +2 -0
  30. package/lib/Utils/cache-manager.d.ts +16 -0
  31. package/lib/Utils/cache-manager.js +22 -5
  32. package/lib/Utils/chat-utils.js +17 -13
  33. package/lib/Utils/decode-wa-message.js +1 -11
  34. package/lib/Utils/event-buffer.js +103 -2
  35. package/lib/Utils/generics.js +5 -6
  36. package/lib/Utils/index.d.ts +5 -0
  37. package/lib/Utils/index.js +3 -0
  38. package/lib/Utils/jid-validation.d.ts +2 -0
  39. package/lib/Utils/jid-validation.js +43 -10
  40. package/lib/Utils/link-preview.js +38 -28
  41. package/lib/Utils/messages-media.d.ts +1 -1
  42. package/lib/Utils/messages-media.js +22 -53
  43. package/lib/Utils/messages.js +653 -65
  44. package/lib/Utils/performance-config.d.ts +2 -0
  45. package/lib/Utils/performance-config.js +16 -7
  46. package/lib/Utils/process-message.js +124 -12
  47. package/lib/Utils/rate-limiter.js +15 -20
  48. package/lib/WABinary/generic-utils.js +5 -1
  49. package/lib/WABinary/jid-utils.d.ts +1 -0
  50. package/lib/WABinary/jid-utils.js +265 -5
  51. package/lib/WAUSync/Protocols/USyncContactProtocol.js +75 -5
  52. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +59 -6
  53. package/lib/WAUSync/USyncQuery.js +64 -6
  54. package/lib/index.d.ts +1 -0
  55. package/lib/index.js +5 -4
  56. package/package.json +10 -15
  57. package/WAProto/index.ts.ts +0 -53473
@@ -36,6 +36,7 @@ export interface PerformanceConfigInterface {
36
36
  lidCache: CacheConfig;
37
37
  jidCache: CacheConfig;
38
38
  lidToJidCache: CacheConfig;
39
+ groupMetadataCache: CacheConfig;
39
40
  };
40
41
  performance: PerformanceSettings;
41
42
  debug: DebugSettings;
@@ -46,6 +47,7 @@ export declare class PerformanceConfig implements PerformanceConfigInterface {
46
47
  lidCache: CacheConfig;
47
48
  jidCache: CacheConfig;
48
49
  lidToJidCache: CacheConfig;
50
+ groupMetadataCache: CacheConfig;
49
51
  };
50
52
  performance: PerformanceSettings;
51
53
  debug: DebugSettings;
@@ -35,18 +35,26 @@ class PerformanceConfig {
35
35
  enableLogging: false,
36
36
  enableMetrics: true,
37
37
  batchSize: 50,
38
- maxRetries: 5,
39
- retryDelay: 5000,
38
+ maxRetries: 3, // Reduced from 5
39
+ retryDelay: 2000, // Reduced from 5000
40
40
  retryBackoffMultiplier: 1.5,
41
- maxRetryDelay: 60000,
42
- maxMsgRetryCount: 3,
41
+ maxRetryDelay: 30000, // Reduced from 60000
42
+ maxMsgRetryCount: 2, // Reduced from 3
43
43
  memoryThreshold: 0.85,
44
44
  markOnlineOnConnect: false,
45
45
  syncFullHistory: false,
46
46
  keepAliveIntervalMs: 30000,
47
47
  enableNativeLidCache: true,
48
48
  enableLidLogging: process.env.DEBUG_LID === 'true',
49
- logLevel: process.env.LOG_LEVEL || 'debug'
49
+ logLevel: process.env.LOG_LEVEL || 'debug',
50
+ // Performance optimizations
51
+ enableMessageBatching: true,
52
+ messageBatchSize: 10,
53
+ messageBatchTimeout: 100,
54
+ enableParallelProcessing: true,
55
+ maxConcurrentMessages: 5,
56
+ enableFastAck: true,
57
+ reduceSyncValidation: true
50
58
  };
51
59
 
52
60
  this.debug = {
@@ -63,11 +71,12 @@ class PerformanceConfig {
63
71
  },
64
72
  antiBan: {
65
73
  enabled: true,
66
- maxConsecutiveErrors: 3,
67
- cooldownPeriod: 30000
74
+ maxConsecutiveErrors: 5,
75
+ cooldownPeriod: 15000
68
76
  }
69
77
  };
70
78
  }
79
+
71
80
 
72
81
  /**
73
82
  * Aggiorna configurazione cache
@@ -18,24 +18,52 @@ const REAL_MSG_STUB_TYPES = new Set([
18
18
  const REAL_MSG_REQ_ME_STUB_TYPES = new Set([
19
19
  Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD
20
20
  ]);
21
+
22
+ /** Helper function to normalize JID */
23
+ const normalizeToJid = (jid) => {
24
+ if (!jid || typeof jid !== 'string') {
25
+ return jid;
26
+ }
27
+ if ((0, WABinary_1.isLid)(jid)) {
28
+ const decoded = (0, WABinary_1.lidToJid)(jid);
29
+ return decoded || jid;
30
+ }
31
+ return jid;
32
+ };
33
+
21
34
  /** Cleans a received message to further processing */
22
35
  const cleanMessage = (message, meId) => {
23
36
  // ensure remoteJid and participant doesn't have device or agent in it
24
37
  // normalize JIDs but catch errors to avoid throwing on invalid LIDs/JIDs
25
38
  try {
26
- message.key.remoteJid = (0, WABinary_1.jidNormalizedUser)(message.key.remoteJid);
39
+ const r0 = message.key.remoteJid;
40
+ if (r0) {
41
+ const rd = (0, WABinary_1.jidDecode)(r0);
42
+ if (rd === null || rd === void 0 ? void 0 : rd.user) {
43
+ message.key.remoteLid = message.key.remoteLid || ((0, WABinary_1.isLid)(r0) ? r0 : (0, WABinary_1.jidEncode)(rd.user, 'lid'));
44
+ }
45
+ }
46
+ message.key.remoteJid = normalizeToJid(message.key.remoteJid);
27
47
  }
28
48
  catch (_e) {
29
49
  // if normalization fails, retain original remoteJid
30
50
  }
31
51
  if (message.key.participant) {
32
52
  try {
33
- message.key.participant = (0, WABinary_1.jidNormalizedUser)(message.key.participant);
53
+ const p0 = message.key.participant;
54
+ if (p0) {
55
+ const pd = (0, WABinary_1.jidDecode)(p0);
56
+ if (pd === null || pd === void 0 ? void 0 : pd.user) {
57
+ message.key.participantLid = message.key.participantLid || ((0, WABinary_1.isLid)(p0) ? p0 : (0, WABinary_1.jidEncode)(pd.user, 'lid'));
58
+ }
59
+ }
60
+ message.key.participant = normalizeToJid(message.key.participant);
34
61
  }
35
62
  catch (_e) {
36
63
  // ignore if can't normalize participant
37
64
  }
38
65
  }
66
+ message.key.remoteJidNormalized = message.key.remoteJid;
39
67
  const content = (0, messages_1.normalizeMessageContent)(message.message);
40
68
  // if the message has a reaction, ensure fromMe & remoteJid are from our perspective
41
69
  if (content === null || content === void 0 ? void 0 : content.reactionMessage) {
@@ -211,9 +239,10 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
211
239
  const response = protocolMsg.peerDataOperationRequestResponseMessage;
212
240
  if (response) {
213
241
  placeholderResendCache === null || placeholderResendCache === void 0 ? void 0 : placeholderResendCache.del(response.stanzaId);
214
- // TODO: IMPLEMENT HISTORY SYNC ETC (sticker uploads etc.).
242
+ // Handle peer data operation results including sticker uploads, media uploads, link previews, and history sync
215
243
  const { peerDataOperationResult } = response;
216
244
  for (const result of peerDataOperationResult) {
245
+ // Handle placeholder message resend responses
217
246
  const { placeholderMessageResendResponse: retryResponse } = result;
218
247
  if (retryResponse) {
219
248
  const webMessageInfo = WAProto_1.proto.WebMessageInfo.decode(retryResponse.webMessageInfoBytes);
@@ -226,6 +255,51 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
226
255
  });
227
256
  }, 500);
228
257
  }
258
+
259
+ // Handle sticker message results
260
+ if (result.stickerMessage) {
261
+ ev.emit('sticker.upload-result', {
262
+ stickerMessage: result.stickerMessage,
263
+ requestId: response.stanzaId
264
+ });
265
+ }
266
+
267
+ // Handle media upload results
268
+ if (result.mediaUploadResult !== undefined) {
269
+ ev.emit('media.upload-result', {
270
+ result: result.mediaUploadResult,
271
+ requestId: response.stanzaId
272
+ });
273
+ }
274
+
275
+ // Handle link preview responses
276
+ if (result.linkPreviewResponse) {
277
+ ev.emit('link-preview.response', {
278
+ response: result.linkPreviewResponse,
279
+ requestId: response.stanzaId
280
+ });
281
+ }
282
+
283
+ // Handle history sync chunk retry responses
284
+ if (result.historySyncChunkRetryResponse) {
285
+ const historySyncResponse = result.historySyncChunkRetryResponse;
286
+ ev.emit('history-sync.chunk-retry', {
287
+ syncType: historySyncResponse.syncType,
288
+ chunkOrder: historySyncResponse.chunkOrder,
289
+ requestId: historySyncResponse.requestId,
290
+ responseCode: historySyncResponse.responseCode,
291
+ canRecover: historySyncResponse.canRecover,
292
+ stanzaId: response.stanzaId
293
+ });
294
+ }
295
+
296
+ // Handle full history sync on-demand responses
297
+ if (result.fullHistorySyncOnDemandRequestResponse) {
298
+ ev.emit('history-sync.on-demand-response', {
299
+ response: result.fullHistorySyncOnDemandRequestResponse,
300
+ requestId: response.stanzaId
301
+ });
302
+ }
229
303
  }
230
304
  }
231
305
  case WAProto_1.proto.Message.ProtocolMessage.Type.MESSAGE_EDIT:
@@ -262,25 +336,39 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
262
336
  const jid = (_f = message.key) === null || _f === void 0 ? void 0 : _f.remoteJid;
263
337
  //let actor = whatsappID (message.participant)
264
338
  let participants;
265
- const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, author: message.participant, participants, action }));
339
+ const author = normalizeToJid(message.participant);
340
+ const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, author, participants, action }));
266
341
  const emitGroupUpdate = (update) => {
267
342
  var _a;
268
- ev.emit('groups.update', [{ id: jid, ...update, author: (_a = message.participant) !== null && _a !== void 0 ? _a : undefined }]);
343
+ ev.emit('groups.update', [{ id: jid, ...update, author: (_a = author) !== null && _a !== void 0 ? _a : undefined }]);
269
344
  };
270
345
  const emitGroupRequestJoin = (participant, action, method) => {
271
- ev.emit('group.join-request', { id: jid, author: message.participant, participant, action, method: method });
346
+ ev.emit('group.join-request', { id: jid, author, participant: normalizeToJid(participant), action, method: method });
272
347
  };
273
348
  const participantsIncludesMe = () => participants.find(jid => (0, WABinary_1.areJidsSameUser)(meId, jid));
274
349
  switch (message.messageStubType) {
275
350
  case Types_1.WAMessageStubType.GROUP_PARTICIPANT_CHANGE_NUMBER:
276
- participants = message.messageStubParameters || [];
351
+ participants = (message.messageStubParameters || []).map(normalizeToJid);
352
+ participants = participants.map(p => {
353
+ if (typeof p === 'string') {
354
+ const cleanedJid = (0, WABinary_1.validateAndCleanJid)(p);
355
+ return cleanedJid;
356
+ }
357
+ return p;
358
+ });
277
359
  emitParticipantsUpdate('modify');
278
360
  break;
279
361
  case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:
280
362
  case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:
281
- participants = message.messageStubParameters || [];
363
+ participants = (message.messageStubParameters || []).map(normalizeToJid);
364
+ participants = participants.map(p => {
365
+ if (typeof p === 'string') {
366
+ const cleanedJid = (0, WABinary_1.validateAndCleanJid)(p);
367
+ return cleanedJid;
368
+ }
369
+ return p;
370
+ });
282
371
  emitParticipantsUpdate('remove');
283
- // mark the chat read only if you left the group
284
372
  if (participantsIncludesMe()) {
285
373
  chat.readOnly = true;
286
374
  }
@@ -288,18 +376,42 @@ const processMessage = async (message, { shouldProcessHistoryMsg, placeholderRes
288
376
  case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:
289
377
  case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:
290
378
  case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:
291
- participants = message.messageStubParameters || [];
379
+ participants = (message.messageStubParameters || []).map(normalizeToJid);
380
+ // Additional safety check: ensure all participants are proper JIDs
381
+ participants = participants.map(p => {
382
+ if (typeof p === 'string') {
383
+ const cleanedJid = (0, WABinary_1.validateAndCleanJid)(p);
384
+ return cleanedJid;
385
+ }
386
+ return p;
387
+ });
292
388
  if (participantsIncludesMe()) {
293
389
  chat.readOnly = false;
294
390
  }
295
391
  emitParticipantsUpdate('add');
296
392
  break;
297
393
  case Types_1.WAMessageStubType.GROUP_PARTICIPANT_DEMOTE:
298
- participants = message.messageStubParameters || [];
394
+ participants = (message.messageStubParameters || []).map(normalizeToJid);
395
+ // Additional safety check: ensure all participants are proper JIDs
396
+ participants = participants.map(p => {
397
+ if (typeof p === 'string') {
398
+ const cleanedJid = (0, WABinary_1.validateAndCleanJid)(p);
399
+ return cleanedJid;
400
+ }
401
+ return p;
402
+ });
299
403
  emitParticipantsUpdate('demote');
300
404
  break;
301
405
  case Types_1.WAMessageStubType.GROUP_PARTICIPANT_PROMOTE:
302
- participants = message.messageStubParameters || [];
406
+ participants = (message.messageStubParameters || []).map(normalizeToJid);
407
+ // Additional safety check: ensure all participants are proper JIDs
408
+ participants = participants.map(p => {
409
+ if (typeof p === 'string') {
410
+ const cleanedJid = (0, WABinary_1.validateAndCleanJid)(p);
411
+ return cleanedJid;
412
+ }
413
+ return p;
414
+ });
303
415
  emitParticipantsUpdate('promote');
304
416
  break;
305
417
  case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:
@@ -1,13 +1,18 @@
1
1
  "use strict";
2
2
 
3
- /**
4
- * RateLimiter class for controlling message sending frequency
5
- * to prevent ban detection by simulating human-like behavior
6
- */
7
3
  class RateLimiter {
8
- constructor(limitPerSecond = 1) {
4
+ /**
5
+ * @param {object} [options] - Configuration options
6
+ * @param {number} [options.limitPerSecond=1] - Number of tasks to process per second
7
+ * @param {number} [options.maxRandomDelayMs=500] - Maximum random delay to add after each task in milliseconds
8
+ */
9
+ constructor(options = {}) {
10
+ const { limitPerSecond = 1, maxRandomDelayMs = 500 } = options;
11
+
9
12
  this.limitPerSecond = limitPerSecond;
10
- this.interval = 1000 / limitPerSecond; // milliseconds between messages
13
+ this.interval = 1000 / this.limitPerSecond;
14
+ this.maxRandomDelayMs = maxRandomDelayMs;
15
+
11
16
  this.queue = [];
12
17
  this.processing = false;
13
18
  this.lastSendTime = 0;
@@ -26,10 +31,6 @@ class RateLimiter {
26
31
  }
27
32
  });
28
33
  }
29
-
30
- /**
31
- * Process the queue with rate limiting
32
- */
33
34
  async process() {
34
35
  if (this.processing || this.queue.length === 0) {
35
36
  return;
@@ -41,8 +42,6 @@ class RateLimiter {
41
42
  const { task, resolve, reject } = this.queue.shift();
42
43
  const now = Date.now();
43
44
  const timeSinceLastSend = now - this.lastSendTime;
44
-
45
- // Wait if we need to respect the rate limit
46
45
  if (timeSinceLastSend < this.interval) {
47
46
  const waitTime = this.interval - timeSinceLastSend;
48
47
  await new Promise(r => setTimeout(r, waitTime));
@@ -55,10 +54,10 @@ class RateLimiter {
55
54
  } catch (error) {
56
55
  reject(error);
57
56
  }
58
-
59
- // Add small random delay to simulate human behavior (0-500ms)
60
- const randomDelay = Math.random() * 500;
61
- await new Promise(r => setTimeout(r, randomDelay));
57
+ if (this.maxRandomDelayMs > 0) {
58
+ const randomDelay = Math.random() * this.maxRandomDelayMs;
59
+ await new Promise(r => setTimeout(r, randomDelay));
60
+ }
62
61
  }
63
62
 
64
63
  this.processing = false;
@@ -80,10 +79,6 @@ class RateLimiter {
80
79
  getQueueLength() {
81
80
  return this.queue.length;
82
81
  }
83
-
84
- /**
85
- * Clear all pending tasks
86
- */
87
82
  clear() {
88
83
  this.queue.forEach(({ reject }) => {
89
84
  reject(new Error('Rate limiter cleared'));
@@ -54,7 +54,11 @@ exports.getBinaryNodeChildUInt = getBinaryNodeChildUInt;
54
54
  const assertNodeErrorFree = (node) => {
55
55
  const errNode = (0, exports.getBinaryNodeChild)(node, 'error');
56
56
  if (errNode) {
57
- throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', { data: +errNode.attrs.code });
57
+ const code = +errNode.attrs.code;
58
+ throw new boom_1.Boom(errNode.attrs.text || 'Unknown error', {
59
+ data: code,
60
+ statusCode: code || undefined
61
+ });
58
62
  }
59
63
  };
60
64
  exports.assertNodeErrorFree = assertNodeErrorFree;
@@ -34,4 +34,5 @@ export declare const isJidStatusBroadcast: (jid: string) => jid is "status@broad
34
34
  export declare const isJidBot: (jid: string | undefined) => boolean | "" | undefined;
35
35
  export declare const jidNormalizedUser: (jid: string | undefined) => string;
36
36
  export declare const lidToJid: (jid: string) => string;
37
+ export declare const resolveJid: (jid: string | undefined) => string | undefined;
37
38
  export declare const getBotJid: (jid: string) => string;