Skip to content

Commit

Permalink
fix: pass the timeout to the resolveProtocol calls (#131)
Browse files Browse the repository at this point in the history
ALPN negotiation with an unresponsive server caused prolonged hangs, as
it didn't respect the specified timeouts.

Fixes #130
  • Loading branch information
barjin committed Feb 16, 2024
1 parent d72dc83 commit 22661df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/hooks/browser-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof http2.auto.resolveProtocol>) => 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<void> {
Expand Down
6 changes: 5 additions & 1 deletion src/hooks/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/resolve-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<string, string>,
resolveAlpnQueue,
connectWithProxy,
);

return async (...args: Parameters<ResolveProtocolFunction>) => resolveProtocol({
...args[0],
timeout,
});
};

0 comments on commit 22661df

Please sign in to comment.