@nexustechpro/baileys 2.0.2 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +924 -1299
  3. package/WAProto/index.js +22 -18
  4. package/lib/Defaults/baileys-version.json +6 -2
  5. package/lib/Defaults/index.js +173 -172
  6. package/lib/Signal/libsignal.js +395 -292
  7. package/lib/Signal/lid-mapping.js +264 -171
  8. package/lib/Socket/Client/index.js +2 -2
  9. package/lib/Socket/Client/types.js +10 -10
  10. package/lib/Socket/Client/websocket.js +45 -310
  11. package/lib/Socket/business.js +375 -375
  12. package/lib/Socket/chats.js +916 -963
  13. package/lib/Socket/communities.js +430 -430
  14. package/lib/Socket/groups.js +342 -342
  15. package/lib/Socket/index.js +21 -22
  16. package/lib/Socket/messages-recv.js +963 -743
  17. package/lib/Socket/messages-send.js +273 -321
  18. package/lib/Socket/mex.js +50 -50
  19. package/lib/Socket/newsletter.js +148 -148
  20. package/lib/Socket/nexus-handler.js +296 -247
  21. package/lib/Socket/registration.js +50 -33
  22. package/lib/Socket/socket.js +872 -1201
  23. package/lib/Store/index.js +5 -5
  24. package/lib/Store/make-cache-manager-store.js +81 -81
  25. package/lib/Store/make-in-memory-store.js +416 -416
  26. package/lib/Store/make-ordered-dictionary.js +81 -81
  27. package/lib/Store/object-repository.js +30 -30
  28. package/lib/Types/Auth.js +1 -1
  29. package/lib/Types/Bussines.js +1 -1
  30. package/lib/Types/Call.js +1 -1
  31. package/lib/Types/Chat.js +7 -7
  32. package/lib/Types/Contact.js +1 -1
  33. package/lib/Types/Events.js +1 -1
  34. package/lib/Types/GroupMetadata.js +1 -1
  35. package/lib/Types/Label.js +24 -24
  36. package/lib/Types/LabelAssociation.js +6 -6
  37. package/lib/Types/Message.js +10 -10
  38. package/lib/Types/Newsletter.js +37 -29
  39. package/lib/Types/Product.js +1 -1
  40. package/lib/Types/Signal.js +1 -1
  41. package/lib/Types/Socket.js +2 -2
  42. package/lib/Types/State.js +55 -12
  43. package/lib/Types/USync.js +1 -1
  44. package/lib/Types/index.js +25 -25
  45. package/lib/Utils/auth-utils.js +264 -256
  46. package/lib/Utils/baileys-event-stream.js +55 -55
  47. package/lib/Utils/browser-utils.js +27 -27
  48. package/lib/Utils/business.js +228 -230
  49. package/lib/Utils/chat-utils.js +726 -764
  50. package/lib/Utils/companion-reg-client-utils.js +34 -0
  51. package/lib/Utils/crypto.js +109 -135
  52. package/lib/Utils/decode-wa-message.js +342 -314
  53. package/lib/Utils/event-buffer.js +547 -547
  54. package/lib/Utils/generics.js +295 -297
  55. package/lib/Utils/history.js +91 -83
  56. package/lib/Utils/index.js +25 -20
  57. package/lib/Utils/key-store.js +17 -0
  58. package/lib/Utils/link-preview.js +107 -98
  59. package/lib/Utils/logger.js +2 -2
  60. package/lib/Utils/lt-hash.js +47 -47
  61. package/lib/Utils/make-mutex.js +39 -39
  62. package/lib/Utils/message-retry-manager.js +148 -148
  63. package/lib/Utils/messages-media.js +579 -535
  64. package/lib/Utils/messages.js +821 -706
  65. package/lib/Utils/noise-handler.js +255 -255
  66. package/lib/Utils/pre-key-manager.js +105 -105
  67. package/lib/Utils/process-message.js +430 -412
  68. package/lib/Utils/reporting-utils.js +155 -0
  69. package/lib/Utils/signal.js +191 -159
  70. package/lib/Utils/sync-action-utils.js +33 -0
  71. package/lib/Utils/tc-token-utils.js +162 -0
  72. package/lib/Utils/use-multi-file-auth-state.js +120 -120
  73. package/lib/Utils/validate-connection.js +194 -194
  74. package/lib/WABinary/constants.js +1306 -1300
  75. package/lib/WABinary/decode.js +237 -237
  76. package/lib/WABinary/encode.js +232 -232
  77. package/lib/WABinary/generic-utils.js +252 -211
  78. package/lib/WABinary/index.js +6 -5
  79. package/lib/WABinary/jid-utils.js +279 -95
  80. package/lib/WABinary/types.js +1 -1
  81. package/lib/WAM/BinaryInfo.js +9 -9
  82. package/lib/WAM/constants.js +22852 -22852
  83. package/lib/WAM/encode.js +149 -149
  84. package/lib/WAM/index.js +3 -3
  85. package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
  86. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
  87. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
  88. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
  89. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
  90. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
  91. package/lib/WAUSync/Protocols/index.js +4 -4
  92. package/lib/WAUSync/USyncQuery.js +93 -93
  93. package/lib/WAUSync/USyncUser.js +22 -22
  94. package/lib/WAUSync/index.js +3 -3
  95. package/lib/index.js +65 -66
  96. package/package.json +172 -143
  97. package/lib/Signal/Group/ciphertext-message.js +0 -12
  98. package/lib/Signal/Group/group-session-builder.js +0 -30
  99. package/lib/Signal/Group/group_cipher.js +0 -100
  100. package/lib/Signal/Group/index.js +0 -12
  101. package/lib/Signal/Group/keyhelper.js +0 -18
  102. package/lib/Signal/Group/sender-chain-key.js +0 -26
  103. package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
  104. package/lib/Signal/Group/sender-key-message.js +0 -66
  105. package/lib/Signal/Group/sender-key-name.js +0 -48
  106. package/lib/Signal/Group/sender-key-record.js +0 -41
  107. package/lib/Signal/Group/sender-key-state.js +0 -84
  108. package/lib/Signal/Group/sender-message-key.js +0 -26
@@ -1,535 +1,579 @@
1
- import { Boom } from '@hapi/boom';
2
- import { exec } from 'child_process';
3
- import * as Crypto from 'crypto';
4
- import { once } from 'events';
5
- import { createReadStream, createWriteStream, promises as fs } 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 } 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
-
16
- export const getImageProcessingLibrary = async () => {
17
- const [jimp, sharp] = await Promise.all([
18
- import('jimp').catch(() => null),
19
- import('sharp').catch(() => null)
20
- ]);
21
- if (sharp) return { sharp };
22
- if (jimp) return { jimp };
23
- throw new Boom('No image processing library available');
24
- };
25
-
26
- export const hkdfInfoKey = (type) => `WhatsApp ${MEDIA_HKDF_KEY_MAPPING[type]} Keys`;
27
-
28
- export const getRawMediaUploadData = async (media, mediaType, logger) => {
29
- const { stream } = await getStream(media);
30
- const hasher = Crypto.createHash('sha256');
31
- const filePath = join(tmpdir(), mediaType + generateMessageIDV2());
32
- const fileWriteStream = createWriteStream(filePath);
33
- let fileLength = 0;
34
- try {
35
- for await (const data of stream) {
36
- fileLength += data.length;
37
- hasher.update(data);
38
- if (!fileWriteStream.write(data)) await once(fileWriteStream, 'drain');
39
- }
40
- fileWriteStream.end();
41
- await once(fileWriteStream, 'finish');
42
- stream.destroy();
43
- logger?.debug('hashed data for raw upload');
44
- return { filePath, fileSha256: hasher.digest(), fileLength };
45
- } catch (error) {
46
- fileWriteStream.destroy();
47
- stream.destroy();
48
- try { await fs.unlink(filePath); } catch { }
49
- throw error;
50
- }
51
- };
52
-
53
- export async function getMediaKeys(buffer, mediaType) {
54
- if (!buffer) throw new Boom('Cannot derive from empty media key');
55
- if (typeof buffer === 'string') buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64');
56
- const expandedMediaKey = hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) });
57
- return {
58
- iv: expandedMediaKey.slice(0, 16),
59
- cipherKey: expandedMediaKey.slice(16, 48),
60
- macKey: expandedMediaKey.slice(48, 80)
61
- };
62
- }
63
-
64
- const extractVideoThumb = (path, destPath, time, size) => new Promise((resolve, reject) => {
65
- exec(`ffmpeg -ss ${time} -i ${path} -y -vf scale=${size.width}:-1 -vframes 1 -f image2 ${destPath}`, err => err ? reject(err) : resolve());
66
- });
67
-
68
- export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
69
- if (bufferOrFilePath instanceof Readable) bufferOrFilePath = await toBuffer(bufferOrFilePath);
70
- const lib = await getImageProcessingLibrary();
71
- if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
72
- const img = lib.sharp.default(bufferOrFilePath);
73
- const dimensions = await img.metadata();
74
- const buffer = await img.resize(width).jpeg({ quality: 95 }).toBuffer();
75
- return { buffer, original: { width: dimensions.width, height: dimensions.height } };
76
- } else if ('jimp' in lib && typeof lib.jimp?.Jimp === 'object') {
77
- const jimp = await lib.jimp.Jimp.read(bufferOrFilePath);
78
- const buffer = await jimp.resize({ w: width, mode: lib.jimp.ResizeStrategy.BILINEAR }).getBuffer('image/jpeg', { quality: 95 });
79
- return { buffer, original: { width: jimp.width, height: jimp.height } };
80
- }
81
- throw new Boom('No image processing library available');
82
- };
83
-
84
- export const encodeBase64EncodedStringForUpload = (b64) => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''));
85
-
86
- export const generateProfilePicture = async (mediaUpload) => {
87
- let bufferOrFilePath = Buffer.isBuffer(mediaUpload) ? mediaUpload : 'url' in mediaUpload ? mediaUpload.url.toString() : await toBuffer(mediaUpload.stream);
88
- const lib = await getImageProcessingLibrary();
89
- if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
90
- const img = await lib.sharp.default(bufferOrFilePath).resize(720, 720, { fit: 'inside' }).jpeg({ quality: 50 }).toBuffer();
91
- return { img };
92
- } else if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
93
- const { read, MIME_JPEG } = lib.jimp;
94
- const image = await read(bufferOrFilePath);
95
- const min = image.getWidth(), max = image.getHeight();
96
- const img = await image.crop(0, 0, min, max).scaleToFit(720, 720).getBufferAsync(MIME_JPEG);
97
- return { img };
98
- }
99
- throw new Boom('No image processing library available');
100
- };
101
-
102
- export const mediaMessageSHA256B64 = (message) => {
103
- const media = Object.values(message)[0];
104
- return media?.fileSha256 && Buffer.from(media.fileSha256).toString('base64');
105
- };
106
-
107
- export async function getAudioDuration(buffer) {
108
- const musicMetadata = await import('music-metadata');
109
- if (Buffer.isBuffer(buffer)) return (await musicMetadata.parseBuffer(buffer, undefined, { duration: true })).format.duration;
110
- if (typeof buffer === 'string') return (await musicMetadata.parseFile(buffer, { duration: true })).format.duration;
111
- return (await musicMetadata.parseStream(buffer, undefined, { duration: true })).format.duration;
112
- }
113
-
114
- export async function getAudioWaveform(buffer, logger) {
115
- try {
116
- const { default: decoder } = await import('audio-decode');
117
- let audioData = Buffer.isBuffer(buffer) ? buffer : typeof buffer === 'string' ? await toBuffer(createReadStream(buffer)) : await toBuffer(buffer);
118
- const audioBuffer = await decoder(audioData);
119
- const rawData = audioBuffer.getChannelData(0);
120
- const samples = 64, blockSize = Math.floor(rawData.length / samples);
121
- const filteredData = [];
122
- for (let i = 0; i < samples; i++) {
123
- let sum = 0;
124
- for (let j = 0; j < blockSize; j++) sum += Math.abs(rawData[i * blockSize + j]);
125
- filteredData.push(sum / blockSize);
126
- }
127
- const multiplier = Math.pow(Math.max(...filteredData), -1);
128
- return new Uint8Array(filteredData.map(n => Math.floor(100 * n * multiplier)));
129
- } catch (e) {
130
- logger?.debug('Failed to generate waveform: ' + e);
131
- return new Uint8Array([0, 99, 0, 99, 0, 99, 0, 99, 88, 99, 0, 99, 0, 55, 0, 99, 0, 99, 0, 99, 0, 99, 0, 99, 88, 99, 0, 99, 0, 55, 0, 99]);
132
- }
133
- }
134
-
135
- const convertToOpusBuffer = (buffer, logger) => new Promise((resolve, reject) => {
136
- const ffmpeg = exec('ffmpeg -i pipe:0 -c:a libopus -b:a 64k -vbr on -compression_level 10 -frame_duration 20 -application voip -f ogg pipe:1');
137
- const chunks = [];
138
- ffmpeg.stdin.write(buffer);
139
- ffmpeg.stdin.end();
140
- ffmpeg.stdout.on('data', chunk => chunks.push(chunk));
141
- ffmpeg.stderr.on('data', () => { });
142
- ffmpeg.on('close', code => code === 0 ? resolve(Buffer.concat(chunks)) : reject(new Error(`FFmpeg Opus conversion exited with code ${code}`)));
143
- ffmpeg.on('error', reject);
144
- });
145
-
146
- const convertToMp4Buffer = (buffer, logger) => new Promise((resolve, reject) => {
147
- const ffmpeg = exec('ffmpeg -i pipe:0 -c:v libx264 -preset veryfast -crf 23 -c:a aac -b:a 128k -movflags faststart -f mp4 pipe:1');
148
- const chunks = [];
149
- ffmpeg.stdin.write(buffer);
150
- ffmpeg.stdin.end();
151
- ffmpeg.stdout.on('data', chunk => chunks.push(chunk));
152
- ffmpeg.stderr.on('data', () => { });
153
- ffmpeg.on('close', code => code === 0 ? resolve(Buffer.concat(chunks)) : reject(new Error(`FFmpeg MP4 conversion exited with code ${code}`)));
154
- ffmpeg.on('error', reject);
155
- });
156
-
157
- export const toReadable = (buffer) => {
158
- const readable = new Readable({ read: () => { } });
159
- readable.push(buffer);
160
- readable.push(null);
161
- return readable;
162
- };
163
-
164
- export const toBuffer = async (stream) => {
165
- const chunks = [];
166
- for await (const chunk of stream) chunks.push(chunk);
167
- stream.destroy();
168
- return Buffer.concat(chunks);
169
- };
170
-
171
- export const getStream = async (item, opts) => {
172
- if (!item) throw new Boom('Item is required for getStream', { statusCode: 400 });
173
- if (Buffer.isBuffer(item)) return { stream: toReadable(item), type: 'buffer' };
174
- if (item?.stream?.pipe) return { stream: item.stream, type: 'readable' };
175
- if (item?.pipe) return { stream: item, type: 'readable' };
176
- if (item && typeof item === 'object' && 'url' in item) {
177
- const urlStr = item.url.toString();
178
- if (Buffer.isBuffer(item.url)) return { stream: toReadable(item.url), type: 'buffer' };
179
- if (urlStr.startsWith('data:')) return { stream: toReadable(Buffer.from(urlStr.split(',')[1], 'base64')), type: 'buffer' };
180
- if (urlStr.startsWith('http')) return { stream: await getHttpStream(item.url, opts), type: 'remote' };
181
- return { stream: createReadStream(item.url), type: 'file' };
182
- }
183
- if (typeof item === 'string') {
184
- if (item.startsWith('data:')) return { stream: toReadable(Buffer.from(item.split(',')[1], 'base64')), type: 'buffer' };
185
- if (item.startsWith('http')) return { stream: await getHttpStream(item, opts), type: 'remote' };
186
- return { stream: createReadStream(item), type: 'file' };
187
- }
188
- throw new Boom(`Invalid input type for getStream: ${typeof item}`, { statusCode: 400 });
189
- };
190
-
191
- export async function generateThumbnail(file, mediaType, options) {
192
- let thumbnail, originalImageDimensions;
193
- if (mediaType === 'image') {
194
- const { buffer, original } = await extractImageThumb(file);
195
- thumbnail = buffer.toString('base64');
196
- if (original.width && original.height) originalImageDimensions = original;
197
- } else if (mediaType === 'video') {
198
- const imgFilename = join(tmpdir(), generateMessageIDV2() + '.jpg');
199
- try {
200
- await extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 });
201
- thumbnail = (await fs.readFile(imgFilename)).toString('base64');
202
- await fs.unlink(imgFilename);
203
- } catch (err) {
204
- options.logger?.debug('could not generate video thumb: ' + err);
205
- }
206
- }
207
- return { thumbnail, originalImageDimensions };
208
- }
209
-
210
- export const getHttpStream = async (url, options = {}) => {
211
- const response = await fetch(url.toString(), { dispatcher: options.dispatcher, method: 'GET', headers: options.headers });
212
- if (!response.ok) throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } });
213
- const body = response.body;
214
- if (body && typeof body === 'object' && 'pipeTo' in body && typeof body.pipeTo === 'function') return Readable.fromWeb(body);
215
- if (body && typeof body.pipe === 'function' && typeof body.read === 'function') return body;
216
- throw new Error('Response body is not a readable stream');
217
- };
218
-
219
- export const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, convertVideo } = {}) => {
220
- const { stream, type } = await getStream(media, opts);
221
- logger?.debug('fetched media stream');
222
- let buffer = await toBuffer(stream);
223
- if (mediaType === 'video' && convertVideo) {
224
- try { buffer = await convertToMp4Buffer(buffer, logger); logger?.debug('converted video to mp4 for newsletter'); }
225
- catch (e) { logger?.error('failed to convert video for newsletter:', e); }
226
- }
227
- let bodyPath, didSaveToTmpPath = false;
228
- try {
229
- if (type === 'file') bodyPath = media.url;
230
- else if (saveOriginalFileIfRequired) {
231
- bodyPath = join(tmpdir(), mediaType + generateMessageIDV2());
232
- await fs.writeFile(bodyPath, buffer);
233
- didSaveToTmpPath = true;
234
- }
235
- return { mediaKey: undefined, encWriteStream: buffer, fileLength: buffer.length, fileSha256: Crypto.createHash('sha256').update(buffer).digest(), fileEncSha256: undefined, bodyPath, didSaveToTmpPath };
236
- } catch (error) {
237
- if (didSaveToTmpPath && bodyPath) try { await fs.unlink(bodyPath); } catch { }
238
- throw error;
239
- }
240
- };
241
-
242
- export const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, mediaKey: providedMediaKey, isPtt, forceOpus, convertVideo } = {}) => {
243
- const { stream, type } = await getStream(media, opts);
244
- let finalStream = stream, opusConverted = false;
245
-
246
- if (mediaType === 'audio' && (isPtt === true || forceOpus === true)) {
247
- try {
248
- finalStream = toReadable(await convertToOpusBuffer(await toBuffer(stream), logger));
249
- opusConverted = true;
250
- logger?.debug('converted audio to Opus');
251
- } catch (error) {
252
- logger?.error('failed to convert audio to Opus, using original');
253
- finalStream = (await getStream(media, opts)).stream;
254
- }
255
- }
256
-
257
- if (mediaType === 'video' && convertVideo === true) {
258
- try {
259
- finalStream = toReadable(await convertToMp4Buffer(await toBuffer(finalStream), logger));
260
- logger?.debug('converted video to mp4');
261
- } catch (error) {
262
- logger?.error('failed to convert video to mp4, using original');
263
- finalStream = (await getStream(media, opts)).stream;
264
- }
265
- }
266
-
267
- const mediaKey = providedMediaKey || Crypto.randomBytes(32);
268
- const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType);
269
- const encFilePath = join(tmpdir(), mediaType + generateMessageIDV2() + '-enc');
270
- const encFileWriteStream = createWriteStream(encFilePath);
271
- let originalFileStream, originalFilePath;
272
-
273
- if (saveOriginalFileIfRequired) {
274
- originalFilePath = join(tmpdir(), mediaType + generateMessageIDV2() + '-original');
275
- originalFileStream = createWriteStream(originalFilePath);
276
- }
277
-
278
- let fileLength = 0;
279
- const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
280
- const hmac = Crypto.createHmac('sha256', macKey).update(iv);
281
- const sha256Plain = Crypto.createHash('sha256');
282
- const sha256Enc = Crypto.createHash('sha256');
283
-
284
- try {
285
- for await (const data of finalStream) {
286
- fileLength += data.length;
287
- if (type === 'remote' && opts?.maxContentLength && fileLength > opts.maxContentLength) throw new Boom('content length exceeded', { data: { media, type } });
288
- if (originalFileStream && !originalFileStream.write(data)) await once(originalFileStream, 'drain');
289
- sha256Plain.update(data);
290
- const encrypted = aes.update(data);
291
- sha256Enc.update(encrypted);
292
- hmac.update(encrypted);
293
- encFileWriteStream.write(encrypted);
294
- }
295
- const finalData = aes.final();
296
- sha256Enc.update(finalData);
297
- hmac.update(finalData);
298
- encFileWriteStream.write(finalData);
299
- const mac = hmac.digest().slice(0, 10);
300
- sha256Enc.update(mac);
301
- encFileWriteStream.write(mac);
302
- encFileWriteStream.end();
303
- originalFileStream?.end?.();
304
- finalStream.destroy();
305
- logger?.debug('encrypted data successfully');
306
- return { mediaKey, bodyPath: originalFilePath, encFilePath, mac, fileEncSha256: sha256Enc.digest(), fileSha256: sha256Plain.digest(), fileLength, opusConverted };
307
- } catch (error) {
308
- encFileWriteStream.destroy();
309
- originalFileStream?.destroy?.();
310
- aes.destroy();
311
- hmac.destroy();
312
- sha256Plain.destroy();
313
- sha256Enc.destroy();
314
- finalStream.destroy();
315
- try { await fs.unlink(encFilePath); if (originalFilePath) await fs.unlink(originalFilePath); } catch (err) { logger?.error({ err }, 'failed deleting tmp files'); }
316
- throw error;
317
- }
318
- };
319
-
320
- const DEF_HOST = 'mmg.whatsapp.net';
321
- const AES_CHUNK_SIZE = 16;
322
- const toSmallestChunkSize = (num) => Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE;
323
-
324
- export const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`;
325
-
326
- export const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
327
- const isValidMediaUrl = url?.startsWith('https://mmg.whatsapp.net/');
328
- const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath);
329
- if (!downloadUrl) throw new Boom('No valid media URL or directPath present', { statusCode: 400 });
330
- return downloadEncryptedContent(downloadUrl, await getMediaKeys(mediaKey, type), opts);
331
- };
332
-
333
- export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
334
- let bytesFetched = 0, startChunk = 0, firstBlockIsIV = false;
335
- if (startByte) {
336
- const chunk = toSmallestChunkSize(startByte || 0);
337
- if (chunk) { startChunk = chunk - AES_CHUNK_SIZE; bytesFetched = chunk; firstBlockIsIV = true; }
338
- }
339
- const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined;
340
- const headers = { ...(options?.headers ? (Array.isArray(options.headers) ? Object.fromEntries(options.headers) : options.headers) : {}), Origin: DEFAULT_ORIGIN };
341
- if (startChunk || endChunk) headers.Range = `bytes=${startChunk}-${endChunk || ''}`;
342
-
343
- const fetched = await getHttpStream(downloadUrl, { ...(options || {}), headers });
344
- let remainingBytes = Buffer.from([]), aes;
345
-
346
- const pushBytes = (bytes, push) => {
347
- if (startByte || endByte) {
348
- const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0);
349
- const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0);
350
- push(bytes.slice(start, end));
351
- bytesFetched += bytes.length;
352
- } else {
353
- push(bytes);
354
- }
355
- };
356
-
357
- const output = new Transform({
358
- transform(chunk, _, callback) {
359
- let data = Buffer.concat([remainingBytes, chunk]);
360
- const decryptLength = toSmallestChunkSize(data.length);
361
- remainingBytes = data.slice(decryptLength);
362
- data = data.slice(0, decryptLength);
363
- if (!aes) {
364
- let ivValue = iv;
365
- if (firstBlockIsIV) { ivValue = data.slice(0, AES_CHUNK_SIZE); data = data.slice(AES_CHUNK_SIZE); }
366
- aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue);
367
- if (endByte) aes.setAutoPadding(false);
368
- }
369
- try { pushBytes(aes.update(data), b => this.push(b)); callback(); } catch (error) { callback(error); }
370
- },
371
- final(callback) {
372
- try { pushBytes(aes.final(), b => this.push(b)); callback(); } catch (error) { callback(error); }
373
- }
374
- });
375
- return fetched.pipe(output, { end: true });
376
- };
377
-
378
- export function extensionForMediaMessage(message) {
379
- const getExtension = (mimetype) => mimetype.split(';')[0]?.split('/')[1];
380
- const type = Object.keys(message)[0];
381
- if (type === 'locationMessage' || type === 'liveLocationMessage' || type === 'productMessage') return '.jpeg';
382
- return getExtension(message[type].mimetype);
383
- }
384
-
385
- export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
386
- return async (stream, { mediaType, fileEncSha256B64, newsletter, timeoutMs }) => {
387
- // Accepts Buffer, file path, Node stream, Web ReadableStream, or async iterable.
388
- // File paths are streamed directly from disk no RAM cost for large files.
389
- const toUploadBody = async (input) => {
390
- if (!input) throw new Boom('Upload input is null or undefined', { statusCode: 400 });
391
- if (Buffer.isBuffer(input)) return input;
392
- if (typeof input === 'string') return createReadStream(input);
393
- if (typeof ReadableStream !== 'undefined' && input instanceof ReadableStream) return Readable.fromWeb(input);
394
- if (typeof input.pipe === 'function' || typeof input[Symbol.asyncIterator] === 'function') return input;
395
- throw new Boom(`Unsupported upload input type: ${Object.prototype.toString.call(input)}`, { statusCode: 400 });
396
- };
397
-
398
- let reqBody;
399
- try { reqBody = await toUploadBody(stream); }
400
- catch (err) { logger?.error({ err: err.message }, 'failed to prepare upload body'); throw err; }
401
-
402
- fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64);
403
-
404
- let media = MEDIA_PATH_MAP[mediaType];
405
- if (newsletter) media = media?.replace('/mms/', '/newsletter/newsletter-');
406
- if (!media) throw new Boom(`No media path found for type: ${mediaType}`, { statusCode: 400 });
407
-
408
- // Force-refresh auth upfront to avoid stale token failures
409
- let uploadInfo = await refreshMediaConn(true);
410
- const hosts = [...(customUploadHosts ?? []), ...(uploadInfo.hosts ?? [])];
411
- if (!hosts.length) throw new Boom('No upload hosts available', { statusCode: 503 });
412
-
413
- const MAX_RETRIES = 2;
414
- let urls, lastError;
415
-
416
- for (const { hostname, maxContentLengthBytes } of hosts) {
417
- for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
418
- try {
419
- if (attempt > 1) {
420
- uploadInfo = await refreshMediaConn(true);
421
- reqBody = await toUploadBody(stream);
422
- }
423
-
424
- if (maxContentLengthBytes && Buffer.isBuffer(reqBody) && reqBody.length > maxContentLengthBytes) {
425
- logger?.warn({ hostname, maxContentLengthBytes }, 'body too large for host, skipping');
426
- break;
427
- }
428
-
429
- const auth = encodeURIComponent(uploadInfo.auth);
430
- const url = `https://${hostname}${media}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
431
- const controller = new AbortController();
432
- const timer = timeoutMs ? setTimeout(() => controller.abort(), timeoutMs) : null;
433
-
434
- let response;
435
- try {
436
- response = await fetch(url, {
437
- dispatcher: fetchAgent,
438
- method: 'POST',
439
- body: reqBody,
440
- headers: {
441
- ...(Array.isArray(options?.headers) ? Object.fromEntries(options.headers) : (options?.headers ?? {})),
442
- 'Content-Type': 'application/octet-stream',
443
- Origin: DEFAULT_ORIGIN
444
- },
445
- duplex: 'half',
446
- signal: controller.signal
447
- });
448
- } finally {
449
- if (timer) clearTimeout(timer);
450
- }
451
-
452
- let result;
453
- try { result = await response.json(); } catch { result = null; }
454
-
455
- if (result?.url || result?.directPath) {
456
- urls = { mediaUrl: result.url, directPath: result.direct_path, handle: result.handle };
457
- break;
458
- }
459
-
460
- lastError = new Error(`${hostname} rejected upload (HTTP ${response.status}): ${JSON.stringify(result)}`);
461
- logger?.warn({ hostname, attempt, status: response.status, result }, 'upload rejected');
462
-
463
- } catch (err) {
464
- lastError = err;
465
- logger?.warn({ hostname, attempt, err: err.message, timedOut: err.name === 'AbortError' }, 'upload attempt failed');
466
- if (attempt < MAX_RETRIES) await new Promise(r => setTimeout(r, 500 * attempt));
467
- }
468
- }
469
- if (urls) break;
470
- }
471
-
472
- if (!urls) {
473
- const msg = `Media upload failed on all hosts. Last error: ${lastError?.message ?? 'unknown'}`;
474
- logger?.error({ hosts: hosts.map(h => h.hostname), lastError: lastError?.message }, msg);
475
- throw new Boom(msg, { statusCode: 500, data: { lastError: lastError?.message } });
476
- }
477
-
478
- return urls;
479
- };
480
- };
481
-
482
- const getMediaRetryKey = (mediaKey) => hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
483
-
484
- export const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
485
- const recp = { stanzaId: key.id };
486
- const recpBuffer = proto.ServerErrorReceipt.encode(recp).finish();
487
- const iv = Crypto.randomBytes(12);
488
- const retryKey = await getMediaRetryKey(mediaKey);
489
- const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id));
490
- return {
491
- tag: 'receipt',
492
- attrs: { id: key.id, to: jidNormalizedUser(meId), type: 'server-error' },
493
- content: [
494
- {
495
- tag: 'encrypt', attrs: {}, content: [
496
- { tag: 'enc_p', attrs: {}, content: ciphertext },
497
- { tag: 'enc_iv', attrs: {}, content: iv }
498
- ]
499
- },
500
- { tag: 'rmr', attrs: { jid: key.remoteJid, from_me: (!!key.fromMe).toString(), participant: key.participant } }
501
- ]
502
- };
503
- };
504
-
505
- export const decodeMediaRetryNode = (node) => {
506
- const rmrNode = getBinaryNodeChild(node, 'rmr');
507
- const event = {
508
- key: { id: node.attrs.id, remoteJid: rmrNode.attrs.jid, fromMe: rmrNode.attrs.from_me === 'true', participant: rmrNode.attrs.participant }
509
- };
510
- const errorNode = getBinaryNodeChild(node, 'error');
511
- if (errorNode) {
512
- event.error = new Boom(`Failed to re-upload media (${+errorNode.attrs.code})`, { data: errorNode.attrs, statusCode: getStatusCodeForMediaRetry(+errorNode.attrs.code) });
513
- } else {
514
- const encryptedInfoNode = getBinaryNodeChild(node, 'encrypt');
515
- const ciphertext = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_p');
516
- const iv = getBinaryNodeChildBuffer(encryptedInfoNode, 'enc_iv');
517
- if (ciphertext && iv) event.media = { ciphertext, iv };
518
- else event.error = new Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 });
519
- }
520
- return event;
521
- };
522
-
523
- export const decryptMediaRetryData = async ({ ciphertext, iv }, mediaKey, msgId) => {
524
- const plaintext = aesDecryptGCM(ciphertext, await getMediaRetryKey(mediaKey), iv, Buffer.from(msgId));
525
- return proto.MediaRetryNotification.decode(plaintext);
526
- };
527
-
528
- export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code];
529
-
530
- const MEDIA_RETRY_STATUS_MAP = {
531
- [proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
532
- [proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
533
- [proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
534
- [proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
535
- };
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 } 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 } 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
+
16
+ // ─── IMAGE PROCESSING ─────────────────────────────────────────────────────────
17
+ export const getImageProcessingLibrary = async () => {
18
+ const [jimp, sharp] = await Promise.all([
19
+ import('jimp').catch(() => null),
20
+ import('sharp').catch(() => null)
21
+ ])
22
+ if (sharp) return { sharp }
23
+ if (jimp) return { jimp }
24
+ throw new Boom('No image processing library available')
25
+ }
26
+
27
+ // ─── FFMPEG ───────────────────────────────────────────────────────────────────
28
+ let ffmpegPathResolved = null
29
+ const getFfmpegPath = async () => {
30
+ if (ffmpegPathResolved) return ffmpegPathResolved
31
+ try {
32
+ const { default: staticPath } = await import('ffmpeg-static')
33
+ if (staticPath) { ffmpegPathResolved = staticPath; return staticPath }
34
+ } catch { }
35
+ ffmpegPathResolved = 'ffmpeg'
36
+ return 'ffmpeg'
37
+ }
38
+
39
+ // ─── HKDF ─────────────────────────────────────────────────────────────────────
40
+ export const hkdfInfoKey = (type) => `WhatsApp ${MEDIA_HKDF_KEY_MAPPING[type]} Keys`
41
+
42
+ export const getMediaKeys = async (buffer, mediaType) => {
43
+ if (!buffer) throw new Boom('Cannot derive from empty media key')
44
+ if (typeof buffer === 'string') buffer = Buffer.from(buffer.replace('data:;base64,', ''), 'base64')
45
+ const expandedMediaKey = await hkdf(buffer, 112, { info: hkdfInfoKey(mediaType) })
46
+ return {
47
+ iv: expandedMediaKey.slice(0, 16),
48
+ cipherKey: expandedMediaKey.slice(16, 48),
49
+ macKey: expandedMediaKey.slice(48, 80)
50
+ }
51
+ }
52
+
53
+ // ─── RAW UPLOAD ───────────────────────────────────────────────────────────────
54
+ export const getRawMediaUploadData = async (media, mediaType, logger) => {
55
+ const { stream } = await getStream(media)
56
+ const hasher = Crypto.createHash('sha256')
57
+ const filePath = join(tmpdir(), mediaType + generateMessageIDV2())
58
+ const fileWriteStream = createWriteStream(filePath)
59
+ let fileLength = 0
60
+ try {
61
+ for await (const data of stream) {
62
+ fileLength += data.length
63
+ hasher.update(data)
64
+ if (!fileWriteStream.write(data)) await once(fileWriteStream, 'drain')
65
+ }
66
+ fileWriteStream.end()
67
+ await once(fileWriteStream, 'finish')
68
+ stream.destroy()
69
+ logger?.debug('hashed data for raw upload')
70
+ return { filePath, fileSha256: hasher.digest(), fileLength }
71
+ } catch (error) {
72
+ fileWriteStream.destroy()
73
+ stream.destroy()
74
+ try { await fs.unlink(filePath) } catch { }
75
+ throw error
76
+ }
77
+ }
78
+
79
+ // ─── THUMBNAILS ───────────────────────────────────────────────────────────────
80
+ const extractVideoThumb = async (path, destPath, time, size) => {
81
+ const ffmpegPath = await getFfmpegPath()
82
+ return new Promise((resolve, reject) => {
83
+ const ff = spawn(ffmpegPath, ['-ss', time, '-i', path, '-y', '-vf', `scale=${size.width}:-1`, '-vframes', '1', '-f', 'image2', destPath])
84
+ ff.on('close', code => code === 0 ? resolve() : reject(new Error(`FFmpeg thumb exited with code ${code}`)))
85
+ ff.on('error', reject)
86
+ })
87
+ }
88
+
89
+ export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
90
+ if (bufferOrFilePath instanceof Readable) bufferOrFilePath = await toBuffer(bufferOrFilePath)
91
+ const lib = await getImageProcessingLibrary()
92
+ if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
93
+ const img = lib.sharp.default(bufferOrFilePath)
94
+ const dimensions = await img.metadata()
95
+ const buffer = await img.resize(width).jpeg({ quality: 95 }).toBuffer()
96
+ return { buffer, original: { width: dimensions.width, height: dimensions.height } }
97
+ }
98
+ if ('jimp' in lib && typeof lib.jimp?.Jimp === 'object') {
99
+ const jimp = await lib.jimp.Jimp.read(bufferOrFilePath)
100
+ const buffer = await jimp.resize({ w: width, mode: lib.jimp.ResizeStrategy.BILINEAR }).getBuffer('image/jpeg', { quality: 95 })
101
+ return { buffer, original: { width: jimp.width, height: jimp.height } }
102
+ }
103
+ throw new Boom('No image processing library available')
104
+ }
105
+
106
+ export async function generateThumbnail(file, mediaType, options) {
107
+ let thumbnail, originalImageDimensions
108
+ if (mediaType === 'image') {
109
+ const { buffer, original } = await extractImageThumb(file)
110
+ thumbnail = buffer.toString('base64')
111
+ if (original.width && original.height) originalImageDimensions = original
112
+ } else if (mediaType === 'video') {
113
+ const imgFilename = join(tmpdir(), generateMessageIDV2() + '.jpg')
114
+ try {
115
+ await extractVideoThumb(file, imgFilename, '00:00:00', { width: 32, height: 32 })
116
+ thumbnail = (await fs.readFile(imgFilename)).toString('base64')
117
+ await fs.unlink(imgFilename)
118
+ } catch (err) {
119
+ options.logger?.debug('could not generate video thumb: ' + err)
120
+ }
121
+ }
122
+ return { thumbnail, originalImageDimensions }
123
+ }
124
+
125
+ // ─── PROFILE PICTURE ──────────────────────────────────────────────────────────
126
+ export const encodeBase64EncodedStringForUpload = (b64) => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''))
127
+
128
+ export const generateProfilePicture = async (mediaUpload) => {
129
+ const bufferOrFilePath = Buffer.isBuffer(mediaUpload) ? mediaUpload : 'url' in mediaUpload ? mediaUpload.url.toString() : await toBuffer(mediaUpload.stream)
130
+ const lib = await getImageProcessingLibrary()
131
+ if ('sharp' in lib && typeof lib.sharp?.default === 'function') {
132
+ const img = await lib.sharp.default(bufferOrFilePath).resize(720, 720, { fit: 'inside' }).jpeg({ quality: 50 }).toBuffer()
133
+ return { img }
134
+ }
135
+ if ('jimp' in lib && typeof lib.jimp?.read === 'function') {
136
+ const { read, MIME_JPEG } = lib.jimp
137
+ const image = await read(bufferOrFilePath)
138
+ const img = await image.crop(0, 0, image.getWidth(), image.getHeight()).scaleToFit(720, 720).getBufferAsync(MIME_JPEG)
139
+ return { img }
140
+ }
141
+ throw new Boom('No image processing library available')
142
+ }
143
+
144
+ // ─── AUDIO ────────────────────────────────────────────────────────────────────
145
+ export const mediaMessageSHA256B64 = (message) => {
146
+ const media = Object.values(message)[0]
147
+ return media?.fileSha256 && Buffer.from(media.fileSha256).toString('base64')
148
+ }
149
+
150
+ export async function getAudioDuration(buffer) {
151
+ const musicMetadata = await import('music-metadata')
152
+ if (Buffer.isBuffer(buffer)) return (await musicMetadata.parseBuffer(buffer, undefined, { duration: true })).format.duration
153
+ if (typeof buffer === 'string') return (await musicMetadata.parseFile(buffer, { duration: true })).format.duration
154
+ return (await musicMetadata.parseStream(buffer, undefined, { duration: true })).format.duration
155
+ }
156
+
157
+ export async function getAudioWaveform(buffer, logger) {
158
+ const bars = 64
159
+ const fallback = new Uint8Array([0, 99, 0, 99, 0, 99, 0, 99, 88, 99, 0, 99, 0, 55, 0, 99, 0, 99, 0, 99, 0, 99, 0, 99, 88, 99, 0, 99, 0, 55, 0, 99, 0, 99, 0, 99, 0, 99, 88, 99, 0, 99, 0, 55, 0, 99, 0, 99, 0, 99, 0, 99, 0, 99, 88, 99, 0, 99, 0, 55, 0, 99, 0, 99])
160
+ try {
161
+ // prefer fluent-ffmpeg for broad format support (mp3, m4a, ogg, opus, wav, etc.)
162
+ // falls back to audio-decode for lightweight envs without ffmpeg
163
+ let rawPCM = null
164
+ try {
165
+ const ffmpegModule = await import('fluent-ffmpeg')
166
+ const ff = ffmpegModule.default || ffmpegModule
167
+ const ffmpegPath = await getFfmpegPath()
168
+ let input
169
+ if (Buffer.isBuffer(buffer) || typeof buffer === 'string') {
170
+ input = buffer
171
+ } else {
172
+ input = await toBuffer(buffer)
173
+ }
174
+ rawPCM = await new Promise((resolve, reject) => {
175
+ const chunks = []
176
+ ff(input)
177
+ .setFfmpegPath(ffmpegPath)
178
+ .audioChannels(1)
179
+ .audioFrequency(16000)
180
+ .format('s16le')
181
+ .on('error', reject)
182
+ .on('end', () => resolve(Buffer.concat(chunks)))
183
+ .pipe()
184
+ .on('data', chunk => chunks.push(chunk))
185
+ })
186
+ if (!rawPCM?.length) throw new Error('empty PCM output')
187
+ const samples = Math.floor(rawPCM.length / 2)
188
+ const amplitudes = new Array(samples)
189
+ for (let i = 0; i < samples; i++) amplitudes[i] = Math.abs(rawPCM.readInt16LE(i * 2)) / 32768
190
+ const blockSize = Math.max(1, Math.floor(amplitudes.length / bars))
191
+ const avg = Array.from({ length: bars }, (_, i) => {
192
+ const start = i * blockSize
193
+ const end = i === bars - 1 ? amplitudes.length : Math.min(start + blockSize, amplitudes.length)
194
+ const block = amplitudes.slice(start, end)
195
+ return block.length ? block.reduce((a, b) => a + b, 0) / block.length : 0
196
+ })
197
+ const max = Math.max(...avg, 0.0001)
198
+ return new Uint8Array(avg.map(v => Math.max(0, Math.min(100, Math.round((v / max) * 100)))))
199
+ } catch {
200
+ // fluent-ffmpeg unavailable or failed try audio-decode
201
+ const { default: decoder } = await import('audio-decode')
202
+ let audioData = Buffer.isBuffer(buffer) ? buffer : typeof buffer === 'string' ? await toBuffer(createReadStream(buffer)) : await toBuffer(buffer)
203
+ const audioBuffer = await decoder(audioData)
204
+ const rawData = audioBuffer.getChannelData(0)
205
+ const blockSize = Math.floor(rawData.length / bars)
206
+ const filteredData = Array.from({ length: bars }, (_, i) => {
207
+ let sum = 0
208
+ for (let j = 0; j < blockSize; j++) sum += Math.abs(rawData[i * blockSize + j])
209
+ return sum / blockSize
210
+ })
211
+ const multiplier = Math.pow(Math.max(...filteredData), -1)
212
+ return new Uint8Array(filteredData.map(n => Math.floor(100 * n * multiplier)))
213
+ }
214
+ } catch (e) {
215
+ logger?.debug({ trace: e?.stack || e }, 'failed to generate waveform, using fallback')
216
+ return fallback
217
+ }
218
+ }
219
+
220
+ // ─── FFMPEG CONVERTERS ────────────────────────────────────────────────────────
221
+ const convertToOpusBuffer = async (buffer, logger) => {
222
+ const ffmpegPath = await getFfmpegPath()
223
+ const inputPath = join(tmpdir(), 'opus-in-' + generateMessageIDV2())
224
+ await fs.writeFile(inputPath, buffer)
225
+ try {
226
+ return await new Promise((resolve, reject) => {
227
+ const ff = spawn(ffmpegPath, ['-y', '-i', inputPath, '-c:a', 'libopus', '-b:a', '64k', '-vbr', 'on', '-compression_level', '10', '-frame_duration', '20', '-application', 'voip', '-f', 'ogg', 'pipe:1'], { stdio: ['ignore', 'pipe', 'pipe'] })
228
+ const chunks = []
229
+ ff.stdout.on('data', chunk => chunks.push(chunk))
230
+ ff.stderr.on('data', () => { })
231
+ ff.on('close', code => code === 0 ? resolve(Buffer.concat(chunks)) : reject(new Error(`FFmpeg Opus exited with code ${code}`)))
232
+ ff.on('error', reject)
233
+ })
234
+ } finally {
235
+ try { await fs.unlink(inputPath) } catch { }
236
+ }
237
+ }
238
+
239
+ const convertToMp4Buffer = async (buffer, logger) => {
240
+ const ffmpegPath = await getFfmpegPath()
241
+ return new Promise((resolve, reject) => {
242
+ const ff = spawn(ffmpegPath, ['-i', 'pipe:0', '-c:v', 'libx264', '-preset', 'veryfast', '-crf', '23', '-c:a', 'aac', '-b:a', '128k', '-movflags', 'faststart', '-f', 'mp4', 'pipe:1'], { stdio: ['pipe', 'pipe', 'pipe'] })
243
+ const chunks = []
244
+ ff.stdin.write(buffer)
245
+ ff.stdin.end()
246
+ ff.stdout.on('data', chunk => chunks.push(chunk))
247
+ ff.stderr.on('data', () => { })
248
+ ff.on('close', code => code === 0 ? resolve(Buffer.concat(chunks)) : reject(new Error(`FFmpeg MP4 exited with code ${code}`)))
249
+ ff.on('error', reject)
250
+ })
251
+ }
252
+
253
+ // ─── STREAM UTILS ─────────────────────────────────────────────────────────────
254
+ export const toReadable = (buffer) => {
255
+ const readable = new Readable({ read: () => { } })
256
+ readable.push(buffer)
257
+ readable.push(null)
258
+ return readable
259
+ }
260
+
261
+ export const toBuffer = async (stream) => {
262
+ const chunks = []
263
+ for await (const chunk of stream) chunks.push(chunk)
264
+ stream.destroy()
265
+ return Buffer.concat(chunks)
266
+ }
267
+
268
+ export const getStream = async (item, opts) => {
269
+ if (!item) throw new Boom('Item is required for getStream', { statusCode: 400 })
270
+ if (Buffer.isBuffer(item)) return { stream: toReadable(item), type: 'buffer' }
271
+ if (item?.stream?.pipe) return { stream: item.stream, type: 'readable' }
272
+ if (item?.pipe) return { stream: item, type: 'readable' }
273
+ if (item && typeof item === 'object' && 'url' in item) {
274
+ const urlStr = item.url.toString()
275
+ if (Buffer.isBuffer(item.url)) return { stream: toReadable(item.url), type: 'buffer' }
276
+ if (urlStr.startsWith('data:')) return { stream: toReadable(Buffer.from(urlStr.split(',')[1], 'base64')), type: 'buffer' }
277
+ if (urlStr.startsWith('http')) return { stream: await getHttpStream(item.url, opts), type: 'remote' }
278
+ return { stream: createReadStream(item.url), type: 'file' }
279
+ }
280
+ if (typeof item === 'string') {
281
+ if (item.startsWith('data:')) return { stream: toReadable(Buffer.from(item.split(',')[1], 'base64')), type: 'buffer' }
282
+ if (item.startsWith('http')) return { stream: await getHttpStream(item, opts), type: 'remote' }
283
+ return { stream: createReadStream(item), type: 'file' }
284
+ }
285
+ throw new Boom(`Invalid input type for getStream: ${typeof item}`, { statusCode: 400 })
286
+ }
287
+
288
+ export const getHttpStream = async (url, options = {}) => {
289
+ const response = await fetch(url.toString(), { dispatcher: options.dispatcher, method: 'GET', headers: options.headers })
290
+ if (!response.ok) throw new Boom(`Failed to fetch stream from ${url}`, { statusCode: response.status, data: { url } })
291
+ const body = response.body
292
+ if (body && typeof body === 'object' && 'pipeTo' in body && typeof body.pipeTo === 'function') return Readable.fromWeb(body)
293
+ if (body && typeof body.pipe === 'function' && typeof body.read === 'function') return body
294
+ throw new Error('Response body is not a readable stream')
295
+ }
296
+
297
+ // ─── ENCRYPT / PREPARE STREAM ─────────────────────────────────────────────────
298
+ export const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, convertVideo } = {}) => {
299
+ const { stream, type } = await getStream(media, opts)
300
+ logger?.debug('fetched media stream')
301
+ let buffer = await toBuffer(stream)
302
+ if (mediaType === 'video' && convertVideo) {
303
+ try { buffer = await convertToMp4Buffer(buffer, logger); logger?.debug('converted video to mp4') }
304
+ catch (e) { logger?.error('failed to convert video:', e) }
305
+ }
306
+ let bodyPath, didSaveToTmpPath = false
307
+ try {
308
+ if (type === 'file') bodyPath = media.url
309
+ else if (saveOriginalFileIfRequired) {
310
+ bodyPath = join(tmpdir(), mediaType + generateMessageIDV2())
311
+ await fs.writeFile(bodyPath, buffer)
312
+ didSaveToTmpPath = true
313
+ }
314
+ return { mediaKey: undefined, encWriteStream: buffer, fileLength: buffer.length, fileSha256: Crypto.createHash('sha256').update(buffer).digest(), fileEncSha256: undefined, bodyPath, didSaveToTmpPath }
315
+ } catch (error) {
316
+ if (didSaveToTmpPath && bodyPath) try { await fs.unlink(bodyPath) } catch { }
317
+ throw error
318
+ }
319
+ }
320
+
321
+ export const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, mediaKey: providedMediaKey, isPtt, forceOpus, convertVideo } = {}) => {
322
+ const { stream, type } = await getStream(media, opts)
323
+ let finalStream = stream, opusConverted = false
324
+ if (mediaType === 'audio' && (isPtt === true || forceOpus === true)) {
325
+ try {
326
+ finalStream = toReadable(await convertToOpusBuffer(await toBuffer(stream), logger))
327
+ opusConverted = true
328
+ logger?.debug('converted audio to Opus')
329
+ } catch (error) {
330
+ logger?.error('failed to convert audio to Opus, using original')
331
+ finalStream = (await getStream(media, opts)).stream
332
+ }
333
+ }
334
+ if (mediaType === 'video' && convertVideo === true) {
335
+ try {
336
+ finalStream = toReadable(await convertToMp4Buffer(await toBuffer(finalStream), logger))
337
+ logger?.debug('converted video to mp4')
338
+ } catch (error) {
339
+ logger?.error('failed to convert video to mp4, using original')
340
+ finalStream = (await getStream(media, opts)).stream
341
+ }
342
+ }
343
+ const mediaKey = providedMediaKey || Crypto.randomBytes(32)
344
+ const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType)
345
+ const encFilePath = join(tmpdir(), mediaType + generateMessageIDV2() + '-enc')
346
+ const encFileWriteStream = createWriteStream(encFilePath)
347
+ let originalFileStream, originalFilePath
348
+ if (saveOriginalFileIfRequired) {
349
+ originalFilePath = join(tmpdir(), mediaType + generateMessageIDV2() + '-original')
350
+ originalFileStream = createWriteStream(originalFilePath)
351
+ }
352
+ let fileLength = 0
353
+ const aes = Crypto.createCipheriv('aes-256-cbc', cipherKey, iv)
354
+ const hmac = Crypto.createHmac('sha256', macKey).update(iv)
355
+ const sha256Plain = Crypto.createHash('sha256')
356
+ const sha256Enc = Crypto.createHash('sha256')
357
+ try {
358
+ for await (const data of finalStream) {
359
+ fileLength += data.length
360
+ if (type === 'remote' && opts?.maxContentLength && fileLength > opts.maxContentLength) throw new Boom('content length exceeded', { data: { media, type } })
361
+ if (originalFileStream && !originalFileStream.write(data)) await once(originalFileStream, 'drain')
362
+ sha256Plain.update(data)
363
+ const encrypted = aes.update(data)
364
+ sha256Enc.update(encrypted)
365
+ hmac.update(encrypted)
366
+ encFileWriteStream.write(encrypted)
367
+ }
368
+ const finalData = aes.final()
369
+ sha256Enc.update(finalData)
370
+ hmac.update(finalData)
371
+ encFileWriteStream.write(finalData)
372
+ const mac = hmac.digest().slice(0, 10)
373
+ sha256Enc.update(mac)
374
+ encFileWriteStream.write(mac)
375
+ encFileWriteStream.end()
376
+ originalFileStream?.end?.()
377
+ finalStream.destroy()
378
+ logger?.debug('encrypted data successfully')
379
+ return { mediaKey, bodyPath: originalFilePath, encFilePath, mac, fileEncSha256: sha256Enc.digest(), fileSha256: sha256Plain.digest(), fileLength, opusConverted }
380
+ } catch (error) {
381
+ encFileWriteStream.destroy()
382
+ originalFileStream?.destroy?.()
383
+ aes.destroy()
384
+ hmac.destroy()
385
+ sha256Plain.destroy()
386
+ sha256Enc.destroy()
387
+ finalStream.destroy()
388
+ try { await fs.unlink(encFilePath); if (originalFilePath) await fs.unlink(originalFilePath) } catch (err) { logger?.error({ err }, 'failed deleting tmp files') }
389
+ throw error
390
+ }
391
+ }
392
+
393
+ // ─── DOWNLOAD ─────────────────────────────────────────────────────────────────
394
+ const DEF_HOST = 'mmg.whatsapp.net'
395
+ const AES_CHUNK_SIZE = 16
396
+ const toSmallestChunkSize = (num) => Math.floor(num / AES_CHUNK_SIZE) * AES_CHUNK_SIZE
397
+
398
+ export const getUrlFromDirectPath = (directPath) => `https://${DEF_HOST}${directPath}`
399
+
400
+ export const downloadContentFromMessage = async ({ mediaKey, directPath, url }, type, opts = {}) => {
401
+ const isValidMediaUrl = url?.startsWith('https://mmg.whatsapp.net/')
402
+ const downloadUrl = isValidMediaUrl ? url : getUrlFromDirectPath(directPath)
403
+ if (!downloadUrl) throw new Boom('No valid media URL or directPath present', { statusCode: 400 })
404
+ return downloadEncryptedContent(downloadUrl, await getMediaKeys(mediaKey, type), opts)
405
+ }
406
+
407
+ export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, { startByte, endByte, options } = {}) => {
408
+ let bytesFetched = 0, startChunk = 0, firstBlockIsIV = false
409
+ if (startByte) {
410
+ const chunk = toSmallestChunkSize(startByte || 0)
411
+ if (chunk) { startChunk = chunk - AES_CHUNK_SIZE; bytesFetched = chunk; firstBlockIsIV = true }
412
+ }
413
+ const endChunk = endByte ? toSmallestChunkSize(endByte || 0) + AES_CHUNK_SIZE : undefined
414
+ const headers = { ...(options?.headers ? (Array.isArray(options.headers) ? Object.fromEntries(options.headers) : options.headers) : {}), Origin: DEFAULT_ORIGIN }
415
+ if (startChunk || endChunk) headers.Range = `bytes=${startChunk}-${endChunk || ''}`
416
+ const fetched = await getHttpStream(downloadUrl, { ...(options || {}), headers })
417
+ let remainingBytes = Buffer.from([]), aes
418
+ const pushBytes = (bytes, push) => {
419
+ if (startByte || endByte) {
420
+ const start = bytesFetched >= startByte ? undefined : Math.max(startByte - bytesFetched, 0)
421
+ const end = bytesFetched + bytes.length < endByte ? undefined : Math.max(endByte - bytesFetched, 0)
422
+ push(bytes.slice(start, end))
423
+ bytesFetched += bytes.length
424
+ } else {
425
+ push(bytes)
426
+ }
427
+ }
428
+ const output = new Transform({
429
+ transform(chunk, _, callback) {
430
+ let data = Buffer.concat([remainingBytes, chunk])
431
+ const decryptLength = toSmallestChunkSize(data.length)
432
+ remainingBytes = data.slice(decryptLength)
433
+ data = data.slice(0, decryptLength)
434
+ if (!aes) {
435
+ let ivValue = iv
436
+ if (firstBlockIsIV) { ivValue = data.slice(0, AES_CHUNK_SIZE); data = data.slice(AES_CHUNK_SIZE) }
437
+ aes = Crypto.createDecipheriv('aes-256-cbc', cipherKey, ivValue)
438
+ if (endByte) aes.setAutoPadding(false)
439
+ }
440
+ try { pushBytes(aes.update(data), b => this.push(b)); callback() } catch (error) { callback(error) }
441
+ },
442
+ final(callback) {
443
+ try { pushBytes(aes.final(), b => this.push(b)); callback() } catch (error) { callback(error) }
444
+ }
445
+ })
446
+ return fetched.pipe(output, { end: true })
447
+ }
448
+
449
+ // ─── UPLOAD ───────────────────────────────────────────────────────────────────
450
+ export function extensionForMediaMessage(message) {
451
+ const getExtension = (mimetype) => mimetype.split(';')[0]?.split('/')[1]
452
+ const type = Object.keys(message)[0]
453
+ if (type === 'locationMessage' || type === 'liveLocationMessage' || type === 'productMessage') return '.jpeg'
454
+ return getExtension(message[type].mimetype)
455
+ }
456
+
457
+ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
458
+ return async (stream, { mediaType, fileEncSha256B64, newsletter, timeoutMs }) => {
459
+ const toUploadBody = async (input) => {
460
+ if (!input) throw new Boom('Upload input is null or undefined', { statusCode: 400 })
461
+ if (Buffer.isBuffer(input)) return input
462
+ if (typeof input === 'string') return createReadStream(input)
463
+ if (typeof ReadableStream !== 'undefined' && input instanceof ReadableStream) return Readable.fromWeb(input)
464
+ if (typeof input.pipe === 'function' || typeof input[Symbol.asyncIterator] === 'function') return input
465
+ throw new Boom(`Unsupported upload input type: ${Object.prototype.toString.call(input)}`, { statusCode: 400 })
466
+ }
467
+ let reqBody
468
+ try { reqBody = await toUploadBody(stream) }
469
+ catch (err) { logger?.error({ err: err.message }, 'failed to prepare upload body'); throw err }
470
+ fileEncSha256B64 = encodeBase64EncodedStringForUpload(fileEncSha256B64)
471
+ let media = MEDIA_PATH_MAP[mediaType]
472
+ if (newsletter) media = media?.replace('/mms/', '/newsletter/newsletter-')
473
+ if (!media) throw new Boom(`No media path found for type: ${mediaType}`, { statusCode: 400 })
474
+ let uploadInfo = await refreshMediaConn(false)
475
+ const hosts = [...(customUploadHosts ?? []), ...(uploadInfo.hosts ?? [])]
476
+ if (!hosts.length) throw new Boom('No upload hosts available', { statusCode: 503 })
477
+ const MAX_RETRIES = 2
478
+ let urls, lastError
479
+ for (const { hostname, maxContentLengthBytes } of hosts) {
480
+ for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
481
+ try {
482
+ if (attempt > 1) { uploadInfo = await refreshMediaConn(true); reqBody = await toUploadBody(stream) }
483
+ if (maxContentLengthBytes && Buffer.isBuffer(reqBody) && reqBody.length > maxContentLengthBytes) {
484
+ logger?.warn({ hostname, maxContentLengthBytes }, 'body too large for host, skipping')
485
+ break
486
+ }
487
+ const auth = encodeURIComponent(uploadInfo.auth)
488
+ const url = `https://${hostname}${media}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`
489
+ const controller = new AbortController()
490
+ const timer = timeoutMs ? setTimeout(() => controller.abort(), timeoutMs) : null
491
+ let response
492
+ try {
493
+ response = await fetch(url, {
494
+ dispatcher: fetchAgent,
495
+ method: 'POST',
496
+ body: reqBody,
497
+ headers: {
498
+ ...(Array.isArray(options?.headers) ? Object.fromEntries(options.headers) : (options?.headers ?? {})),
499
+ 'Content-Type': 'application/octet-stream',
500
+ Origin: DEFAULT_ORIGIN
501
+ },
502
+ duplex: 'half',
503
+ signal: controller.signal
504
+ })
505
+ } finally {
506
+ if (timer) clearTimeout(timer)
507
+ }
508
+ let result
509
+ try { result = await response.json() } catch { result = null }
510
+ if (result?.url || result?.directPath) {
511
+ urls = { mediaUrl: result.url, directPath: result.direct_path, handle: result.handle }
512
+ break
513
+ }
514
+ lastError = new Error(`${hostname} rejected upload (HTTP ${response.status}): ${JSON.stringify(result)}`)
515
+ logger?.warn({ hostname, attempt, status: response.status, result }, 'upload rejected')
516
+ } catch (err) {
517
+ lastError = err
518
+ logger?.warn({ hostname, attempt, err: err.message, timedOut: err.name === 'AbortError' }, 'upload attempt failed')
519
+ if (attempt < MAX_RETRIES) await new Promise(r => setTimeout(r, 500 * attempt))
520
+ }
521
+ }
522
+ if (urls) break
523
+ }
524
+ if (!urls) {
525
+ const msg = `Media upload failed on all hosts. Last error: ${lastError?.message ?? 'unknown'}`
526
+ logger?.error({ hosts: hosts.map(h => h.hostname), lastError: lastError?.message }, msg)
527
+ throw new Boom(msg, { statusCode: 500, data: { lastError: lastError?.message } })
528
+ }
529
+ return urls
530
+ }
531
+ }
532
+
533
+ // ─── MEDIA RETRY ──────────────────────────────────────────────────────────────
534
+ const getMediaRetryKey = (mediaKey) => hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' })
535
+
536
+ export const encryptMediaRetryRequest = async (key, mediaKey, meId) => {
537
+ const recpBuffer = proto.ServerErrorReceipt.encode({ stanzaId: key.id }).finish()
538
+ const iv = Crypto.randomBytes(12)
539
+ const retryKey = await getMediaRetryKey(mediaKey)
540
+ const ciphertext = aesEncryptGCM(recpBuffer, retryKey, iv, Buffer.from(key.id))
541
+ return {
542
+ tag: 'receipt',
543
+ attrs: { id: key.id, to: jidNormalizedUser(meId), type: 'server-error' },
544
+ content: [
545
+ { tag: 'encrypt', attrs: {}, content: [{ tag: 'enc_p', attrs: {}, content: ciphertext }, { tag: 'enc_iv', attrs: {}, content: iv }] },
546
+ { tag: 'rmr', attrs: { jid: key.remoteJid, from_me: (!!key.fromMe).toString(), participant: key.participant } }
547
+ ]
548
+ }
549
+ }
550
+
551
+ export const decodeMediaRetryNode = (node) => {
552
+ const rmrNode = getBinaryNodeChild(node, 'rmr')
553
+ const event = { key: { id: node.attrs.id, remoteJid: rmrNode.attrs.jid, fromMe: rmrNode.attrs.from_me === 'true', participant: rmrNode.attrs.participant } }
554
+ const errorNode = getBinaryNodeChild(node, 'error')
555
+ if (errorNode) {
556
+ event.error = new Boom(`Failed to re-upload media (${+errorNode.attrs.code})`, { data: errorNode.attrs, statusCode: getStatusCodeForMediaRetry(+errorNode.attrs.code) })
557
+ } else {
558
+ const encNode = getBinaryNodeChild(node, 'encrypt')
559
+ const ciphertext = getBinaryNodeChildBuffer(encNode, 'enc_p')
560
+ const iv = getBinaryNodeChildBuffer(encNode, 'enc_iv')
561
+ if (ciphertext && iv) event.media = { ciphertext, iv }
562
+ else event.error = new Boom('Failed to re-upload media (missing ciphertext)', { statusCode: 404 })
563
+ }
564
+ return event
565
+ }
566
+
567
+ export const decryptMediaRetryData = async ({ ciphertext, iv }, mediaKey, msgId) => {
568
+ const plaintext = aesDecryptGCM(ciphertext, await getMediaRetryKey(mediaKey), iv, Buffer.from(msgId))
569
+ return proto.MediaRetryNotification.decode(plaintext)
570
+ }
571
+
572
+ export const getStatusCodeForMediaRetry = (code) => MEDIA_RETRY_STATUS_MAP[code]
573
+
574
+ const MEDIA_RETRY_STATUS_MAP = {
575
+ [proto.MediaRetryNotification.ResultType.SUCCESS]: 200,
576
+ [proto.MediaRetryNotification.ResultType.DECRYPTION_ERROR]: 412,
577
+ [proto.MediaRetryNotification.ResultType.NOT_FOUND]: 404,
578
+ [proto.MediaRetryNotification.ResultType.GENERAL_ERROR]: 418
579
+ }