@rizzkezik/bails 6.1.0

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 (115) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +535 -0
  3. package/WAProto/GenerateStatics.sh +3 -0
  4. package/WAProto/WAProto.proto +6902 -0
  5. package/WAProto/fix-imports.js +85 -0
  6. package/WAProto/index.d.ts +79257 -0
  7. package/WAProto/index.js +242946 -0
  8. package/engine-requirements.js +10 -0
  9. package/lib/Defaults/index.js +130 -0
  10. package/lib/Signal/Group/ciphertext-message.js +12 -0
  11. package/lib/Signal/Group/group-session-builder.js +30 -0
  12. package/lib/Signal/Group/group_cipher.js +82 -0
  13. package/lib/Signal/Group/index.js +12 -0
  14. package/lib/Signal/Group/keyhelper.js +18 -0
  15. package/lib/Signal/Group/sender-chain-key.js +26 -0
  16. package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
  17. package/lib/Signal/Group/sender-key-message.js +66 -0
  18. package/lib/Signal/Group/sender-key-name.js +48 -0
  19. package/lib/Signal/Group/sender-key-record.js +41 -0
  20. package/lib/Signal/Group/sender-key-state.js +84 -0
  21. package/lib/Signal/Group/sender-message-key.js +26 -0
  22. package/lib/Signal/libsignal.js +431 -0
  23. package/lib/Signal/lid-mapping.js +277 -0
  24. package/lib/Socket/Client/index.js +3 -0
  25. package/lib/Socket/Client/types.js +11 -0
  26. package/lib/Socket/Client/websocket.js +54 -0
  27. package/lib/Socket/business.js +379 -0
  28. package/lib/Socket/chats.js +1193 -0
  29. package/lib/Socket/communities.js +431 -0
  30. package/lib/Socket/groups.js +374 -0
  31. package/lib/Socket/index.js +12 -0
  32. package/lib/Socket/luxu.js +387 -0
  33. package/lib/Socket/messages-recv.js +1916 -0
  34. package/lib/Socket/messages-send.js +1435 -0
  35. package/lib/Socket/mex.js +42 -0
  36. package/lib/Socket/newsletter.js +270 -0
  37. package/lib/Socket/socket.js +967 -0
  38. package/lib/Store/index.js +10 -0
  39. package/lib/Store/keyed-db.js +108 -0
  40. package/lib/Store/make-cache-manager-store.js +85 -0
  41. package/lib/Store/make-in-memory-store.js +198 -0
  42. package/lib/Store/make-ordered-dictionary.js +75 -0
  43. package/lib/Store/object-repository.js +32 -0
  44. package/lib/Types/Auth.js +2 -0
  45. package/lib/Types/Bussines.js +2 -0
  46. package/lib/Types/Call.js +2 -0
  47. package/lib/Types/Chat.js +8 -0
  48. package/lib/Types/Contact.js +2 -0
  49. package/lib/Types/Events.js +2 -0
  50. package/lib/Types/GroupMetadata.js +2 -0
  51. package/lib/Types/Label.js +25 -0
  52. package/lib/Types/LabelAssociation.js +7 -0
  53. package/lib/Types/Message.js +11 -0
  54. package/lib/Types/Mex.js +37 -0
  55. package/lib/Types/Product.js +2 -0
  56. package/lib/Types/Signal.js +2 -0
  57. package/lib/Types/Socket.js +3 -0
  58. package/lib/Types/State.js +56 -0
  59. package/lib/Types/USync.js +2 -0
  60. package/lib/Types/index.js +26 -0
  61. package/lib/Utils/auth-utils.js +302 -0
  62. package/lib/Utils/browser-utils.js +48 -0
  63. package/lib/Utils/business.js +231 -0
  64. package/lib/Utils/chat-utils.js +872 -0
  65. package/lib/Utils/companion-reg-client-utils.js +35 -0
  66. package/lib/Utils/crypto.js +118 -0
  67. package/lib/Utils/decode-wa-message.js +350 -0
  68. package/lib/Utils/event-buffer.js +622 -0
  69. package/lib/Utils/generics.js +403 -0
  70. package/lib/Utils/history.js +134 -0
  71. package/lib/Utils/identity-change-handler.js +50 -0
  72. package/lib/Utils/index.js +23 -0
  73. package/lib/Utils/link-preview.js +85 -0
  74. package/lib/Utils/logger.js +3 -0
  75. package/lib/Utils/lt-hash.js +8 -0
  76. package/lib/Utils/make-mutex.js +33 -0
  77. package/lib/Utils/message-composer.js +273 -0
  78. package/lib/Utils/message-retry-manager.js +265 -0
  79. package/lib/Utils/messages-media.js +788 -0
  80. package/lib/Utils/messages.js +1253 -0
  81. package/lib/Utils/noise-handler.js +201 -0
  82. package/lib/Utils/offline-node-processor.js +40 -0
  83. package/lib/Utils/pre-key-manager.js +106 -0
  84. package/lib/Utils/process-message.js +630 -0
  85. package/lib/Utils/reporting-utils.js +258 -0
  86. package/lib/Utils/signal.js +201 -0
  87. package/lib/Utils/stanza-ack.js +38 -0
  88. package/lib/Utils/sync-action-utils.js +49 -0
  89. package/lib/Utils/tc-token-utils.js +163 -0
  90. package/lib/Utils/use-multi-file-auth-state.js +121 -0
  91. package/lib/Utils/validate-connection.js +203 -0
  92. package/lib/WABinary/constants.js +1301 -0
  93. package/lib/WABinary/decode.js +262 -0
  94. package/lib/WABinary/encode.js +220 -0
  95. package/lib/WABinary/generic-utils.js +204 -0
  96. package/lib/WABinary/index.js +6 -0
  97. package/lib/WABinary/jid-utils.js +98 -0
  98. package/lib/WABinary/types.js +2 -0
  99. package/lib/WAM/BinaryInfo.js +10 -0
  100. package/lib/WAM/constants.js +22853 -0
  101. package/lib/WAM/encode.js +150 -0
  102. package/lib/WAM/index.js +4 -0
  103. package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
  104. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
  105. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
  106. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
  107. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  108. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
  109. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
  110. package/lib/WAUSync/Protocols/index.js +6 -0
  111. package/lib/WAUSync/USyncQuery.js +98 -0
  112. package/lib/WAUSync/USyncUser.js +31 -0
  113. package/lib/WAUSync/index.js +4 -0
  114. package/lib/index.js +31 -0
  115. package/package.json +143 -0
@@ -0,0 +1,403 @@
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
+ };
53
+ // code is inspired by whatsmeow
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;
73
+ /** unix timestamp of a date in seconds */
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;
131
+ }
132
+ // inspired from whatsmeow code
133
+ // https://github.com/tulir/whatsmeow/blob/64bc969fbe78d31ae0dd443b8d4c80a5d026d07a/send.go#L42
134
+ export const generateMessageIDV2 = (userId) => {
135
+ const data = Buffer.alloc(8 + 20 + 16);
136
+ data.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 1000)));
137
+ if (userId) {
138
+ const id = jidDecode(userId);
139
+ if (id?.user) {
140
+ data.write(id.user, 8);
141
+ data.write('@c.us', 8 + id.user.length);
142
+ }
143
+ }
144
+ const random = randomBytes(16);
145
+ random.copy(data, 28);
146
+ const hash = createHash('sha256').update(data).digest();
147
+ return 'XZCYYX-' + hash.toString('hex').toUpperCase().substring(0, 18);
148
+ };
149
+ // generate a random ID to attach to a message
150
+ export const generateMessageID = () => 'XZCYYX-' + 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 = () => {
175
+ const prefix = '3A';
176
+ const random = randomBytes(9.5); // 19 hex chars = 9.5 bytes
177
+ return (prefix + random.toString('hex')).toUpperCase().substring(0, 21);
178
+ };
179
+ export const generateAndroMessageID = () => {
180
+ const prefix = '3A';
181
+ const random = randomBytes(16); // 32 hex chars = 16 bytes
182
+ return (random.toString('hex')).toUpperCase().substring(0, 21);
183
+ };
184
+ export const bindWaitForConnectionUpdate = (ev) => bindWaitForEvent(ev, 'connection.update');
185
+ /**
186
+ * utility that fetches latest baileys version from the master branch.
187
+ * Use to ensure your WA connection is always on the latest version
188
+ */
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
+ };
224
+ /**
225
+ * A utility that fetches the latest web version of whatsapp.
226
+ * Use to ensure your WA connection is always on the latest version
227
+ */
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
+ };
270
+ /** unique message tag prefix for MD clients */
271
+ export const generateMdTagPrefix = () => {
272
+ const bytes = randomBytes(4);
273
+ return `${bytes.readUInt16BE()}.${bytes.readUInt16BE(2)}-`;
274
+ };
275
+ const STATUS_MAP = {
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
+ };
281
+ /**
282
+ * Given a type of receipt, returns what the new status of the message should be
283
+ * @param type type from receipt
284
+ */
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
+ };
292
+ const CODE_MAP = {
293
+ conflict: DisconnectReason.connectionReplaced
294
+ };
295
+ /**
296
+ * Stream errors generally provide a reason, map that to a baileys DisconnectReason
297
+ * @param reason the string reason given, eg. "conflict"
298
+ */
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
+ };
366
+ /**
367
+ * Is the given platform WA business
368
+ * @param platform AuthenticationCreds.platform
369
+ */
370
+ export const isWABusinessPlatform = (platform) => {
371
+ return platform === 'smbi' || platform === 'smba';
372
+ };
373
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
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;
381
+ }
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('');
399
+ }
400
+ export function encodeNewsletterMessage(message) {
401
+ return proto.Message.encode(message).finish();
402
+ }
403
+ //# sourceMappingURL=generics.js.map
@@ -0,0 +1,134 @@
1
+ import { pipeline } from 'stream/promises';
2
+ import { promisify } from 'util';
3
+ import { createInflate, inflate } from 'zlib';
4
+ import { proto } from '../../WAProto/index.js';
5
+ import { WAMessageStubType } from '../Types/index.js';
6
+ import { isHostedLidUser, isHostedPnUser, isLidUser, isPnUser } from '../WABinary/index.js';
7
+ import { toNumber } from './generics.js';
8
+ import { normalizeMessageContent } from './messages.js';
9
+ import { downloadContentFromMessage } from './messages-media.js';
10
+ const inflatePromise = promisify(inflate);
11
+ const extractPnFromMessages = (messages) => {
12
+ for (const msgItem of messages) {
13
+ const message = msgItem.message;
14
+ // Only extract from outgoing messages (fromMe: true) in 1:1 chats
15
+ // because userReceipt.userJid is the recipient's JID
16
+ if (!message?.key?.fromMe || !message.userReceipt?.length) {
17
+ continue;
18
+ }
19
+ const userJid = message.userReceipt[0]?.userJid;
20
+ if (userJid && (isPnUser(userJid) || isHostedPnUser(userJid))) {
21
+ return userJid;
22
+ }
23
+ }
24
+ return undefined;
25
+ };
26
+ export const downloadHistory = async (msg, options) => {
27
+ const stream = await downloadContentFromMessage(msg, 'md-msg-hist', { options });
28
+ // Pipe decrypted stream directly through zlib inflate
29
+ // This avoids allocating an intermediate buffer for the compressed data
30
+ const inflater = createInflate();
31
+ const chunks = [];
32
+ inflater.on('data', (chunk) => chunks.push(chunk));
33
+ await pipeline(stream, inflater);
34
+ const buffer = Buffer.concat(chunks);
35
+ const syncData = proto.HistorySync.decode(buffer);
36
+ return syncData;
37
+ };
38
+ export const processHistoryMessage = (item, logger) => {
39
+ const messages = [];
40
+ const contacts = [];
41
+ const chats = [];
42
+ const lidPnMappings = [];
43
+ logger?.trace({ progress: item.progress }, 'processing history of type ' + item.syncType?.toString());
44
+ // Extract LID-PN mappings for all sync types
45
+ for (const m of item.phoneNumberToLidMappings || []) {
46
+ if (m.lidJid && m.pnJid) {
47
+ lidPnMappings.push({ lid: m.lidJid, pn: m.pnJid });
48
+ }
49
+ }
50
+ switch (item.syncType) {
51
+ case proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP:
52
+ case proto.HistorySync.HistorySyncType.RECENT:
53
+ case proto.HistorySync.HistorySyncType.FULL:
54
+ case proto.HistorySync.HistorySyncType.ON_DEMAND:
55
+ for (const chat of item.conversations) {
56
+ contacts.push({
57
+ id: chat.id,
58
+ name: chat.displayName || chat.name || chat.username || undefined,
59
+ username: chat.username || undefined,
60
+ lid: chat.lidJid || chat.accountLid || undefined,
61
+ phoneNumber: chat.pnJid || undefined
62
+ });
63
+ const chatId = chat.id;
64
+ const isLid = isLidUser(chatId) || isHostedLidUser(chatId);
65
+ const isPn = isPnUser(chatId) || isHostedPnUser(chatId);
66
+ if (isLid && chat.pnJid) {
67
+ lidPnMappings.push({ lid: chatId, pn: chat.pnJid });
68
+ }
69
+ else if (isPn && chat.lidJid) {
70
+ lidPnMappings.push({ lid: chat.lidJid, pn: chatId });
71
+ }
72
+ else if (isLid && !chat.pnJid) {
73
+ // Fallback: extract PN from userReceipt in messages when pnJid is missing
74
+ const pnFromReceipt = extractPnFromMessages(chat.messages || []);
75
+ if (pnFromReceipt) {
76
+ lidPnMappings.push({ lid: chatId, pn: pnFromReceipt });
77
+ }
78
+ }
79
+ const msgs = chat.messages || [];
80
+ delete chat.messages;
81
+ for (const item of msgs) {
82
+ const message = item.message;
83
+ messages.push(message);
84
+ if (!chat.messages?.length) {
85
+ // keep only the most recent message in the chat array
86
+ chat.messages = [{ message }];
87
+ }
88
+ if (!message.key.fromMe && !chat.lastMessageRecvTimestamp) {
89
+ chat.lastMessageRecvTimestamp = toNumber(message.messageTimestamp);
90
+ }
91
+ if ((message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_BSP ||
92
+ message.messageStubType === WAMessageStubType.BIZ_PRIVACY_MODE_TO_FB) &&
93
+ message.messageStubParameters?.[0]) {
94
+ contacts.push({
95
+ id: message.key.participant || message.key.remoteJid,
96
+ verifiedName: message.messageStubParameters?.[0]
97
+ });
98
+ }
99
+ }
100
+ chats.push(chat);
101
+ }
102
+ break;
103
+ case proto.HistorySync.HistorySyncType.PUSH_NAME:
104
+ for (const c of item.pushnames) {
105
+ contacts.push({ id: c.id, notify: c.pushname });
106
+ }
107
+ break;
108
+ }
109
+ return {
110
+ chats,
111
+ contacts,
112
+ messages,
113
+ lidPnMappings,
114
+ pastParticipants: item.pastParticipants,
115
+ syncType: item.syncType,
116
+ progress: item.progress
117
+ };
118
+ };
119
+ export const downloadAndProcessHistorySyncNotification = async (msg, options, logger) => {
120
+ let historyMsg;
121
+ if (msg.initialHistBootstrapInlinePayload) {
122
+ historyMsg = proto.HistorySync.decode(await inflatePromise(msg.initialHistBootstrapInlinePayload));
123
+ }
124
+ else {
125
+ historyMsg = await downloadHistory(msg, options);
126
+ }
127
+ return processHistoryMessage(historyMsg, logger);
128
+ };
129
+ export const getHistoryMsg = (message) => {
130
+ const normalizedContent = !!message ? normalizeMessageContent(message) : undefined;
131
+ const anyHistoryMsg = normalizedContent?.protocolMessage?.historySyncNotification;
132
+ return anyHistoryMsg;
133
+ };
134
+ //# sourceMappingURL=history.js.map
@@ -0,0 +1,50 @@
1
+ import NodeCache from '@cacheable/node-cache';
2
+ import { areJidsSameUser, getBinaryNodeChild, jidDecode } from '../WABinary/index.js';
3
+ import { isStringNullOrEmpty } from './generics.js';
4
+ export async function handleIdentityChange(node, ctx) {
5
+ const from = node.attrs.from;
6
+ if (!from) {
7
+ return { action: 'invalid_notification' };
8
+ }
9
+ const identityNode = getBinaryNodeChild(node, 'identity');
10
+ if (!identityNode) {
11
+ return { action: 'no_identity_node' };
12
+ }
13
+ ctx.logger.info({ jid: from }, 'identity changed');
14
+ const decoded = jidDecode(from);
15
+ if (decoded?.device && decoded.device !== 0) {
16
+ ctx.logger.debug({ jid: from, device: decoded.device }, 'ignoring identity change from companion device');
17
+ return { action: 'skipped_companion_device', device: decoded.device };
18
+ }
19
+ const isSelfPrimary = ctx.meId && (areJidsSameUser(from, ctx.meId) || (ctx.meLid && areJidsSameUser(from, ctx.meLid)));
20
+ if (isSelfPrimary) {
21
+ ctx.logger.info({ jid: from }, 'self primary identity changed');
22
+ return { action: 'skipped_self_primary' };
23
+ }
24
+ if (ctx.debounceCache.get(from)) {
25
+ ctx.logger.debug({ jid: from }, 'skipping identity assert (debounced)');
26
+ return { action: 'debounced' };
27
+ }
28
+ ctx.debounceCache.set(from, true);
29
+ const isOfflineNotification = !isStringNullOrEmpty(node.attrs.offline);
30
+ const hasExistingSession = await ctx.validateSession(from);
31
+ if (!hasExistingSession.exists) {
32
+ ctx.logger.debug({ jid: from }, 'no old session, skipping session refresh');
33
+ return { action: 'skipped_no_session' };
34
+ }
35
+ ctx.logger.debug({ jid: from }, 'old session exists, will refresh session');
36
+ if (isOfflineNotification) {
37
+ ctx.logger.debug({ jid: from }, 'skipping session refresh during offline processing');
38
+ return { action: 'skipped_offline' };
39
+ }
40
+ ctx.onBeforeSessionRefresh?.(from);
41
+ try {
42
+ await ctx.assertSessions([from], true);
43
+ return { action: 'session_refreshed' };
44
+ }
45
+ catch (error) {
46
+ ctx.logger.warn({ error, jid: from }, 'failed to assert sessions after identity change');
47
+ return { action: 'session_refresh_failed', error };
48
+ }
49
+ }
50
+ //# sourceMappingURL=identity-change-handler.js.map
@@ -0,0 +1,23 @@
1
+ export * from './generics.js';
2
+ export * from './decode-wa-message.js';
3
+ export * from './messages.js';
4
+ export * from './messages-media.js';
5
+ export * from './validate-connection.js';
6
+ export * from './crypto.js';
7
+ export * from './signal.js';
8
+ export * from './noise-handler.js';
9
+ export * from './history.js';
10
+ export * from './chat-utils.js';
11
+ export * from './lt-hash.js';
12
+ export * from './auth-utils.js';
13
+ export * from './use-multi-file-auth-state.js';
14
+ export * from './link-preview.js';
15
+ export * from './event-buffer.js';
16
+ export * from './process-message.js';
17
+ export * from './message-retry-manager.js';
18
+ export * from './browser-utils.js';
19
+ export * from './companion-reg-client-utils.js';
20
+ export * from './identity-change-handler.js';
21
+ export * from './stanza-ack.js';
22
+ export * from './message-composer.js';
23
+ //# sourceMappingURL=index.js.map