Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't override user defined MaxListeners #3372

Merged
merged 10 commits into from
Jun 26, 2024
4 changes: 1 addition & 3 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const { webidl } = require('./webidl')
const { URLSerializer } = require('./data-url')
const { kConstruct } = require('../../core/symbols')
const assert = require('node:assert')
const { getMaxListeners, setMaxListeners, getEventListeners, defaultMaxListeners } = require('node:events')
const { getMaxListeners, setMaxListeners, defaultMaxListeners } = require('node:events')

const kAbortController = Symbol('abortController')

Expand Down Expand Up @@ -429,8 +429,6 @@ class Request {
// This is only available in node >= v19.9.0
if (typeof getMaxListeners === 'function' && getMaxListeners(signal) === defaultMaxListeners) {
setMaxListeners(1500, signal)
} else if (getEventListeners(signal, 'abort').length >= defaultMaxListeners) {
setMaxListeners(1500, signal)
}
} catch {}

Expand Down
13 changes: 13 additions & 0 deletions test/fetch/max-listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { setMaxListeners, getMaxListeners } = require('events')
fawazahmed0 marked this conversation as resolved.
Show resolved Hide resolved
const { test } = require('node:test')
const assert = require('node:assert')
const { fetch } = require('../..')
fawazahmed0 marked this conversation as resolved.
Show resolved Hide resolved

test('test max listeners', async (t) => {
const controller = new AbortController();
setMaxListeners(Infinity, controller.signal)
let url = URL.createObjectURL(new Blob())
for(let i=0;i<1600;i++)
fetch(url, { signal: controller.signal })
fawazahmed0 marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(getMaxListeners(controller.signal), Infinity);
});