From 22661dfc6f2e8b10f143cd417a1fe43338770659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20B=C3=A4r?= Date: Fri, 16 Feb 2024 10:41:28 +0100 Subject: [PATCH] fix: pass the timeout to the `resolveProtocol` calls (#131) ALPN negotiation with an unresponsive server caused prolonged hangs, as it didn't respect the specified timeouts. Fixes #130 --- src/hooks/browser-headers.ts | 7 +++++-- src/hooks/http2.ts | 6 +++++- src/resolve-protocol.ts | 9 +++++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/hooks/browser-headers.ts b/src/hooks/browser-headers.ts index b3285fa..eb9140b 100644 --- a/src/hooks/browser-headers.ts +++ b/src/hooks/browser-headers.ts @@ -39,10 +39,13 @@ const getResolveProtocolFunction = (options: Options, proxyUrl: string | undefin } if (proxyUrl) { - return createResolveProtocol(proxyUrl, sessionData as any); + return createResolveProtocol(proxyUrl, sessionData as any, Math.min(options?.timeout?.connect ?? 60_000, options?.timeout?.request ?? 60_000)); } - return http2.auto.resolveProtocol; + return (...args: Parameters) => http2.auto.resolveProtocol({ + ...args[0], + timeout: Math.min(options?.timeout?.connect ?? 60_000, options?.timeout?.request ?? 60_000), + }); }; export async function browserHeadersHook(options: Options): Promise { diff --git a/src/hooks/http2.ts b/src/hooks/http2.ts index 30923ba..c0037da 100644 --- a/src/hooks/http2.ts +++ b/src/hooks/http2.ts @@ -11,7 +11,11 @@ export function http2Hook(options: Options): void { options.request = (url, requestOptions, callback) => { const typedRequestOptions = requestOptions as AutoRequestOptions; if (proxyUrl) { - typedRequestOptions.resolveProtocol = createResolveProtocol(proxyUrl, sessionData as any); + typedRequestOptions.resolveProtocol = createResolveProtocol( + proxyUrl, + sessionData as any, + Math.min(options?.timeout?.connect ?? 60_000, options?.timeout?.request ?? 60_000), + ); } return auto(url, typedRequestOptions, callback); diff --git a/src/resolve-protocol.ts b/src/resolve-protocol.ts index 1e03998..6c8ed95 100644 --- a/src/resolve-protocol.ts +++ b/src/resolve-protocol.ts @@ -73,7 +73,7 @@ export interface ProtocolCache { resolveAlpnQueue?: typeof defaults.resolveAlpnQueue; } -export const createResolveProtocol = (proxyUrl: string, sessionData?: ProtocolCache): ResolveProtocolFunction => { +export const createResolveProtocol = (proxyUrl: string, sessionData?: ProtocolCache, timeout?: number): ResolveProtocolFunction => { let { protocolCache, resolveAlpnQueue } = defaults; if (sessionData) { @@ -89,9 +89,14 @@ export const createResolveProtocol = (proxyUrl: string, sessionData?: ProtocolCa return connect(proxyUrl, pOptions, pCallback); }; - return auto.createResolveProtocol( + const resolveProtocol: ResolveProtocolFunction = auto.createResolveProtocol( protocolCache as unknown as Map, resolveAlpnQueue, connectWithProxy, ); + + return async (...args: Parameters) => resolveProtocol({ + ...args[0], + timeout, + }); };