@dnuzi/baileys 0.0.1

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 (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +374 -0
  3. package/WAProto/index.js +104216 -0
  4. package/engine-requirements.js +19 -0
  5. package/lib/@dnuzi +1 -0
  6. package/lib/Defaults/index.js +143 -0
  7. package/lib/Signal/Group/ciphertext-message.js +11 -0
  8. package/lib/Signal/Group/group-session-builder.js +29 -0
  9. package/lib/Signal/Group/group_cipher.js +81 -0
  10. package/lib/Signal/Group/index.js +11 -0
  11. package/lib/Signal/Group/keyhelper.js +17 -0
  12. package/lib/Signal/Group/sender-chain-key.js +25 -0
  13. package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
  14. package/lib/Signal/Group/sender-key-message.js +65 -0
  15. package/lib/Signal/Group/sender-key-name.js +47 -0
  16. package/lib/Signal/Group/sender-key-record.js +40 -0
  17. package/lib/Signal/Group/sender-key-state.js +83 -0
  18. package/lib/Signal/Group/sender-message-key.js +25 -0
  19. package/lib/Signal/libsignal.js +402 -0
  20. package/lib/Signal/lid-mapping.js +270 -0
  21. package/lib/Socket/Client/index.js +2 -0
  22. package/lib/Socket/Client/types.js +10 -0
  23. package/lib/Socket/Client/websocket.js +53 -0
  24. package/lib/Socket/business.js +378 -0
  25. package/lib/Socket/chats.js +1048 -0
  26. package/lib/Socket/communities.js +430 -0
  27. package/lib/Socket/groups.js +328 -0
  28. package/lib/Socket/index.js +11 -0
  29. package/lib/Socket/messages-recv.js +1463 -0
  30. package/lib/Socket/messages-send.js +1241 -0
  31. package/lib/Socket/mex.js +41 -0
  32. package/lib/Socket/newsletter.js +227 -0
  33. package/lib/Socket/socket.js +951 -0
  34. package/lib/Store/index.js +3 -0
  35. package/lib/Store/make-in-memory-store.js +421 -0
  36. package/lib/Store/make-ordered-dictionary.js +78 -0
  37. package/lib/Store/object-repository.js +23 -0
  38. package/lib/Types/Auth.js +1 -0
  39. package/lib/Types/Bussines.js +1 -0
  40. package/lib/Types/Call.js +1 -0
  41. package/lib/Types/Chat.js +7 -0
  42. package/lib/Types/Contact.js +1 -0
  43. package/lib/Types/Events.js +1 -0
  44. package/lib/Types/GroupMetadata.js +1 -0
  45. package/lib/Types/Label.js +24 -0
  46. package/lib/Types/LabelAssociation.js +6 -0
  47. package/lib/Types/Message.js +17 -0
  48. package/lib/Types/Newsletter.js +33 -0
  49. package/lib/Types/Product.js +1 -0
  50. package/lib/Types/Signal.js +1 -0
  51. package/lib/Types/Socket.js +2 -0
  52. package/lib/Types/State.js +12 -0
  53. package/lib/Types/USync.js +1 -0
  54. package/lib/Types/index.js +25 -0
  55. package/lib/Utils/auth-utils.js +289 -0
  56. package/lib/Utils/browser-utils.js +28 -0
  57. package/lib/Utils/business.js +230 -0
  58. package/lib/Utils/chat-utils.js +811 -0
  59. package/lib/Utils/companion-reg-client-utils.js +32 -0
  60. package/lib/Utils/crypto.js +117 -0
  61. package/lib/Utils/decode-wa-message.js +282 -0
  62. package/lib/Utils/event-buffer.js +573 -0
  63. package/lib/Utils/generics.js +385 -0
  64. package/lib/Utils/history.js +130 -0
  65. package/lib/Utils/identity-change-handler.js +48 -0
  66. package/lib/Utils/index.js +22 -0
  67. package/lib/Utils/link-preview.js +84 -0
  68. package/lib/Utils/logger.js +2 -0
  69. package/lib/Utils/lt-hash.js +7 -0
  70. package/lib/Utils/make-mutex.js +32 -0
  71. package/lib/Utils/message-retry-manager.js +224 -0
  72. package/lib/Utils/messages-media.js +830 -0
  73. package/lib/Utils/messages.js +1887 -0
  74. package/lib/Utils/noise-handler.js +200 -0
  75. package/lib/Utils/offline-node-processor.js +39 -0
  76. package/lib/Utils/pre-key-manager.js +105 -0
  77. package/lib/Utils/process-message.js +527 -0
  78. package/lib/Utils/reporting-utils.js +257 -0
  79. package/lib/Utils/signal.js +158 -0
  80. package/lib/Utils/stanza-ack.js +37 -0
  81. package/lib/Utils/sync-action-utils.js +47 -0
  82. package/lib/Utils/tc-token-utils.js +17 -0
  83. package/lib/Utils/use-multi-file-auth-state.js +120 -0
  84. package/lib/Utils/use-single-file-auth-state.js +96 -0
  85. package/lib/Utils/validate-connection.js +206 -0
  86. package/lib/WABinary/constants.js +1300 -0
  87. package/lib/WABinary/decode.js +261 -0
  88. package/lib/WABinary/encode.js +219 -0
  89. package/lib/WABinary/generic-utils.js +227 -0
  90. package/lib/WABinary/index.js +5 -0
  91. package/lib/WABinary/jid-utils.js +95 -0
  92. package/lib/WABinary/types.js +1 -0
  93. package/lib/WAM/BinaryInfo.js +9 -0
  94. package/lib/WAM/constants.js +22852 -0
  95. package/lib/WAM/encode.js +149 -0
  96. package/lib/WAM/index.js +3 -0
  97. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
  98. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
  99. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  100. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  101. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
  102. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  103. package/lib/WAUSync/Protocols/index.js +4 -0
  104. package/lib/WAUSync/USyncQuery.js +93 -0
  105. package/lib/WAUSync/USyncUser.js +22 -0
  106. package/lib/WAUSync/index.js +3 -0
  107. package/lib/index.js +51 -0
  108. package/package.json +77 -0
@@ -0,0 +1,830 @@
1
+ import { Boom } from '@hapi/boom';
2
+ import { spawn } from 'child_process';
3
+ import * as Crypto from 'crypto';
4
+ import { once } from 'events';
5
+ import { createReadStream, createWriteStream, promises as fs, WriteStream } from 'fs';
6
+ import { tmpdir } from 'os';
7
+ import { join } from 'path';
8
+ import { Readable, Transform } from 'stream';
9
+ import { URL } from 'url';
10
+ import { proto } from '../../WAProto/index.js';
11
+ import { DEFAULT_ORIGIN, MEDIA_HKDF_KEY_MAPPING, MEDIA_PATH_MAP, NEWSLETTER_MEDIA_PATH_MAP } from '../Defaults/index.js';
12
+ import { getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from '../WABinary/index.js';
13
+ import { aesDecryptGCM, aesEncryptGCM, hkdf } from './crypto.js';
14
+ import { generateMessageIDV2 } from './generics.js';
15
+ const getTmpFilesDirectory = () => tmpdir();
16
+ let imageProcessingLibrary;
17
+ export const getImageProcessingLibrary = async () => {
18
+ if (imageProcessingLibrary) {
19
+ return imageProcessingLibrary;
20
+ }
21
+ //@ts-ignore
22
+ const [sharp, image, jimp] = await Promise.all([
23
+ import('sharp').catch(() => { }),
24
+ import('@napi-rs/image').catch(() => { }),
25
+ import('jimp').catch(() => { })
26
+ ]);
27
+ if (sharp) {
28
+ imageProcessingLibrary = { sharp };
29
+ }
30
+ else if (image) {
31
+ imageProcessingLibrary = { image };
32
+ }
33
+ else if (jimp) {
34
+ imageProcessingLibrary = { jimp };
35
+ }
36
+ else {
37
+ throw new Boom('No image processing library available');
38
+ }
39
+ return imageProcessingLibrary;
40
+ };
41
+ export const hkdfInfoKey = (type) => {
42
+ const hkdfInfo = MEDIA_HKDF_KEY_MAPPING[type];
43
+ return `WhatsApp ${hkdfInfo} Keys`;
44
+ };
45
+ export const getRawMediaUploadData = async (media, mediaType, logger) => {
46
+ const { stream } = await getStream(media);
47
+ logger?.debug('got stream for raw upload');
48
+ const hasher = Crypto.createHash('sha256');
49
+ const filePath = join(tmpdir(), mediaType + generateMessageIDV2());
50
+ const fileWriteStream = createWriteStream(filePath);
51
+ let fileLength = 0;
52
+ try {
53
+ for await (const data of stream) {
54
+ fileLength += data.length;
55
+ hasher.update(data);
56
+ if (!fileWriteStream.write(data)) {
57
+ await once(fileWriteStream, 'drain');
58
+ }
59
+ }
60
+ fileWriteStream.end();
61
+ await once(fileWriteStream, 'finish');
62
+ stream.destroy();
63
+ const fileSha256 = hasher.digest();
64
+ logger?.debug('hashed data for raw upload');
65
+ return {
66
+ filePath: filePath,
67
+ fileSha256,
68
+ fileLength
69
+ };
70
+ }
71
+ catch (error) {
72
+ fileWriteStream.destroy();
73
+ stream.destroy();
74
+ try {
75
+ await fs.unlink(filePath);
76
+ }
77
+ catch {
78
+ //
79
+ }
80
+ throw error;
81
+ }
82
+ };
83
+ /** generates all the keys required to encrypt/decrypt & sign a media message */
84
+ export async function getMediaKeys(buffer, mediaType) {
85
+ if (!buffer) {
86
+ throw new Boom('Cannot derive from empty media key');
87
+ }
88
+ if (typeof buffer === 'string') {
89
+ buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64');
90
+ }
91
+ // expand using HKDF to 112 bytes, also pass in the relevant app info
92
+ const expandedMediaKey = hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) });
93
+ return {
94
+ iv: expandedMediaKey.slice(0, 16),
95
+ cipherKey: expandedMediaKey.slice(16, 48),
96
+ macKey: expandedMediaKey.slice(48, 80)
97
+ };
98
+ }
99
+ /** Extracts video thumb using FFMPEG */
100
+ export const extractVideoThumb = async (path, time, size) => {
101
+ const ffmpeg = spawn('ffmpeg', [
102
+ '-loglevel', 'error',
103
+ '-ss', String(time),
104
+ '-i', path,
105
+ '-an', '-sn', '-dn',
106
+ '-map_metadata', '-1',
107
+ '-vf', `scale=${size.width}:-1`,
108
+ '-frames:v', '1',
109
+ '-c:v', 'mjpeg',
110
+ '-f', 'image2pipe',
111
+ 'pipe:1'
112
+ ], {
113
+ stdio: ['ignore', 'pipe', 'pipe']
114
+ });
115
+ let buffer = Buffer.alloc(0);
116
+ const stderrChunks = [];
117
+ ffmpeg.stdout.on('data', (chunk) => {
118
+ buffer = Buffer.concat([buffer, chunk]);
119
+ });
120
+ ffmpeg.stderr.on('data', (chunk) => stderrChunks.push(chunk));
121
+ const [code] = await once(ffmpeg, 'close');
122
+ if (code !== 0) {
123
+ throw new Boom(
124
+ `FFmpeg failed (code ${code}):\n` +
125
+ Buffer.concat(stderrChunks).toString('utf8')
126
+ );
127
+ }
128
+ return buffer;
129
+ };
130
+ export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
131
+ // TODO: Move entirely to sharp, removing jimp as it supports readable streams
132
+ // This will have positive speed and performance impacts as well as minimizing RAM usage.
133
+ if (bufferOrFilePath instanceof Readable) {
134
+ bufferOrFilePath = await toBuffer(bufferOrFilePath);
135
+ }
136
+ const lib = await getImageProcessingLibrary();
137
+ if ('sharp' in lib && lib.sharp?.default) {
138
+ const img = lib.sharp.default(bufferOrFilePath);
139
+ const dimensions = await img.metadata();
140
+ const buffer = await img.resize(width).jpeg({ quality: 50 }).toBuffer();
141
+ return {
142
+ buffer,
143
+ original: {
144
+ width: dimensions.width,
145
+ height: dimensions.height
146
+ }
147
+ };
148
+ }
149
+ else if ('image' in lib && lib.image?.Transformer) {
150
+ if (!Buffer.isBuffer(bufferOrFilePath)) {
151
+ bufferOrFilePath = await fs.readFile(bufferOrFilePath);
152
+ }
153
+ const img = new lib.image.Transformer(bufferOrFilePath);
154
+ const dimensions = await img.metadata();
155
+ const buffer = await img.resize(width, undefined, 0).jpeg(50);
156
+ return {
157
+ buffer,
158
+ original: {
159
+ width: dimensions.width,
160
+ height: dimensions.height
161
+ }
162
+ };
163
+ }
164
+ else if ('jimp' in lib && lib.jimp?.Jimp) {
165
+ const jimp = await lib.jimp.Jimp.read(bufferOrFilePath);
166
+ const dimensions = {
167
+ width: jimp.width,
168
+ height: jimp.height
169
+ };
170
+ const buffer = await jimp
171
+ .resize({ w: width, mode: lib.jimp.ResizeStrategy.BILINEAR })
172
+ .getBuffer('image/jpeg', { quality: 50 });
173
+ return {
174
+ buffer,
175
+ original: dimensions
176
+ };
177
+ }
178
+ else {
179
+ throw new Boom('No image processing library available');
180
+ }
181
+ };
182
+ export const encodeBase64EncodedStringForUpload = (b64) => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, ''));
183
+ export const generateProfilePicture = async (mediaUpload, dimensions) => {
184
+ let buffer;
185
+ const { width: w = 720, height: h = 720 } = dimensions || {};
186
+ if (Buffer.isBuffer(mediaUpload)) {
187
+ buffer = mediaUpload;
188
+ }
189
+ else {
190
+ // Use getStream to handle all WAMediaUpload types (Buffer, Stream, URL)
191
+ const { stream } = await getStream(mediaUpload);
192
+ // Convert the resulting stream to a buffer
193
+ buffer = await toBuffer(stream);
194
+ }
195
+ const lib = await getImageProcessingLibrary();
196
+ let img;
197
+ if ('sharp' in lib && lib.sharp?.default) {
198
+ img = lib.sharp
199
+ .default(buffer)
200
+ .resize(w, h)
201
+ .jpeg({
202
+ quality: 80
203
+ })
204
+ .toBuffer();
205
+ }
206
+ else if ('image' in lib && lib.image?.Transformer) {
207
+ img = new lib.image
208
+ .Transformer(buffer)
209
+ .resize(w, h, 0)
210
+ .jpeg(80);
211
+ }
212
+ else if ('jimp' in lib && lib.jimp?.Jimp) {
213
+ const jimp = await lib.jimp.Jimp.read(buffer);
214
+ const min = Math.min(jimp.width, jimp.height);
215
+ const cropped = jimp.crop({ x: 0, y: 0, w: min, h: min });
216
+ img = cropped.resize({ w, h, mode: lib.jimp.ResizeStrategy.BILINEAR }).getBuffer('image/jpeg', { quality: 80 });
217
+ }
218
+ else {
219
+ throw new Boom('No image processing library available');
220
+ }
221
+ return {
222
+ img: await img
223
+ };
224
+ };
225
+ /** gets the SHA256 of the given media message */
226
+ export const mediaMessageSHA256B64 = (message) => {
227
+ const media = Object.values(message)[0];
228
+ return media?.fileSha256 && Buffer.from(media.fileSha256).toString('base64');
229
+ };
230
+ export async function getAudioDuration(buffer) {
231
+ const musicMetadata = await import('music-metadata');
232
+ let metadata;
233
+ const options = {
234
+ duration: true
235
+ };
236
+ if (Buffer.isBuffer(buffer)) {
237
+ metadata = await musicMetadata.parseBuffer(buffer, undefined, options);
238
+ }
239
+ else if (typeof buffer === 'string') {
240
+ metadata = await musicMetadata.parseFile(buffer, options);
241
+ }
242
+ else {
243
+ metadata = await musicMetadata.parseStream(buffer, undefined, options);
244
+ }
245
+ return metadata.format.duration;
246
+ }
247
+ /**
248
+ referenced from and modifying https://github.com/wppconnect-team/wa-js/blob/main/src/chat/functions/prepareAudioWaveform.ts
249
+ */
250
+ export async function getAudioWaveform(buffer, logger) {
251
+ try {
252
+ // @ts-ignore
253
+ const { default: decoder } = await import('audio-decode');
254
+ let audioData;
255
+ if (Buffer.isBuffer(buffer)) {
256
+ audioData = buffer;
257
+ }
258
+ else if (typeof buffer === 'string') {
259
+ const rStream = createReadStream(buffer);
260
+ audioData = await toBuffer(rStream);
261
+ }
262
+ else {
263
+ audioData = await toBuffer(buffer);
264
+ }
265
+ const audioBuffer = await decoder(audioData);
266
+ const rawData = audioBuffer.getChannelData(0); // We only need to work with one channel of data
267
+ const samples = 64; // Number of samples we want to have in our final data set
268
+ const blockSize = Math.floor(rawData.length / samples); // the number of samples in each subdivision
269
+ const filteredData = [];
270
+ for (let i = 0; i < samples; i++) {
271
+ const blockStart = blockSize * i; // the location of the first sample in the block
272
+ let sum = 0;
273
+ for (let j = 0; j < blockSize; j++) {
274
+ sum = sum + Math.abs(rawData[blockStart + j]); // find the sum of all the samples in the block
275
+ }
276
+ filteredData.push(sum / blockSize); // divide the sum by the block size to get the average
277
+ }
278
+ // This guarantees that the largest data point will be set to 1, and the rest of the data will scale proportionally.
279
+ const multiplier = Math.pow(Math.max(...filteredData), -1);
280
+ const normalizedData = filteredData.map(n => n * multiplier);
281
+ // Generate waveform like WhatsApp
282
+ const waveform = new Uint8Array(normalizedData.map(n => Math.floor(100 * n)));
283
+ return waveform;
284
+ }
285
+ catch (e) {
286
+ logger?.debug('Failed to generate waveform: ' + e);
287
+ }
288
+ }
289
+ export const toReadable = (buffer) => {
290
+ const readable = new Readable({ read: () => { } });
291
+ readable.push(buffer);
292
+ readable.push(null);
293
+ return readable;
294
+ };
295
+ export const toBuffer = async (stream) => {
296
+ const chunks = [];
297
+ for await (const chunk of stream) {
298
+ chunks.push(chunk);
299
+ }
300
+ stream.destroy();
301
+ return Buffer.concat(chunks);
302
+ };
303
+ export const getStream = async (item, opts) => {
304
+ if (Buffer.isBuffer(item)) {
305
+ return { stream: toReadable(item), type: 'buffer' };
306
+ }
307
+ if ('stream' in item) {
308
+ return { stream: item.stream, type: 'readable' };
309
+ }
310
+ const urlStr = item.url.toString();
311
+ if (urlStr.startsWith('data:')) {
312
+ const buffer = Buffer.from(urlStr.split(',')[1], 'base64');
313
+ return { stream: toReadable(buffer), type: 'buffer' };
314
+ }
315
+ if (urlStr.startsWith('http://') || urlStr.startsWith('https://')) {
316
+ return { stream: await getHttpStream(item.url, opts), type: 'remote' };
317
+ }
318
+ return { stream: createReadStream(item.url), type: 'file' };
319
+ };
320
+ /** generates a thumbnail for a given media, if required */
321
+ export async function generateThumbnail(file, mediaType, options) {
322
+ let thumbnail;
323
+ let originalImageDimensions;
324
+ if (mediaType === 'image') {
325
+ const { buffer, original } = await extractImageThumb(file);
326
+ thumbnail = buffer;
327
+ if (original.width && original.height) {
328
+ originalImageDimensions = {
329
+ width: original.width,
330
+ height: original.height
331
+ };
332
+ }
333
+ }
334
+ else if (mediaType === 'video') {
335
+ try {
336
+ const buffer = await extractVideoThumb(file, '00:00:00', { width: 32, height: 32 });
337
+ thumbnail = buffer;
338
+ }
339
+ catch (err) {
340
+ options.logger?.debug('could not generate video thumb: ' + err);
341
+ }
342
+ }
343
+ return {
344
+ thumbnail,
345
+ originalImageDimensions
346
+ };
347
+ }
348
+ export const getHttpStream = async (url, options = {}) => {
349
+ const response = await fetch(url.toString(), {
350
+ dispatcher: options.dispatcher,
351
+ method: 'GET',
352
+ headers: options.headers
353
+ });
354
+ if (!response.ok) {
355
+ throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } });
356
+ }
357
+ // @ts-ignore Node18+ Readable.fromWeb exists
358
+ return response.body instanceof Readable ? response.body : Readable.fromWeb(response.body);
359
+ };
360
+ export const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts } = {}) => {
361
+ const { stream, type } = await getStream(media, opts);
362
+ logger?.debug('fetched media stream');
363
+ const mediaKey = Crypto.randomBytes(32);
364
+ const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType);
365
+ const encFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-enc');
366
+ const encFileWriteStream = createWriteStream(encFilePath);
367
+ let originalFileStream;
368
+ let originalFilePath;
369
+ if (saveOriginalFileIfRequired) {
370
+ originalFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-original');
371
+ originalFileStream = createWriteStream(originalFilePath);
372
+ }
373
+ let fileLength = 0;
374
+ const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
375
+ const hmac = Crypto.createHmac('sha256', macKey).update(iv);
376
+ const sha256Plain = Crypto.createHash('sha256');
377
+ const sha256Enc = Crypto.createHash('sha256');
378
+ const onChunk = async (buff) => {
379
+ sha256Enc.update(buff);
380
+ hmac.update(buff);
381
+ // Handle backpressure: if write returns false, wait for drain
382
+ if (!encFileWriteStream.write(buff)) {
383
+ await once(encFileWriteStream, 'drain');
384
+ }
385
+ };
386
+ try {
387
+ for await (const data of stream) {
388
+ fileLength += data.length;
389
+ if (type === 'remote' &&
390
+ opts?.maxContentLength &&
391
+ fileLength + data.length > opts.maxContentLength) {
392
+ throw new Boom(`content length exceeded when encrypting "${type}"`, {
393
+ data: { media, type }
394
+ });
395
+ }
396
+ if (originalFileStream) {
397
+ if (!originalFileStream.write(data)) {
398
+ await once(originalFileStream, 'drain');
399
+ }
400
+ }
401
+ sha256Plain.update(data);
402
+ await onChunk(aes.update(data));
403
+ }
404
+ await onChunk(aes.final());
405
+ const mac = hmac.digest().slice(0, 10);
406
+ sha256Enc.update(mac);
407
+ const fileSha256 = sha256Plain.digest();
408
+ const fileEncSha256 = sha256Enc.digest();
409
+ encFileWriteStream.write(mac);
410
+ const encFinishPromise = once(encFileWriteStream, 'finish');
411
+ const originalFinishPromise = originalFileStream ? once(originalFileStream, 'finish') : Promise.resolve();
412
+ encFileWriteStream.end();
413
+ originalFileStream?.end?.();
414
+ stream.destroy();
415
+ // Wait for write streams to fully flush to disk
416
+ // This helps reduce memory pressure by allowing OS to release buffers
417
+ await encFinishPromise;
418
+ await originalFinishPromise;
419
+ logger?.debug('encrypted data successfully');
420
+ return {
421
+ mediaKey,
422
+ originalFilePath,
423
+ encFilePath,
424
+ mac,
425
+ fileEncSha256,
426
+ fileSha256,
427
+ fileLength
428
+ };
429
+ }
430
+ catch (error) {
431
+ // destroy all streams with error
432
+ encFileWriteStream.destroy();
433
+ originalFileStream?.destroy?.();
434
+ aes.destroy();
435
+ hmac.destroy();
436
+ sha256Plain.destroy();
437
+ sha256Enc.destroy();
438
+ stream.destroy();
439
+ try {
440
+ await fs.unlink(encFilePath);
441
+ if (originalFilePath) {
442
+ await fs.unlink(originalFilePath);
443
+ }
444
+ }
445
+ catch (err) {
446
+ logger?.error({ err }, 'failed deleting tmp files');
447
+ }
448
+ throw error;
449
+ }
450
+ };
451
+ const DEF_HOST = 'mmg.whatsapp.net';
452
+ const AES_CHUNK_SIZE = 16;
453
+ const toSmallestChunkSize = (num) => {
454
+ return Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
455
+ };
456
+ export const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
457
+ export const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
458
+ const isValidMediaUrl = url?.startsWith('https://mmg.whatsapp.net/');
459
+ const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath);
460
+ if (!downloadUrl) {
461
+ throw new Boom('No valid media URL or directPath present in message', { statusCode: 400 });
462
+ }
463
+ const keys = await getMediaKeys(mediaKey, type);
464
+ return downloadEncryptedContent(downloadUrl, keys, opts);
465
+ };
466
+ /**
467
+ * Decrypts and downloads an AES256-CBC encrypted file given the keys.
468
+ * Assumes the SHA256 of the plaintext is appended to the end of the ciphertext
469
+ * */
470
+ export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
471
+ let bytesFetched = 0;
472
+ let startChunk = 0;
473
+ let firstBlockIsIV = false;
474
+ // if a start byte is specified -- then we need to fetch the previous chunk as that will form the IV
475
+ if (startByte) {
476
+ const chunk = toSmallestChunkSize(startByte || 0);
477
+ if (chunk) {
478
+ startChunk = chunk - AES_CHUNK_SIZE;
479
+ bytesFetched = chunk;
480
+ firstBlockIsIV = true;
481
+ }
482
+ }
483
+ const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
484
+ const headersInit = options?.headers ? options.headers : undefined;
485
+ const headers = {
486
+ ...(headersInit
487
+ ? Array.isArray(headersInit)
488
+ ? Object.fromEntries(headersInit)
489
+ : headersInit
490
+ : {}),
491
+ Origin: DEFAULT_ORIGIN
492
+ };
493
+ if (startChunk || endChunk) {
494
+ headers.Range = `bytes=${startChunk}-`;
495
+ if (endChunk) {
496
+ headers.Range += endChunk;
497
+ }
498
+ }
499
+ // download the message
500
+ const fetched = await getHttpStream(downloadUrl, {
501
+ ...(options || {}),
502
+ headers
503
+ });
504
+ let remainingBytes = Buffer.from([]);
505
+ let aes;
506
+ const pushBytes = (bytes, push) => {
507
+ if (startByte || endByte) {
508
+ const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0);
509
+ const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0);
510
+ push(bytes.slice(start, end));
511
+ bytesFetched += bytes.length;
512
+ }
513
+ else {
514
+ push(bytes);
515
+ }
516
+ };
517
+ const output = new Transform({
518
+ transform(chunk, _, callback) {
519
+ let data = Buffer.concat([remainingBytes, chunk]);
520
+ const decryptLength = toSmallestChunkSize(data.length);
521
+ remainingBytes = data.slice(decryptLength);
522
+ data = data.slice(0, decryptLength);
523
+ if (!aes) {
524
+ let ivValue = iv;
525
+ if (firstBlockIsIV) {
526
+ ivValue = data.slice(0, AES_CHUNK_SIZE);
527
+ data = data.slice(AES_CHUNK_SIZE);
528
+ }
529
+ aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue);
530
+ // if an end byte that is not EOF is specified
531
+ // stop auto padding (PKCS7) -- otherwise throws an error for decryption
532
+ if (endByte) {
533
+ aes.setAutoPadding(false);
534
+ }
535
+ }
536
+ try {
537
+ pushBytes(aes.update(data), b => this.push(b));
538
+ callback();
539
+ }
540
+ catch (error) {
541
+ callback(error);
542
+ }
543
+ },
544
+ final(callback) {
545
+ try {
546
+ pushBytes(aes.final(), b => this.push(b));
547
+ callback();
548
+ }
549
+ catch (error) {
550
+ callback(error);
551
+ }
552
+ }
553
+ });
554
+ return fetched.pipe(output, { end: true });
555
+ };
556
+ export function extensionForMediaMessage(message) {
557
+ const getExtension = (mimetype) => mimetype.split(';')[0]?.split('/')[1];
558
+ const type = Object.keys(message)[0];
559
+ let extension;
560
+ if (type === 'locationMessage' || type === 'liveLocationMessage' || type === 'productMessage') {
561
+ extension = '.jpeg';
562
+ }
563
+ else {
564
+ const messageContent = message[type];
565
+ extension = getExtension(messageContent.mimetype);
566
+ }
567
+ return extension;
568
+ }
569
+ const isNodeRuntime = () => {
570
+ return (typeof process !== 'undefined' &&
571
+ process.versions?.node !== null &&
572
+ typeof process.versions.bun === 'undefined' &&
573
+ typeof globalThis.Deno === 'undefined');
574
+ };
575
+ export const uploadWithNodeHttp = async ({ url, filePath, headers, timeoutMs, agent }, redirectCount = 0) => {
576
+ if (redirectCount > 5) {
577
+ throw new Error('Too many redirects');
578
+ }
579
+ const parsedUrl = new URL(url);
580
+ const httpModule = parsedUrl.protocol === 'https:' ? await import('https') : await import('http');
581
+ // Get file size for Content-Length header (required for Node.js streaming)
582
+ const fileStats = await fs.stat(filePath);
583
+ const fileSize = fileStats.size;
584
+ return new Promise((resolve, reject) => {
585
+ const req = httpModule.request({
586
+ hostname: parsedUrl.hostname,
587
+ port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80),
588
+ path: parsedUrl.pathname + parsedUrl.search,
589
+ method: 'POST',
590
+ headers: {
591
+ ...headers,
592
+ 'Content-Length': fileSize
593
+ },
594
+ agent,
595
+ timeout: timeoutMs
596
+ }, res => {
597
+ // Handle redirects (3xx)
598
+ if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
599
+ res.resume(); // Consume response to free resources
600
+ const newUrl = new URL(res.headers.location, url).toString();
601
+ resolve(uploadWithNodeHttp({
602
+ url: newUrl,
603
+ filePath,
604
+ headers,
605
+ timeoutMs,
606
+ agent
607
+ }, redirectCount + 1));
608
+ return;
609
+ }
610
+ let body = '';
611
+ res.on('data', chunk => (body += chunk));
612
+ res.on('end', () => {
613
+ try {
614
+ resolve(JSON.parse(body));
615
+ }
616
+ catch {
617
+ resolve(undefined);
618
+ }
619
+ });
620
+ });
621
+ req.on('error', reject);
622
+ req.on('timeout', () => {
623
+ req.destroy();
624
+ reject(new Error('Upload timeout'));
625
+ });
626
+ const stream = createReadStream(filePath);
627
+ stream.pipe(req);
628
+ stream.on('error', err => {
629
+ req.destroy();
630
+ reject(err);
631
+ });
632
+ });
633
+ };
634
+ const uploadWithFetch = async ({ url, filePath, headers, timeoutMs, agent }) => {
635
+ // Convert Node.js Readable to Web ReadableStream
636
+ const nodeStream = createReadStream(filePath);
637
+ const webStream = Readable.toWeb(nodeStream);
638
+ const response = await fetch(url, {
639
+ dispatcher: agent,
640
+ method: 'POST',
641
+ body: webStream,
642
+ headers,
643
+ duplex: 'half',
644
+ signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined
645
+ });
646
+ try {
647
+ return (await response.json());
648
+ }
649
+ catch {
650
+ return undefined;
651
+ }
652
+ };
653
+ /**
654
+ * Uploads media to WhatsApp servers.
655
+ *
656
+ * ## Why we have two upload implementations:
657
+ *
658
+ * Node.js's native `fetch` (powered by undici) has a known bug where it buffers
659
+ * the entire request body in memory before sending, even when using streams.
660
+ * This causes memory issues with large files (e.g., 1GB file = 1GB+ memory usage).
661
+ * See: https://github.com/nodejs/undici/issues/4058
662
+ *
663
+ * Other runtimes (Bun, Deno, browsers) correctly stream the request body without
664
+ * buffering, so we can use the web-standard Fetch API there.
665
+ *
666
+ * ## Future considerations:
667
+ * Once the undici bug is fixed, we can simplify this to use only the Fetch API
668
+ * across all runtimes. Monitor the GitHub issue for updates.
669
+ */
670
+ const uploadMedia = async (params, logger) => {
671
+ if (isNodeRuntime()) {
672
+ logger?.debug('Using Node.js https module for upload (avoids undici buffering bug)');
673
+ return uploadWithNodeHttp(params);
674
+ }
675
+ else {
676
+ logger?.debug('Using web-standard Fetch API for upload');
677
+ return uploadWithFetch(params);
678
+ }
679
+ };
680
+ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
681
+ return async (filePath, { mediaType, fileEncSha256B64, timeoutMs, newsletter }) => {
682
+ // send a query JSON to obtain the url & auth token to upload our media
683
+ let uploadInfo = await refreshMediaConn(false);
684
+ let urls;
685
+ const hosts = [...customUploadHosts, ...uploadInfo.hosts];
686
+ fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
687
+ // Prepare common headers
688
+ const customHeaders = (() => {
689
+ const hdrs = options?.headers;
690
+ if (!hdrs)
691
+ return {};
692
+ return Array.isArray(hdrs) ? Object.fromEntries(hdrs) : hdrs;
693
+ })();
694
+ const headers = {
695
+ ...customHeaders,
696
+ 'Content-Type': 'application/octet-stream',
697
+ Origin: DEFAULT_ORIGIN
698
+ };
699
+ for (const { hostname } of hosts) {
700
+ logger.debug(`uploading to "${hostname}"`);
701
+ const auth = encodeURIComponent(uploadInfo.auth);
702
+ // Lia@Changes 06-02-26 --- Switch media path map for newsletter uploads
703
+ const mediaPathMap = newsletter ? NEWSLETTER_MEDIA_PATH_MAP : MEDIA_PATH_MAP
704
+ // Lia@Changes 20-03-26 --- Add server thumb for newsletter media
705
+ const serverThumb = newsletter ? '&server_thumb_gen=1' : ''
706
+ const url = `https://${hostname}${mediaPathMap[mediaType]}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}${serverThumb}`;
707
+ let result;
708
+ try {
709
+ result = await uploadMedia({
710
+ url,
711
+ filePath,
712
+ headers,
713
+ timeoutMs,
714
+ agent: fetchAgent
715
+ }, logger);
716
+ if (result?.url || result?.direct_path) {
717
+ urls = {
718
+ mediaUrl: result.url,
719
+ directPath: result.direct_path,
720
+ meta_hmac: result.meta_hmac,
721
+ fbid: result.fbid,
722
+ ts: result.ts,
723
+ thumbnailDirectPath: result.thumbnail_info?.thumbnail_direct_path,
724
+ thumbnailSha256: result.thumbnail_info?.thumbnail_sha256
725
+ };
726
+ break;
727
+ }
728
+ else {
729
+ uploadInfo = await refreshMediaConn(true);
730
+ throw new Error(`upload failed, reason: ${JSON.stringify(result)}`);
731
+ }
732
+ }
733
+ catch (error) {
734
+ const isLast = hostname === hosts[uploadInfo.hosts.length - 1]?.hostname;
735
+ logger.warn({ trace: error?.stack, uploadResult: result }, `Error in uploading to ${hostname} ${isLast ? '' : ', retrying...'}`);
736
+ }
737
+ }
738
+ if (!urls) {
739
+ throw new Boom('Media upload failed on all hosts', { statusCode: 500 });
740
+ }
741
+ return urls;
742
+ };
743
+ };
744
+ const getMediaRetryKey = (mediaKey) => {
745
+ return hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
746
+ };
747
+ /**
748
+ * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
749
+ */
750
+ export const encryptMediaRetryRequest = (key, mediaKey, meId) => {
751
+ const recp = { stanzaId: key.id };
752
+ const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish();
753
+ const iv = Crypto.randomBytes(12);
754
+ const retryKey = getMediaRetryKey(mediaKey);
755
+ const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id));
756
+ const req = {
757
+ tag: 'receipt',
758
+ attrs: {
759
+ id: key.id,
760
+ to: jidNormalizedUser(meId),
761
+ type: 'server-error'
762
+ },
763
+ content: [
764
+ // this encrypt node is actually pretty useless
765
+ // the media is returned even without this node
766
+ // keeping it here to maintain parity with WA Web
767
+ {
768
+ tag: 'encrypt',
769
+ attrs: {},
770
+ content: [
771
+ { tag: 'enc_p', attrs: {}, content: ciphertext },
772
+ { tag: 'enc_iv', attrs: {}, content: iv }
773
+ ]
774
+ },
775
+ {
776
+ tag: 'rmr',
777
+ attrs: {
778
+ jid: key.remoteJid,
779
+ from_me: (!!key.fromMe).toString(),
780
+ // @ts-ignore
781
+ participant: key.participant || undefined
782
+ }
783
+ }
784
+ ]
785
+ };
786
+ return req;
787
+ };
788
+ export const decodeMediaRetryNode = (node) => {
789
+ const rmrNode = getBinaryNodeChild(node, 'rmr');
790
+ const event = {
791
+ key: {
792
+ id: node.attrs.id,
793
+ remoteJid: rmrNode.attrs.jid,
794
+ fromMe: rmrNode.attrs.from_me === 'true',
795
+ participant: rmrNode.attrs.participant
796
+ }
797
+ };
798
+ const errorNode = getBinaryNodeChild(node, 'error');
799
+ if (errorNode) {
800
+ const errorCode = +errorNode.attrs.code;
801
+ event.error = new Boom(`Failed to re-upload media (${errorCode})`, {
802
+ data: errorNode.attrs,
803
+ statusCode: getStatusCodeForMediaRetry(errorCode)
804
+ });
805
+ }
806
+ else {
807
+ const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt');
808
+ const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p');
809
+ const iv = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv');
810
+ if (ciphertext && iv) {
811
+ event.media = { ciphertext, iv };
812
+ }
813
+ else {
814
+ event.error = new Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 });
815
+ }
816
+ }
817
+ return event;
818
+ };
819
+ export const decryptMediaRetryData = ({ ciphertext, iv }, mediaKey, msgId) => {
820
+ const retryKey = getMediaRetryKey(mediaKey);
821
+ const plaintext = aesDecryptGCM(ciphertext, retryKey, iv, Buffer.from(msgId));
822
+ return proto.MediaRetryNotification.decode(plaintext);
823
+ };
824
+ export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
825
+ const MEDIA_RETRY_STATUS_MAP = {
826
+ [proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
827
+ [proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
828
+ [proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
829
+ [proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
830
+ };