Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): label an untyped document URL as octet-stream
Yudhi Armyndharis14876d4824fa
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.tsindex a2c7ef3f3..a821aa8f6 100644--- a/src/engine/adapters/baileys-messaging.ts+++ b/src/engine/adapters/baileys-messaging.ts@@ -165,8 +165,9 @@ const GENERIC_FETCHED_TYPES = new Set(['', 'application/octet-stream', 'binary/o* `sessionProxyUrl` is this session's egress proxy, which the URL fetch leaves through (#1626). It* is required, not optional, so a new call site cannot fetch direct on a proxied session by omission.*- * `fallbackType` is the kind's default (image/jpeg, video/mp4, audio/mpeg), used for a URL whose type- * neither the caller nor the host names. A document send passes none and keeps what it was given.+ * `fallbackType` is the kind's default (image/jpeg, video/mp4, audio/mpeg, application/octet-stream for a+ * document), used for a URL whose type neither the caller nor the host names. A document needs one too:+ * Baileys labels a document with an empty type as application/pdf.*/export async function resolveMediaBuffer(media: MediaInput,@@ -401,7 +402,7 @@ export class BaileysMessaging {async sendDocumentMessage(chatId: string, media: MediaInput): Promise<MessageResult> {this.host.ensureReady();- const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl());+ const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl(), 'application/octet-stream');return this.sendContent(chatId,{diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex ebeb44d44..46a46920f 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -3797,6 +3797,8 @@ describe('BaileysAdapter media sends', () => {a.sendVideoMessage('[email protected]', { mimetype: PLACEHOLDER, data: url }),audio: (a: BaileysAdapter, url: string) =>a.sendAudioMessage('[email protected]', { mimetype: PLACEHOLDER, data: url }),+ document: (a: BaileysAdapter, url: string) =>+ a.sendDocumentMessage('[email protected]', { mimetype: PLACEHOLDER, data: url, filename: 'm.docx' }),};const sentMimetype = (): unknown =>(fakeSock.sendMessage.mock.calls[0] as [string, { mimetype?: string }])[1].mimetype;@@ -3808,6 +3810,9 @@ describe('BaileysAdapter media sends', () => {['video', 'video/mp4', 'Application/Octet-Stream'],['audio', 'audio/mpeg', ''],['audio', 'audio/mpeg', 'binary/octet-stream'],+ // Baileys labels a document with no type as application/pdf, so a .docx would arrive unopenable.+ ['document', 'application/octet-stream', ''],+ ['document', 'application/octet-stream', 'binary/octet-stream'],] as const)('labels an undeclared %s URL as %s when the host answers %j', async (kind, fallback, fetchedType) => {(loadRemoteMediaBuffer as jest.Mock).mockResolvedValue({ data: Buffer.from([1]), mimetype: fetchedType });const adapter = await ready();@@ -3822,15 +3827,11 @@ describe('BaileysAdapter media sends', () => {expect(sentMimetype()).toBe('image/png');});- it('leaves a document with the type it was fetched with', async () => {- (loadRemoteMediaBuffer as jest.Mock).mockResolvedValue({ data: Buffer.from([1]), mimetype: PLACEHOLDER });+ it('keeps a specific fetched type for an undeclared document URL', async () => {+ (loadRemoteMediaBuffer as jest.Mock).mockResolvedValue({ data: Buffer.from([1]), mimetype: 'application/zip' });const adapter = await ready();- await adapter.sendDocumentMessage('[email protected]', {- mimetype: PLACEHOLDER,- data: 'https://cdn.example/m',- filename: 'm.bin',- });- expect(sentMimetype()).toBe(PLACEHOLDER);+ await sendByKind.document(adapter, 'https://cdn.example/m');+ expect(sentMimetype()).toBe('application/zip');});it('uses the caller-declared mimetype over the fetched content-type for a URL', async () => {