@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.
- package/LICENSE +1 -1
- package/README.md +924 -1299
- package/WAProto/index.js +22 -18
- package/lib/Defaults/baileys-version.json +6 -2
- package/lib/Defaults/index.js +173 -172
- package/lib/Signal/libsignal.js +395 -292
- package/lib/Signal/lid-mapping.js +264 -171
- package/lib/Socket/Client/index.js +2 -2
- package/lib/Socket/Client/types.js +10 -10
- package/lib/Socket/Client/websocket.js +45 -310
- package/lib/Socket/business.js +375 -375
- package/lib/Socket/chats.js +916 -963
- package/lib/Socket/communities.js +430 -430
- package/lib/Socket/groups.js +342 -342
- package/lib/Socket/index.js +21 -22
- package/lib/Socket/messages-recv.js +963 -743
- package/lib/Socket/messages-send.js +273 -321
- package/lib/Socket/mex.js +50 -50
- package/lib/Socket/newsletter.js +148 -148
- package/lib/Socket/nexus-handler.js +296 -247
- package/lib/Socket/registration.js +50 -33
- package/lib/Socket/socket.js +872 -1201
- package/lib/Store/index.js +5 -5
- package/lib/Store/make-cache-manager-store.js +81 -81
- package/lib/Store/make-in-memory-store.js +416 -416
- package/lib/Store/make-ordered-dictionary.js +81 -81
- package/lib/Store/object-repository.js +30 -30
- package/lib/Types/Auth.js +1 -1
- package/lib/Types/Bussines.js +1 -1
- package/lib/Types/Call.js +1 -1
- package/lib/Types/Chat.js +7 -7
- package/lib/Types/Contact.js +1 -1
- package/lib/Types/Events.js +1 -1
- package/lib/Types/GroupMetadata.js +1 -1
- package/lib/Types/Label.js +24 -24
- package/lib/Types/LabelAssociation.js +6 -6
- package/lib/Types/Message.js +10 -10
- package/lib/Types/Newsletter.js +37 -29
- package/lib/Types/Product.js +1 -1
- package/lib/Types/Signal.js +1 -1
- package/lib/Types/Socket.js +2 -2
- package/lib/Types/State.js +55 -12
- package/lib/Types/USync.js +1 -1
- package/lib/Types/index.js +25 -25
- package/lib/Utils/auth-utils.js +264 -256
- package/lib/Utils/baileys-event-stream.js +55 -55
- package/lib/Utils/browser-utils.js +27 -27
- package/lib/Utils/business.js +228 -230
- package/lib/Utils/chat-utils.js +726 -764
- package/lib/Utils/companion-reg-client-utils.js +34 -0
- package/lib/Utils/crypto.js +109 -135
- package/lib/Utils/decode-wa-message.js +342 -314
- package/lib/Utils/event-buffer.js +547 -547
- package/lib/Utils/generics.js +295 -297
- package/lib/Utils/history.js +91 -83
- package/lib/Utils/index.js +25 -20
- package/lib/Utils/key-store.js +17 -0
- package/lib/Utils/link-preview.js +107 -98
- package/lib/Utils/logger.js +2 -2
- package/lib/Utils/lt-hash.js +47 -47
- package/lib/Utils/make-mutex.js +39 -39
- package/lib/Utils/message-retry-manager.js +148 -148
- package/lib/Utils/messages-media.js +579 -535
- package/lib/Utils/messages.js +821 -706
- package/lib/Utils/noise-handler.js +255 -255
- package/lib/Utils/pre-key-manager.js +105 -105
- package/lib/Utils/process-message.js +430 -412
- package/lib/Utils/reporting-utils.js +155 -0
- package/lib/Utils/signal.js +191 -159
- package/lib/Utils/sync-action-utils.js +33 -0
- package/lib/Utils/tc-token-utils.js +162 -0
- package/lib/Utils/use-multi-file-auth-state.js +120 -120
- package/lib/Utils/validate-connection.js +194 -194
- package/lib/WABinary/constants.js +1306 -1300
- package/lib/WABinary/decode.js +237 -237
- package/lib/WABinary/encode.js +232 -232
- package/lib/WABinary/generic-utils.js +252 -211
- package/lib/WABinary/index.js +6 -5
- package/lib/WABinary/jid-utils.js +279 -95
- package/lib/WABinary/types.js +1 -1
- package/lib/WAM/BinaryInfo.js +9 -9
- package/lib/WAM/constants.js +22852 -22852
- package/lib/WAM/encode.js +149 -149
- package/lib/WAM/index.js +3 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -28
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -53
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -26
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -37
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -50
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -28
- package/lib/WAUSync/Protocols/index.js +4 -4
- package/lib/WAUSync/USyncQuery.js +93 -93
- package/lib/WAUSync/USyncUser.js +22 -22
- package/lib/WAUSync/index.js +3 -3
- package/lib/index.js +65 -66
- package/package.json +172 -143
- package/lib/Signal/Group/ciphertext-message.js +0 -12
- package/lib/Signal/Group/group-session-builder.js +0 -30
- package/lib/Signal/Group/group_cipher.js +0 -100
- package/lib/Signal/Group/index.js +0 -12
- package/lib/Signal/Group/keyhelper.js +0 -18
- package/lib/Signal/Group/sender-chain-key.js +0 -26
- package/lib/Signal/Group/sender-key-distribution-message.js +0 -63
- package/lib/Signal/Group/sender-key-message.js +0 -66
- package/lib/Signal/Group/sender-key-name.js +0 -48
- package/lib/Signal/Group/sender-key-record.js +0 -41
- package/lib/Signal/Group/sender-key-state.js +0 -84
- package/lib/Signal/Group/sender-message-key.js +0 -26
|
@@ -1,535 +1,579 @@
|
|
|
1
|
-
import { Boom } from '@hapi/boom'
|
|
2
|
-
import {
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
import('
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
export
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
if (
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
const
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
const
|
|
411
|
-
if (
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
if (
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
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
|
+
}
|