@badzz88/baileys 8.5.7 → 8.5.9

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 (142) hide show
  1. package/README.md +337 -1055
  2. package/WAProto/WAProto.proto +284 -264
  3. package/WAProto/index.js +5 -5
  4. package/engine-requirements.js +7 -15
  5. package/lib/Defaults/index.js +130 -187
  6. package/lib/Signal/Group/ciphertext-message.js +11 -14
  7. package/lib/Signal/Group/group-session-builder.js +29 -91
  8. package/lib/Signal/Group/group_cipher.js +81 -88
  9. package/lib/Signal/Group/index.js +12 -136
  10. package/lib/Signal/Group/keyhelper.js +15 -70
  11. package/lib/Signal/Group/sender-chain-key.js +25 -31
  12. package/lib/Signal/Group/sender-key-distribution-message.js +62 -65
  13. package/lib/Signal/Group/sender-key-message.js +65 -68
  14. package/lib/Signal/Group/sender-key-name.js +41 -43
  15. package/lib/Signal/Group/sender-key-record.js +40 -43
  16. package/lib/Signal/Group/sender-key-state.js +83 -96
  17. package/lib/Signal/Group/sender-message-key.js +25 -29
  18. package/lib/Signal/libsignal.js +425 -453
  19. package/lib/Signal/lid-mapping.js +276 -259
  20. package/lib/Socket/Client/index.js +3 -30
  21. package/lib/Socket/Client/types.js +10 -12
  22. package/lib/Socket/Client/websocket.js +53 -61
  23. package/lib/Socket/business.js +379 -422
  24. package/lib/Socket/chats.js +1193 -1816
  25. package/lib/Socket/communities.js +431 -549
  26. package/lib/Socket/graphql.js +672 -612
  27. package/lib/Socket/groups.js +374 -764
  28. package/lib/Socket/index.js +17 -38
  29. package/lib/Socket/interop.js +417 -503
  30. package/lib/Socket/messages-recv.js +1915 -2441
  31. package/lib/Socket/messages-send.js +1213 -1630
  32. package/lib/Socket/mex.js +9 -15
  33. package/lib/Socket/newsletter.js +746 -808
  34. package/lib/Socket/privacy.js +310 -441
  35. package/lib/Socket/socket.js +1025 -1022
  36. package/lib/Socket/username.js +8 -15
  37. package/lib/Store/index.js +10 -36
  38. package/lib/Store/make-cache-manager-store.js +80 -85
  39. package/lib/Store/make-in-memory-store.js +231 -591
  40. package/lib/Store/make-ordered-dictionary.js +72 -78
  41. package/lib/Store/object-repository.js +28 -25
  42. package/lib/Types/Auth.js +2 -38
  43. package/lib/Types/Bussines.js +2 -2
  44. package/lib/Types/Call.js +2 -2
  45. package/lib/Types/Chat.js +8 -4
  46. package/lib/Types/Contact.js +2 -2
  47. package/lib/Types/Events.js +2 -2
  48. package/lib/Types/GroupMetadata.js +2 -2
  49. package/lib/Types/Label.js +24 -26
  50. package/lib/Types/LabelAssociation.js +6 -8
  51. package/lib/Types/Message.js +11 -95
  52. package/lib/Types/Mex.js +111 -112
  53. package/lib/Types/Product.js +2 -2
  54. package/lib/Types/Signal.js +2 -2
  55. package/lib/Types/Socket.js +3 -2
  56. package/lib/Types/State.js +56 -70
  57. package/lib/Types/USync.js +2 -2
  58. package/lib/Types/index.js +26 -55
  59. package/lib/Utils/auth-utils.js +288 -292
  60. package/lib/Utils/browser-utils.js +47 -113
  61. package/lib/Utils/business.js +224 -240
  62. package/lib/Utils/chat-utils.js +869 -1205
  63. package/lib/Utils/companion-reg-client-utils.js +34 -41
  64. package/lib/Utils/crypto.js +107 -144
  65. package/lib/Utils/decode-wa-message.js +308 -518
  66. package/lib/Utils/event-buffer.js +611 -596
  67. package/lib/Utils/generics.js +355 -515
  68. package/lib/Utils/history.js +134 -244
  69. package/lib/Utils/identity-change-handler.js +49 -51
  70. package/lib/Utils/index.js +23 -57
  71. package/lib/Utils/link-preview.js +77 -135
  72. package/lib/Utils/logger.js +3 -9
  73. package/lib/Utils/lt-hash.js +6 -37
  74. package/lib/Utils/make-mutex.js +33 -36
  75. package/lib/Utils/message-composer.js +233 -439
  76. package/lib/Utils/message-retry-manager.js +260 -265
  77. package/lib/Utils/messages-media.js +829 -855
  78. package/lib/Utils/messages.js +961 -2528
  79. package/lib/Utils/noise-handler.js +200 -195
  80. package/lib/Utils/offline-node-processor.js +33 -35
  81. package/lib/Utils/pre-key-manager.js +102 -103
  82. package/lib/Utils/process-message.js +560 -978
  83. package/lib/Utils/reporting-utils.js +253 -257
  84. package/lib/Utils/signal.js +195 -218
  85. package/lib/Utils/stanza-ack.js +29 -31
  86. package/lib/Utils/sticker.js +109 -144
  87. package/lib/Utils/sync-action-utils.js +45 -50
  88. package/lib/Utils/tc-token-utils.js +139 -137
  89. package/lib/Utils/use-multi-file-auth-state.js +109 -109
  90. package/lib/Utils/validate-connection.js +202 -218
  91. package/lib/WABinary/constants.js +1299 -1302
  92. package/lib/WABinary/decode.js +262 -343
  93. package/lib/WABinary/encode.js +4 -12
  94. package/lib/WABinary/generic-utils.js +187 -223
  95. package/lib/WABinary/index.js +6 -33
  96. package/lib/WABinary/jid-utils.js +88 -351
  97. package/lib/WABinary/types.js +2 -2
  98. package/lib/WAM/BinaryInfo.js +9 -12
  99. package/lib/WAM/constants.js +22853 -39486
  100. package/lib/WAM/encode.js +140 -132
  101. package/lib/WAM/index.js +4 -31
  102. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +21 -23
  103. package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +9 -9
  104. package/lib/WAUSync/Protocols/USyncContactProtocol.js +7 -7
  105. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +10 -10
  106. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +7 -7
  107. package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +11 -12
  108. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +4 -5
  109. package/lib/WAUSync/Protocols/USyncPictureProtocol.js +7 -7
  110. package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +7 -8
  111. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +7 -7
  112. package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +8 -8
  113. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +7 -7
  114. package/lib/WAUSync/Protocols/index.js +12 -39
  115. package/lib/WAUSync/USyncQuery.js +67 -49
  116. package/lib/WAUSync/USyncUser.js +11 -5
  117. package/lib/WAUSync/index.js +3 -31
  118. package/lib/index.js +15 -48
  119. package/package.json +129 -99
  120. package/LICENSE +0 -21
  121. package/lib/Defaults/phonenumber-mcc.json +0 -223
  122. package/lib/Socket/aigroups.js +0 -240
  123. package/lib/Socket/interactive-handler.js +0 -522
  124. package/lib/Socket/managed-account.js +0 -183
  125. package/lib/Socket/registration.js +0 -427
  126. package/lib/Socket/text-router.js +0 -83
  127. package/lib/Types/Newsletter.js +0 -121
  128. package/lib/Utils/command-loader.d.ts +0 -31
  129. package/lib/Utils/command-loader.js +0 -100
  130. package/lib/Utils/consumer-application.js +0 -107
  131. package/lib/Utils/curve25519-js.js +0 -275
  132. package/lib/Utils/group-history.js +0 -60
  133. package/lib/Utils/jid-display-normalization.js +0 -218
  134. package/lib/Utils/meta-ai-msmsg.js +0 -262
  135. package/lib/Utils/native-bridge.js +0 -82
  136. package/lib/Utils/session-pool.d.ts +0 -16
  137. package/lib/Utils/session-pool.js +0 -94
  138. package/lib/Utils/sticker.d.ts +0 -17
  139. package/lib/Utils/view-once-cache.d.ts +0 -9
  140. package/lib/Utils/view-once-cache.js +0 -79
  141. package/lib/Utils/voip-rekey.js +0 -22
  142. package/lib/antiban.js +0 -4637
@@ -1,563 +1,403 @@
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
- replacer: (k, value) => {
58
- if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
59
- return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') }
60
- }
61
- return value
62
- },
63
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
- reviver: (_, value) => {
65
- if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
66
- return Buffer.from(value.data, 'base64')
67
- }
68
- if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
69
- const keys = Object.keys(value)
70
- if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
71
- const values = Object.values(value)
72
- if (values.every(v => typeof v === 'number')) {
73
- return Buffer.from(values)
74
- }
75
- }
76
- }
77
- return value
78
- }
79
- }
80
- const getKeyAuthor = (key, meId = 'me') =>
81
- (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || ''
82
- exports.getKeyAuthor = getKeyAuthor
83
- const isStringNullOrEmpty = value =>
84
- // eslint-disable-next-line eqeqeq
85
- value == null || value === ''
86
- exports.isStringNullOrEmpty = isStringNullOrEmpty
87
- const writeRandomPadMax16 = msg => {
88
- const pad = (0, crypto_1.randomBytes)(1)
89
- const padLength = (pad[0] & 0x0f) + 1
90
- return Buffer.concat([msg, Buffer.alloc(padLength, padLength)])
91
- }
92
- exports.writeRandomPadMax16 = writeRandomPadMax16
93
- const unpadRandomMax16 = e => {
94
- const t = new Uint8Array(e)
95
- if (0 === t.length) {
96
- throw new Error('unpadPkcs7 given empty bytes')
97
- }
98
- var r = t[t.length - 1]
99
- if (r > t.length) {
100
- throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`)
101
- }
102
- return new Uint8Array(t.buffer, t.byteOffset, t.length - r)
103
- }
104
- exports.unpadRandomMax16 = unpadRandomMax16
1
+ import { Boom } from '@hapi/boom';
2
+ import { createHash, randomBytes } from 'crypto';
3
+ import { proto } from '../../WAProto/index.js';
4
+ const baileysVersion = [2, 3000, 1035194821];
5
+ import { DisconnectReason } from '../Types/index.js';
6
+ import { getAllBinaryNodeChildren, jidDecode } from '../WABinary/index.js';
7
+ import { sha256 } from './crypto.js';
8
+ export const BufferJSON = {
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ replacer: (k, value) => {
11
+ if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
12
+ return { type: 'Buffer', data: Buffer.from(value?.data || value).toString('base64') };
13
+ }
14
+ return value;
15
+ },
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ reviver: (_, value) => {
18
+ if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
19
+ return Buffer.from(value.data, 'base64');
20
+ }
21
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
22
+ const keys = Object.keys(value);
23
+ if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
24
+ const values = Object.values(value);
25
+ if (values.every(v => typeof v === 'number')) {
26
+ return Buffer.from(values);
27
+ }
28
+ }
29
+ }
30
+ return value;
31
+ }
32
+ };
33
+ export const getKeyAuthor = (key, meId = 'me') => (key?.fromMe ? meId : key?.participantAlt || key?.remoteJidAlt || key?.participant || key?.remoteJid) || '';
34
+ export const isStringNullOrEmpty = (value) =>
35
+ // eslint-disable-next-line eqeqeq
36
+ value == null || value === '';
37
+ export const writeRandomPadMax16 = (msg) => {
38
+ const pad = randomBytes(1);
39
+ const padLength = (pad[0] & 0x0f) + 1;
40
+ return Buffer.concat([msg, Buffer.alloc(padLength, padLength)]);
41
+ };
42
+ export const unpadRandomMax16 = (e) => {
43
+ const t = new Uint8Array(e);
44
+ if (0 === t.length) {
45
+ throw new Error('unpadPkcs7 given empty bytes');
46
+ }
47
+ var r = t[t.length - 1];
48
+ if (r > t.length) {
49
+ throw new Error(`unpad given ${t.length} bytes, but pad is ${r}`);
50
+ }
51
+ return new Uint8Array(t.buffer, t.byteOffset, t.length - r);
52
+ };
105
53
  // code is inspired by whatsmeow
106
- const generateParticipantHashV2 = participants => {
107
- participants.sort()
108
- const sha256Hash = (0, crypto_2.sha256)(Buffer.from(participants.join(''))).toString('base64')
109
- return '2:' + sha256Hash.slice(0, 6)
110
- }
111
- exports.generateParticipantHashV2 = generateParticipantHashV2
112
- const encodeWAMessage = message => (0, exports.writeRandomPadMax16)(index_js_1.proto.Message.encode(message).finish())
113
- exports.encodeWAMessage = encodeWAMessage
114
- const generateRegistrationId = () => {
115
- return Uint16Array.from((0, crypto_1.randomBytes)(2))[0] & 16383
116
- }
117
- exports.generateRegistrationId = generateRegistrationId
118
- const encodeBigEndian = (e, t = 4) => {
119
- let r = e
120
- const a = new Uint8Array(t)
121
- for (let i = t - 1; i >= 0; i--) {
122
- a[i] = 255 & r
123
- r >>>= 8
124
- }
125
- return a
126
- }
127
- exports.encodeBigEndian = encodeBigEndian
128
- const toNumber = t => (typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0)
129
- exports.toNumber = toNumber
54
+ export const generateParticipantHashV2 = (participants) => {
55
+ participants.sort();
56
+ const sha256Hash = sha256(Buffer.from(participants.join(''))).toString('base64');
57
+ return '2:' + sha256Hash.slice(0, 6);
58
+ };
59
+ export const encodeWAMessage = (message) => writeRandomPadMax16(proto.Message.encode(message).finish());
60
+ export const generateRegistrationId = () => {
61
+ return Uint16Array.from(randomBytes(2))[0] & 16383;
62
+ };
63
+ export const encodeBigEndian = (e, t = 4) => {
64
+ let r = e;
65
+ const a = new Uint8Array(t);
66
+ for (let i = t - 1; i >= 0; i--) {
67
+ a[i] = 255 & r;
68
+ r >>>= 8;
69
+ }
70
+ return a;
71
+ };
72
+ export const toNumber = (t) => typeof t === 'object' && t ? ('toNumber' in t ? t.toNumber() : t.low) : t || 0;
130
73
  /** unix timestamp of a date in seconds */
131
- const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000)
132
- exports.unixTimestampSeconds = unixTimestampSeconds
133
- const debouncedTimeout = (intervalMs = 1000, task) => {
134
- let timeout
135
- return {
136
- start: (newIntervalMs, newTask) => {
137
- task = newTask || task
138
- intervalMs = newIntervalMs || intervalMs
139
- timeout && clearTimeout(timeout)
140
- timeout = setTimeout(() => task?.(), intervalMs)
141
- },
142
- cancel: () => {
143
- timeout && clearTimeout(timeout)
144
- timeout = undefined
145
- },
146
- setTask: newTask => (task = newTask),
147
- setInterval: newInterval => (intervalMs = newInterval)
148
- }
149
- }
150
- exports.debouncedTimeout = debouncedTimeout
151
- const delay = ms => (0, exports.delayCancellable)(ms).delay
152
- exports.delay = delay
153
- const delayCancellable = ms => {
154
- const stack = new Error().stack
155
- let timeout
156
- let reject
157
- const delay = new Promise((resolve, _reject) => {
158
- timeout = setTimeout(resolve, ms)
159
- reject = _reject
160
- })
161
- const cancel = () => {
162
- clearTimeout(timeout)
163
- reject(
164
- new boom_1.Boom('Cancelled', {
165
- statusCode: 500,
166
- data: {
167
- stack
168
- }
169
- })
170
- )
171
- }
172
- return { delay, cancel }
173
- }
174
- exports.delayCancellable = delayCancellable
175
- async function promiseTimeout(ms, promise) {
176
- if (!ms) {
177
- return new Promise(promise)
178
- }
179
- const stack = new Error().stack
180
- // Create a promise that rejects in <ms> milliseconds
181
- const { delay, cancel } = (0, exports.delayCancellable)(ms)
182
- const p = new Promise((resolve, reject) => {
183
- delay
184
- .then(() =>
185
- reject(
186
- new boom_1.Boom('Timed Out', {
187
- statusCode: Types_1.DisconnectReason.timedOut,
188
- data: {
189
- stack
190
- }
191
- })
192
- )
193
- )
194
- .catch(err => reject(err))
195
- promise(resolve, reject)
196
- }).finally(cancel)
197
- return p
74
+ export const unixTimestampSeconds = (date = new Date()) => Math.floor(date.getTime() / 1000);
75
+ export const debouncedTimeout = (intervalMs = 1000, task) => {
76
+ let timeout;
77
+ return {
78
+ start: (newIntervalMs, newTask) => {
79
+ task = newTask || task;
80
+ intervalMs = newIntervalMs || intervalMs;
81
+ timeout && clearTimeout(timeout);
82
+ timeout = setTimeout(() => task?.(), intervalMs);
83
+ },
84
+ cancel: () => {
85
+ timeout && clearTimeout(timeout);
86
+ timeout = undefined;
87
+ },
88
+ setTask: (newTask) => (task = newTask),
89
+ setInterval: (newInterval) => (intervalMs = newInterval)
90
+ };
91
+ };
92
+ export const delay = (ms) => delayCancellable(ms).delay;
93
+ export const delayCancellable = (ms) => {
94
+ const stack = new Error().stack;
95
+ let timeout;
96
+ let reject;
97
+ const delay = new Promise((resolve, _reject) => {
98
+ timeout = setTimeout(resolve, ms);
99
+ reject = _reject;
100
+ });
101
+ const cancel = () => {
102
+ clearTimeout(timeout);
103
+ reject(new Boom('Cancelled', {
104
+ statusCode: 500,
105
+ data: {
106
+ stack
107
+ }
108
+ }));
109
+ };
110
+ return { delay, cancel };
111
+ };
112
+ export async function promiseTimeout(ms, promise) {
113
+ if (!ms) {
114
+ return new Promise(promise);
115
+ }
116
+ const stack = new Error().stack;
117
+ // Create a promise that rejects in <ms> milliseconds
118
+ const { delay, cancel } = delayCancellable(ms);
119
+ const p = new Promise((resolve, reject) => {
120
+ delay
121
+ .then(() => reject(new Boom('Timed Out', {
122
+ statusCode: DisconnectReason.timedOut,
123
+ data: {
124
+ stack
125
+ }
126
+ })))
127
+ .catch(err => reject(err));
128
+ promise(resolve, reject);
129
+ }).finally(cancel);
130
+ return p;
198
131
  }
199
132
  // inspired from whatsmeow code
200
133
  // https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
201
- const generateMessageIDV2 = (userId) => {
134
+ export const generateMessageIDV2 = (userId) => {
202
135
  const data = Buffer.alloc(8 + 20 + 16);
203
136
  data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
204
137
  if (userId) {
205
- const id = (0, WABinary_1.jidDecode)(userId);
138
+ const id = jidDecode(userId);
206
139
  if (id?.user) {
207
140
  data.write(id.user, 8);
208
141
  data.write('@c.us', 8 + id.user.length);
209
142
  }
210
143
  }
211
- const random = (0, crypto_1.randomBytes)(16);
144
+ const random = randomBytes(16);
212
145
  random.copy(data, 28);
213
- const hash = (0, crypto_1.createHash)('sha256').update(data).digest();
214
- return 'XZCYYX-' + hash.toString('hex').toUpperCase().substring(0, 18);
146
+ const hash = createHash('sha256').update(data).digest();
147
+ return 'VINNXSYNC-' + hash.toString('hex').toUpperCase().substring(0, 18);
215
148
  };
216
- exports.generateMessageIDV2 = generateMessageIDV2
217
- const generateIOSMessageID = () => {
149
+ // generate a random ID to attach to a message
150
+ export const generateMessageID = () => 'VINNXSYNC-' + randomBytes(18).toString('hex').toUpperCase();
151
+ export function bindWaitForEvent(ev, event) {
152
+ return async (check, timeoutMs) => {
153
+ let listener;
154
+ let closeListener;
155
+ await promiseTimeout(timeoutMs, (resolve, reject) => {
156
+ closeListener = ({ connection, lastDisconnect }) => {
157
+ if (connection === 'close') {
158
+ reject(lastDisconnect?.error || new Boom('Connection Closed', { statusCode: DisconnectReason.connectionClosed }));
159
+ }
160
+ };
161
+ ev.on('connection.update', closeListener);
162
+ listener = async (update) => {
163
+ if (await check(update)) {
164
+ resolve();
165
+ }
166
+ };
167
+ ev.on(event, listener);
168
+ }).finally(() => {
169
+ ev.off(event, listener);
170
+ ev.off('connection.update', closeListener);
171
+ });
172
+ };
173
+ }
174
+ export const generateIOSMessageID = () => {
218
175
  const prefix = '3A';
219
- const random = (0, crypto_1.randomBytes)(9.5); // 19 hex chars = 9.5 bytes
176
+ const random = randomBytes(9.5); // 19 hex chars = 9.5 bytes
220
177
  return (prefix + random.toString('hex')).toUpperCase().substring(0, 21);
221
178
  };
222
- exports.generateIOSMessageID = generateIOSMessageID
223
- const generateAndroMessageID = () => {
179
+ export const generateAndroMessageID = () => {
224
180
  const prefix = '3A';
225
- const random = (0, crypto_1.randomBytes)(16); // 32 hex chars = 16 bytes
181
+ const random = randomBytes(16); // 32 hex chars = 16 bytes
226
182
  return (random.toString('hex')).toUpperCase().substring(0, 21);
227
183
  };
228
- exports.generateAndroMessageID = generateAndroMessageID
229
- // generate a random ID to attach to a message
230
- const generateMessageID = () => 'XZCYYX-' + (0, crypto_1.randomBytes)(18).toString('hex').toUpperCase()
231
- exports.generateMessageID = generateMessageID
232
- function bindWaitForEvent(ev, event) {
233
- return async (check, timeoutMs) => {
234
- let listener
235
- let closeListener
236
- await promiseTimeout(timeoutMs, (resolve, reject) => {
237
- closeListener = ({ connection, lastDisconnect }) => {
238
- if (connection === 'close') {
239
- reject(
240
- lastDisconnect?.error ||
241
- new boom_1.Boom('Connection Closed', { statusCode: Types_1.DisconnectReason.connectionClosed })
242
- )
243
- }
244
- }
245
- ev.on('connection.update', closeListener)
246
- listener = async update => {
247
- if (await check(update)) {
248
- resolve()
249
- }
250
- }
251
- ev.on(event, listener)
252
- }).finally(() => {
253
- ev.off(event, listener)
254
- ev.off('connection.update', closeListener)
255
- })
256
- }
257
- }
258
- const bindWaitForConnectionUpdate = ev => bindWaitForEvent(ev, 'connection.update')
259
- exports.bindWaitForConnectionUpdate = bindWaitForConnectionUpdate
184
+ export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
260
185
  /**
261
186
  * utility that fetches latest baileys version from the master branch.
262
187
  * Use to ensure your WA connection is always on the latest version
263
188
  */
264
- const fetchLatestBaileysVersion = async (options = {}) => {
265
- const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts'
266
- try {
267
- const response = await fetch(URL, {
268
- dispatcher: options.dispatcher,
269
- method: 'GET',
270
- headers: options.headers
271
- })
272
- if (!response.ok) {
273
- throw new boom_1.Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, {
274
- statusCode: response.status
275
- })
276
- }
277
- const text = await response.text()
278
- // Extract version from line 7 (const version = [...])
279
- const lines = text.split('\n')
280
- const versionLine = lines[6] // Line 7 (0-indexed)
281
- const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/)
282
- if (versionMatch) {
283
- const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])]
284
- return {
285
- version,
286
- isLatest: true
287
- }
288
- } else {
289
- throw new Error('Could not parse version from Defaults/index.ts')
290
- }
291
- } catch (error) {
292
- return {
293
- version: baileysVersion,
294
- isLatest: false,
295
- error
296
- }
297
- }
298
- }
299
- exports.fetchLatestBaileysVersion = fetchLatestBaileysVersion
189
+ export const fetchLatestBaileysVersion = async (options = {}) => {
190
+ const URL = 'https://raw.githubusercontent.com/WhiskeySockets/Baileys/master/src/Defaults/index.ts';
191
+ try {
192
+ const response = await fetch(URL, {
193
+ dispatcher: options.dispatcher,
194
+ method: 'GET',
195
+ headers: options.headers
196
+ });
197
+ if (!response.ok) {
198
+ throw new Boom(`Failed to fetch latest Baileys version: ${response.statusText}`, { statusCode: response.status });
199
+ }
200
+ const text = await response.text();
201
+ // Extract version from line 7 (const version = [...])
202
+ const lines = text.split('\n');
203
+ const versionLine = lines[6]; // Line 7 (0-indexed)
204
+ const versionMatch = versionLine.match(/const version = \[(\d+),\s*(\d+),\s*(\d+)\]/);
205
+ if (versionMatch) {
206
+ const version = [parseInt(versionMatch[1]), parseInt(versionMatch[2]), parseInt(versionMatch[3])];
207
+ return {
208
+ version,
209
+ isLatest: true
210
+ };
211
+ }
212
+ else {
213
+ throw new Error('Could not parse version from Defaults/index.ts');
214
+ }
215
+ }
216
+ catch (error) {
217
+ return {
218
+ version: baileysVersion,
219
+ isLatest: false,
220
+ error
221
+ };
222
+ }
223
+ };
300
224
  /**
301
225
  * A utility that fetches the latest web version of whatsapp.
302
226
  * Use to ensure your WA connection is always on the latest version
303
227
  */
304
- const fetchLatestWaWebVersion = async (options = {}) => {
305
- try {
306
- // Absolute minimal headers required to bypass anti-bot detection
307
- const defaultHeaders = {
308
- 'sec-fetch-site': 'none',
309
- 'user-agent':
310
- 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
311
- }
312
- const headers = { ...defaultHeaders, ...options.headers }
313
- const response = await fetch('https://web.whatsapp.com/sw.js', {
314
- ...options,
315
- method: 'GET',
316
- headers
317
- })
318
- if (!response.ok) {
319
- throw new boom_1.Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status })
320
- }
321
- const data = await response.text()
322
- const regex = /\\?"client_revision\\?":\s*(\d+)/
323
- const match = data.match(regex)
324
- if (!match?.[1]) {
325
- return {
326
- version: baileysVersion,
327
- isLatest: false,
328
- error: {
329
- message: 'Could not find client revision in the fetched content'
330
- }
331
- }
332
- }
333
- const clientRevision = match[1]
334
- return {
335
- version: [2, 3000, +clientRevision],
336
- isLatest: true
337
- }
338
- } catch (error) {
339
- return {
340
- version: baileysVersion,
341
- isLatest: false,
342
- error
343
- }
344
- }
345
- }
346
- exports.fetchLatestWaWebVersion = fetchLatestWaWebVersion
228
+ export const fetchLatestWaWebVersion = async (options = {}) => {
229
+ try {
230
+ // Absolute minimal headers required to bypass anti-bot detection
231
+ const defaultHeaders = {
232
+ 'sec-fetch-site': 'none',
233
+ 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
234
+ };
235
+ const headers = { ...defaultHeaders, ...options.headers };
236
+ const response = await fetch('https://web.whatsapp.com/sw.js', {
237
+ ...options,
238
+ method: 'GET',
239
+ headers
240
+ });
241
+ if (!response.ok) {
242
+ throw new Boom(`Failed to fetch sw.js: ${response.statusText}`, { statusCode: response.status });
243
+ }
244
+ const data = await response.text();
245
+ const regex = /\\?"client_revision\\?":\s*(\d+)/;
246
+ const match = data.match(regex);
247
+ if (!match?.[1]) {
248
+ return {
249
+ version: baileysVersion,
250
+ isLatest: false,
251
+ error: {
252
+ message: 'Could not find client revision in the fetched content'
253
+ }
254
+ };
255
+ }
256
+ const clientRevision = match[1];
257
+ return {
258
+ version: [2, 3000, +clientRevision],
259
+ isLatest: true
260
+ };
261
+ }
262
+ catch (error) {
263
+ return {
264
+ version: baileysVersion,
265
+ isLatest: false,
266
+ error
267
+ };
268
+ }
269
+ };
347
270
  /** unique message tag prefix for MD clients */
348
- const generateMdTagPrefix = () => {
349
- const bytes = (0, crypto_1.randomBytes)(4)
350
- return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`
351
- }
352
- exports.generateMdTagPrefix = generateMdTagPrefix
271
+ export const generateMdTagPrefix = () => {
272
+ const bytes = randomBytes(4);
273
+ return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
274
+ };
353
275
  const STATUS_MAP = {
354
- sender: index_js_1.proto.WebMessageInfo.Status.SERVER_ACK,
355
- played: index_js_1.proto.WebMessageInfo.Status.PLAYED,
356
- read: index_js_1.proto.WebMessageInfo.Status.READ,
357
- 'read-self': index_js_1.proto.WebMessageInfo.Status.READ
358
- }
276
+ sender: proto.WebMessageInfo.Status.SERVER_ACK,
277
+ played: proto.WebMessageInfo.Status.PLAYED,
278
+ read: proto.WebMessageInfo.Status.READ,
279
+ 'read-self': proto.WebMessageInfo.Status.READ
280
+ };
359
281
  /**
360
282
  * Given a type of receipt, returns what the new status of the message should be
361
283
  * @param type type from receipt
362
284
  */
363
- const getStatusFromReceiptType = type => {
364
- const status = STATUS_MAP[type]
365
- if (typeof type === 'undefined') {
366
- return index_js_1.proto.WebMessageInfo.Status.DELIVERY_ACK
367
- }
368
- return status
369
- }
370
- exports.getStatusFromReceiptType = getStatusFromReceiptType
285
+ export const getStatusFromReceiptType = (type) => {
286
+ const status = STATUS_MAP[type];
287
+ if (typeof type === 'undefined') {
288
+ return proto.WebMessageInfo.Status.DELIVERY_ACK;
289
+ }
290
+ return status;
291
+ };
371
292
  const CODE_MAP = {
372
- conflict: Types_1.DisconnectReason.connectionReplaced
373
- }
293
+ conflict: DisconnectReason.connectionReplaced
294
+ };
374
295
  /**
375
296
  * Stream errors generally provide a reason, map that to a baileys DisconnectReason
376
297
  * @param reason the string reason given, eg. "conflict"
377
298
  */
378
- const getErrorCodeFromStreamError = node => {
379
- const [reasonNode] = (0, WABinary_1.getAllBinaryNodeChildren)(node)
380
- let reason = reasonNode?.tag || 'unknown'
381
- const statusCode = +(node.attrs.code || CODE_MAP[reason] || Types_1.DisconnectReason.badSession)
382
- if (statusCode === Types_1.DisconnectReason.restartRequired) {
383
- reason = 'restart required'
384
- }
385
- return {
386
- reason,
387
- statusCode
388
- }
389
- }
390
- exports.getErrorCodeFromStreamError = getErrorCodeFromStreamError
391
- const getCallStatusFromNode = ({ tag, attrs }) => {
392
- let status
393
- switch (tag) {
394
- case 'offer':
395
- case 'offer_notice':
396
- status = 'offer'
397
- break
398
- case 'terminate':
399
- if (attrs.reason === 'timeout') {
400
- status = 'timeout'
401
- } else {
402
- //fired when accepted/rejected/timeout/caller hangs up
403
- status = 'terminate'
404
- }
405
- break
406
- case 'reject':
407
- status = 'reject'
408
- break
409
- case 'accept':
410
- status = 'accept'
411
- break
412
- default:
413
- status = 'ringing'
414
- break
415
- }
416
- return status
417
- }
418
- exports.getCallStatusFromNode = getCallStatusFromNode
419
- const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: '
420
- const getCodeFromWSError = error => {
421
- let statusCode = 500
422
- if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
423
- const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length)
424
- if (!Number.isNaN(code) && code >= 400) {
425
- statusCode = code
426
- }
427
- } else if (
428
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
429
- error?.code?.startsWith('E') ||
430
- error?.message?.includes('timed out')
431
- ) {
432
- // handle ETIMEOUT, ENOTFOUND etc
433
- statusCode = 408
434
- }
435
- return statusCode
436
- }
437
- exports.getCodeFromWSError = getCodeFromWSError
299
+ export const getErrorCodeFromStreamError = (node) => {
300
+ const [reasonNode] = getAllBinaryNodeChildren(node);
301
+ let reason = reasonNode?.tag || 'unknown';
302
+ const statusCode = +(node.attrs.code || CODE_MAP[reason] || DisconnectReason.badSession);
303
+ if (statusCode === DisconnectReason.restartRequired) {
304
+ reason = 'restart required';
305
+ }
306
+ return {
307
+ reason,
308
+ statusCode
309
+ };
310
+ };
311
+ export const getCallStatusFromNode = ({ tag, attrs }) => {
312
+ let status;
313
+ switch (tag) {
314
+ case 'offer':
315
+ case 'offer_notice':
316
+ status = 'offer';
317
+ break;
318
+ case 'terminate':
319
+ if (attrs.reason === 'timeout') {
320
+ status = 'timeout';
321
+ }
322
+ else {
323
+ //fired when accepted/rejected/timeout/caller hangs up
324
+ status = 'terminate';
325
+ }
326
+ break;
327
+ case 'preaccept':
328
+ status = 'preaccept';
329
+ break;
330
+ case 'transport':
331
+ status = 'transport';
332
+ break;
333
+ case 'relaylatency':
334
+ status = 'relaylatency';
335
+ break;
336
+ case 'reject':
337
+ status = 'reject';
338
+ break;
339
+ case 'accept':
340
+ status = 'accept';
341
+ break;
342
+ default:
343
+ status = 'ringing';
344
+ break;
345
+ }
346
+ return status;
347
+ };
348
+ const UNEXPECTED_SERVER_CODE_TEXT = 'Unexpected server response: ';
349
+ export const getCodeFromWSError = (error) => {
350
+ let statusCode = 500;
351
+ if (error?.message?.includes(UNEXPECTED_SERVER_CODE_TEXT)) {
352
+ const code = +error?.message.slice(UNEXPECTED_SERVER_CODE_TEXT.length);
353
+ if (!Number.isNaN(code) && code >= 400) {
354
+ statusCode = code;
355
+ }
356
+ }
357
+ else if (
358
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
359
+ error?.code?.startsWith('E') ||
360
+ error?.message?.includes('timed out')) {
361
+ // handle ETIMEOUT, ENOTFOUND etc
362
+ statusCode = 408;
363
+ }
364
+ return statusCode;
365
+ };
438
366
  /**
439
367
  * Is the given platform WA business
440
368
  * @param platform AuthenticationCreds.platform
441
369
  */
442
- const isWABusinessPlatform = platform => {
443
- return platform === 'smbi' || platform === 'smba'
444
- }
445
- exports.isWABusinessPlatform = isWABusinessPlatform
370
+ export const isWABusinessPlatform = (platform) => {
371
+ return platform === 'smbi' || platform === 'smba';
372
+ };
446
373
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
447
- function trimUndefined(obj) {
448
- for (const key in obj) {
449
- if (typeof obj[key] === 'undefined') {
450
- delete obj[key]
451
- }
452
- }
453
- return obj
454
- }
455
- const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ'
456
- function bytesToCrockford(buffer) {
457
- let value = 0
458
- let bitCount = 0
459
- const crockford = []
460
- for (const element of buffer) {
461
- value = (value << 8) | (element & 0xff)
462
- bitCount += 8
463
- while (bitCount >= 5) {
464
- crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31))
465
- bitCount -= 5
466
- }
467
- }
468
- if (bitCount > 0) {
469
- crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31))
470
- }
471
- return crockford.join('')
472
- }
473
- function encodeNewsletterMessage(message) {
474
- return index_js_1.proto.Message.encode(message).finish()
475
- }
476
-
477
- /**
478
- * Add human-like jitter to a base delay
479
- * @param baseMs base delay in milliseconds
480
- * @param varianceFraction fraction of baseMs to use as variance (0–1), default 0.3
481
- */
482
- const jitterDelay = (baseMs, varianceFraction = 0.3) => {
483
- const variance = baseMs * varianceFraction
484
- return baseMs + (Math.random() * 2 - 1) * variance
485
- }
486
- exports.jitterDelay = jitterDelay
487
-
488
- /**
489
- * Compute exponential backoff delay for a given attempt number (0-based)
490
- * @param attempt attempt index (0 = first retry)
491
- * @param baseMs starting delay in ms (default 500)
492
- * @param maxMs maximum delay cap in ms (default 30000)
493
- */
494
- const exponentialBackoff = (attempt, baseMs = 500, maxMs = 30000) => {
495
- return Math.min(baseMs * Math.pow(2, attempt), maxMs)
374
+ export function trimUndefined(obj) {
375
+ for (const key in obj) {
376
+ if (typeof obj[key] === 'undefined') {
377
+ delete obj[key];
378
+ }
379
+ }
380
+ return obj;
496
381
  }
497
- exports.exponentialBackoff = exponentialBackoff
498
-
499
- /** Convert buffer/Uint8Array to hex string */
500
- const bytesToHex = buf => Buffer.from(buf).toString('hex')
501
- exports.bytesToHex = bytesToHex
502
-
503
- /** Convert hex string to Buffer */
504
- const hexToBytes = hex => Buffer.from(hex, 'hex')
505
- exports.hexToBytes = hexToBytes
506
-
507
- /** Convert buffer to base64url string */
508
- const bytesToBase64Url = buf => Buffer.from(buf).toString('base64url')
509
- exports.bytesToBase64Url = bytesToBase64Url
510
-
511
- /** Compute SHA-256 hex digest of data */
512
- const sha256Hex = data => (0, crypto_1.createHash)('sha256').update(data).digest('hex')
513
- exports.sha256Hex = sha256Hex
514
-
515
- /** Compute HMAC-SHA-256 of data with key, returns Buffer */
516
- const hmacSha256 = (data, key) => (0, crypto_1.createHmac)('sha256', key).update(data).digest()
517
- exports.hmacSha256 = hmacSha256
518
-
519
- /**
520
- * Normalize and deduplicate an array of JIDs
521
- */
522
- const normalizeJidBatch = (jids, normalizer) => {
523
- const seen = new Set()
524
- const result = []
525
- for (const jid of jids) {
526
- if (!jid) continue
527
- const normalized = normalizer ? normalizer(jid) : jid
528
- if (!seen.has(normalized)) {
529
- seen.add(normalized)
530
- result.push(normalized)
531
- }
532
- }
533
- return result
382
+ const CROCKFORD_CHARACTERS = '123456789ABCDEFGHJKLMNPQRSTVWXYZ';
383
+ export function bytesToCrockford(buffer) {
384
+ let value = 0;
385
+ let bitCount = 0;
386
+ const crockford = [];
387
+ for (const element of buffer) {
388
+ value = (value << 8) | (element & 0xff);
389
+ bitCount += 8;
390
+ while (bitCount >= 5) {
391
+ crockford.push(CROCKFORD_CHARACTERS.charAt((value >>> (bitCount - 5)) & 31));
392
+ bitCount -= 5;
393
+ }
394
+ }
395
+ if (bitCount > 0) {
396
+ crockford.push(CROCKFORD_CHARACTERS.charAt((value << (5 - bitCount)) & 31));
397
+ }
398
+ return crockford.join('');
534
399
  }
535
- exports.normalizeJidBatch = normalizeJidBatch
536
-
537
- /**
538
- * Sleep for a given number of milliseconds, returns a cancellable promise
539
- */
540
- const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
541
- exports.sleep = sleep
542
-
543
- /**
544
- * Run an async function with retry using exponential backoff
545
- * @param fn async function to retry
546
- * @param maxAttempts maximum number of attempts
547
- * @param baseDelayMs base delay between retries
548
- */
549
- const withRetry = async (fn, maxAttempts = 3, baseDelayMs = 500) => {
550
- let lastErr
551
- for (let i = 0; i < maxAttempts; i++) {
552
- try {
553
- return await fn()
554
- } catch (err) {
555
- lastErr = err
556
- if (i < maxAttempts - 1) {
557
- await sleep(exponentialBackoff(i, baseDelayMs))
558
- }
559
- }
560
- }
561
- throw lastErr
400
+ export function encodeNewsletterMessage(message) {
401
+ return proto.Message.encode(message).finish();
562
402
  }
563
- exports.withRetry = withRetry
403
+ //# sourceMappingURL=generics.js.map