Skip to content

Commit eb81bb3

Browse files
committed
events: validate options of on and once
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
1 parent 4830a6c commit eb81bb3

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

lib/events.js

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const {
8181
validateAbortSignal,
8282
validateBoolean,
8383
validateFunction,
84+
validateObject,
8485
validateString,
8586
} = require('internal/validators');
8687

@@ -931,6 +932,7 @@ function getEventListeners(emitterOrTarget, type) {
931932
* @returns {Promise}
932933
*/
933934
async function once(emitter, name, options = kEmptyObject) {
935+
validateObject(options, 'options');
934936
const signal = options?.signal;
935937
validateAbortSignal(signal, 'options.signal');
936938
if (signal?.aborted)
@@ -1015,6 +1017,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
10151017
*/
10161018
function on(emitter, event, options = {}) {
10171019
// Parameters validation
1020+
validateObject(options, 'options');
10181021
const signal = options.signal;
10191022
validateAbortSignal(signal, 'options.signal');
10201023
if (signal?.aborted)

test/parallel/test-events-on-async-iterator.js

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ async function invalidArgType() {
4040
code: 'ERR_INVALID_ARG_TYPE',
4141
name: 'TypeError',
4242
}));
43+
44+
const ee = new EventEmitter();
45+
46+
[1, 'hi', null, false].map((options) => {
47+
return assert.throws(() => on(ee, 'foo', options), common.expectsError({
48+
code: 'ERR_INVALID_ARG_TYPE',
49+
name: 'TypeError',
50+
}));
51+
});
4352
}
4453

4554
async function error() {

test/parallel/test-events-once.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
const common = require('../common');
55
const { once, EventEmitter } = require('events');
66
const {
7-
strictEqual,
87
deepStrictEqual,
98
fail,
109
rejects,
10+
strictEqual,
1111
} = require('assert');
1212
const { kEvents } = require('internal/event_target');
1313

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

27-
async function onceAnEventWithNullOptions() {
27+
async function onceAnEventWithInvalidOptions() {
2828
const ee = new EventEmitter();
2929

30-
process.nextTick(() => {
31-
ee.emit('myevent', 42);
32-
});
33-
34-
const [value] = await once(ee, 'myevent', null);
35-
strictEqual(value, 42);
30+
await Promise.all([1, 'hi', null, false].map((options) => {
31+
return rejects(once(ee, 'myevent', options), {
32+
code: 'ERR_INVALID_ARG_TYPE',
33+
});
34+
}));
3635
}
3736

38-
3937
async function onceAnEventWithTwoArgs() {
4038
const ee = new EventEmitter();
4139

@@ -248,7 +246,7 @@ async function eventTargetAbortSignalAfterEvent() {
248246

249247
Promise.all([
250248
onceAnEvent(),
251-
onceAnEventWithNullOptions(),
249+
onceAnEventWithInvalidOptions(),
252250
onceAnEventWithTwoArgs(),
253251
catchesErrors(),
254252
catchesErrorsWithAbortSignal(),

0 commit comments

Comments
 (0)