From 02723d8fa6b70ab14f38b4fa24c24dc1bfa584e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20B=C3=A4r?= Date: Thu, 15 Feb 2024 17:41:12 +0100 Subject: [PATCH 1/2] fix: pass the timeout to the `resolveProtocol` calls --- 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..e75af53 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, options?.timeout?.connect ?? options?.timeout?.request); } - return http2.auto.resolveProtocol; + return (...args: Parameters) => http2.auto.resolveProtocol({ + ...args[0], + timeout: options?.timeout?.connect ?? options?.timeout?.request, + }); }; export async function browserHeadersHook(options: Options): Promise { diff --git a/src/hooks/http2.ts b/src/hooks/http2.ts index 30923ba..2231350 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, + options?.timeout?.connect ?? options?.timeout?.request, + ); } 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, + }); }; From c6c031b11dd90b089c46fac8d9dc6450c267c36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20B=C3=A4r?= Date: Fri, 16 Feb 2024 10:38:00 +0100 Subject: [PATCH 2/2] fix: update the timeout logic --- src/hooks/browser-headers.ts | 4 ++-- src/hooks/http2.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hooks/browser-headers.ts b/src/hooks/browser-headers.ts index e75af53..eb9140b 100644 --- a/src/hooks/browser-headers.ts +++ b/src/hooks/browser-headers.ts @@ -39,12 +39,12 @@ const getResolveProtocolFunction = (options: Options, proxyUrl: string | undefin } if (proxyUrl) { - return createResolveProtocol(proxyUrl, sessionData as any, options?.timeout?.connect ?? options?.timeout?.request); + return createResolveProtocol(proxyUrl, sessionData as any, Math.min(options?.timeout?.connect ?? 60_000, options?.timeout?.request ?? 60_000)); } return (...args: Parameters) => http2.auto.resolveProtocol({ ...args[0], - timeout: options?.timeout?.connect ?? options?.timeout?.request, + timeout: Math.min(options?.timeout?.connect ?? 60_000, options?.timeout?.request ?? 60_000), }); }; diff --git a/src/hooks/http2.ts b/src/hooks/http2.ts index 2231350..c0037da 100644 --- a/src/hooks/http2.ts +++ b/src/hooks/http2.ts @@ -14,7 +14,7 @@ export function http2Hook(options: Options): void { typedRequestOptions.resolveProtocol = createResolveProtocol( proxyUrl, sessionData as any, - options?.timeout?.connect ?? options?.timeout?.request, + Math.min(options?.timeout?.connect ?? 60_000, options?.timeout?.request ?? 60_000), ); }