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

fix(node/events): Accept only functions as listener arguments #916

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions node/_tools/config.json
Expand Up @@ -10,6 +10,7 @@
"test-assert.js",
"test-assert-async.js",
"test-assert-fail.js",
"test-event-emitter-invalid-listener.js",
"test-event-emitter-listener-count.js"
]
},
Expand Down
@@ -0,0 +1,27 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 15.5.1
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

'use strict';

require('../common');
const assert = require('assert');
const EventEmitter = require('events');

const eventsMethods = ['on', 'once', 'removeListener', 'prependOnceListener'];

// Verify that the listener must be a function for events methods
for (const method of eventsMethods) {
assert.throws(() => {
const ee = new EventEmitter();
ee[method]('foo', null);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "listener" argument must be of type function. ' +
'Received null'
}, `event.${method}('foo', null) should throw the proper error`);
}
10 changes: 10 additions & 0 deletions node/events.ts
Expand Up @@ -23,6 +23,7 @@

import { validateIntegerRange } from "./_utils.ts";
import { assert } from "../_util/assert.ts";
import { ERR_INVALID_ARG_TYPE } from "./_errors.ts";

// deno-lint-ignore no-explicit-any
export type GenericFunction = (...args: any[]) => any;
Expand Down Expand Up @@ -76,6 +77,7 @@ export class EventEmitter {
listener: GenericFunction | WrappedFunction,
prepend: boolean,
): this {
this.checkListenerArgument(listener);
this.emit("newListener", eventName, listener);
if (this._events.has(eventName)) {
const listeners = this._events.get(eventName) as Array<
Expand Down Expand Up @@ -256,6 +258,7 @@ export class EventEmitter {
eventName: string | symbol,
listener: GenericFunction,
): WrappedFunction {
this.checkListenerArgument(listener);
const wrapper = function (
this: {
eventName: string | symbol;
Expand Down Expand Up @@ -348,6 +351,7 @@ export class EventEmitter {
eventName: string | symbol,
listener: GenericFunction,
): this {
this.checkListenerArgument(listener);
if (this._events.has(eventName)) {
const arr:
| Array<GenericFunction | WrappedFunction>
Expand Down Expand Up @@ -556,6 +560,12 @@ export class EventEmitter {
iterator.return();
}
}

private checkListenerArgument(listener: unknown): void {
if (typeof listener !== "function") {
throw new ERR_INVALID_ARG_TYPE("listener", "function", listener);
}
}
}

export default Object.assign(EventEmitter, { EventEmitter });
Expand Down