From 56f9daa0713f12b8a18bc516070c9f926368e3b5 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Wed, 7 Dec 2022 23:53:28 +0900 Subject: [PATCH 1/2] events: refactor to use `validateNumber` Need to use validateNumber for checking `TypeError` --- lib/events.js | 16 +++------- .../test-event-emitter-max-listeners.js | 32 ++++++++++++++----- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/events.js b/lib/events.js index e05a3bc6b8b168..f8c69c9655eb65 100644 --- a/lib/events.js +++ b/lib/events.js @@ -33,7 +33,6 @@ const { ErrorCaptureStackTrace, FunctionPrototypeBind, FunctionPrototypeCall, - NumberIsNaN, NumberMAX_SAFE_INTEGER, ObjectCreate, ObjectDefineProperty, @@ -70,7 +69,6 @@ const { codes: { ERR_INVALID_ARG_TYPE, ERR_INVALID_THIS, - ERR_OUT_OF_RANGE, ERR_UNHANDLED_ERROR }, genericNodeError, @@ -82,6 +80,7 @@ const { validateBoolean, validateFunction, validateString, + validateNumber, } = require('internal/validators'); const kCapture = Symbol('kCapture'); @@ -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; } }); @@ -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, 'setMaxListeners', 0); if (eventTargets.length === 0) { defaultMaxListeners = n; } else { @@ -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, 'setMaxListeners', 0); this._maxListeners = n; return this; }; diff --git a/test/parallel/test-event-emitter-max-listeners.js b/test/parallel/test-event-emitter-max-listeners.js index 7bd1a6d619af23..9b9c2ad0d5d403 100644 --- a/test/parallel/test-event-emitter-max-listeners.js +++ b/test/parallel/test-event-emitter-max-listeners.js @@ -23,7 +23,6 @@ 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()); @@ -31,16 +30,15 @@ e.on('maxListeners', common.mustCall()); // Should not corrupt the 'maxListeners' queue. e.setMaxListeners(42); -const throwsObjs = [NaN, -1, 'and even this']; +const rangeErrorObjs = [NaN, -1]; +const typeErrorObj = 'and even this'; -for (const obj of throwsObjs) { +for (const obj of rangeErrorObjs) { 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)}`, } ); @@ -49,22 +47,40 @@ for (const obj of throwsObjs) { { 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)}`, } ); } +assert.throws( + () => e.setMaxListeners(typeErrorObj), + { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + } +); + +assert.throws( + () => events.defaultMaxListeners = typeErrorObj, + { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + } +); + e.emit('maxListeners'); { const { EventEmitter, defaultMaxListeners } = events; - for (const obj of throwsObjs) { + for (const obj of rangeErrorObjs) { assert.throws(() => EventEmitter.setMaxListeners(obj), { code: 'ERR_OUT_OF_RANGE', }); } + assert.throws(() => EventEmitter.setMaxListeners(typeErrorObj), { + code: 'ERR_INVALID_ARG_TYPE', + }); + assert.throws( () => EventEmitter.setMaxListeners(defaultMaxListeners, 'INVALID_EMITTER'), { code: 'ERR_INVALID_ARG_TYPE' } From 47fa7bae3dfdc27aa171a484edf9279f88a56a36 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Thu, 8 Dec 2022 18:17:11 +0900 Subject: [PATCH 2/2] Apply ASCII order to importing header --- lib/events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index f8c69c9655eb65..0342f9ec7016c2 100644 --- a/lib/events.js +++ b/lib/events.js @@ -79,8 +79,8 @@ const { validateAbortSignal, validateBoolean, validateFunction, - validateString, validateNumber, + validateString, } = require('internal/validators'); const kCapture = Symbol('kCapture');