diff --git a/packages/core/src/mime.ts b/packages/core/src/mime.ts index 31338268aa4..a84e94ba284 100644 --- a/packages/core/src/mime.ts +++ b/packages/core/src/mime.ts @@ -9,7 +9,13 @@ export function detectMedia(bytes: Uint8Array) { if (startsWith(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg" const signature = ascii(bytes, 0, 6) if (signature === "GIF87a" || signature === "GIF89a") return "image/gif" - if (startsWith(bytes, [0x42, 0x4d])) return "image/bmp" + if (bytes.length >= 18 && startsWith(bytes, [0x42, 0x4d])) { + const header = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength) + const pixels = header.getUint32(10, true) + const dib = header.getUint32(14, true) + // Pixels follow the 14-byte file header and a Windows or OS/2 DIB header, possibly beyond this sample. + if ([12, 16, 40, 52, 56, 64, 108, 124].includes(dib) && pixels >= 14 + dib) return "image/bmp" + } if (startsWith(bytes, [0x25, 0x50, 0x44, 0x46, 0x2d])) return "application/pdf" if (startsWith(bytes, [0x52, 0x49, 0x46, 0x46]) && startsWith(bytes.subarray(8), [0x57, 0x45, 0x42, 0x50])) return "image/webp" diff --git a/packages/core/test/mime.test.ts b/packages/core/test/mime.test.ts index efe65b4b254..93af27cf06e 100644 --- a/packages/core/test/mime.test.ts +++ b/packages/core/test/mime.test.ts @@ -13,6 +13,21 @@ describe("Mime.detect", () => { expect(Mime.detect(bytes)).toBe(mime) }) + test("keeps ordinary text starting with BM as text", () => { + for (const text of ["BM", "BMW service schedule\n"]) expect(Mime.detect(Buffer.from(text))).toBe("text/plain") + }) + + test("detects Windows and OS/2 bitmaps from a prefix", () => { + // 1x1, 24-bit bitmaps with a four-byte padded pixel row. + const windows = "424d3a000000000000003600000028000000010000000100000001001800" + "00".repeat(28) + const os2 = "424d1e000000000000001a0000000c000000010001000100180000000000" + for (const hex of [windows, os2]) { + const bytes = Buffer.from(hex, "hex") + expect(Mime.detect(bytes)).toBe("image/bmp") + expect(Mime.detect(bytes.subarray(0, 18))).toBe("image/bmp") + } + }) + test("tells ISO-BMFF brands apart", () => { expect(Mime.detect(ftyp("000000206674797061766966000000004d695072617669666d6961666d696631"))).toBe("image/avif") expect(Mime.detect(ftyp("000000186674797061766973000000006d73663161766973"))).toBe("image/avif") diff --git a/packages/core/test/session-prompt.test.ts b/packages/core/test/session-prompt.test.ts index 6c50de01f37..d00ef5ebd7d 100644 --- a/packages/core/test/session-prompt.test.ts +++ b/packages/core/test/session-prompt.test.ts @@ -843,11 +843,15 @@ describe("Session.prompt", () => { yield* setup const session = yield* Session.Service const uri = `data:video/mp2t;base64,${Buffer.from("export const value = 1\n").toString("base64")}` + const notes = Buffer.from("BMW service schedule\n").toString("base64") const message = yield* session.prompt({ sessionID, text: "Inspect this", - files: [{ uri, name: "main.ts" }], + files: [ + { uri, name: "main.ts" }, + { uri: `data:;base64,${notes}`, name: "notes" }, + ], resume: false, }) @@ -858,6 +862,7 @@ describe("Session.prompt", () => { source: { type: "inline" }, name: "main.ts", }, + { data: notes, mime: "text/plain", source: { type: "inline" }, name: "notes" }, ]) }), )