events, doc: check input in defaultMaxListeners
PR-URL: https://github.com/nodejs/node/pull/11938 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
parent
81ab78e62e
commit
221b03ad20
@ -262,7 +262,8 @@ By default, a maximum of `10` listeners can be registered for any single
|
||||
event. This limit can be changed for individual `EventEmitter` instances
|
||||
using the [`emitter.setMaxListeners(n)`][] method. To change the default
|
||||
for *all* `EventEmitter` instances, the `EventEmitter.defaultMaxListeners`
|
||||
property can be used.
|
||||
property can be used. If this value is not a positive number, a `TypeError`
|
||||
will be thrown.
|
||||
|
||||
Take caution when setting the `EventEmitter.defaultMaxListeners` because the
|
||||
change effects *all* `EventEmitter` instances, including those created before
|
||||
|
@ -56,6 +56,10 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
|
||||
// force global console to be compiled.
|
||||
// see https://github.com/nodejs/node/issues/4467
|
||||
console;
|
||||
// check whether the input is a positive number (whose value is zero or
|
||||
// greater and not a NaN).
|
||||
if (typeof arg !== 'number' || arg < 0 || arg !== arg)
|
||||
throw new TypeError('"defaultMaxListeners" must be a positive number');
|
||||
defaultMaxListeners = arg;
|
||||
}
|
||||
});
|
||||
|
@ -30,16 +30,13 @@ e.on('maxListeners', common.mustCall(function() {}));
|
||||
// Should not corrupt the 'maxListeners' queue.
|
||||
e.setMaxListeners(42);
|
||||
|
||||
assert.throws(function() {
|
||||
e.setMaxListeners(NaN);
|
||||
}, /^TypeError: "n" argument must be a positive number$/);
|
||||
const throwsObjs = [NaN, -1, 'and even this'];
|
||||
|
||||
assert.throws(function() {
|
||||
e.setMaxListeners(-1);
|
||||
}, /^TypeError: "n" argument must be a positive number$/);
|
||||
|
||||
assert.throws(function() {
|
||||
e.setMaxListeners('and even this');
|
||||
}, /^TypeError: "n" argument must be a positive number$/);
|
||||
for (const obj of throwsObjs) {
|
||||
assert.throws(() => e.setMaxListeners(obj),
|
||||
/^TypeError: "n" argument must be a positive number$/);
|
||||
assert.throws(() => events.defaultMaxListeners = obj,
|
||||
/^TypeError: "defaultMaxListeners" must be a positive number$/);
|
||||
}
|
||||
|
||||
e.emit('maxListeners');
|
||||
|
Loading…
x
Reference in New Issue
Block a user