amiudmodz 5.0.0 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,4 +7,4 @@ if (major < 20) {
7
7
  ` Please upgrade to Node.js 20+ to proceed.\n`
8
8
  );
9
9
  process.exit(1);
10
- }
10
+ }
@@ -141,7 +141,7 @@ exports.MEDIA_HKDF_KEY_MAPPING = {
141
141
  exports.MEDIA_KEYS = Object.keys(exports.MEDIA_PATH_MAP);
142
142
  exports.MIN_PREKEY_COUNT = 5;
143
143
  exports.INITIAL_PREKEY_COUNT = 30;
144
- exports.UPLOAD_TIMEOUT = 600000;
144
+ exports.UPLOAD_TIMEOUT = 7200000; // 2 hours in ms
145
145
  exports.MIN_UPLOAD_INTERVAL = 5000;
146
146
 
147
147
  exports.TimeMs = {
@@ -400,23 +400,41 @@ const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequi
400
400
  logger === null || logger === void 0 ? void 0 : logger.debug('fetched media stream');
401
401
  let bodyPath;
402
402
  let didSaveToTmpPath = false;
403
+ let fileLength = 0;
404
+ const sha256 = Crypto.createHash('sha256');
405
+ const encWriteStream = new stream_1.PassThrough();
406
+
407
+ // if it's already a file, we can use its path
408
+ if (type === 'file') {
409
+ bodyPath = media.url;
410
+ } else if (saveOriginalFileIfRequired) {
411
+ bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.generateMessageID)());
412
+ didSaveToTmpPath = true;
413
+ }
414
+
415
+ const writeStream = bodyPath && !media.url ? (0, fs_1.createWriteStream)(bodyPath) : undefined;
416
+
403
417
  try {
404
- const buffer = await (0, exports.toBuffer)(stream);
405
- if (type === 'file') {
406
- bodyPath = media.url;
407
- }
408
- else if (saveOriginalFileIfRequired) {
409
- bodyPath = (0, path_1.join)(getTmpFilesDirectory(), mediaType + (0, generics_1.generateMessageID)());
410
- (0, fs_1.writeFileSync)(bodyPath, buffer);
411
- didSaveToTmpPath = true;
418
+ for await (const chunk of stream) {
419
+ fileLength += chunk.length;
420
+ sha256.update(chunk);
421
+ if (writeStream) {
422
+ if (!writeStream.write(chunk)) {
423
+ await (0, events_1.once)(writeStream, 'drain');
424
+ }
425
+ }
426
+ encWriteStream.write(chunk);
412
427
  }
413
- const fileLength = buffer.length;
414
- const fileSha256 = Crypto.createHash('sha256').update(buffer).digest();
415
- stream === null || stream === void 0 ? void 0 : stream.destroy();
428
+
429
+ encWriteStream.end();
430
+ writeStream?.end();
431
+
432
+ const fileSha256 = sha256.digest();
416
433
  logger === null || logger === void 0 ? void 0 : logger.debug('prepare stream data successfully');
434
+
417
435
  return {
418
436
  mediaKey: undefined,
419
- encWriteStream: buffer,
437
+ encWriteStream, // Now a stream, not a buffer
420
438
  fileLength,
421
439
  fileSha256,
422
440
  fileEncSha256: undefined,
@@ -425,8 +443,9 @@ const prepareStream = async (media, mediaType, { logger, saveOriginalFileIfRequi
425
443
  };
426
444
  }
427
445
  catch (error) {
428
-
429
446
  stream.destroy();
447
+ encWriteStream.destroy();
448
+ writeStream?.destroy();
430
449
  if (didSaveToTmpPath) {
431
450
  try {
432
451
  await fs_1.promises.unlink(bodyPath);
@@ -442,6 +461,7 @@ exports.prepareStream = prepareStream;
442
461
  const encryptedStream = async (media, mediaType, { logger, saveOriginalFileIfRequired, opts, isPtt, forceOpus } = {}) => {
443
462
  const { stream, type } = await (0, exports.getStream)(media, opts);
444
463
 
464
+ let finalStream = stream;
445
465
  let opusConverted = false;
446
466
  if (mediaType === 'audio' && (isPtt === true || forceOpus === true)) {
447
467
  try {
@@ -664,7 +684,7 @@ function extensionForMediaMessage(message) {
664
684
  }
665
685
  exports.extensionForMediaMessage = extensionForMediaMessage;
666
686
  const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options }, refreshMediaConn) => {
667
- return async (stream, { mediaType, fileEncSha256B64, newsletter, timeoutMs }) => {
687
+ return async (stream, { mediaType, fileEncSha256B64, newsletter, timeoutMs, fileLength }) => {
668
688
  var _a, _b;
669
689
  const { default: axios } = await import('axios');
670
690
 
@@ -683,9 +703,9 @@ const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, options },
683
703
  const url = `https://${hostname}${media}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
684
704
  let result;
685
705
  try {
686
- const bodyLength = Buffer.isBuffer(reqBody) ? reqBody.length : 0;
706
+ const bodyLength = fileLength || (Buffer.isBuffer(reqBody) ? reqBody.length : 0);
687
707
  if (maxContentLengthBytes && bodyLength > maxContentLengthBytes) {
688
- throw new boom_1.Boom(`Body too large for "${hostname}"`, { statusCode: 413 });
708
+ throw new boom_1.Boom(`Body too large for "${hostname}" (${bodyLength} > ${maxContentLengthBytes})`, { statusCode: 413 });
689
709
  }
690
710
  const body = await axios.post(url, reqBody, {
691
711
  ...options,
@@ -165,7 +165,7 @@ const prepareWAMessageMedia = async (message, options) => {
165
165
 
166
166
  const [{ mediaUrl, directPath, handle }] = await Promise.all([
167
167
  (async () => {
168
- const result = await options.upload(encWriteStream, { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs });
168
+ const result = await options.upload(encWriteStream, { fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs, fileLength });
169
169
  logger === null || logger === void 0 ? void 0 : logger.debug({ mediaType, cacheableKey }, 'uploaded media');
170
170
  return result;
171
171
  })(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amiudmodz",
3
- "version": "5.0.0",
3
+ "version": "5.0.1",
4
4
  "description": "WhatsApp Baileys mod Powered by UDMODZ",
5
5
  "keywords": [
6
6
  "whatsapp",