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

events: refactor to use validator #45448

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const {
ErrorCaptureStackTrace,
FunctionPrototypeBind,
FunctionPrototypeCall,
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
ObjectCreate,
ObjectDefineProperty,
Expand Down Expand Up @@ -70,7 +69,6 @@ const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_THIS,
ERR_OUT_OF_RANGE,
ERR_UNHANDLED_ERROR
},
genericNodeError,
Expand All @@ -81,6 +79,7 @@ const {
validateAbortSignal,
validateBoolean,
validateFunction,
validateNumber,
validateString,
} = require('internal/validators');

Expand Down Expand Up @@ -279,11 +278,7 @@ ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {
return defaultMaxListeners;
},
set: function(arg) {
if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
throw new ERR_OUT_OF_RANGE('defaultMaxListeners',
'a non-negative number',
arg);
}
validateNumber(arg, 'defaultMaxListeners', 0);
defaultMaxListeners = arg;
}
});
Expand Down Expand Up @@ -313,8 +308,7 @@ ObjectDefineProperties(EventEmitter, {
*/
EventEmitter.setMaxListeners =
function(n = defaultMaxListeners, ...eventTargets) {
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n))
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
validateNumber(n, 'n', 0);
if (eventTargets.length === 0) {
defaultMaxListeners = n;
} else {
Expand Down Expand Up @@ -410,9 +404,7 @@ function emitUnhandledRejectionOrErr(ee, err, type, args) {
* @returns {EventEmitter}
*/
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
}
validateNumber(n, 'n', 0);
this._maxListeners = n;
return this;
};
Expand Down
99 changes: 70 additions & 29 deletions test/parallel/test-event-emitter-max-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,86 @@ const common = require('../common');
const assert = require('assert');
const events = require('events');
const { inspect } = require('util');
const e = new events.EventEmitter();

e.on('maxListeners', common.mustCall());
const throwInvalidType = [true, 'string'];
const throwOutOfRange = [-1, NaN];

// Should not corrupt the 'maxListeners' queue.
e.setMaxListeners(42);
// for EventEmitter.prototype.setMaxListeners
{
const e = new events.EventEmitter();

const throwsObjs = [NaN, -1, 'and even this'];
e.on('maxListeners', common.mustCall());

for (const obj of throwsObjs) {
assert.throws(
() => e.setMaxListeners(obj),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "n" is out of range. ' +
`It must be a non-negative number. Received ${inspect(obj)}`
}
);
// Should not corrupt the 'maxListeners' queue.
e.setMaxListeners(42);

assert.throws(
() => events.defaultMaxListeners = obj,
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "defaultMaxListeners" is out of range. ' +
`It must be a non-negative number. Received ${inspect(obj)}`
}
);
for (const obj of throwInvalidType) {
assert.throws(
() => e.setMaxListeners(obj),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: `The "n" argument must be of type number. Received type ${typeof obj} (${inspect(obj)})`
Lxxyx marked this conversation as resolved.
Show resolved Hide resolved
}
);
}

for (const obj of throwOutOfRange) {
assert.throws(
() => e.setMaxListeners(obj),
{
code: 'ERR_OUT_OF_R