fix: flush proxied event stream headers

This commit is contained in:
Codex
2026-07-30 22:11:48 +08:00
parent 144e17c220
commit 25502a52bb
2 changed files with 26 additions and 0 deletions
+25
View File
@@ -334,6 +334,31 @@ describe('same-origin canary gateway', () => {
await reader?.cancel(); await reader?.cancel();
}); });
it('flushes SSE response headers before the first upstream event', async () => {
const upstream = createServer((_request, response) => {
response.writeHead(200, {
'content-type': 'text/event-stream',
'cache-control': 'no-cache',
});
response.flushHeaders();
});
servers.push(upstream);
const gateway = await startGateway(await fixtureDist(), await listen(upstream));
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 500);
try {
const response = await fetch(`${gateway.origin}/api/v1/events`, {
signal: controller.signal,
});
expect(response.status).toBe(200);
expect(response.headers.get('content-type')).toBe('text/event-stream');
await response.body?.cancel();
} finally {
clearTimeout(timeout);
}
});
it('has reversible, idempotent start/stop lifecycle', async () => { it('has reversible, idempotent start/stop lifecycle', async () => {
const gateway = createCanaryGateway({ distDir: await fixtureDist(), port: 0, upstreamPort: 1 }); const gateway = createCanaryGateway({ distDir: await fixtureDist(), port: 0, upstreamPort: 1 });
gateways.push(gateway); gateways.push(gateway);
+1
View File
@@ -177,6 +177,7 @@ async function proxyRequest(
upstreamResponse.statusMessage, upstreamResponse.statusMessage,
withoutHopByHop(upstreamResponse.headers), withoutHopByHop(upstreamResponse.headers),
); );
response.flushHeaders();
pipeline(upstreamResponse, response) pipeline(upstreamResponse, response)
.then(resolvePromise) .then(resolvePromise)
.catch(() => { .catch(() => {