Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): keep only the latest pinned WhatsApp Web page in memory

Yudhi Armyndharisc53245a2bd71
diff --git a/src/engine/wa-web-version.spec.ts b/src/engine/wa-web-version.spec.ts
index 72050e058..3fba218a5 100644
--- a/src/engine/wa-web-version.spec.ts
+++ b/src/engine/wa-web-version.spec.ts
@@ -397,6 +397,21 @@ describe('resolveWebVersionPin with a cache directory', () => {
expect(fetcher).toHaveBeenCalledTimes(2);
});
+ // An auto pin moves to a newer build roughly daily, so keeping every build's page in memory grew
+ // without bound over a long uptime. Only the current build is kept; an older one is fetched again.
+ it('keeps only the latest build in memory', async () => {
+ const fetcher = jest.fn(() => Promise.resolve(page(HTML)));
+
+ await resolveWebVersionPin(fetcher as never, dir);
+ process.env.WWEBJS_WEB_VERSION = '2.3000.5678';
+ await resolveWebVersionPin(fetcher as never, dir);
+ await resolveWebVersionPin(fetcher as never, dir);
+ process.env.WWEBJS_WEB_VERSION = VERSION;
+ await resolveWebVersionPin(fetcher as never, dir);
+
+ expect(fetcher).toHaveBeenCalledTimes(3);
+ });
+
it('refuses a version that is not a build number, without fetching or writing', async () => {
process.env.WWEBJS_WEB_VERSION = '../../etc/x';
const fetcher = jest.fn(() => Promise.resolve(page(HTML)));
diff --git a/src/engine/wa-web-version.ts b/src/engine/wa-web-version.ts
index 901ac1361..a871a4faa 100644
--- a/src/engine/wa-web-version.ts
+++ b/src/engine/wa-web-version.ts
@@ -51,8 +51,9 @@ let warnedRemoteTrust = false;
// non-strict, loads WhatsApp's live build when the fetch fails: the #488 class the pin exists to
// prevent. A non-OK answer falls back with nothing logged, a network error reaches only a bare
// console.error, and a hang never ends. It is fetched here instead, bounded, and handed to the library
-// as a strict local cache. A successful download is kept per URL for the life of the process (a
-// build's HTML does not change); a failure is not, so the next start retries.
+// as a strict local cache. The latest successful download is kept in memory (a build's HTML does not
+// change, and a pin only moves forward, so older builds are dropped); a failure is not, so the next
+// start retries.
export const PINNED_HTML_TIMEOUT_MS = 10_000;
const PINNED_HTML_MIN_LENGTH = 1024;
const PINNED_HTML_KEEP_MS = 7 * 24 * 60 * 60 * 1000;
@@ -302,6 +303,7 @@ async function downloadPinnedHtml(remotePath: string, fetcher: typeof fetch): Pr
if (html.length < PINNED_HTML_MIN_LENGTH || !/<html/i.test(html)) {
throw new Error('the response is not a WhatsApp Web page');
}
+ pinnedHtml.clear();
pinnedHtml.set(remotePath, html);
return html;
} catch (error) {