Skip to content

Commit

Permalink
fix: don't override user defined MaxListeners (#3372)
Browse files Browse the repository at this point in the history
fawazahmed0 authored Jun 26, 2024
1 parent 040d9da commit 71358de
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
@@ -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')

@@ -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 {}

16 changes: 16 additions & 0 deletions test/fetch/max-listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const { setMaxListeners, getMaxListeners, defaultMaxListeners } = require('events')
const { test } = require('node:test')
const assert = require('node:assert')
const { Request } = require('../..')

test('test max listeners', (t) => {
const controller = new AbortController()
setMaxListeners(Infinity, controller.signal)
for (let i = 0; i <= defaultMaxListeners; i++) {
// eslint-disable-next-line no-new
new Request('http://asd', { signal: controller.signal })
}
assert.strictEqual(getMaxListeners(controller.signal), Infinity)
})

0 comments on commit 71358de

Please sign in to comment.