@badzz88/baileys 8.5.4 → 8.5.6

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 (269) hide show
  1. package/README.md +2 -2
  2. package/WAProto/fix-imports.js +69 -79
  3. package/WAProto/index.d.ts +15305 -87935
  4. package/WAProto/index.js +41109 -194076
  5. package/engine-requirements.js +15 -8
  6. package/package.json +23 -19
  7. package/src/Defaults/index.js +186 -0
  8. package/src/Signal/Group/ciphertext-message.js +15 -0
  9. package/src/Signal/Group/group-session-builder.js +86 -0
  10. package/src/Signal/Group/group_cipher.js +82 -0
  11. package/src/Signal/Group/index.js +140 -0
  12. package/src/Signal/Group/keyhelper.js +77 -0
  13. package/src/Signal/Group/sender-chain-key.js +29 -0
  14. package/src/Signal/Group/sender-key-distribution-message.js +66 -0
  15. package/src/Signal/Group/sender-key-message.js +69 -0
  16. package/src/Signal/Group/sender-key-name.js +51 -0
  17. package/src/Signal/Group/sender-key-record.js +44 -0
  18. package/src/Signal/Group/sender-key-state.js +87 -0
  19. package/src/Signal/Group/sender-message-key.js +29 -0
  20. package/src/Signal/libsignal.js +455 -0
  21. package/src/Signal/lid-mapping.js +273 -0
  22. package/src/Socket/Client/index.js +31 -0
  23. package/src/Socket/Client/types.js +13 -0
  24. package/src/Socket/Client/websocket.js +61 -0
  25. package/src/Socket/aigroups.js +221 -0
  26. package/src/Socket/business.js +411 -0
  27. package/src/Socket/chats.js +1449 -0
  28. package/src/Socket/communities.js +463 -0
  29. package/src/Socket/graphql.js +523 -0
  30. package/src/Socket/groups.js +516 -0
  31. package/src/Socket/index.js +31 -0
  32. package/src/Socket/interactive-handler.js +527 -0
  33. package/src/Socket/interop.js +339 -0
  34. package/src/Socket/luxu.js +349 -0
  35. package/src/Socket/managed-account.js +98 -0
  36. package/src/Socket/messages-recv.js +2685 -0
  37. package/src/Socket/messages-send.js +2047 -0
  38. package/src/Socket/mex.js +57 -0
  39. package/src/Socket/newsletter.js +455 -0
  40. package/src/Socket/privacy.js +125 -0
  41. package/src/Socket/registration.js +236 -0
  42. package/src/Socket/socket.js +983 -0
  43. package/src/Socket/text-router.js +65 -0
  44. package/src/Socket/username.js +130 -0
  45. package/src/Store/index.js +33 -0
  46. package/src/Store/keyed-db.js +118 -0
  47. package/src/Store/make-cache-manager-store.js +84 -0
  48. package/src/Store/make-in-memory-store.js +551 -0
  49. package/src/Store/make-ordered-dictionary.js +81 -0
  50. package/src/Store/object-repository.js +26 -0
  51. package/src/Types/Auth.js +31 -0
  52. package/src/Types/Bussines.js +2 -0
  53. package/src/Types/Call.js +2 -0
  54. package/src/Types/Chat.js +4 -0
  55. package/src/Types/Contact.js +2 -0
  56. package/src/Types/Events.js +2 -0
  57. package/src/Types/GroupMetadata.js +2 -0
  58. package/src/Types/Label.js +26 -0
  59. package/src/Types/LabelAssociation.js +8 -0
  60. package/src/Types/Message.js +93 -0
  61. package/src/Types/Mex.js +112 -0
  62. package/src/Types/Newsletter.js +109 -0
  63. package/src/Types/Product.js +2 -0
  64. package/src/Types/Signal.js +2 -0
  65. package/src/Types/Socket.js +2 -0
  66. package/src/Types/State.js +62 -0
  67. package/src/Types/USync.js +2 -0
  68. package/src/Types/index.js +56 -0
  69. package/src/Utils/auth-utils.js +254 -0
  70. package/src/Utils/browser-utils.js +112 -0
  71. package/src/Utils/business.js +240 -0
  72. package/src/Utils/chat-utils.js +1230 -0
  73. package/src/Utils/command-loader.d.ts +27 -0
  74. package/src/Utils/command-loader.js +89 -0
  75. package/src/Utils/companion-reg-client-utils.js +40 -0
  76. package/src/Utils/consumer-application.js +105 -0
  77. package/src/Utils/crypto.js +118 -0
  78. package/src/Utils/curve25519-js.js +242 -0
  79. package/src/Utils/decode-wa-message.js +529 -0
  80. package/src/Utils/event-buffer.js +580 -0
  81. package/src/Utils/generics.js +483 -0
  82. package/src/Utils/group-history.js +44 -0
  83. package/src/Utils/history.js +232 -0
  84. package/src/Utils/identity-change-handler.js +52 -0
  85. package/src/Utils/index.js +58 -0
  86. package/src/Utils/jid-display-normalization.js +195 -0
  87. package/src/Utils/link-preview.js +135 -0
  88. package/src/Utils/logger.js +8 -0
  89. package/src/Utils/lt-hash.js +25 -0
  90. package/src/Utils/make-mutex.js +36 -0
  91. package/src/Utils/message-composer.js +471 -0
  92. package/src/Utils/message-retry-manager.js +217 -0
  93. package/src/Utils/messages-media.js +843 -0
  94. package/src/Utils/messages.js +2817 -0
  95. package/src/Utils/meta-ai-msmsg.js +222 -0
  96. package/src/Utils/native-bridge.js +68 -0
  97. package/src/Utils/noise-handler.js +169 -0
  98. package/src/Utils/offline-node-processor.js +34 -0
  99. package/src/Utils/pre-key-manager.js +90 -0
  100. package/src/Utils/process-message.js +950 -0
  101. package/src/Utils/reporting-utils.js +261 -0
  102. package/src/Utils/session-pool.d.ts +14 -0
  103. package/src/Utils/session-pool.js +70 -0
  104. package/src/Utils/signal.js +217 -0
  105. package/src/Utils/stanza-ack.js +30 -0
  106. package/src/Utils/sticker.d.ts +13 -0
  107. package/src/Utils/sticker.js +123 -0
  108. package/src/Utils/sync-action-utils.js +45 -0
  109. package/src/Utils/tc-token-utils.js +158 -0
  110. package/src/Utils/use-multi-file-auth-state.js +111 -0
  111. package/src/Utils/validate-connection.js +209 -0
  112. package/src/Utils/view-once-cache.d.ts +12 -0
  113. package/src/Utils/view-once-cache.js +63 -0
  114. package/src/Utils/voip-rekey.js +22 -0
  115. package/src/WABinary/constants.js +1304 -0
  116. package/src/WABinary/decode.js +342 -0
  117. package/src/WABinary/encode.js +225 -0
  118. package/src/WABinary/generic-utils.js +238 -0
  119. package/src/WABinary/index.js +34 -0
  120. package/src/WABinary/jid-utils.js +351 -0
  121. package/src/WABinary/types.js +2 -0
  122. package/src/WAM/BinaryInfo.js +13 -0
  123. package/src/WAM/constants.js +39484 -0
  124. package/src/WAM/encode.js +149 -0
  125. package/src/WAM/index.js +32 -0
  126. package/src/WAUSync/Protocols/USyncBotProfileProtocol.js +53 -0
  127. package/src/WAUSync/Protocols/USyncBusinessProtocol.js +44 -0
  128. package/src/WAUSync/Protocols/USyncContactProtocol.js +55 -0
  129. package/src/WAUSync/Protocols/USyncDeviceProtocol.js +55 -0
  130. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
  131. package/src/WAUSync/Protocols/USyncFeatureProtocol.js +54 -0
  132. package/src/WAUSync/Protocols/USyncLIDProtocol.js +32 -0
  133. package/src/WAUSync/Protocols/USyncNewsletterProtocol.js +473 -0
  134. package/src/WAUSync/Protocols/USyncPictureProtocol.js +34 -0
  135. package/src/WAUSync/Protocols/USyncSidelistProtocol.js +29 -0
  136. package/src/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  137. package/src/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
  138. package/src/WAUSync/Protocols/USyncUsernameProtocol.js +28 -0
  139. package/src/WAUSync/Protocols/index.js +40 -0
  140. package/src/WAUSync/USyncQuery.js +126 -0
  141. package/src/WAUSync/USyncUser.js +50 -0
  142. package/src/WAUSync/index.js +32 -0
  143. package/src/antiban.js +4152 -0
  144. package/{lib → src}/index.js +51 -49
  145. package/lib/Defaults/index.js +0 -195
  146. package/lib/Signal/Group/ciphertext-message.js +0 -15
  147. package/lib/Signal/Group/group-session-builder.js +0 -92
  148. package/lib/Signal/Group/group_cipher.js +0 -89
  149. package/lib/Signal/Group/index.js +0 -136
  150. package/lib/Signal/Group/keyhelper.js +0 -73
  151. package/lib/Signal/Group/sender-chain-key.js +0 -32
  152. package/lib/Signal/Group/sender-key-distribution-message.js +0 -66
  153. package/lib/Signal/Group/sender-key-message.js +0 -69
  154. package/lib/Signal/Group/sender-key-name.js +0 -50
  155. package/lib/Signal/Group/sender-key-record.js +0 -44
  156. package/lib/Signal/Group/sender-key-state.js +0 -97
  157. package/lib/Signal/Group/sender-message-key.js +0 -30
  158. package/lib/Signal/libsignal.js +0 -470
  159. package/lib/Signal/lid-mapping.js +0 -280
  160. package/lib/Socket/Client/index.js +0 -30
  161. package/lib/Socket/Client/types.js +0 -13
  162. package/lib/Socket/Client/websocket.js +0 -62
  163. package/lib/Socket/aigroups.js +0 -240
  164. package/lib/Socket/business.js +0 -414
  165. package/lib/Socket/chats.js +0 -2165
  166. package/lib/Socket/communities.js +0 -545
  167. package/lib/Socket/graphql.js +0 -863
  168. package/lib/Socket/groups.js +0 -685
  169. package/lib/Socket/index.js +0 -41
  170. package/lib/Socket/interactive-handler.js +0 -579
  171. package/lib/Socket/interop.js +0 -430
  172. package/lib/Socket/managed-account.js +0 -214
  173. package/lib/Socket/messages-recv.js +0 -3000
  174. package/lib/Socket/messages-send.js +0 -2155
  175. package/lib/Socket/mex.js +0 -47
  176. package/lib/Socket/newsletter.js +0 -814
  177. package/lib/Socket/privacy.js +0 -261
  178. package/lib/Socket/registration.js +0 -434
  179. package/lib/Socket/socket.js +0 -1074
  180. package/lib/Socket/username.js +0 -160
  181. package/lib/Store/index.js +0 -36
  182. package/lib/Store/make-cache-manager-store.js +0 -90
  183. package/lib/Store/make-in-memory-store.js +0 -488
  184. package/lib/Store/make-ordered-dictionary.js +0 -81
  185. package/lib/Store/object-repository.js +0 -29
  186. package/lib/Types/Auth.js +0 -38
  187. package/lib/Types/Bussines.js +0 -2
  188. package/lib/Types/Call.js +0 -2
  189. package/lib/Types/Chat.js +0 -4
  190. package/lib/Types/Contact.js +0 -2
  191. package/lib/Types/Events.js +0 -2
  192. package/lib/Types/GroupMetadata.js +0 -2
  193. package/lib/Types/Label.js +0 -27
  194. package/lib/Types/LabelAssociation.js +0 -9
  195. package/lib/Types/Message.js +0 -95
  196. package/lib/Types/Newsletter.js +0 -152
  197. package/lib/Types/Product.js +0 -2
  198. package/lib/Types/Signal.js +0 -2
  199. package/lib/Types/Socket.js +0 -2
  200. package/lib/Types/State.js +0 -70
  201. package/lib/Types/USync.js +0 -2
  202. package/lib/Types/index.js +0 -55
  203. package/lib/Utils/auth-utils.js +0 -301
  204. package/lib/Utils/browser-utils.js +0 -114
  205. package/lib/Utils/business.js +0 -243
  206. package/lib/Utils/chat-utils.js +0 -1272
  207. package/lib/Utils/consumer-application.js +0 -107
  208. package/lib/Utils/crypto.js +0 -125
  209. package/lib/Utils/decode-wa-message.js +0 -793
  210. package/lib/Utils/event-buffer.js +0 -583
  211. package/lib/Utils/generics.js +0 -617
  212. package/lib/Utils/group-history.js +0 -51
  213. package/lib/Utils/history.js +0 -244
  214. package/lib/Utils/identity-change-handler.js +0 -52
  215. package/lib/Utils/index.js +0 -53
  216. package/lib/Utils/jid-display-normalization.js +0 -218
  217. package/lib/Utils/link-preview.js +0 -138
  218. package/lib/Utils/logger.js +0 -9
  219. package/lib/Utils/lt-hash.js +0 -6
  220. package/lib/Utils/make-mutex.js +0 -36
  221. package/lib/Utils/message-composer.js +0 -479
  222. package/lib/Utils/message-inspect.js +0 -393
  223. package/lib/Utils/message-retry-manager.js +0 -205
  224. package/lib/Utils/messages-media.js +0 -925
  225. package/lib/Utils/messages.js +0 -2482
  226. package/lib/Utils/meta-ai-msmsg.js +0 -122
  227. package/lib/Utils/noise-handler.js +0 -194
  228. package/lib/Utils/offline-node-processor.js +0 -37
  229. package/lib/Utils/pre-key-manager.js +0 -97
  230. package/lib/Utils/process-message.js +0 -1139
  231. package/lib/Utils/reporting-utils.js +0 -262
  232. package/lib/Utils/signal.js +0 -189
  233. package/lib/Utils/stanza-ack.js +0 -67
  234. package/lib/Utils/sync-action-utils.js +0 -51
  235. package/lib/Utils/tc-token-utils.js +0 -156
  236. package/lib/Utils/use-multi-file-auth-state.js +0 -137
  237. package/lib/Utils/validate-connection.js +0 -259
  238. package/lib/Utils/voip-rekey.js +0 -22
  239. package/lib/WABinary/constants.js +0 -1304
  240. package/lib/WABinary/decode.js +0 -377
  241. package/lib/WABinary/encode.js +0 -58
  242. package/lib/WABinary/generic-utils.js +0 -150
  243. package/lib/WABinary/index.js +0 -33
  244. package/lib/WABinary/jid-utils.js +0 -374
  245. package/lib/WABinary/types.js +0 -2
  246. package/lib/WAM/BinaryInfo.js +0 -13
  247. package/lib/WAM/constants.js +0 -39486
  248. package/lib/WAM/encode.js +0 -142
  249. package/lib/WAM/index.js +0 -31
  250. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +0 -55
  251. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +0 -100
  252. package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -60
  253. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -65
  254. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -27
  255. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +0 -72
  256. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +0 -31
  257. package/lib/WAUSync/Protocols/USyncNewsletterProtocol.js +0 -865
  258. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +0 -32
  259. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +0 -29
  260. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -44
  261. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +0 -38
  262. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +0 -28
  263. package/lib/WAUSync/Protocols/index.js +0 -40
  264. package/lib/WAUSync/USyncBackoff.js +0 -26
  265. package/lib/WAUSync/USyncQuery.js +0 -204
  266. package/lib/WAUSync/USyncUser.js +0 -58
  267. package/lib/WAUSync/index.js +0 -32
  268. package/lib/antiban.js +0 -4391
  269. /package/{lib → src}/Defaults/phonenumber-mcc.json +0 -0
@@ -0,0 +1,483 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.isWABusinessPlatform =
4
+ exports.getCodeFromWSError =
5
+ exports.getCallStatusFromNode =
6
+ exports.getErrorCodeFromStreamError =
7
+ exports.getStatusFromReceiptType =
8
+ exports.generateMdTagPrefix =
9
+ exports.fetchLatestWaWebVersion =
10
+ exports.fetchLatestBaileysVersion =
11
+ exports.bindWaitForConnectionUpdate =
12
+ exports.generateMessageID =
13
+ exports.generateMessageIDV2 =
14
+ exports.delayCancellable =
15
+ exports.delay =
16
+ exports.debouncedTimeout =
17
+ exports.unixTimestampSeconds =
18
+ exports.toNumber =
19
+ exports.encodeBigEndian =
20
+ exports.generateRegistrationId =
21
+ exports.encodeWAMessage =
22
+ exports.generateParticipantHashV2 =
23
+ exports.unpadRandomMax16 =
24
+ exports.writeRandomPadMax16 =
25
+ exports.isStringNullOrEmpty =
26
+ exports.getKeyAuthor =
27
+ exports.BufferJSON =
28
+ exports.jitterDelay =
29
+ exports.exponentialBackoff =
30
+ exports.bytesToHex =
31
+ exports.hexToBytes =
32
+ exports.bytesToBase64Url =
33
+ exports.sha256Hex =
34
+ exports.hmacSha256 =
35
+ exports.normalizeJidBatch =
36
+ exports.sleep =
37
+ exports.withRetry =
38
+ void 0;
39
+ exports.promiseTimeout = promiseTimeout;
40
+ exports.bindWaitForEvent = bindWaitForEvent;
41
+ exports.trimUndefined = trimUndefined;
42
+ exports.bytesToCrockford = bytesToCrockford;
43
+ exports.encodeNewsletterMessage = encodeNewsletterMessage;
44
+ exports.generateIOSMessageID = void 0;
45
+ exports.generateAndroMessageID = void 0;
46
+ const boom_1 = require('@hapi/boom');
47
+ const crypto_1 = require('crypto');
48
+ const Crypto_1 = require('./crypto');
49
+ const DEFAULTS_1 = require('../Defaults');
50
+ const index_js_1 = require('../../WAProto/index.js');
51
+ const baileysVersion = DEFAULTS_1.VERSION;
52
+ const Types_1 = require('../Types');
53
+ const WABinary_1 = require('../WABinary');
54
+ const crypto_2 = require('./crypto');
55
+ exports.BufferJSON = {
56
+ replacer: (k, value) => {
57
+ if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
58
+ return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') };
59
+ }
60
+ return value;
61
+ },
62
+ reviver: (_, value) => {
63
+ if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
64
+ return Buffer.from(value.data, 'base64');
65
+ }
66
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
67
+ const keys = Object.keys(value);
68
+ if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
69
+ const values = Object.values(value);
70
+ if (values.every(v => typeof v === 'number')) {
71
+ return Buffer.from(values);
72
+ }
73
+ }
74
+ }
75
+ return value;
76
+ }
77
+ };
78
+ const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || '';
79
+ exports.getKeyAuthor = getKeyAuthor;
80
+ const isStringNullOrEmpty = value => value == null || value === '';
81
+ exports.isStringNullOrEmpty = isStringNullOrEmpty;
82
+ const writeRandomPadMax16 = msg => {
83
+ const pad = (0, crypto_1.randomBytes)(1);
84
+ const padLength = (pad[0] & 0x0f) + 1;
85
+ return Buffer.concat([msg, Buffer.alloc(padLength, padLength)]);
86
+ };
87
+ exports.writeRandomPadMax16 = writeRandomPadMax16;
88
+ const unpadRandomMax16 = e => {
89
+ const t = new Uint8Array(e);
90
+ if (0 === t.length) {
91
+ throw new Error('unpadPkcs7 given empty bytes');
92
+ }
93
+ var r = t[t.length - 1];
94
+ if (r > t.length) {
95
+ throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
96
+ }
97
+ return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
98
+ };
99
+ exports.unpadRandomMax16 = unpadRandomMax16;
100
+ const generateParticipantHashV2 = participants => {
101
+ participants.sort();
102
+ const sha256Hash = (0, crypto_2.sha256)(Buffer.from(participants.join(''))).toString('base64');
103
+ return '2:' + sha256Hash.slice(0, 6);
104
+ };
105
+ exports.generateParticipantHashV2 = generateParticipantHashV2;
106
+ const encodeWAMessage = message => (0, exports.writeRandomPadMax16)(index_js_1.proto.Message.encode(message).finish());
107
+ exports.encodeWAMessage = encodeWAMessage;
108
+ const generateRegistrationId = () => {
109
+ return Uint16Array.from((0, crypto_1.randomBytes)(2))[0] & 16383;
110
+ };
111
+ exports.generateRegistrationId = generateRegistrationId;
112
+ const encodeBigEndian = (e, t = 4) => {
113
+ let r = e;
114
+ const a = new Uint8Array(t);
115
+ for (let i = t - 1; i >= 0; i--) {
116
+ a[i] = 255 & r;
117
+ r >>>= 8;
118
+ }
119
+ return a;
120
+ };
121
+ exports.encodeBigEndian = encodeBigEndian;
122
+ const toNumber = t => (typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0);
123
+ exports.toNumber = toNumber;
124
+ const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000);
125
+ exports.unixTimestampSeconds = unixTimestampSeconds;
126
+ const debouncedTimeout = (intervalMs = 1000, task) => {
127
+ let timeout;
128
+ return {
129
+ start: (newIntervalMs, newTask) => {
130
+ task = newTask || task;
131
+ intervalMs = newIntervalMs || intervalMs;
132
+ timeout && clearTimeout(timeout);
133
+ timeout = setTimeout(() => task?.(), intervalMs);
134
+ },
135
+ cancel: () => {
136
+ timeout && clearTimeout(timeout);
137
+ timeout = undefined;
138
+ },
139
+ setTask: newTask => (task = newTask),
140
+ setInterval: newInterval => (intervalMs = newInterval)
141
+ };
142
+ };
143
+ exports.debouncedTimeout = debouncedTimeout;
144
+ const delay = ms => (0, exports.delayCancellable)(ms).delay;
145
+ exports.delay = delay;
146
+ const delayCancellable = ms => {
147
+ const stack = new Error().stack;
148
+ let timeout;
149
+ let reject;
150
+ const delay = new Promise((resolve, _reject) => {
151
+ timeout = setTimeout(resolve, ms);
152
+ reject = _reject;
153
+ });
154
+ const cancel = () => {
155
+ clearTimeout(timeout);
156
+ reject(new boom_1.Boom('Cancelled', {
157
+ statusCode: 500,
158
+ data: {
159
+ stack
160
+ }
161
+ }));
162
+ };
163
+ return { delay, cancel };
164
+ };
165
+ exports.delayCancellable = delayCancellable;
166
+ async function promiseTimeout(ms, promise) {
167
+ if (!ms) {
168
+ return new Promise(promise);
169
+ }
170
+ const stack = new Error().stack;
171
+ const { delay, cancel } = (0, exports.delayCancellable)(ms);
172
+ const p = new Promise((resolve, reject) => {
173
+ delay
174
+ .then(() => reject(new boom_1.Boom('Timed Out', {
175
+ statusCode: Types_1.DisconnectReason.timedOut,
176
+ data: {
177
+ stack
178
+ }
179
+ })))
180
+ .catch(err => reject(err));
181
+ promise(resolve, reject);
182
+ }).finally(cancel);
183
+ return p;
184
+ }
185
+ const generateMessageIDV2 = (userId) => {
186
+ const data = Buffer.alloc(8 + 20 + 16);
187
+ data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
188
+ if (userId) {
189
+ const id = (0, WABinary_1.jidDecode)(userId);
190
+ if (id?.user) {
191
+ data.write(id.user, 8);
192
+ data.write('@c.us', 8 + id.user.length);
193
+ }
194
+ }
195
+ const random = (0, crypto_1.randomBytes)(16);
196
+ random.copy(data, 28);
197
+ const hash = (0, crypto_1.createHash)('sha256').update(data).digest();
198
+ return 'XZCYYX-' + hash.toString('hex').toUpperCase().substring(0, 18);
199
+ };
200
+ exports.generateMessageIDV2 = generateMessageIDV2;
201
+ const generateIOSMessageID = () => {
202
+ const prefix = '3A';
203
+ const random = (0, crypto_1.randomBytes)(9.5);
204
+ return (prefix + random.toString('hex')).toUpperCase().substring(0, 21);
205
+ };
206
+ exports.generateIOSMessageID = generateIOSMessageID;
207
+ const generateAndroMessageID = () => {
208
+ const prefix = '3A';
209
+ const random = (0, crypto_1.randomBytes)(16);
210
+ return (random.toString('hex')).toUpperCase().substring(0, 21);
211
+ };
212
+ exports.generateAndroMessageID = generateAndroMessageID;
213
+ const generateMessageID = () => 'XZCYYX-' + (0, crypto_1.randomBytes)(18).toString('hex').toUpperCase();
214
+ exports.generateMessageID = generateMessageID;
215
+ function bindWaitForEvent(ev, event) {
216
+ return async (check, timeoutMs) => {
217
+ let listener;
218
+ let closeListener;
219
+ await promiseTimeout(timeoutMs, (resolve, reject) => {
220
+ closeListener = ({ connection, lastDisconnect }) => {
221
+ if (connection === 'close') {
222
+ reject(lastDisconnect?.error ||
223
+ new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed }));
224
+ }
225
+ };
226
+ ev.on('connection.update', closeListener);
227
+ listener = async (update) => {
228
+ if (await check(update)) {
229
+ resolve();
230
+ }
231
+ };
232
+ ev.on(event, listener);
233
+ }).finally(() => {
234
+ ev.off(event, listener);
235
+ ev.off('connection.update', closeListener);
236
+ });
237
+ };
238
+ }
239
+ const bindWaitForConnectionUpdate = ev => bindWaitForEvent(ev, 'connection.update');
240
+ exports.bindWaitForConnectionUpdate = bindWaitForConnectionUpdate;
241
+ const fetchLatestBaileysVersion = async (options = {}) => {
242
+ const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts';
243
+ try {
244
+ const response = await fetch(URL, {
245
+ dispatcher: options.dispatcher,
246
+ method: 'GET',
247
+ headers: options.headers
248
+ });
249
+ if (!response.ok) {
250
+ throw new boom_1.Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, {
251
+ statusCode: response.status
252
+ });
253
+ }
254
+ const text = await response.text();
255
+ const lines = text.split('\n');
256
+ const versionLine = lines[6];
257
+ const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/);
258
+ if (versionMatch) {
259
+ const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])];
260
+ return {
261
+ version,
262
+ isLatest: true
263
+ };
264
+ }
265
+ else {
266
+ throw new Error('Could not parse version from Defaults/index.ts');
267
+ }
268
+ }
269
+ catch (error) {
270
+ return {
271
+ version: baileysVersion,
272
+ isLatest: false,
273
+ error
274
+ };
275
+ }
276
+ };
277
+ exports.fetchLatestBaileysVersion = fetchLatestBaileysVersion;
278
+ const fetchLatestWaWebVersion = async (options = {}) => {
279
+ try {
280
+ const defaultHeaders = {
281
+ 'sec-fetch-site': 'none',
282
+ 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
283
+ };
284
+ const headers = { ...defaultHeaders, ...options.headers };
285
+ const response = await fetch('https://web.whatsapp.com/sw.js', {
286
+ ...options,
287
+ method: 'GET',
288
+ headers
289
+ });
290
+ if (!response.ok) {
291
+ throw new boom_1.Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status });
292
+ }
293
+ const data = await response.text();
294
+ const regex = /\\?"client_revision\\?":\s*(\d+)/;
295
+ const match = data.match(regex);
296
+ if (!match?.[1]) {
297
+ return {
298
+ version: baileysVersion,
299
+ isLatest: false,
300
+ error: {
301
+ message: 'Could not find client revision in the fetched content'
302
+ }
303
+ };
304
+ }
305
+ const clientRevision = match[1];
306
+ return {
307
+ version: [2, 3000, +clientRevision],
308
+ isLatest: true
309
+ };
310
+ }
311
+ catch (error) {
312
+ return {
313
+ version: baileysVersion,
314
+ isLatest: false,
315
+ error
316
+ };
317
+ }
318
+ };
319
+ exports.fetchLatestWaWebVersion = fetchLatestWaWebVersion;
320
+ const generateMdTagPrefix = () => {
321
+ const bytes = (0, crypto_1.randomBytes)(4);
322
+ return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
323
+ };
324
+ exports.generateMdTagPrefix = generateMdTagPrefix;
325
+ const STATUS_MAP = {
326
+ sender: index_js_1.proto.WebMessageInfo.Status.SERVER_ACK,
327
+ played: index_js_1.proto.WebMessageInfo.Status.PLAYED,
328
+ read: index_js_1.proto.WebMessageInfo.Status.READ,
329
+ 'read-self': index_js_1.proto.WebMessageInfo.Status.READ
330
+ };
331
+ const getStatusFromReceiptType = type => {
332
+ const status = STATUS_MAP[type];
333
+ if (typeof type === 'undefined') {
334
+ return index_js_1.proto.WebMessageInfo.Status.DELIVERY_ACK;
335
+ }
336
+ return status;
337
+ };
338
+ exports.getStatusFromReceiptType = getStatusFromReceiptType;
339
+ const CODE_MAP = {
340
+ conflict: Types_1.DisconnectReason.connectionReplaced
341
+ };
342
+ const getErrorCodeFromStreamError = node => {
343
+ const [reasonNode] = (0, WABinary_1.getAllBinaryNodeChildren)(node);
344
+ let reason = reasonNode?.tag || 'unknown';
345
+ const statusCode = +(node.attrs.code || CODE_MAP[reason] || Types_1.DisconnectReason.badSession);
346
+ if (statusCode === Types_1.DisconnectReason.restartRequired) {
347
+ reason = 'restart required';
348
+ }
349
+ return {
350
+ reason,
351
+ statusCode
352
+ };
353
+ };
354
+ exports.getErrorCodeFromStreamError = getErrorCodeFromStreamError;
355
+ const getCallStatusFromNode = ({ tag, attrs }) => {
356
+ let status;
357
+ switch (tag) {
358
+ case 'offer':
359
+ case 'offer_notice':
360
+ status = 'offer';
361
+ break;
362
+ case 'terminate':
363
+ if (attrs.reason === 'timeout') {
364
+ status = 'timeout';
365
+ }
366
+ else {
367
+ status = 'terminate';
368
+ }
369
+ break;
370
+ case 'reject':
371
+ status = 'reject';
372
+ break;
373
+ case 'accept':
374
+ status = 'accept';
375
+ break;
376
+ default:
377
+ status = 'ringing';
378
+ break;
379
+ }
380
+ return status;
381
+ };
382
+ exports.getCallStatusFromNode = getCallStatusFromNode;
383
+ const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
384
+ const getCodeFromWSError = error => {
385
+ let statusCode = 500;
386
+ if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
387
+ const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length);
388
+ if (!Number.isNaN(code) && code >= 400) {
389
+ statusCode = code;
390
+ }
391
+ }
392
+ else if (error?.code?.startsWith('E') ||
393
+ error?.message?.includes('timed out')) {
394
+ statusCode = 408;
395
+ }
396
+ return statusCode;
397
+ };
398
+ exports.getCodeFromWSError = getCodeFromWSError;
399
+ const isWABusinessPlatform = platform => {
400
+ return platform === 'smbi' || platform === 'smba';
401
+ };
402
+ exports.isWABusinessPlatform = isWABusinessPlatform;
403
+ function trimUndefined(obj) {
404
+ for (const key in obj) {
405
+ if (typeof obj[key] === 'undefined') {
406
+ delete obj[key];
407
+ }
408
+ }
409
+ return obj;
410
+ }
411
+ const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
412
+ function bytesToCrockford(buffer) {
413
+ let value = 0;
414
+ let bitCount = 0;
415
+ const crockford = [];
416
+ for (const element of buffer) {
417
+ value = (value << 8) | (element & 0xff);
418
+ bitCount += 8;
419
+ while (bitCount >= 5) {
420
+ crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
421
+ bitCount -= 5;
422
+ }
423
+ }
424
+ if (bitCount > 0) {
425
+ crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
426
+ }
427
+ return crockford.join('');
428
+ }
429
+ function encodeNewsletterMessage(message) {
430
+ return index_js_1.proto.Message.encode(message).finish();
431
+ }
432
+ const jitterDelay = (baseMs, varianceFraction = 0.3) => {
433
+ const variance = baseMs * varianceFraction;
434
+ return baseMs + (Math.random() * 2 - 1) * variance;
435
+ };
436
+ exports.jitterDelay = jitterDelay;
437
+ const exponentialBackoff = (attempt, baseMs = 500, maxMs = 30000) => {
438
+ return Math.min(baseMs * Math.pow(2, attempt), maxMs);
439
+ };
440
+ exports.exponentialBackoff = exponentialBackoff;
441
+ const bytesToHex = buf => Buffer.from(buf).toString('hex');
442
+ exports.bytesToHex = bytesToHex;
443
+ const hexToBytes = hex => Buffer.from(hex, 'hex');
444
+ exports.hexToBytes = hexToBytes;
445
+ const bytesToBase64Url = buf => Buffer.from(buf).toString('base64url');
446
+ exports.bytesToBase64Url = bytesToBase64Url;
447
+ const sha256Hex = data => (0, crypto_1.createHash)('sha256').update(data).digest('hex');
448
+ exports.sha256Hex = sha256Hex;
449
+ const hmacSha256 = (data, key) => (0, crypto_1.createHmac)('sha256', key).update(data).digest();
450
+ exports.hmacSha256 = hmacSha256;
451
+ const normalizeJidBatch = (jids, normalizer) => {
452
+ const seen = new Set();
453
+ const result = [];
454
+ for (const jid of jids) {
455
+ if (!jid)
456
+ continue;
457
+ const normalized = normalizer ? normalizer(jid) : jid;
458
+ if (!seen.has(normalized)) {
459
+ seen.add(normalized);
460
+ result.push(normalized);
461
+ }
462
+ }
463
+ return result;
464
+ };
465
+ exports.normalizeJidBatch = normalizeJidBatch;
466
+ const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
467
+ exports.sleep = sleep;
468
+ const withRetry = async (fn, maxAttempts = 3, baseDelayMs = 500) => {
469
+ let lastErr;
470
+ for (let i = 0; i < maxAttempts; i++) {
471
+ try {
472
+ return await fn();
473
+ }
474
+ catch (err) {
475
+ lastErr = err;
476
+ if (i < maxAttempts - 1) {
477
+ await sleep(exponentialBackoff(i, baseDelayMs));
478
+ }
479
+ }
480
+ }
481
+ throw lastErr;
482
+ };
483
+ exports.withRetry = withRetry;
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.processGroupHistory = exports.decodeGroupHistory = void 0;
4
+ const zlib_1 = require('zlib');
5
+ const index_js_1 = require('../../WAProto/index.js');
6
+ const decodeGroupHistory = (buffer, options = {}) => {
7
+ const { inflate = true, withMessageBytes = false } = options;
8
+ if (!Buffer.isBuffer(buffer) && !(buffer instanceof Uint8Array)) {
9
+ throw new TypeError('decodeGroupHistory: buffer must be Buffer or Uint8Array');
10
+ }
11
+ let data = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
12
+ if (inflate) {
13
+ try {
14
+ data = zlib_1.inflateSync(data);
15
+ }
16
+ catch {
17
+ }
18
+ }
19
+ if (withMessageBytes) {
20
+ const decoded = index_js_1.proto.GroupHistoryWithMessageBytes.decode(data);
21
+ const expand = list => (list || []).map(entry => entry?.messageBytes ? index_js_1.proto.WebMessageInfo.decode(entry.messageBytes) : { key: entry?.key });
22
+ return {
23
+ messages: expand(decoded.messages),
24
+ commentMessages: expand(decoded.commentMessages),
25
+ outOfWindowPinnedMessages: expand(decoded.outOfWindowPinnedMessages),
26
+ uncountedAssociatedMessageLists: (decoded.uncountedAssociatedMessageLists || []).map(l => ({
27
+ parentMessage: l.parentMessage,
28
+ messages: expand(l.messages)
29
+ }))
30
+ };
31
+ }
32
+ return index_js_1.proto.GroupHistory.decode(data);
33
+ };
34
+ exports.decodeGroupHistory = decodeGroupHistory;
35
+ const processGroupHistory = groupHistory => {
36
+ const gh = groupHistory || {};
37
+ return {
38
+ messages: gh.messages || [],
39
+ commentMessages: gh.commentMessages || [],
40
+ outOfWindowPinnedMessages: gh.outOfWindowPinnedMessages || [],
41
+ uncountedAssociatedMessageLists: gh.uncountedAssociatedMessageLists || []
42
+ };
43
+ };
44
+ exports.processGroupHistory = processGroupHistory;