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, + }); };