@fvckerss/baileys 1.0.13 → 1.0.14

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.
@@ -523,63 +523,162 @@ const isNodeRuntime = () => {
523
523
  typeof process.versions.bun === 'undefined' &&
524
524
  typeof globalThis.Deno === 'undefined');
525
525
  };
526
- export const uploadWithNodeHttp = async ({ url, filePath, headers, timeoutMs, agent }, redirectCount = 0) => {
526
+ export const uploadWithNodeHttp = async (
527
+ { url, filePath, headers, timeoutMs, agent },
528
+ redirectCount = 0
529
+ ) => {
527
530
  if (redirectCount > 5) {
528
531
  throw new Error('Too many redirects');
529
532
  }
533
+
530
534
  const parsedUrl = new URL(url);
531
- const httpModule = parsedUrl.protocol === 'https:' ? await import('https') : await import('http');
532
- // Get file size for Content-Length header (required for Node.js streaming)
533
- const fileStats = await fs.stat(filePath);
534
- const fileSize = fileStats.size;
535
+
536
+ const httpModule =
537
+ parsedUrl.protocol === 'https:'
538
+ ? await import('https')
539
+ : await import('http');
540
+
535
541
  return new Promise((resolve, reject) => {
536
- const req = httpModule.request({
537
- hostname: parsedUrl.hostname,
538
- port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80),
539
- path: parsedUrl.pathname + parsedUrl.search,
540
- method: 'POST',
541
- headers: {
542
- ...headers,
543
- 'Content-Length': fileSize
542
+ const req = httpModule.request(
543
+ {
544
+ hostname: parsedUrl.hostname,
545
+ port:
546
+ parsedUrl.port ||
547
+ (parsedUrl.protocol === 'https:' ? 443 : 80),
548
+ path: parsedUrl.pathname + parsedUrl.search,
549
+ method: 'POST',
550
+
551
+ headers: {
552
+ ...headers
553
+ },
554
+
555
+ agent,
556
+ timeout: timeoutMs
544
557
  },
545
- agent,
546
- timeout: timeoutMs
547
- }, res => {
548
- // Handle redirects (3xx)
549
- if (res.statusCode && res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
550
- res.resume(); // Consume response to free resources
551
- const newUrl = new URL(res.headers.location, url).toString();
552
- resolve(uploadWithNodeHttp({
553
- url: newUrl,
554
- filePath,
555
- headers,
556
- timeoutMs,
557
- agent
558
- }, redirectCount + 1));
559
- return;
560
- }
561
- let body = '';
562
- res.on('data', chunk => (body += chunk));
563
- res.on('end', () => {
564
- try {
565
- resolve(JSON.parse(body));
566
- }
567
- catch {
568
- resolve(undefined);
558
+
559
+ res => {
560
+ // Handle redirect
561
+ if (
562
+ res.statusCode >= 300 &&
563
+ res.statusCode < 400 &&
564
+ res.headers.location
565
+ ) {
566
+ res.resume();
567
+
568
+ const newUrl = new URL(
569
+ res.headers.location,
570
+ url
571
+ ).toString();
572
+
573
+ resolve(
574
+ uploadWithNodeHttp(
575
+ {
576
+ url: newUrl,
577
+ filePath,
578
+ headers,
579
+ timeoutMs,
580
+ agent
581
+ },
582
+ redirectCount + 1
583
+ )
584
+ );
585
+
586
+ return;
569
587
  }
570
- });
571
- });
588
+
589
+ let body = '';
590
+
591
+ res.on('data', chunk => {
592
+ body += chunk.toString();
593
+ });
594
+
595
+ res.on('end', () => {
596
+ if (
597
+ res.statusCode < 200 ||
598
+ res.statusCode >= 300
599
+ ) {
600
+ const error = new Error(
601
+ `HTTP ${res.statusCode}: ${body}`
602
+ );
603
+
604
+ error.statusCode = res.statusCode;
605
+ error.responseBody = body;
606
+
607
+ reject(error);
608
+ return;
609
+ }
610
+
611
+ try {
612
+ resolve(JSON.parse(body));
613
+ } catch {
614
+ resolve(undefined);
615
+ }
616
+ });
617
+ }
618
+ );
619
+
572
620
  req.on('error', reject);
621
+
573
622
  req.on('timeout', () => {
574
623
  req.destroy();
575
- reject(new Error('Upload timeout'));
576
- });
577
- const stream = createReadStream(filePath);
578
- stream.pipe(req);
579
- stream.on('error', err => {
580
- req.destroy();
581
- reject(err);
624
+
625
+ reject(
626
+ new Error('Upload timeout')
627
+ );
582
628
  });
629
+
630
+ /*
631
+ * Baileys encryptedStream
632
+ *
633
+ * filePath sebenarnya adalah:
634
+ * encWriteStream
635
+ */
636
+
637
+ if (
638
+ filePath &&
639
+ typeof filePath.pipe === 'function'
640
+ ) {
641
+ filePath.on('error', err => {
642
+ req.destroy(err);
643
+ });
644
+
645
+ filePath.pipe(req);
646
+
647
+ return;
648
+ }
649
+
650
+ /*
651
+ * Fallback kalau ternyata Buffer
652
+ */
653
+
654
+ if (Buffer.isBuffer(filePath)) {
655
+ req.end(filePath);
656
+ return;
657
+ }
658
+
659
+ /*
660
+ * Fallback kalau benar-benar file path
661
+ */
662
+
663
+ if (typeof filePath === 'string') {
664
+ const stream = createReadStream(filePath);
665
+
666
+ stream.on('error', err => {
667
+ req.destroy(err);
668
+ });
669
+
670
+ stream.pipe(req);
671
+
672
+ return;
673
+ }
674
+
675
+ req.destroy();
676
+
677
+ reject(
678
+ new Error(
679
+ 'Invalid upload input: expected stream, Buffer, or file path'
680
+ )
681
+ );
583
682
  });
584
683
  };
585
684
  const uploadWithFetch = async ({ url, filePath, headers, timeoutMs, agent }) => {
@@ -644,18 +743,6 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, opt
644
743
  'Content-Type': 'application/octet-stream',
645
744
  Origin: DEFAULT_ORIGIN
646
745
  };
647
- let reqBuffer;
648
- if (Buffer.isBuffer(streamOrPath)) {
649
- reqBuffer = streamOrPath;
650
- }
651
- else if (typeof streamOrPath === 'string') {
652
- reqBuffer = await fs.readFile(streamOrPath);
653
- }
654
- else {
655
- const chunks = [];
656
- for await (const chunk of streamOrPath) chunks.push(chunk);
657
- reqBuffer = Buffer.concat(chunks);
658
- }
659
746
  let mediaPath = MEDIA_PATH_MAP[mediaType];
660
747
  if (newsletter && mediaPath) {
661
748
  mediaPath = mediaPath.replace('/mms/', '/newsletter/newsletter-');
@@ -666,16 +753,14 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, opt
666
753
  const url = `https://${hostname}${mediaPath}/${fileEncSha256B64}?auth=${auth}&token=${fileEncSha256B64}`;
667
754
  let result;
668
755
  try {
669
- const axios = (await import('axios')).default;
670
- const body = await axios.post(url, reqBuffer, {
756
+ const body = await uploadMedia({
757
+ url,
758
+ filePath: streamOrPath,
671
759
  headers,
672
- httpsAgent: fetchAgent,
673
- timeout: timeoutMs,
674
- responseType: 'json',
675
- maxBodyLength: Infinity,
676
- maxContentLength: Infinity
677
- });
678
- result = body.data;
760
+ timeoutMs,
761
+ agent: fetchAgent
762
+ }, logger);
763
+ result = body;
679
764
  if (result?.url || result?.direct_path) {
680
765
  urls = {
681
766
  mediaUrl: result.url,
@@ -901,6 +901,8 @@ export const generateWAMessageContent = async (message, options) => {
901
901
  }
902
902
  }
903
903
  };
904
+ } else if (hasNonNullishProperty(message, "orderMessage")) {
905
+ m.orderMessage = proto.Message.OrderMessage.create(message.orderMessage);
904
906
  } else if (hasNonNullishProperty(message, "interactiveMessage")) {
905
907
  const im = { ...message.interactiveMessage }
906
908
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fvckerss/baileys",
3
3
  "type": "module",
4
- "version": "1.0.13",
4
+ "version": "1.0.14",
5
5
  "description": "Baileys mod by Swiper",
6
6
  "keywords": [
7
7
  "whatsapp",