Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): derive the wwebjs ingest media budget from the clamped cap

Yudhi Armyndharisf90eee6a7edc
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index 7821c73a1..d2e9d8721 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -1028,6 +1028,29 @@ describe('WhatsAppWebJsAdapter.getChatHistory enrichment (parity with the live p
expect(m2.downloadMedia).toHaveBeenCalled();
});
+ // The per-item cap is clamped to MEDIA_DOWNLOAD_MAX_BYTES, so an override above it must not
+ // inflate the aggregate budget derived from it either.
+ it('derives the per-item-cap budget from the clamped cap, not a larger override', async () => {
+ process.env[ENV] = '4';
+ const origMax = process.env.MEDIA_DOWNLOAD_MAX_BYTES;
+ process.env.MEDIA_DOWNLOAD_MAX_BYTES = '3'; // budget = ceil(3 * 4 * 1.37) = 17 base64 chars
+ try {
+ const msgs = ['M16', 'M17', 'M18', 'M19', 'M20', 'M21'].map(id => ({
+ ...mediaMsg(id, 'QUJD'),
+ _data: { size: 3, mimetype: 'image/jpeg' },
+ }));
+
+ const out = await readyAdapter(clientFor(...msgs)).getChatHistory('status@broadcast', 50, true, 1024 * 1024);
+
+ expect(out.slice(0, 5).map(m => m.media)).toEqual(Array(5).fill({ mimetype: 'image/jpeg', data: 'QUJD' }));
+ expect(out[5].media).toEqual({ mimetype: 'image/jpeg', omitted: true, sizeBytes: 3 });
+ expect(msgs[5].downloadMedia).not.toHaveBeenCalled();
+ } finally {
+ if (origMax === undefined) delete process.env.MEDIA_DOWNLOAD_MAX_BYTES;
+ else process.env.MEDIA_DOWNLOAD_MAX_BYTES = origMax;
+ }
+ });
+
it('stops the read loop when the abort signal fires (client disconnect)', async () => {
const controller = new AbortController();
const m1 = mediaMsg('M11', 'QUJD');
diff --git a/src/engine/adapters/wwebjs-messaging.ts b/src/engine/adapters/wwebjs-messaging.ts
index aca7272a0..29440bddb 100644
--- a/src/engine/adapters/wwebjs-messaging.ts
+++ b/src/engine/adapters/wwebjs-messaging.ts
@@ -18,7 +18,12 @@ import { MessageNotFoundError } from '../../common/errors/message-not-found.erro
import { EngineRefusedError } from '../../common/errors/engine-refused.error';
import { loadRemoteMediaBuffer } from '../../common/media/load-remote-media';
import { chatKind, userPart } from '../identity/wa-id';
-import { chatHistoryMediaBudgetBytes, coerceDeclaredSize, ingestMediaBudgetBytes } from './inbound-media-cap';
+import {
+ chatHistoryMediaBudgetBytes,
+ coerceDeclaredSize,
+ inboundMediaMaxBytes,
+ ingestMediaBudgetBytes,
+} from './inbound-media-cap';
import { buildIncomingMessageBase, mapContactFields } from './message-mapper';
import { buildVCard } from './vcard';
import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';
@@ -785,12 +790,13 @@ export class WwebjsMessaging {
// into a store instead (mediaMaxBytes — the status seed): two ~10 MiB videos are ~28 MiB of
// base64 and would strip every later status. Such a caller gets a budget derived from its own
// per-item cap rather than an exemption — unbounded here would mean a 50-item seed could stack
- // ~650 MiB of base64 on the heap at connect time.
+ // ~650 MiB of base64 on the heap at connect time. The cap is clamped to MEDIA_DOWNLOAD_MAX_BYTES
+ // first, as capInboundMediaFor clamps each item, so an override above it cannot inflate the budget.
let mediaBudget = !includeMedia
? Number.POSITIVE_INFINITY
: mediaMaxBytes === undefined
? chatHistoryMediaBudgetBytes()
- : ingestMediaBudgetBytes(mediaMaxBytes);
+ : ingestMediaBudgetBytes(Math.min(mediaMaxBytes, inboundMediaMaxBytes()));
// Sender contacts resolved so far, keyed like Message.getContact() (`author || from`). Each lookup
// is a page round trip and a history page repeats the same few senders, so resolve each once; a
// failed lookup is remembered as undefined rather than retried for every later message.