From af1e66be2956cc99d0348c3301f1d3226c9bc1b4 Mon Sep 17 00:00:00 2001 From: Ivo Murrell Date: Wed, 16 Oct 2024 12:41:48 +0100 Subject: [PATCH] fix: support timeouts when using native fetch timeout is a node-fetch extension not present in the spec-compliant native fetch implementation. Use an AbortSignal (available in Node 18+, which is what we currently support) as an alternative so that both node-fetch and native fetch will time out as expected. --- src/poller.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/poller.js b/src/poller.js index 3608d5c..2dce017 100644 --- a/src/poller.js +++ b/src/poller.js @@ -87,7 +87,14 @@ module.exports = EventEmitter => { // later is discarded within pollers const _fetch = this.options.retry ? this.eagerFetch : fetch; - return _fetch (this.url, this.options) + const options = {...this.options} + if (options.timeout) { + // add signal option to support native fetch, but keep timeout option + // too to support node-fetch@<2.3.0 + options.signal = AbortSignal.timeout(options.timeout) + } + + return _fetch (this.url, options) .then ((response) => { const latency = new Date () - time; if (response.ok) {