This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
chore: move withTimeoutOption to core-utils #3407
Merged
achingbrain
merged 2 commits into
master
from
chore/move-with-timeout-option-to-core-utils
Nov 18, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
|
||
class TimeoutError extends Error { | ||
constructor (message = 'request timed out') { | ||
super(message) | ||
this.name = 'TimeoutError' | ||
this.code = TimeoutError.code | ||
} | ||
} | ||
|
||
TimeoutError.code = 'ERR_TIMEOUT' | ||
exports.TimeoutError = TimeoutError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
'use strict' | ||
|
||
/** | ||
* @template {any[]} ARGS | ||
* @template R | ||
* @typedef {(...args: ARGS) => R} Fn | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* eslint-disable no-unreachable */ | ||
'use strict' | ||
|
||
const TimeoutController = require('timeout-abort-controller') | ||
const { anySignal } = require('any-signal') | ||
const parseDuration = require('parse-duration').default | ||
const { TimeoutError } = require('./errors') | ||
|
||
/** | ||
* @template {any[]} ARGS | ||
* @template {Promise<any> | AsyncIterable<any>} R - The return type of `fn` | ||
* @param {Fn<ARGS, R>} fn | ||
* @param {number} [optionsArgIndex] | ||
* @returns {Fn<ARGS, R>} | ||
*/ | ||
function withTimeoutOption (fn, optionsArgIndex) { | ||
// eslint-disable-next-line | ||
return /** @returns {R} */(/** @type {ARGS} */...args) => { | ||
const options = args[optionsArgIndex == null ? args.length - 1 : optionsArgIndex] | ||
if (!options || !options.timeout) return fn(...args) | ||
|
||
const timeout = typeof options.timeout === 'string' | ||
? parseDuration(options.timeout) | ||
: options.timeout | ||
|
||
const controller = new TimeoutController(timeout) | ||
|
||
options.signal = anySignal([options.signal, controller.signal]) | ||
|
||
const fnRes = fn(...args) | ||
// eslint-disable-next-line promise/param-names | ||
const timeoutPromise = new Promise((_resolve, reject) => { | ||
controller.signal.addEventListener('abort', () => { | ||
reject(new TimeoutError()) | ||
}) | ||
}) | ||
|
||
const start = Date.now() | ||
|
||
const maybeThrowTimeoutError = () => { | ||
if (controller.signal.aborted) { | ||
throw new TimeoutError() | ||
} | ||
|
||
const timeTaken = Date.now() - start | ||
|
||
// if we have starved the event loop by adding microtasks, we could have | ||
// timed out already but the TimeoutController will never know because it's | ||
// setTimeout will not fire until we stop adding microtasks | ||
if (timeTaken > timeout) { | ||
controller.abort() | ||
throw new TimeoutError() | ||
} | ||
} | ||
|
||
if (fnRes[Symbol.asyncIterator]) { | ||
// @ts-ignore | ||
return (async function * () { | ||
const it = fnRes[Symbol.asyncIterator]() | ||
|
||
try { | ||
while (true) { | ||
const { value, done } = await Promise.race([it.next(), timeoutPromise]) | ||
|
||
if (done) { | ||
break | ||
} | ||
|
||
maybeThrowTimeoutError() | ||
|
||
yield value | ||
} | ||
} catch (err) { | ||
maybeThrowTimeoutError() | ||
|
||
throw err | ||
} finally { | ||
controller.clear() | ||
|
||
if (it.return) { | ||
it.return() | ||
} | ||
} | ||
})() | ||
} | ||
|
||
// @ts-ignore | ||
return (async () => { | ||
try { | ||
const res = await Promise.race([fnRes, timeoutPromise]) | ||
|
||
maybeThrowTimeoutError() | ||
|
||
return res | ||
} catch (err) { | ||
maybeThrowTimeoutError() | ||
|
||
throw err | ||
} finally { | ||
controller.clear() | ||
} | ||
})() | ||
} | ||
} | ||
|
||
module.exports = withTimeoutOption |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@achingbrain this seems to have introduced a regression, because
Fn
typedef ended up inindex.js
which is not imported here. Which for some reason TS still seems to kind of pick up, but it no longer behaves correctly & causing returned function to be inferred as any.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the whole
Fn
type is kind of redundant and we should probably change type annotation forwithTimeoutOptions
to something like this instead: