From 3b0154bf16834bed6ad62e10050d04f5ed3c5b76 Mon Sep 17 00:00:00 2001 From: jztdjfcsdj Date: Tue, 6 Oct 2026 13:25:31 +0800 Subject: [PATCH 1/2] fix: preserve binary proxy request bodies --- routes/proxy.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/proxy.ts b/routes/proxy.ts index eb7848a..20c0a3d 100644 --- a/routes/proxy.ts +++ b/routes/proxy.ts @@ -24,8 +24,8 @@ export default withRouteSpec({ } let body = undefined - if (["POST", "PUT", "PATCH"].includes(req.method)) { - body = await req.clone().text() + if (!["GET", "HEAD"].includes(req.method)) { + body = await req.clone().arrayBuffer() } const headers = new Headers(req.headers) From b0719462a251ff520c1bc44edcb06709270d58b3 Mon Sep 17 00:00:00 2001 From: jztdjfcsdj Date: Tue, 6 Oct 2026 13:32:17 +0800 Subject: [PATCH 2/2] Create proxy-binary-body.test.ts --- tests/routes/proxy-binary-body.test.ts | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/routes/proxy-binary-body.test.ts diff --git a/tests/routes/proxy-binary-body.test.ts b/tests/routes/proxy-binary-body.test.ts new file mode 100644 index 0000000..ddf8778 --- /dev/null +++ b/tests/routes/proxy-binary-body.test.ts @@ -0,0 +1,34 @@ +import { expect, test } from "bun:test" +import { getTestServer } from "../fixtures/get-test-server" + +test.each(["POST", "PUT", "PATCH", "DELETE", "OPTIONS"])( + "proxy preserves binary request bytes for %s", + async (method) => { + const { url } = await getTestServer() + const payload = new Uint8Array([0x00, 0xff, 0xfe, 0x80, 0x41]) + const upstream = Bun.serve({ + port: 0, + async fetch(req) { + return new Response(await req.arrayBuffer(), { + headers: { "Content-Type": "application/octet-stream" }, + }) + }, + }) + + try { + const response = await fetch(`${url}/proxy`, { + method, + headers: { + "X-Target-Url": `http://127.0.0.1:${upstream.port}`, + "Content-Type": "application/octet-stream", + }, + body: payload, + signal: AbortSignal.timeout(3000), + }) + expect(response.status).toBe(200) + expect(new Uint8Array(await response.arrayBuffer())).toEqual(payload) + } finally { + upstream.stop(true) + } + }, +)