diff --git a/packages/core/src/mime.ts b/packages/core/src/mime.ts index 15f24023fa9..3424be634dd 100644 --- a/packages/core/src/mime.ts +++ b/packages/core/src/mime.ts @@ -3,7 +3,8 @@ export * as Mime from "./mime.js" export function detect(bytes: Uint8Array) { if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png" if (startsWith(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg" - if (startsWith(bytes, [0x47, 0x49, 0x46, 0x38])) return "image/gif" + const signature = ascii(bytes, 0, 6) + if (signature === "GIF87a" || signature === "GIF89a") return "image/gif" if (startsWith(bytes, [0x42, 0x4d])) 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])) diff --git a/packages/core/test/tool-read-filesystem.test.ts b/packages/core/test/tool-read-filesystem.test.ts index 2e413f6f583..fa38fa154b2 100644 --- a/packages/core/test/tool-read-filesystem.test.ts +++ b/packages/core/test/tool-read-filesystem.test.ts @@ -23,6 +23,13 @@ const absolute = (value: string) => AbsolutePath.make(value) describe("ReadTool text serialization", () => { const cases = [ + { + name: "reads a partial GIF signature as text", + content: "GIF8 is text", + page: {}, + output: { type: "file", content: "GIF8 is text", encoding: "utf8" }, + model: "Read file lines.txt, lines 1-1\n1: GIF8 is text", + }, { name: "preserves a selected trailing blank line before continuation", content: "alpha\n\nomega\n", @@ -177,6 +184,30 @@ describe("ReadToolFileSystem", () => { }), ) + it.live("reads extensionless and renamed GIFs as images", () => + Effect.gen(function* () { + const { environment, files, directory } = yield* fixture + for (const [version, name] of [ + ["GIF87a", "pixel"], + ["GIF89a", "pixel.txt"], + ]) { + // 1x1 screen, black/white palette, one pixel and trailer. + const bytes = Buffer.concat([ + Buffer.from(version), + Buffer.from("01000100800000000000ffffff2c00000000010001000002024401003b", "hex"), + ]) + const file = absolute(path.join(directory, name)) + yield* files.writeFile(file, bytes) + expect(yield* ReadToolFileSystem.read(environment, file, name)).toMatchObject({ + type: "file", + mime: "image/gif", + encoding: "base64", + content: bytes.toString("base64"), + }) + } + }), + ) + it.effect("preserves the environment not-found error", () => Effect.gen(function* () { const { environment, directory } = yield* fixture