From 90d5ca2cbb07e46364d05e3558f177355ceed975 Mon Sep 17 00:00:00 2001 From: "Kai (Tam Nhu) Tran" <61256810+kaitranntt@users.noreply.github.com> Date: Sat, 23 May 2026 21:45:09 -0400 Subject: [PATCH] fix(proxy): honor backpressure in cliproxy local streaming (#1379) --- src/web-server/routes/cliproxy-local-proxy.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/web-server/routes/cliproxy-local-proxy.ts b/src/web-server/routes/cliproxy-local-proxy.ts index 4454a297..b03e2ff6 100644 --- a/src/web-server/routes/cliproxy-local-proxy.ts +++ b/src/web-server/routes/cliproxy-local-proxy.ts @@ -116,9 +116,22 @@ export function createCliproxyLocalProxyRouter(deps: CliproxyLocalProxyDeps = {} } res.writeHead(proxyStatus, proxyRes.headers); - // Manual streaming instead of pipe() for Bun runtime compatibility - proxyRes.on('data', (chunk: Buffer) => res.write(chunk)); - proxyRes.on('end', () => res.end()); + // Manual streaming instead of pipe() for Bun runtime compatibility. + // Explicitly honor downstream backpressure to avoid unbounded buffering. + const onDrain = () => proxyRes.resume(); + + proxyRes.on('data', (chunk: Buffer) => { + const canContinue = res.write(chunk); + if (!canContinue) { + proxyRes.pause(); + res.once('drain', onDrain); + } + }); + + proxyRes.on('end', () => { + res.off('drain', onDrain); + res.end(); + }); } );