Skip to content

Commit

Permalink
fix: support timeouts when using native fetch
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ivomurrell committed Oct 16, 2024
1 parent 0f6d1b4 commit af1e66b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/poller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit af1e66b

Please sign in to comment.