proxy-backend support sse connections closing

Signed-off-by: Nina Berg <nikb100@gmail.com>
This commit is contained in:
Nina Berg
2025-04-07 11:30:24 -04:00
parent d67fd32c6b
commit d65ec54f23
+14 -2
View File
@@ -214,8 +214,12 @@ export function buildMiddleware(
].map(h => h.toLocaleLowerCase()),
);
// only forward the allowed headers in backend->client
fullConfig.onProxyRes = (proxyRes: http.IncomingMessage) => {
fullConfig.onProxyRes = (
proxyRes: http.IncomingMessage,
_,
res: express.Response,
) => {
// only forward the allowed headers in backend->client
const headerNames = Object.keys(proxyRes.headers);
headerNames.forEach(h => {
@@ -223,6 +227,14 @@ export function buildMiddleware(
delete proxyRes.headers[h];
}
});
// handle SSE connections closed by the backend
// https://github.com/chimurai/http-proxy-middleware/discussions/765
proxyRes.on('close', () => {
if (!res.writableEnded) {
res.end();
}
});
};
if (reviveConsumedRequestBodies) {