Skip to content

Commit 799339b

Browse files
committed
Workaround nodejs/node#56807
1 parent 0f50cf8 commit 799339b

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

registry/lib/constants.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
// event loop.
66
const signalExit = require('signal-exit')
77

8-
const { envAsBoolean, envAsString } = require('./env')
9-
10-
const abortController = new AbortController()
11-
const { signal: abortSignal } = abortController
12-
138
let _browserList
149
function getBrowserList() {
1510
if (_browserList === undefined) {
@@ -134,6 +129,32 @@ function getYarnPkgExtensions() {
134129
return _yarnPkgExtensions
135130
}
136131

132+
const abortController = new AbortController()
133+
134+
const { signal: abortSignal } = abortController
135+
;(() => {
136+
// By manually setting `kMaxEventTargetListeners` on `abortSignal` we avoid:
137+
// TypeError [ERR_INVALID_ARG_TYPE]: The "emitter" argument must be an
138+
// instance of EventEmitter or EventTarget. Received an instance of
139+
// AbortSignal
140+
//
141+
// in some patch releases of Node 18-23 when calling events.getMaxListeners(abortSignal).
142+
// See https://github.com/nodejs/node/pull/56807.
143+
//
144+
// Instead of calling events.setMaxListeners(10, abortSignal) we set the symbol
145+
// property directly to keep the constants initialization platform agnostic and
146+
// not rely on the Node specific 'node:events' module up front.
147+
const symbols = Object.getOwnPropertySymbols(abortSignal)
148+
const kMaxEventTargetListeners = symbols.find(
149+
s => s.description === 'events.maxEventTargetListeners'
150+
)
151+
if (kMaxEventTargetListeners) {
152+
// The default events.defaultMaxListeners value is 10.
153+
// https://nodejs.org/api/events.html#eventsdefaultmaxlisteners
154+
abortSignal[kMaxEventTargetListeners] = 10
155+
}
156+
})()
157+
137158
// Detect ^C, i.e. Ctrl + C.
138159
signalExit.onExit(() => {
139160
abortController.abort()
@@ -431,6 +452,8 @@ const LAZY_SUPPORTS_PROCESS_SEND = () =>
431452
typeof getProcess().send === 'function'
432453

433454
const LAZY_ENV = () => {
455+
// Lazily require('./env').
456+
const { envAsBoolean, envAsString } = require('./env')
434457
const { env } = getProcess()
435458
return Object.freeze({
436459
__proto__: null,
@@ -726,7 +749,7 @@ const lazySkipTestsByEcosystem = () =>
726749
})
727750

728751
const lazySpinner = () =>
729-
// Lazily access require('./spinner').Spinner to avoid cyclical imports.
752+
// Lazily require('./spinner').Spinner to avoid cyclical imports.
730753
require('./spinner').Spinner()
731754

732755
const lazyWin32EnsureTestsByEcosystem = () =>

0 commit comments

Comments
 (0)