mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
fix(engine): an upstream reply the capture proxy cannot re-send fails only that request (#8646)
This commit is contained in:
@@ -151,6 +151,51 @@ describe("capture proxy", () => {
|
||||
expect(refused).toEqual(["private"]);
|
||||
});
|
||||
|
||||
// fails without the fix: writeHead threw on the status below 100 and the uncaught throw ended the host process.
|
||||
it("answers an upstream status below 100 with 502 and keeps serving, direct and through an upstream proxy", async () => {
|
||||
const target = createTcpServer((socket) => {
|
||||
socket.on("error", () => {});
|
||||
socket.once("data", (data) => {
|
||||
const status = /^GET \S*\/status\/(\d{3}) /.exec(String(data))?.[1] ?? "200";
|
||||
socket.end(`HTTP/1.1 ${status} Odd\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok`);
|
||||
});
|
||||
});
|
||||
await new Promise<void>((resolve) => target.listen(0, "127.0.0.1", resolve));
|
||||
const port = (target.address() as { port: number }).port;
|
||||
resolveAddress.mockResolvedValue("127.0.0.1");
|
||||
try {
|
||||
for (const upstream of [null, { host: "127.0.0.1", port }]) {
|
||||
const failures: CaptureFailure[] = [];
|
||||
const proxy = await startCaptureProxy(upstream, undefined, undefined, {
|
||||
onFailed: (failure) => failures.push(failure),
|
||||
});
|
||||
proxies.push(proxy);
|
||||
const get = (path: string) =>
|
||||
new Promise<number>((resolve, reject) => {
|
||||
const req = request(
|
||||
proxy.url,
|
||||
{ path: `http://odd.example.com:${port}${path}` },
|
||||
(res) => {
|
||||
res.resume();
|
||||
res.on("end", () => resolve(res.statusCode!));
|
||||
},
|
||||
);
|
||||
req.on("error", reject);
|
||||
req.end();
|
||||
});
|
||||
expect(await get("/status/099")).toBe(502);
|
||||
expect(await get("/status/000")).toBe(502);
|
||||
expect(await get("/fine")).toBe(200);
|
||||
expect(failures.map((failure) => failure.reason)).toEqual([
|
||||
"upstream_failed",
|
||||
"upstream_failed",
|
||||
]);
|
||||
}
|
||||
} finally {
|
||||
await new Promise<void>((resolve) => target.close(() => resolve()));
|
||||
}
|
||||
});
|
||||
|
||||
it("listens on the fixed loopback port it is given", async () => {
|
||||
const free = createServer();
|
||||
await new Promise<void>((resolve) => free.listen(0, "127.0.0.1", resolve));
|
||||
|
||||
@@ -108,6 +108,15 @@ async function httpPeer(url: URL, proxy: UpstreamProxy | undefined, fail: () =>
|
||||
return { hostname: proxy.host, port: proxy.port, path: url.href };
|
||||
}
|
||||
|
||||
function relayedReplyHead(received: IncomingMessage, response: ServerResponse): boolean {
|
||||
try {
|
||||
response.writeHead(received.statusCode ?? 502, forwardHeaders(received));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function forwardHttp(
|
||||
incoming: IncomingMessage,
|
||||
response: ServerResponse,
|
||||
@@ -123,6 +132,11 @@ async function forwardHttp(
|
||||
const headers = forwardHeaders(incoming);
|
||||
headers.host = url.host;
|
||||
if (proxy?.authorization) headers["proxy-authorization"] = proxy.authorization;
|
||||
const badGateway = () => {
|
||||
fail();
|
||||
if (!response.headersSent) response.writeHead(502, { Connection: "close" });
|
||||
response.end();
|
||||
};
|
||||
const upstream = request(
|
||||
{
|
||||
...peer,
|
||||
@@ -132,7 +146,10 @@ async function forwardHttp(
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
},
|
||||
(received) => {
|
||||
response.writeHead(received.statusCode ?? 502, forwardHeaders(received));
|
||||
if (!relayedReplyHead(received, response)) {
|
||||
received.resume();
|
||||
return badGateway();
|
||||
}
|
||||
received.on("error", () => response.destroy());
|
||||
received.pipe(response);
|
||||
},
|
||||
@@ -141,11 +158,7 @@ async function forwardHttp(
|
||||
upstream.setTimeout(CONNECTION_TIMEOUT_MS, () =>
|
||||
upstream.destroy(new Error("Capture request timed out")),
|
||||
);
|
||||
upstream.on("error", () => {
|
||||
fail();
|
||||
if (!response.headersSent) response.writeHead(502, { Connection: "close" });
|
||||
response.end();
|
||||
});
|
||||
upstream.on("error", badGateway);
|
||||
response.on("close", () => upstream.destroy());
|
||||
incoming.on("error", () => upstream.destroy());
|
||||
incoming.pipe(upstream);
|
||||
|
||||
Reference in New Issue
Block a user