Skip to content

Commit

Permalink
stream: fix eventNames() to not return not defined events
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasShabiCS committed Jan 1, 2024
1 parent 5fb6305 commit 9a4ba8d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const {
SymbolFor,
SymbolAsyncIterator,
SymbolDispose,
ArrayIsArray

Check failure on line 52 in lib/events.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
} = primordials;
const kRejection = SymbolFor('nodejs.rejection');

Expand Down Expand Up @@ -888,7 +889,13 @@ function listenerCount(type, listener) {
* @returns {any[]}
*/
EventEmitter.prototype.eventNames = function eventNames() {
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
const names = [];
for (const key of ReflectOwnKeys(this._events)) {
if(typeof this._events[key] === 'function' || ArrayIsArray(this._events[key]) && this._events[key].length > 0){

Check failure on line 894 in lib/events.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected space(s) after "if"

Check failure on line 894 in lib/events.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected mix of '||' and '&&'. Use parentheses to clarify the intended order of operations

Check failure on line 894 in lib/events.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected mix of '||' and '&&'. Use parentheses to clarify the intended order of operations

Check failure on line 894 in lib/events.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing space before opening brace
names.push(key);
}
}
return names;
};

function arrayClone(arr) {
Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-stream-event-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

require('../common');
const assert = require('assert');
const { Readable, Writable, Duplex } = require('stream');

{
const stream = new Readable();
assert.equal(stream.eventNames().length, 0);

Check failure on line 9 in test/parallel/test-stream-event-names.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'assert.equal' is restricted from being used. Use `assert.strictEqual()` rather than `assert.equal()`
}

{
const stream = new Readable();
stream.on('foo', () => {});
stream.on('data', () => {});
stream.on('error', () => {});
assert.deepStrictEqual(stream.eventNames(), ['error', 'data', 'foo']);
}

{
const stream = new Writable();
assert.equal(stream.eventNames().length, 0);

Check failure on line 22 in test/parallel/test-stream-event-names.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'assert.equal' is restricted from being used. Use `assert.strictEqual()` rather than `assert.equal()`
}

{
const stream = new Writable();
stream.on('foo', () => {});
stream.on('drain', () => {});
stream.on('prefinish', () => {});
assert.deepStrictEqual(stream.eventNames(), ['prefinish', 'drain', 'foo']);
}
{
const stream = new Duplex();
assert.equal(stream.eventNames().length, 0);

Check failure on line 34 in test/parallel/test-stream-event-names.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'assert.equal' is restricted from being used. Use `assert.strictEqual()` rather than `assert.equal()`
}

{
const stream = new Duplex();
stream.on('foo', () => {});
stream.on('finish', () => {});
assert.deepStrictEqual(stream.eventNames(), ['finish', 'foo']);
}

0 comments on commit 9a4ba8d

Please sign in to comment.