From 475bfe5a96a0f0a8f15086618c652275d217669e Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Sat, 30 May 2020 21:22:04 +0300 Subject: [PATCH 1/2] events: use internal/validators in event_target.js --- lib/internal/event_target.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 8c4ea11a14ed8b..bccb2e0edc6a0d 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -7,7 +7,6 @@ const { Object, Set, Symbol, - NumberIsNaN, SymbolToStringTag, } = primordials; @@ -15,10 +14,10 @@ const { codes: { ERR_INVALID_ARG_TYPE, ERR_EVENT_RECURSION, - ERR_OUT_OF_RANGE, ERR_MISSING_ARGS } } = require('internal/errors'); +const { validateInteger, validateObject } = require('internal/validators'); const { customInspectSymbol } = require('internal/util'); const { inspect } = require('util'); @@ -54,11 +53,10 @@ class Event { constructor(type, options) { - if (arguments.length === 0) { + if (arguments.length === 0) throw new ERR_MISSING_ARGS('type'); - } - if (options != null && typeof options !== 'object') - throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + if (options != null) + validateObject(options, 'options'); const { cancelable, bubbles, composed } = { ...options }; this.#cancelable = !!cancelable; this.#bubbles = !!bubbles; @@ -350,9 +348,7 @@ class NodeEventTarget extends EventTarget { } setMaxListeners(n) { - if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) { - throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n); - } + validateInteger(n, 'n', 0); this.#maxListeners = n; return this; } @@ -430,11 +426,9 @@ function validateListener(listener) { } function validateEventListenerOptions(options) { - if (typeof options === 'boolean') { - options = { capture: options }; - } - if (options == null || typeof options !== 'object') - throw new ERR_INVALID_ARG_TYPE('options', 'object', options); + if (typeof options === 'boolean') + return { capture: options }; + validateObject(options, 'options'); const { once = false, capture = false, From 8713e07bcaa59fdbd26c110a91ce06cf1f0cffec Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Sat, 30 May 2020 23:00:43 +0300 Subject: [PATCH 2/2] events: fix depth in customInspectSymbol and clean up --- lib/internal/event_target.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index bccb2e0edc6a0d..21d3774a586a7f 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -2,8 +2,10 @@ const { ArrayFrom, + Boolean, Error, Map, + NumberIsInteger, Object, Set, Symbol, @@ -78,7 +80,7 @@ class Event { return name; const opts = Object.assign({}, options, { - dept: options.depth === null ? null : options.depth - 1 + depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth }); return `${name} ${inspect({ @@ -294,7 +296,7 @@ class EventTarget { this.#emitting.delete(event.type); event[kTarget] = undefined; - return event.defaultPrevented === true ? false : true; + return event.defaultPrevented !== true; } [customInspectSymbol](depth, options) { @@ -303,7 +305,7 @@ class EventTarget { return name; const opts = Object.assign({}, options, { - dept: options.depth === null ? null : options.depth - 1 + depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth }); return `${name} ${inspect({}, opts)}`; @@ -429,15 +431,10 @@ function validateEventListenerOptions(options) { if (typeof options === 'boolean') return { capture: options }; validateObject(options, 'options'); - const { - once = false, - capture = false, - passive = false, - } = options; return { - once: !!once, - capture: !!capture, - passive: !!passive, + once: Boolean(options.once), + capture: Boolean(options.capture), + passive: Boolean(options.passive), }; }