mirror of
https://github.com/tiennm99/ccs.git
synced 2026-07-17 06:17:09 +00:00
fix(cliproxy): clean up upstream proxy on response disconnect (#1361)
* fix(cliproxy): clean up upstream proxy on response disconnect * style: apply prettier formatting
This commit is contained in:
@@ -134,13 +134,21 @@ export function createCliproxyLocalProxyRouter(deps: CliproxyLocalProxyDeps = {}
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Clean up proxy connection only when the client aborts the request.
|
const cleanupProxyRequest = () => {
|
||||||
// Avoid res.on('close') here because Bun may emit it during local error
|
if (!proxyReq.destroyed) {
|
||||||
// responses before the JSON body is flushed, which can truncate 502 payloads.
|
|
||||||
req.on('aborted', () => {
|
|
||||||
if (!res.writableEnded) {
|
|
||||||
proxyReq.destroy();
|
proxyReq.destroy();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Request-abort cleanup covers disconnects before the response starts.
|
||||||
|
req.on('aborted', cleanupProxyRequest);
|
||||||
|
|
||||||
|
// Response close cleanup covers disconnects while streaming the proxied response.
|
||||||
|
// Guard on writableEnded/finished so successful proxy completions are untouched.
|
||||||
|
res.on('close', () => {
|
||||||
|
if (!res.writableEnded || !res.finished) {
|
||||||
|
cleanupProxyRequest();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (bodyBuffer) {
|
if (bodyBuffer) {
|
||||||
|
|||||||
Reference in New Issue
Block a user