@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.
- package/lib/Utils/messages-media.js +152 -67
- package/lib/Utils/messages.js +2 -0
- package/package.json +1 -1
|
@@ -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 (
|
|
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
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
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
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
'
|
|
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
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
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
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
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
|
|
670
|
-
|
|
756
|
+
const body = await uploadMedia({
|
|
757
|
+
url,
|
|
758
|
+
filePath: streamOrPath,
|
|
671
759
|
headers,
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
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,
|
package/lib/Utils/messages.js
CHANGED
|
@@ -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
|
|