Buffer request body before proxying to unix socket

Streaming req.body through the double proxy (toes -> bun -> go)
caused POST requests to fail. Buffer the body as an ArrayBuffer
first so content-length is set correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Corey Johnson 2026-03-10 18:02:12 -07:00
parent 02482d9beb
commit a344e293f1

View File

@ -8,7 +8,7 @@ export interface WsData {
const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>() const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>()
export function createProxy(socketPath: string, isHealthy: () => boolean, isRunning: () => boolean) { export function createProxy(socketPath: string, isHealthy: () => boolean, isRunning: () => boolean) {
function proxyFetch(req: Request): Promise<Response> | Response { async function proxyFetch(req: Request): Promise<Response> {
const url = new URL(req.url) const url = new URL(req.url)
if (url.pathname === '/ok') { if (url.pathname === '/ok') {
@ -21,11 +21,12 @@ export function createProxy(socketPath: string, isHealthy: () => boolean, isRunn
const hasBody = req.method !== 'GET' && req.method !== 'HEAD' const hasBody = req.method !== 'GET' && req.method !== 'HEAD'
const headers = new Headers(req.headers) const headers = new Headers(req.headers)
headers.delete('accept-encoding') headers.delete('accept-encoding')
const body = hasBody ? await req.arrayBuffer() : undefined
return fetch(`http://localhost${url.pathname}${url.search}`, { return fetch(`http://localhost${url.pathname}${url.search}`, {
method: req.method, method: req.method,
headers, headers,
body: hasBody ? req.body : undefined, body,
unix: socketPath, unix: socketPath,
}).then((r) => { }).then((r) => {
const respHeaders = new Headers(r.headers) const respHeaders = new Headers(r.headers)