|
| 1 | +/* eslint-disable no-unreachable */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const TimeoutController = require('timeout-abort-controller') |
| 5 | +const { anySignal } = require('any-signal') |
| 6 | +const parseDuration = require('parse-duration').default |
| 7 | +const { TimeoutError } = require('./errors') |
| 8 | + |
| 9 | +/** |
| 10 | + * @template {any[]} ARGS |
| 11 | + * @template {Promise<any> | AsyncIterable<any>} R - The return type of `fn` |
| 12 | + * @param {Fn<ARGS, R>} fn |
| 13 | + * @param {number} [optionsArgIndex] |
| 14 | + * @returns {Fn<ARGS, R>} |
| 15 | + */ |
| 16 | +function withTimeoutOption (fn, optionsArgIndex) { |
| 17 | + // eslint-disable-next-line |
| 18 | + return /** @returns {R} */(/** @type {ARGS} */...args) => { |
| 19 | + const options = args[optionsArgIndex == null ? args.length - 1 : optionsArgIndex] |
| 20 | + if (!options || !options.timeout) return fn(...args) |
| 21 | + |
| 22 | + const timeout = typeof options.timeout === 'string' |
| 23 | + ? parseDuration(options.timeout) |
| 24 | + : options.timeout |
| 25 | + |
| 26 | + const controller = new TimeoutController(timeout) |
| 27 | + |
| 28 | + options.signal = anySignal([options.signal, controller.signal]) |
| 29 | + |
| 30 | + const fnRes = fn(...args) |
| 31 | + // eslint-disable-next-line promise/param-names |
| 32 | + const timeoutPromise = new Promise((_resolve, reject) => { |
| 33 | + controller.signal.addEventListener('abort', () => { |
| 34 | + reject(new TimeoutError()) |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + const start = Date.now() |
| 39 | + |
| 40 | + const maybeThrowTimeoutError = () => { |
| 41 | + if (controller.signal.aborted) { |
| 42 | + throw new TimeoutError() |
| 43 | + } |
| 44 | + |
| 45 | + const timeTaken = Date.now() - start |
| 46 | + |
| 47 | + // if we have starved the event loop by adding microtasks, we could have |
| 48 | + // timed out already but the TimeoutController will never know because it's |
| 49 | + // setTimeout will not fire until we stop adding microtasks |
| 50 | + if (timeTaken > timeout) { |
| 51 | + controller.abort() |
| 52 | + throw new TimeoutError() |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (fnRes[Symbol.asyncIterator]) { |
| 57 | + // @ts-ignore |
| 58 | + return (async function * () { |
| 59 | + const it = fnRes[Symbol.asyncIterator]() |
| 60 | + |
| 61 | + try { |
| 62 | + while (true) { |
| 63 | + const { value, done } = await Promise.race([it.next(), timeoutPromise]) |
| 64 | + |
| 65 | + if (done) { |
| 66 | + break |
| 67 | + } |
| 68 | + |
| 69 | + maybeThrowTimeoutError() |
| 70 | + |
| 71 | + yield value |
| 72 | + } |
| 73 | + } catch (err) { |
| 74 | + maybeThrowTimeoutError() |
| 75 | + |
| 76 | + throw err |
| 77 | + } finally { |
| 78 | + controller.clear() |
| 79 | + |
| 80 | + if (it.return) { |
| 81 | + it.return() |
| 82 | + } |
| 83 | + } |
| 84 | + })() |
| 85 | + } |
| 86 | + |
| 87 | + // @ts-ignore |
| 88 | + return (async () => { |
| 89 | + try { |
| 90 | + const res = await Promise.race([fnRes, timeoutPromise]) |
| 91 | + |
| 92 | + maybeThrowTimeoutError() |
| 93 | + |
| 94 | + return res |
| 95 | + } catch (err) { |
| 96 | + maybeThrowTimeoutError() |
| 97 | + |
| 98 | + throw err |
| 99 | + } finally { |
| 100 | + controller.clear() |
| 101 | + } |
| 102 | + })() |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +module.exports = withTimeoutOption |
0 commit comments