mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
fix(engine): an asset download fails once when the egress proxy drops its tunnel (#8417)
* test(engine): a tunnel the egress proxy drops fails the download after its one retry * fix(engine): an asset download fails once when the egress proxy drops its tunnel undici 7 keeps a request queued and reopens the tunnel with no pause or limit when the proxy closes it without a reply. The proxy transport's pools now report that close as a reset, so the request fails and the downloader's one retry is the bound.
This commit is contained in:
@@ -293,6 +293,7 @@ describe("downloads with the render-service setting off", () => {
|
||||
|
||||
describe("downloads on a render service through the egress proxy in HYPERFRAMES_CAPTURE_PROXY", () => {
|
||||
const HOST = "cdn.egress-assets.com";
|
||||
const HOST_WHOSE_TUNNEL_THE_PROXY_DROPS = "gone.egress-assets.com";
|
||||
const tunnels: string[] = [];
|
||||
const defaultCAs = getCACertificates("default");
|
||||
let origin: Server;
|
||||
@@ -316,6 +317,7 @@ describe("downloads on a render service through the egress proxy in HYPERFRAMES_
|
||||
egress = createServer();
|
||||
egress.on("connect", (request, client) => {
|
||||
tunnels.push(request.url ?? "");
|
||||
if (request.url === `${HOST_WHOSE_TUNNEL_THE_PROXY_DROPS}:443`) return void client.destroy();
|
||||
const upstream = connect(originPort, "127.0.0.1", () => {
|
||||
client.write("HTTP/1.1 200 Connection Established\r\n\r\n");
|
||||
upstream.pipe(client).pipe(upstream);
|
||||
@@ -340,6 +342,12 @@ describe("downloads on a render service through the egress proxy in HYPERFRAMES_
|
||||
);
|
||||
});
|
||||
|
||||
it("a tunnel the proxy drops fails the download without opening it again and again", async () => {
|
||||
const url = `https://${HOST_WHOSE_TUNNEL_THE_PROXY_DROPS}/a.png`;
|
||||
await expect(downloadToTemp(url, tempDir())).rejects.toMatchObject({ kind: "network" });
|
||||
expect(tunnels).toEqual([0, 1].map(() => `${HOST_WHOSE_TUNNEL_THE_PROXY_DROPS}:443`));
|
||||
}, 10_000);
|
||||
|
||||
it("downloads by hostname through the proxy, with the usual headers, resolving no names", async () => {
|
||||
routes[`https://${HOST}/a.png`] = { status: 200, body: BODY };
|
||||
const path = await downloadToTemp(`https://${HOST}/a.png`, tempDir());
|
||||
|
||||
@@ -20,6 +20,7 @@ import { createHash, randomUUID } from "crypto";
|
||||
import dns from "node:dns";
|
||||
import { fetchGoesThroughEnvProxy } from "@hyperframes/core/env-proxy";
|
||||
import { type LookupFunction } from "node:net";
|
||||
import type { buildConnector } from "undici";
|
||||
import { isBlockedNetworkHost } from "./networkPolicy.js";
|
||||
import { resolveRestrictPageNetwork } from "../config.js";
|
||||
import {
|
||||
@@ -515,16 +516,44 @@ function undiciTransport(lookup: LookupFunction): Promise<DownloadFetch> {
|
||||
|
||||
const proxyTransports = new Map<string, Promise<DownloadFetch>>();
|
||||
|
||||
/** undici 7 reopens a tunnel the proxy closed without a reply with no pause or limit; this fails the request. */
|
||||
function failRequestOnDroppedTunnel(connect: buildConnector.connector): buildConnector.connector {
|
||||
return (options, callback) =>
|
||||
connect(options, (...result) => {
|
||||
const [error] = result;
|
||||
if (error && "code" in error && error.code === "UND_ERR_SOCKET") {
|
||||
const closed = new Error("The egress proxy closed the tunnel", { cause: error });
|
||||
return callback(Object.assign(closed, { code: "ECONNRESET" }), null);
|
||||
}
|
||||
callback(...result);
|
||||
});
|
||||
}
|
||||
|
||||
function egressProxyTransport(proxy: UpstreamProxy): Promise<DownloadFetch> {
|
||||
const uri = `http://${proxy.host.includes(":") ? `[${proxy.host}]` : proxy.host}:${proxy.port}`;
|
||||
const key = `${uri} ${proxy.authorization ?? ""}`;
|
||||
let transport = proxyTransports.get(key);
|
||||
if (!transport) {
|
||||
transport = importUndiciPackageEvenUnderBun().then(({ ProxyAgent, fetch: undiciFetch }) => {
|
||||
const dispatcher = new ProxyAgent({ uri, token: proxy.authorization });
|
||||
return (target, options) =>
|
||||
undiciFetch(target, { ...(options as object), dispatcher }) as unknown as Promise<Response>;
|
||||
});
|
||||
transport = importUndiciPackageEvenUnderBun().then(
|
||||
({ Pool, ProxyAgent, fetch: undiciFetch }) => {
|
||||
const dispatcher = new ProxyAgent({
|
||||
uri,
|
||||
token: proxy.authorization,
|
||||
factory: (origin, options) =>
|
||||
new Pool(origin, {
|
||||
...options,
|
||||
connect: failRequestOnDroppedTunnel(
|
||||
(options as { connect: buildConnector.connector }).connect,
|
||||
),
|
||||
}),
|
||||
});
|
||||
return (target, options) =>
|
||||
undiciFetch(target, {
|
||||
...(options as object),
|
||||
dispatcher,
|
||||
}) as unknown as Promise<Response>;
|
||||
},
|
||||
);
|
||||
proxyTransports.set(key, transport);
|
||||
}
|
||||
return transport;
|
||||
|
||||
Reference in New Issue
Block a user