From 01478ed0d06b3da1160150792203d5a4955a245e Mon Sep 17 00:00:00 2001 From: vagdotdev Date: Tue, 30 Jun 2026 11:38:21 +0530 Subject: [PATCH] fix: serve zero-byte binary files correctly --- routes/files/download.ts | 2 +- routes/files/download/[[...file_path]].ts | 2 +- routes/files/static/[[...file_path]].ts | 2 +- tests/routes/files.test.ts | 33 +++++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/routes/files/download.ts b/routes/files/download.ts index 4bb47d3..b1588fa 100644 --- a/routes/files/download.ts +++ b/routes/files/download.ts @@ -28,7 +28,7 @@ export default withRouteSpec({ } const isText = file.text_content !== undefined - if (!isText && file.binary_content_b64) { + if (!isText && file.binary_content_b64 !== undefined) { const binaryBody = decodeBase64ToUint8Array(file.binary_content_b64) const responseBody = uint8ArrayToArrayBuffer(binaryBody) return new Response(responseBody, { diff --git a/routes/files/download/[[...file_path]].ts b/routes/files/download/[[...file_path]].ts index 3e8fa02..4562cbc 100644 --- a/routes/files/download/[[...file_path]].ts +++ b/routes/files/download/[[...file_path]].ts @@ -29,7 +29,7 @@ export default withRouteSpec({ } const isText = file.text_content !== undefined - if (!isText && file.binary_content_b64) { + if (!isText && file.binary_content_b64 !== undefined) { const binaryBody = decodeBase64ToUint8Array(file.binary_content_b64) const responseBody = uint8ArrayToArrayBuffer(binaryBody) return new Response(responseBody, { diff --git a/routes/files/static/[[...file_path]].ts b/routes/files/static/[[...file_path]].ts index 27f2910..ca3968c 100644 --- a/routes/files/static/[[...file_path]].ts +++ b/routes/files/static/[[...file_path]].ts @@ -81,7 +81,7 @@ export default withRouteSpec({ } const mimeType = getMimeType(file.file_path) - if (file.binary_content_b64) { + if (file.binary_content_b64 !== undefined) { const binaryBody = decodeBase64ToUint8Array(file.binary_content_b64) const responseBody = uint8ArrayToArrayBuffer(binaryBody) diff --git a/tests/routes/files.test.ts b/tests/routes/files.test.ts index 196c354..41ea743 100644 --- a/tests/routes/files.test.ts +++ b/tests/routes/files.test.ts @@ -62,6 +62,39 @@ test("binary file operations", async () => { ) }) +test("zero-byte binary files download and serve as binary", async () => { + const { axios, url } = await getTestServer() + + await axios.post("/files/upsert", { + file_path: "/empty.bin", + binary_content_b64: "", + }) + + const queryDownloadRes = await fetch( + `${url}/files/download?file_path=${encodeURIComponent("/empty.bin")}`, + ) + expect(queryDownloadRes.status).toBe(200) + expect(await queryDownloadRes.arrayBuffer()).toHaveProperty("byteLength", 0) + expect(queryDownloadRes.headers.get("content-type")).toBe( + "application/octet-stream", + ) + expect(queryDownloadRes.headers.get("content-length")).toBe("0") + + const pathDownloadRes = await fetch(`${url}/files/download/empty.bin`) + expect(pathDownloadRes.status).toBe(200) + expect(await pathDownloadRes.arrayBuffer()).toHaveProperty("byteLength", 0) + expect(pathDownloadRes.headers.get("content-type")).toBe( + "application/octet-stream", + ) + expect(pathDownloadRes.headers.get("content-length")).toBe("0") + + const staticRes = await fetch(`${url}/files/static/empty.bin`) + expect(staticRes.status).toBe(200) + expect(await staticRes.arrayBuffer()).toHaveProperty("byteLength", 0) + expect(staticRes.headers.get("content-type")).toBe("application/octet-stream") + expect(staticRes.headers.get("content-length")).toBe("0") +}) + test("file download operations", async () => { const { axios } = await getTestServer()