Skip to content
Closed
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
Next Next commit
events: use internal/validators in event_target.js
  • Loading branch information
lundibundi committed Jun 19, 2020
commit 475bfe5a96a0f0a8f15086618c652275d217669e
22 changes: 8 additions & 14 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ const {
Object,
Set,
Symbol,
NumberIsNaN,
SymbolToStringTag,
} = primordials;

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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down