@qadeer-xtech/stablebail 1.0.2 → 1.0.3

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/README.md CHANGED
@@ -28,6 +28,17 @@ Official rc14 mein channel ke ye hisse adhoore ya toote hue thay — 1.0.2 mein
28
28
 
29
29
  Purana `newsletterFetchMessages(jid, count, since, after)` wala andaaz bhi chalta rahega — library khud samajh jati hai.
30
30
 
31
+ ## 1.0.3 mein kya fix hai (Media Stream Download)
32
+
33
+ rc14 (aur official master) mein media STREAM download ke ye hisse toote hue thay — 1.0.3 mein theek hain:
34
+
35
+ - **Stream error aage jata hai** — download darmiyan mein kate to pehle stream hamesha ke liye hang ho jati thi aur process par uncaughtException ata tha; ab error caller ko milta hai, download saaf fail hota hai, hang nahi (official issue #2750).
36
+ - **Purani media par reupload** — CDN 404/410 de to bot phone se reupload mangta hai; fetch (undici) migration ke baad status check kharab tha, reupload kabhi manga hi nahi jata tha (official issue #2767).
37
+ - **Expire link (403) par bhi reupload** — signed media link expire ho jaye (oe param) to 403 ata tha aur media hamesha ke liye fail; ab WhatsApp Web ki tarah reupload se naya link ata hai.
38
+ - **mediaKey string wala bug** — database/JSON se restore hue messages ki mediaKey base64 string hoti thi, jis se retry decryption fail hoti thi; ab pehle bytes mein badalti hai.
39
+
40
+ Baaki sab kuch 1.0.2 wala hi hai — API mein koi tabdeeli nahi, purana code waisa hi chalega.
41
+
31
42
  ## Bot mein istemal
32
43
 
33
44
  ```js
@@ -74,7 +85,8 @@ await sock.newsletterReactMessage(channel.id, updates[0].server_id, '❤️')
74
85
 
75
86
  ## Versions
76
87
 
77
- - **1.0.2** — Channels/newsletter fixes (upar wali list)
88
+ - **1.0.3** — Media stream download fixes (upar wali list)
89
+ - **1.0.2** — Channels/newsletter fixes
78
90
  - **1.0.1** — Dependencies apne packages par: `@qadeer-xtech/libsignal`, `@qadeer-xtech/whatsapp-rust-bridge`
79
91
  - **1.0.0** — Official Baileys 7.0.0-rc14 ki saaf copy
80
92
 
@@ -512,6 +512,12 @@ export const downloadEncryptedContent = async (downloadUrl, { cipherKey, iv }, {
512
512
  }
513
513
  }
514
514
  });
515
+ // stablebail 1.0.3 (official issue #2750): .pipe() does NOT forward
516
+ // source errors — a mid-download connection drop left the returned
517
+ // stream open forever (the download hung and wedged its handler)
518
+ // and surfaced as a process-level uncaughtException. Forward the
519
+ // error so the stream destroys and callers can catch it normally.
520
+ fetched.on('error', (error) => output.destroy(error));
515
521
  return fetched.pipe(output, { end: true });
516
522
  };
517
523
  export function extensionForMediaMessage(message) {
@@ -569,6 +575,10 @@ export const uploadWithNodeHttp = async ({ url, filePath, headers, timeoutMs, ag
569
575
  return;
570
576
  }
571
577
  let body = '';
578
+ // stablebail 1.0.3: a response-stream error mid-upload (server
579
+ // drops while answering) had no handler — unhandled 'error'
580
+ // event / hung promise on large uploads. Reject cleanly.
581
+ res.on('error', reject);
572
582
  res.on('data', chunk => (body += chunk));
573
583
  res.on('end', () => {
574
584
  try {
@@ -700,7 +710,12 @@ export const getWAUploadToServer = ({ customUploadHosts, fetchAgent, logger, opt
700
710
  };
701
711
  };
702
712
  const getMediaRetryKey = (mediaKey) => {
703
- return hkdf(mediaKey, 32, { info: 'WhatsApp Media Retry Notification' });
713
+ // stablebail 1.0.3 (official PR #2834): messages restored from JSON
714
+ // carry mediaKey as a base64 STRING — running hkdf over the string's
715
+ // utf8 bytes derives the wrong key and the phone's retry answer
716
+ // then fails decryption. Decode it to bytes first.
717
+ const key = typeof mediaKey === 'string' ? Buffer.from(mediaKey, 'base64') : mediaKey;
718
+ return hkdf(key, 32, { info: 'WhatsApp Media Retry Notification' });
704
719
  };
705
720
  /**
706
721
  * Generate a binary node that will request the phone to re-upload the media & return the newly uploaded URL
@@ -828,14 +828,51 @@ export const aggregateMessageKeysNotFromMe = (keys) => {
828
828
  return Object.values(keyMap);
829
829
  };
830
830
  const REUPLOAD_REQUIRED_STATUS = [410, 404];
831
+ /**
832
+ * stablebail 1.0.3 (official issue #2767, PRs #2834/#2835): since the
833
+ * axios -> fetch migration, HTTP failures arrive as Boom errors whose
834
+ * status lives on output.statusCode — the old check read error.status,
835
+ * never matched, and the phone reupload was never requested, so aged
836
+ * media simply failed. A 403 also requires reupload when the signed
837
+ * media URL has expired (its oe query param is the expiry time,
838
+ * hex unix seconds) — exactly how WhatsApp Web recovers old media.
839
+ */
840
+ const downloadHttpStatus = (error) => {
841
+ const status = error?.output?.statusCode ?? error?.status ?? error?.statusCode;
842
+ return typeof status === 'number' ? status : undefined;
843
+ };
844
+ const isExpiredSignedUrl = (error) => {
845
+ try {
846
+ const url = error?.data?.url;
847
+ if (!url) {
848
+ return false;
849
+ }
850
+ const oe = new URL(String(url)).searchParams.get('oe');
851
+ if (!oe) {
852
+ return false;
853
+ }
854
+ return parseInt(oe, 16) * 1000 <= Date.now();
855
+ }
856
+ catch {
857
+ return false;
858
+ }
859
+ };
860
+ const reuploadRequired = (error) => {
861
+ const status = downloadHttpStatus(error);
862
+ if (status === undefined) {
863
+ return false;
864
+ }
865
+ if (REUPLOAD_REQUIRED_STATUS.includes(status)) {
866
+ return true;
867
+ }
868
+ return status === 403 && isExpiredSignedUrl(error);
869
+ };
831
870
  /**
832
871
  * Downloads the given message. Throws an error if it's not a media message
833
872
  */
834
873
  export const downloadMediaMessage = async (message, type, options, ctx) => {
835
874
  const result = await downloadMsg().catch(async (error) => {
836
- if (ctx &&
837
- typeof error?.status === 'number' && // treat errors with status as HTTP failures requiring reupload
838
- REUPLOAD_REQUIRED_STATUS.includes(error.status)) {
875
+ if (ctx && reuploadRequired(error)) {
839
876
  ctx.logger.info({ key: message.key }, 'sending reupload media request...');
840
877
  // request reupload
841
878
  message = await ctx.reuploadRequest(message);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qadeer-xtech/stablebail",
3
3
  "type": "module",
4
- "version": "1.0.2",
4
+ "version": "1.0.3",
5
5
  "description": "A WebSockets library for interacting with WhatsApp Web",
6
6
  "keywords": [
7
7
  "whatsapp",
@@ -130,4 +130,4 @@
130
130
  "engines": {
131
131
  "node": ">=20.0.0"
132
132
  }
133
- }
133
+ }