Skip to content

Commit

Permalink
fix(fetch): third party abortcontrollers throwing errors (nodejs#2002)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent cfd0adb commit 472cae0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,13 @@ class Request {
ac.abort(this.reason)
}

if (getEventListeners(signal, 'abort').length >= defaultMaxListeners) {
setMaxListeners(100, signal)
}
// Third-party AbortControllers may not work with these.
// See https://github.com/nodejs/undici/pull/1910#issuecomment-1464495619
try {
if (getEventListeners(signal, 'abort').length >= defaultMaxListeners) {
setMaxListeners(100, signal)
}
} catch {}

signal.addEventListener('abort', abort, { once: true })
requestFinalizer.register(this, { signal, abort })
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"husky": "^8.0.1",
"import-fresh": "^3.3.0",
"jest": "^29.0.2",
"jsdom": "^21.1.0",
"jsfuzz": "^1.0.15",
"mocha": "^10.0.0",
"p-timeout": "^3.2.0",
Expand Down
22 changes: 22 additions & 0 deletions test/fetch/jsdom-abortcontroller-1910-1464495619.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const { test } = require('tap')
const { createServer } = require('http')
const { once } = require('events')
const { fetch } = require('../..')
const { JSDOM } = require('jsdom')

// https://github.com/nodejs/undici/pull/1910#issuecomment-1464495619
test('third party AbortControllers', async (t) => {
const server = createServer((_, res) => res.end()).listen(0)

t.teardown(server.close.bind(server))
await once(server, 'listening')

const { AbortController } = new JSDOM().window
const controller = new AbortController()

await t.resolves(fetch(`http://localhost:${server.address().port}`, {
signal: controller.signal
}))
})

0 comments on commit 472cae0

Please sign in to comment.