@realvare/based 2.7.2 → 2.7.4

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.
@@ -320,8 +320,8 @@ const extractGroupMetadata = (result) => {
320
320
  participants: (0, WABinary_1.getBinaryNodeChildren)(group, 'participant').map(({ attrs }) => {
321
321
  return {
322
322
  id: attrs.jid,
323
- jid: attrs.phone_number || attrs.jid,
324
- lid: attrs.lid || attrs.jid,
323
+ jid: attrs.phone_number || (0, WABinary_1.lidToJid)(attrs.jid),
324
+ lid: attrs.lid || ((0, WABinary_1.isLid)(attrs.jid) ? attrs.jid : undefined),
325
325
  admin: (attrs.type || null),
326
326
  };
327
327
  }),
@@ -35,6 +35,12 @@ const makeMessagesRecvSocket = (config) => {
35
35
  useClones: false
36
36
  });
37
37
  let sendActiveReceipts = false;
38
+ const resolveJid = (jid) => {
39
+ if (typeof jid === 'string' && jid.endsWith('@lid')) {
40
+ return (0, WABinary_1.lidToJid)(jid);
41
+ }
42
+ return jid;
43
+ };
38
44
  const sendMessageAck = async ({ tag, attrs, content }, errorCode) => {
39
45
  const stanza = {
40
46
  tag: 'ack',
@@ -62,6 +68,16 @@ const makeMessagesRecvSocket = (config) => {
62
68
  logger.debug({ recv: { tag, attrs }, sent: stanza.attrs }, 'sent ack');
63
69
  await sendNode(stanza);
64
70
  };
71
+
72
+ // Add withAck wrapper for guaranteed acknowledgments
73
+ const withAck = (processFn) => async (node) => {
74
+ try {
75
+ await processFn(node);
76
+ } finally {
77
+ // Always send ack even on failure to allow potential retry
78
+ await sendMessageAck(node);
79
+ }
80
+ };
65
81
  const offerCall = async (toJid, isVideo = false) => {
66
82
  const callId = (0, crypto_1.randomBytes)(16).toString('hex').toUpperCase().substring(0, 64);
67
83
  const offerContent = [];
@@ -344,22 +360,28 @@ const makeMessagesRecvSocket = (config) => {
344
360
  default:
345
361
  // console.log("BAILEYS-DEBUG:", JSON.stringify({ ...child, content: Buffer.isBuffer(child.content) ? child.content.toString() : child.content, participant }, null, 2))
346
362
  }
347
- // Resolve LIDs to real JIDs for all messageStubParameters
348
- if (msg.messageStubParameters) {
363
+ const needsResolving = (msg.messageStubParameters && msg.messageStubParameters.some(p => typeof p === 'string' && (0, WABinary_1.isLid)(p))) ||
364
+ (participant && (0, WABinary_1.isLid)(participant)) ||
365
+ (msg.key?.participant && (0, WABinary_1.isLid)(msg.key.participant));
366
+ if(needsResolving) {
349
367
  const metadata = await groupMetadata(groupJid);
350
- msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => {
351
- if (typeof param === 'string' && param.endsWith('@lid')) {
352
- const found = metadata.participants.find(p => p.id === param);
353
- return found?.jid || (0, WABinary_1.lidToJid)(param);
354
- }
355
- return param;
356
- }));
357
- }
358
- // Also resolve key.participant if needed
359
- if (msg.key?.participant && msg.key.participant.endsWith('@lid')) {
360
- const metadata = await groupMetadata(groupJid);
361
- const found = metadata.participants.find(p => p.id === msg.key.participant);
362
- msg.key.participant = found?.jid || (0, WABinary_1.lidToJid)(msg.key.participant);
368
+ if (msg.messageStubParameters) {
369
+ msg.messageStubParameters = await Promise.all(msg.messageStubParameters.map(async (param) => {
370
+ if (typeof param === 'string' && (0, WABinary_1.isLid)(param)) {
371
+ const found = metadata.participants.find(p => p.id === param);
372
+ return found?.jid || (0, WABinary_1.lidToJid)(param);
373
+ }
374
+ return param;
375
+ }));
376
+ }
377
+ if(participant && (0, WABinary_1.isLid)(participant)) {
378
+ const found = metadata.participants.find(p => p.id === participant);
379
+ msg.participant = found?.jid || (0, WABinary_1.lidToJid)(participant);
380
+ }
381
+ if (msg.key?.participant && (0, WABinary_1.isLid)(msg.key.participant)) {
382
+ const found = metadata.participants.find(p => p.id === msg.key.participant);
383
+ msg.key.participant = found?.jid || (0, WABinary_1.lidToJid)(msg.key.participant);
384
+ }
363
385
  }
364
386
  };
365
387
  const handleNewsletterNotification = (id, node) => {
@@ -411,12 +433,12 @@ const makeMessagesRecvSocket = (config) => {
411
433
  const result = {};
412
434
  const [child] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
413
435
  const nodeType = node.attrs.type;
414
- const from = (0, WABinary_1.jidNormalizedUser)(node.attrs.from);
436
+ const from = resolveJid((0, WABinary_1.jidNormalizedUser)(node.attrs.from));
415
437
  switch (nodeType) {
416
438
  case 'privacy_token':
417
439
  const tokenList = (0, WABinary_1.getBinaryNodeChildren)(child, 'token');
418
440
  for (const { attrs, content } of tokenList) {
419
- const jid = attrs.jid;
441
+ const jid = resolveJid(attrs.jid);
420
442
  ev.emit('chats.update', [
421
443
  {
422
444
  id: jid,
@@ -445,7 +467,7 @@ const makeMessagesRecvSocket = (config) => {
445
467
  case 'devices':
446
468
  const devices = (0, WABinary_1.getBinaryNodeChildren)(child, 'device');
447
469
  if ((0, WABinary_1.areJidsSameUser)(child.attrs.jid, authState.creds.me.id)) {
448
- const deviceJids = devices.map(d => d.attrs.jid);
470
+ const deviceJids = devices.map(d => resolveJid(d.attrs.jid));
449
471
  logger.info({ deviceJids }, 'got my own devices');
450
472
  }
451
473
  break;
@@ -460,7 +482,7 @@ const makeMessagesRecvSocket = (config) => {
460
482
  const setPicture = (0, WABinary_1.getBinaryNodeChild)(node, 'set');
461
483
  const delPicture = (0, WABinary_1.getBinaryNodeChild)(node, 'delete');
462
484
  ev.emit('contacts.update', [{
463
- id: from || ((_b = (_a = (setPicture || delPicture)) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.hash) || '',
485
+ id: resolveJid(from) || ((_b = (_a = (setPicture || delPicture)) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.hash) || '',
464
486
  imgUrl: setPicture ? 'changed' : 'removed'
465
487
  }]);
466
488
  if ((0, WABinary_1.isJidGroup)(from)) {
@@ -474,6 +496,15 @@ const makeMessagesRecvSocket = (config) => {
474
496
  ...result.key || {},
475
497
  participant: setPicture === null || setPicture === void 0 ? void 0 : setPicture.attrs.author
476
498
  };
499
+ const metadata = await groupMetadata(from);
500
+ if (result.participant && result.participant.endsWith('@lid')) {
501
+ const found = metadata.participants.find(p => p.id === result.participant);
502
+ result.participant = found?.jid || (0, WABinary_1.lidToJid)(result.participant);
503
+ }
504
+ if (result.key?.participant && result.key.participant.endsWith('@lid')) {
505
+ const found = metadata.participants.find(p => p.id === result.key.participant);
506
+ result.key.participant = found?.jid || (0, WABinary_1.lidToJid)(result.key.participant);
507
+ }
477
508
  }
478
509
  break;
479
510
  case 'account_sync':
@@ -494,7 +525,7 @@ const makeMessagesRecvSocket = (config) => {
494
525
  else if (child.tag === 'blocklist') {
495
526
  const blocklists = (0, WABinary_1.getBinaryNodeChildren)(child, 'item');
496
527
  for (const { attrs } of blocklists) {
497
- const blocklist = [attrs.jid];
528
+ const blocklist = [resolveJid(attrs.jid)];
498
529
  const type = (attrs.action === 'block') ? 'add' : 'remove';
499
530
  ev.emit('blocklist.update', { blocklist, type });
500
531
  }
@@ -625,14 +656,14 @@ const makeMessagesRecvSocket = (config) => {
625
656
  var _a, _b;
626
657
  const { attrs, content } = node;
627
658
  const isLid = attrs.from.includes('lid');
628
- const isNodeFromMe = (0, WABinary_1.areJidsSameUser)(attrs.participant || attrs.from, isLid ? (_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.lid : (_b = authState.creds.me) === null || _b === void 0 ? void 0 : _b.id);
629
- const remoteJid = !isNodeFromMe || (0, WABinary_1.isJidGroup)(attrs.from) ? attrs.from : attrs.recipient;
659
+ const isNodeFromMe = (0, WABinary_1.areJidsSameUser)(resolveJid(attrs.participant) || resolveJid(attrs.from), isLid ? (_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.lid : (_b = authState.creds.me) === null || _b === void 0 ? void 0 : _b.id);
660
+ const remoteJid = !isNodeFromMe || (0, WABinary_1.isJidGroup)(attrs.from) ? resolveJid(attrs.from) : attrs.recipient;
630
661
  const fromMe = !attrs.recipient || ((attrs.type === 'retry' || attrs.type === 'sender') && isNodeFromMe);
631
662
  const key = {
632
663
  remoteJid,
633
664
  id: '',
634
665
  fromMe,
635
- participant: attrs.participant
666
+ participant: resolveJid(attrs.participant)
636
667
  };
637
668
  if (shouldIgnoreJid(remoteJid) && remoteJid !== '@s.whatsapp.net') {
638
669
  logger.debug({ remoteJid }, 'ignoring receipt from jid');
@@ -660,7 +691,7 @@ const makeMessagesRecvSocket = (config) => {
660
691
  ev.emit('message-receipt.update', ids.map(id => ({
661
692
  key: { ...key, id },
662
693
  receipt: {
663
- userJid: (0, WABinary_1.jidNormalizedUser)(attrs.participant),
694
+ userJid: (0, WABinary_1.jidNormalizedUser)(resolveJid(attrs.participant)),
664
695
  [updateKey]: +attrs.t
665
696
  }
666
697
  })));
@@ -715,7 +746,7 @@ const makeMessagesRecvSocket = (config) => {
715
746
  }
716
747
  };
717
748
  const handleNotification = async (node) => {
718
- const remoteJid = node.attrs.from;
749
+ const remoteJid = resolveJid(node.attrs.from);
719
750
  if (shouldIgnoreJid(remoteJid) && remoteJid !== '@s.whatsapp.net') {
720
751
  logger.debug({ remoteJid, id: node.attrs.id }, 'ignored notification');
721
752
  await sendMessageAck(node);
@@ -727,15 +758,15 @@ const makeMessagesRecvSocket = (config) => {
727
758
  var _a;
728
759
  const msg = await processNotification(node);
729
760
  if (msg) {
730
- const fromMe = (0, WABinary_1.areJidsSameUser)(node.attrs.participant || remoteJid, authState.creds.me.id);
731
- msg.key = {
732
- remoteJid,
733
- fromMe,
734
- participant: node.attrs.participant,
735
- id: node.attrs.id,
736
- ...(msg.key || {})
737
- };
738
- (_a = msg.participant) !== null && _a !== void 0 ? _a : (msg.participant = node.attrs.participant);
761
+ const participant = msg.participant || resolveJid(node.attrs.participant);
762
+ const fromMe = (0, WABinary_1.areJidsSameUser)(participant || remoteJid, authState.creds.me.id);
763
+ const key = msg.key || {};
764
+ key.remoteJid = remoteJid;
765
+ key.fromMe = fromMe;
766
+ key.id = node.attrs.id;
767
+ key.participant = key.participant || participant;
768
+ msg.key = key;
769
+ msg.participant = participant;
739
770
  msg.messageTimestamp = +node.attrs.t;
740
771
  const fullMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);
741
772
  await upsertMessage(fullMsg, 'append');
@@ -747,23 +778,20 @@ const makeMessagesRecvSocket = (config) => {
747
778
  await sendMessageAck(node);
748
779
  }
749
780
  };
750
- const handleMessage = async (node) => {
781
+ const handleMessage = withAck(async (node) => {
751
782
  var _a, _b, _c;
752
783
  if (shouldIgnoreJid(node.attrs.from) && node.attrs.from !== '@s.whatsapp.net') {
753
784
  logger.debug({ key: node.attrs.key }, 'ignored message');
754
- await sendMessageAck(node);
755
785
  return;
756
786
  }
757
787
  const encNode = (0, WABinary_1.getBinaryNodeChild)(node, 'enc');
758
788
  // TODO: temporary fix for crashes and issues resulting of failed msmsg decryption
759
789
  if (encNode && encNode.attrs.type === 'msmsg') {
760
790
  logger.debug({ key: node.attrs.key }, 'ignored msmsg');
761
- await sendMessageAck(node);
762
791
  return;
763
792
  }
764
793
  let response;
765
794
  if ((0, WABinary_1.getBinaryNodeChild)(node, 'unavailable') && !encNode) {
766
- await sendMessageAck(node);
767
795
  const { key } = (0, Utils_1.decodeMessageNode)(node, authState.creds.me.id, authState.creds.me.lid || '').fullMessage;
768
796
  response = await requestPlaceholderResend(key);
769
797
  if (response === 'RESOLVED') {
@@ -849,7 +877,6 @@ const makeMessagesRecvSocket = (config) => {
849
877
  }
850
878
  }
851
879
  (0, Utils_1.cleanMessage)(msg, authState.creds.me.id);
852
- await sendMessageAck(node);
853
880
  await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');
854
881
  })
855
882
  ]);
@@ -857,7 +884,7 @@ const makeMessagesRecvSocket = (config) => {
857
884
  catch (error) {
858
885
  logger.error({ error, node }, 'error in handling message');
859
886
  }
860
- };
887
+ });
861
888
  const fetchMessageHistory = async (count, oldestMsgKey, oldestMsgTimestamp) => {
862
889
  var _a;
863
890
  if (!((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id)) {
@@ -910,10 +937,10 @@ const makeMessagesRecvSocket = (config) => {
910
937
  const { attrs } = node;
911
938
  const [infoChild] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
912
939
  const callId = infoChild.attrs['call-id'];
913
- const from = infoChild.attrs.from || infoChild.attrs['call-creator'];
940
+ const from = resolveJid(infoChild.attrs.from || infoChild.attrs['call-creator']);
914
941
  const status = (0, Utils_1.getCallStatusFromNode)(infoChild);
915
942
  const call = {
916
- chatId: attrs.from,
943
+ chatId: resolveJid(attrs.from),
917
944
  from,
918
945
  id: callId,
919
946
  date: new Date(+attrs.t * 1000),
@@ -923,7 +950,7 @@ const makeMessagesRecvSocket = (config) => {
923
950
  if (status === 'offer') {
924
951
  call.isVideo = !!(0, WABinary_1.getBinaryNodeChild)(infoChild, 'video');
925
952
  call.isGroup = infoChild.attrs.type === 'group' || !!infoChild.attrs['group-jid'];
926
- call.groupJid = infoChild.attrs['group-jid'];
953
+ call.groupJid = resolveJid(infoChild.attrs['group-jid']);
927
954
  callOfferCache.set(call.id, call);
928
955
  }
929
956
  const existingCall = callOfferCache.get(call.id);
@@ -1049,7 +1076,6 @@ const makeMessagesRecvSocket = (config) => {
1049
1076
  .catch(error => onUnexpectedError(error, 'handling bad ack'));
1050
1077
  });
1051
1078
  ev.on('call', ([call]) => {
1052
- // missed call + group call notification message generation
1053
1079
  if (call.status === 'timeout' || (call.status === 'offer' && call.isGroup)) {
1054
1080
  const msg = {
1055
1081
  key: {
@@ -1087,20 +1113,17 @@ const makeMessagesRecvSocket = (config) => {
1087
1113
  } else if (connection === 'open') {
1088
1114
  sendActiveReceipts = true;
1089
1115
  }
1090
- // Update sendActiveReceipts based on connection status
1091
1116
  if (typeof update.isOnline !== 'undefined') {
1092
1117
  sendActiveReceipts = update.isOnline;
1093
1118
  logger.trace(`sendActiveReceipts set to "${sendActiveReceipts}"`);
1094
1119
  }
1095
1120
  });
1096
- // Enhanced retry logic for stuck pending messages with anti-ban delays
1097
1121
  ev.on('messages.update', (updates) => {
1098
1122
  const config = (0, performance_config_1.getPerformanceConfig)();
1099
1123
  updates.forEach(update => {
1100
1124
  if (update.update.status === WAProto_1.proto.WebMessageInfo.Status.PENDING &&
1101
- Date.now() - (update.update.timestamp || 0) > 30000) { // 30 seconds
1125
+ Date.now() - (update.update.timestamp || 0) > 30000) {
1102
1126
  logger.debug({ key: update.key }, 'retrying stuck pending message with anti-ban delay');
1103
- // Apply anti-ban delay before retry
1104
1127
  setTimeout(async () => {
1105
1128
  try {
1106
1129
  const msg = await getMessage(update.key);
@@ -29,11 +29,20 @@ exports.NACK_REASONS = {
29
29
  */
30
30
  function decodeMessageNode(stanza, meId, meLid) {
31
31
  var _a, _b, _c, _d, _e, _f, _g;
32
+ if (!stanza || !stanza.attrs) {
33
+ throw new boom_1.Boom('Invalid stanza or missing attributes', { statusCode: 400 });
34
+ }
32
35
  let msgType;
33
36
  let chatId;
34
37
  let author;
35
38
  const msgId = stanza.attrs.id;
39
+ if (!msgId) {
40
+ throw new boom_1.Boom('Missing message ID', { statusCode: 400 });
41
+ }
36
42
  const from = stanza.attrs.from;
43
+ if (!from) {
44
+ throw new boom_1.Boom('Missing sender JID', { statusCode: 400 });
45
+ }
37
46
  const senderPn = (_a = stanza === null || stanza === void 0 ? void 0 : stanza.attrs) === null || _a === void 0 ? void 0 : _a.sender_pn;
38
47
  const senderLid = (_b = stanza === null || stanza === void 0 ? void 0 : stanza.attrs) === null || _b === void 0 ? void 0 : _b.sender_lid;
39
48
  const participant = stanza.attrs.participant;
@@ -1194,49 +1194,4 @@ const assertMediaContent = (content) => {
1194
1194
  return mediaContent;
1195
1195
  };
1196
1196
  exports.assertMediaContent = assertMediaContent;
1197
- const getNormalizedJid = (jid) => {
1198
- if (!jid) return jid;
1199
- return (0, WABinary_1.jidNormalizedUser)(jid);
1200
- };
1201
- exports.getNormalizedJid = getNormalizedJid;
1202
-
1203
- const isLidFormat = (jid) => {
1204
- return (0, WABinary_1.isLidUser)(jid);
1205
- };
1206
- exports.isLidFormat = isLidFormat;
1207
1197
 
1208
- // Legacy compatibility functions - deprecated, use native APIs
1209
- const toJid = (id) => {
1210
- console.warn('toJid is deprecated. Use getNormalizedJid instead.');
1211
- return getNormalizedJid(id);
1212
- };
1213
- exports.toJid = toJid;
1214
-
1215
- const getSenderLid = (message) => {
1216
- const sender = message?.key?.participant || message?.key?.remoteJid;
1217
- if (!sender) return null;
1218
-
1219
- const normalized = getNormalizedJid(sender);
1220
- return {
1221
- jid: sender,
1222
- lid: normalized,
1223
- isValid: true,
1224
- user: (0, WABinary_1.jidDecode)(normalized)?.user || 'unknown'
1225
- };
1226
- };
1227
- exports.getSenderLid = getSenderLid;
1228
-
1229
- const validateJid = (jid) => {
1230
- console.warn('validateJid is deprecated. Use native Baileys validation.');
1231
- if (!jid || typeof jid !== 'string') {
1232
- return { isValid: false, error: 'Invalid JID: must be a non-empty string' };
1233
- }
1234
-
1235
- try {
1236
- const decoded = (0, WABinary_1.jidDecode)(jid);
1237
- return { isValid: !!decoded };
1238
- } catch {
1239
- return { isValid: false, error: 'Invalid JID format' };
1240
- }
1241
- };
1242
- exports.validateJid = validateJid;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realvare/based",
3
- "version": "2.7.02",
3
+ "version": "2.7.4",
4
4
  "description": "whatsapp api by sam",
5
5
  "keywords": [
6
6
  "baileys",
@@ -9,7 +9,8 @@
9
9
  "whatsapp-web",
10
10
  "whatsapp-bot",
11
11
  "automation",
12
- "multi-device"
12
+ "multi-device",
13
+ "based"
13
14
  ],
14
15
  "homepage": "https://github.com/realvare/based.git",
15
16
  "repository": {
@@ -44,20 +45,20 @@
44
45
  "@cacheable/node-cache": "^1.4.0",
45
46
  "@hapi/boom": "^10.0.1",
46
47
  "async-mutex": "^0.5.0",
47
- "axios": "^1.7.0",
48
- "cache-manager": "^5.7.6",
48
+ "axios": "^1.13.2",
49
+ "cache-manager": "^7.2.5",
49
50
  "chalk": "^4.1.2",
50
- "cheerio": "^1.0.0-rc.12",
51
+ "cheerio": "^1.1.2",
51
52
  "gradient-string": "^2.0.2",
52
- "jimp": "^0.22.10",
53
- "libphonenumber-js": "^1.10.58",
54
- "libsignal": "github:NaufalYupra/libsignal-node",
53
+ "jimp": "^1.6.0",
54
+ "libphonenumber-js": "^1.12.31",
55
+ "libsignal": "npm:@newfadel/libsignal-node",
55
56
  "lodash": "^4.17.21",
56
- "music-metadata": "^7.12.3",
57
- "pino": "^9.6",
58
- "protobufjs": "^7.2.5",
59
- "uuid": "^10.0.0",
60
- "ws": "^8.18.0"
57
+ "music-metadata": "^11.7.0",
58
+ "pino": "^10.1.0",
59
+ "protobufjs": "^7.5.4",
60
+ "uuid": "^13.0.0",
61
+ "ws": "^8.18.3"
61
62
  },
62
63
  "devDependencies": {
63
64
  "@adiwajshing/eslint-config": "github:adiwajshing/eslint-config",
@@ -69,7 +70,7 @@
69
70
  "conventional-changelog-cli": "^2.2.2",
70
71
  "eslint": "^8.57.0",
71
72
  "jest": "^29.7.0",
72
- "jimp": "^0.22.10",
73
+ "jimp": "^1.6.0",
73
74
  "json": "^11.0.0",
74
75
  "link-preview-js": "^3.0.0",
75
76
  "open": "^10.1.0",
@@ -79,7 +80,7 @@
79
80
  "ts-jest": "^29.1.2",
80
81
  "ts-node": "^10.9.2",
81
82
  "typedoc": "^0.25.12",
82
- "typescript": "^5.6.0"
83
+ "typescript": "^5.9.3"
83
84
  },
84
85
  "peerDependencies": {
85
86
  "audio-decode": "^2.1.3",