Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7ace5ab

Browse filesBrowse files
authored
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 PR-URL: #46018 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent b3ec13d commit 7ace5ab
Copy full SHA for 7ace5ab

File tree

Expand file treeCollapse file tree

3 files changed

+20
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+20
-10
lines changed
Open diff view settings
Collapse file

‎lib/events.js‎

Copy file name to clipboardExpand all lines: lib/events.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const {
8181
validateBoolean,
8282
validateFunction,
8383
validateNumber,
84+
validateObject,
8485
validateString,
8586
} = require('internal/validators');
8687

@@ -960,6 +961,7 @@ function getMaxListeners(emitterOrTarget) {
960961
* @returns {Promise}
961962
*/
962963
async function once(emitter, name, options = kEmptyObject) {
964+
validateObject(options, 'options');
963965
const signal = options?.signal;
964966
validateAbortSignal(signal, 'options.signal');
965967
if (signal?.aborted)
@@ -1047,6 +1049,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
10471049
*/
10481050
function on(emitter, event, options = kEmptyObject) {
10491051
// Parameters validation
1052+
validateObject(options, 'options');
10501053
const signal = options.signal;
10511054
validateAbortSignal(signal, 'options.signal');
10521055
if (signal?.aborted)
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-events-on-async-iterator.js
+9Lines changed: 9 additions & 0 deletions
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, () => {}, Symbol(), 1n].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() {
Collapse file

‎test/parallel/test-events-once.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-events-once.js
+8-10Lines changed: 8 additions & 10 deletions
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, () => {}, Symbol(), 1n].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

@@ -267,7 +265,7 @@ async function eventTargetAbortSignalAfterEvent() {
267265

268266
Promise.all([
269267
onceAnEvent(),
270-
onceAnEventWithNullOptions(),
268+
onceAnEventWithInvalidOptions(),
271269
onceAnEventWithTwoArgs(),
272270
catchesErrors(),
273271
catchesErrorsWithAbortSignal(),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.