diff --git a/source/normalize-arguments.ts b/source/normalize-arguments.ts index bd8aec31d..73e078f36 100644 --- a/source/normalize-arguments.ts +++ b/source/normalize-arguments.ts @@ -73,13 +73,9 @@ export const preNormalizeArguments = (options: Options, defaults?: NormalizedOpt } if (is.number(options.timeout)) { - options.gotTimeout = {request: options.timeout}; - } else if (is.object(options.timeout)) { - options.gotTimeout = options.timeout; + options.timeout = {request: options.timeout}; } - delete options.timeout; - const {retry} = options; options.retry = { calculateDelay: retryObject => retryObject.computedValue, diff --git a/source/request-as-event-emitter.ts b/source/request-as-event-emitter.ts index 4c938da55..c13c8d8dc 100644 --- a/source/request-as-event-emitter.ts +++ b/source/request-as-event-emitter.ts @@ -99,6 +99,8 @@ export default (options: NormalizedOptions, input?: TransformStream) => { let timings: Timings; const handleResponse = async (response: http.ServerResponse | ResponseObject): Promise => { + options.timeout = timeout; + try { /* istanbul ignore next: fixes https://github.com/electron/electron/blob/cbb460d47628a7a146adf4419ed48550a98b2923/lib/browser/api/net.js#L59-L65 */ if (options.useElectronNet) { @@ -194,6 +196,7 @@ export default (options: NormalizedOptions, input?: TransformStream) => { } currentRequest = request; + options.timeout = timeout; const onError = (error: Error): void => { const isTimedOutError = error instanceof TimedOutTimeoutError; @@ -235,8 +238,8 @@ export default (options: NormalizedOptions, input?: TransformStream) => { uploadProgress(request, emitter, uploadBodySize); - if (options.gotTimeout) { - timedOut(request, options.gotTimeout, options); + if (options.timeout) { + timedOut(request, options.timeout, options); } emitter.emit('request', request); @@ -269,9 +272,12 @@ export default (options: NormalizedOptions, input?: TransformStream) => { } }; + const {timeout} = options; + delete options.timeout; + if (options.cache) { const cacheableRequest = new CacheableRequest(requestFn, options.cache); - const cacheRequest = cacheableRequest(options as https.RequestOptions, handleResponse); + const cacheRequest = cacheableRequest(options as unknown as https.RequestOptions, handleResponse); cacheRequest.once('error', error => { if (error instanceof CacheableRequest.RequestError) { @@ -286,7 +292,7 @@ export default (options: NormalizedOptions, input?: TransformStream) => { // Catches errors thrown by calling requestFn(...) try { // @ts-ignore TS complains that URLSearchParams is not the same as URLSearchParams - handleRequest(requestFn(options as any as URL, handleResponse)); + handleRequest(requestFn(options as unknown as URL, handleResponse)); } catch (error) { emitError(new RequestError(error, options)); } diff --git a/source/utils/types.ts b/source/utils/types.ts index 927660127..505e3f8dc 100644 --- a/source/utils/types.ts +++ b/source/utils/types.ts @@ -164,9 +164,9 @@ export interface Options extends Except, 'timeout' | 'dnsCache' | 'retry' | 'auth' | 'body' | 'port'> { +export interface NormalizedOptions extends Except, 'dnsCache' | 'retry' | 'auth' | 'body' | 'port'> { hooks: Hooks; - gotTimeout: Required; + timeout: Required; retry: NormalizedRetryOptions; lookup?: CacheableLookup['lookup']; readonly prefixUrl: string; diff --git a/test/hooks.ts b/test/hooks.ts index 83a523184..eebdab62b 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -538,3 +538,19 @@ test('catches HTTPErrors', withServer, async (t, _server, got) => { } })); }); + +test('timeout can be modified using a hook', withServer, async (t, server, got) => { + server.get('/', () => {}); + + await t.throwsAsync(got({ + timeout: 1000, + hooks: { + init: [ + options => { + options.timeout.request = 500; + } + ] + }, + retry: 0 + }), 'Timeout awaiting \'request\' for 500ms'); +});