Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2bbe1f6

Browse files
committedFeb 26, 2024·
stream: fix eventNames() to not return not defined events
1 parent 60f09c6 commit 2bbe1f6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
 

‎lib/internal/streams/legacy.js

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayIsArray,
55
ObjectSetPrototypeOf,
6+
ReflectOwnKeys,
67
} = primordials;
78

89
const EE = require('events');
@@ -93,6 +94,16 @@ Stream.prototype.pipe = function(dest, options) {
9394
return dest;
9495
};
9596

97+
Stream.prototype.eventNames = function eventNames() {
98+
const names = [];
99+
for (const key of ReflectOwnKeys(this._events)) {
100+
if (typeof this._events[key] === 'function' || (ArrayIsArray(this._events[key]) && this._events[key].length > 0)) {
101+
names.push(key);
102+
}
103+
}
104+
return names;
105+
};
106+
96107
function prependListener(emitter, event, fn) {
97108
// Sadly this is not cacheable as some libraries bundle their own
98109
// event emitter implementation with them.
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const { Readable, Writable, Duplex } = require('stream');
6+
7+
{
8+
const stream = new Readable();
9+
assert.strictEqual(stream.eventNames().length, 0);
10+
}
11+
12+
{
13+
const stream = new Readable();
14+
stream.on('foo', () => {});
15+
stream.on('data', () => {});
16+
stream.on('error', () => {});
17+
assert.deepStrictEqual(stream.eventNames(), ['error', 'data', 'foo']);
18+
}
19+
20+
{
21+
const stream = new Writable();
22+
assert.strictEqual(stream.eventNames().length, 0);
23+
}
24+
25+
{
26+
const stream = new Writable();
27+
stream.on('foo', () => {});
28+
stream.on('drain', () => {});
29+
stream.on('prefinish', () => {});
30+
assert.deepStrictEqual(stream.eventNames(), ['prefinish', 'drain', 'foo']);
31+
}
32+
{
33+
const stream = new Duplex();
34+
assert.strictEqual(stream.eventNames().length, 0);
35+
}
36+
37+
{
38+
const stream = new Duplex();
39+
stream.on('foo', () => {});
40+
stream.on('finish', () => {});
41+
assert.deepStrictEqual(stream.eventNames(), ['finish', 'foo']);
42+
}

0 commit comments

Comments
 (0)
Please sign in to comment.