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: EventTarget use validators and small fix #33663

Closed
Closed
Changes from all commits
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
41 changes: 16 additions & 25 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

const {
ArrayFrom,
Boolean,
Error,
Map,
NumberIsInteger,
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 +55,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 All @@ -80,7 +80,7 @@ class Event {
return name;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, @BridgeAR shouldn't this return this? (as well as the other one)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be ideal. this will just let inspect() to properly know what to do.


const opts = Object.assign({}, options, {
dept: options.depth === null ? null : options.depth - 1
depth: NumberIsInteger(options.depth) ? options.depth - 1 : options.depth
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd assume this was a bug (dept instead of depth) but wanted to point out since I'm not sure how this interaction works (inspecting circular events I assume?).
Also, use NumberIsInteger instead of just null check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is both correct. The user should only ever pass through null or and integer but it would be possible to pass through other types as well.

});

return `${name} ${inspect({
Expand Down Expand Up @@ -296,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) {
Expand All @@ -305,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)}`;
Expand Down Expand Up @@ -350,9 +350,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,20 +428,13 @@ 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);
const {
once = false,
capture = false,
passive = false,
} = options;
if (typeof options === 'boolean')
return { capture: options };
validateObject(options, 'options');
return {
once: !!once,
capture: !!capture,
passive: !!passive,
once: Boolean(options.once),
capture: Boolean(options.capture),
passive: Boolean(options.passive),
};
}

Expand Down