Skip to content

Commit

Permalink
events: validate options of on and once
Browse files Browse the repository at this point in the history
Check whether options is object or not to avoid passing
invalid type as options to `on` and `once`.

Refs: https://nodejs.org/dist/latest-v19.x/docs/api/events.html#eventsonceemitter-name-options
PR-URL: #46018
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
deokjinkim authored Sep 29, 2023
1 parent b3ec13d commit 7ace5ab
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
3 changes: 3 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const {
validateBoolean,
validateFunction,
validateNumber,
validateObject,
validateString,
} = require('internal/validators');

Expand Down Expand Up @@ -960,6 +961,7 @@ function getMaxListeners(emitterOrTarget) {
* @returns {Promise}
*/
async function once(emitter, name, options = kEmptyObject) {
validateObject(options, 'options');
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted)
Expand Down Expand Up @@ -1047,6 +1049,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
*/
function on(emitter, event, options = kEmptyObject) {
// Parameters validation
validateObject(options, 'options');
const signal = options.signal;
validateAbortSignal(signal, 'options.signal');
if (signal?.aborted)
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-events-on-async-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ async function invalidArgType() {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}));

const ee = new EventEmitter();

[1, 'hi', null, false, () => {}, Symbol(), 1n].map((options) => {
return assert.throws(() => on(ee, 'foo', options), common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}));
});
}

async function error() {
Expand Down
18 changes: 8 additions & 10 deletions test/parallel/test-events-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
const common = require('../common');
const { once, EventEmitter } = require('events');
const {
strictEqual,
deepStrictEqual,
fail,
rejects,
strictEqual,
} = require('assert');
const { kEvents } = require('internal/event_target');

Expand All @@ -24,18 +24,16 @@ async function onceAnEvent() {
strictEqual(ee.listenerCount('myevent'), 0);
}

async function onceAnEventWithNullOptions() {
async function onceAnEventWithInvalidOptions() {
const ee = new EventEmitter();

process.nextTick(() => {
ee.emit('myevent', 42);
});

const [value] = await once(ee, 'myevent', null);
strictEqual(value, 42);
await Promise.all([1, 'hi', null, false, () => {}, Symbol(), 1n].map((options) => {
return rejects(once(ee, 'myevent', options), {
code: 'ERR_INVALID_ARG_TYPE',
});
}));
}


async function onceAnEventWithTwoArgs() {
const ee = new EventEmitter();

Expand Down Expand Up @@ -267,7 +265,7 @@ async function eventTargetAbortSignalAfterEvent() {

Promise.all([
onceAnEvent(),
onceAnEventWithNullOptions(),
onceAnEventWithInvalidOptions(),
onceAnEventWithTwoArgs(),
catchesErrors(),
catchesErrorsWithAbortSignal(),
Expand Down

0 comments on commit 7ace5ab

Please sign in to comment.