chore: set default http request timeouts (#17200)

* chore: set default http request timeouts

* Update chromium.ts
This commit is contained in:
Max Schmitt 2022-09-09 06:18:57 +02:00 committed by GitHub
parent 9c996f6cbb
commit 5e56bc413a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -39,11 +39,14 @@ export type HTTPRequestParams = {
timeout?: number, timeout?: number,
}; };
export const NET_DEFAULT_TIMEOUT = 30_000;
export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.IncomingMessage) => void, onError: (error: Error) => void) { export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.IncomingMessage) => void, onError: (error: Error) => void) {
const parsedUrl = URL.parse(params.url); const parsedUrl = URL.parse(params.url);
let options: https.RequestOptions = { ...parsedUrl }; let options: https.RequestOptions = { ...parsedUrl };
options.method = params.method || 'GET'; options.method = params.method || 'GET';
options.headers = params.headers; options.headers = params.headers;
const timeout = params.timeout ?? NET_DEFAULT_TIMEOUT;
const proxyURL = getProxyForUrl(params.url); const proxyURL = getProxyForUrl(params.url);
if (proxyURL) { if (proxyURL) {
@ -74,16 +77,16 @@ export function httpRequest(params: HTTPRequestParams, onResponse: (r: http.Inco
https.request(options, requestCallback) : https.request(options, requestCallback) :
http.request(options, requestCallback); http.request(options, requestCallback);
request.on('error', onError); request.on('error', onError);
if (params.timeout !== undefined) { if (timeout !== undefined) {
const rejectOnTimeout = () => { const rejectOnTimeout = () => {
onError(new Error(`Request to ${params.url} timed out after ${params.timeout}ms`)); onError(new Error(`Request to ${params.url} timed out after ${timeout}ms`));
request.abort(); request.abort();
}; };
if (params.timeout <= 0) { if (timeout <= 0) {
rejectOnTimeout(); rejectOnTimeout();
return; return;
} }
request.setTimeout(params.timeout, rejectOnTimeout); request.setTimeout(timeout, rejectOnTimeout);
} }
request.end(params.data); request.end(params.data);
} }

View File

@ -32,6 +32,7 @@ import { Browser } from '../browser';
import type * as types from '../types'; import type * as types from '../types';
import type * as channels from '../../protocol/channels'; import type * as channels from '../../protocol/channels';
import type { HTTPRequestParams } from '../../common/netUtils'; import type { HTTPRequestParams } from '../../common/netUtils';
import { NET_DEFAULT_TIMEOUT } from '../../common/netUtils';
import { fetchData } from '../../common/netUtils'; import { fetchData } from '../../common/netUtils';
import { getUserAgent } from '../../common/userAgent'; import { getUserAgent } from '../../common/userAgent';
import { debugMode, headersArrayToObject, streamToString, wrapInASCIIBox } from '../../utils'; import { debugMode, headersArrayToObject, streamToString, wrapInASCIIBox } from '../../utils';
@ -365,7 +366,9 @@ async function urlToWSEndpoint(progress: Progress, endpointURL: string) {
const httpURL = endpointURL.endsWith('/') ? `${endpointURL}json/version/` : `${endpointURL}/json/version/`; const httpURL = endpointURL.endsWith('/') ? `${endpointURL}json/version/` : `${endpointURL}/json/version/`;
const request = endpointURL.startsWith('https') ? https : http; const request = endpointURL.startsWith('https') ? https : http;
const json = await new Promise<string>((resolve, reject) => { const json = await new Promise<string>((resolve, reject) => {
request.get(httpURL, resp => { request.get(httpURL, {
timeout: NET_DEFAULT_TIMEOUT,
}, resp => {
if (resp.statusCode! < 200 || resp.statusCode! >= 400) { if (resp.statusCode! < 200 || resp.statusCode! >= 400) {
reject(new Error(`Unexpected status ${resp.statusCode} when connecting to ${httpURL}.\n` + reject(new Error(`Unexpected status ${resp.statusCode} when connecting to ${httpURL}.\n` +
`This does not look like a DevTools server, try connecting via ws://.`)); `This does not look like a DevTools server, try connecting via ws://.`));