Skip to content

Commit 5b06af7

Browse files
IlyasShabimarco-ippolito
authored andcommitted
stream: fix eventNames() to not return not defined events
PR-URL: #51331 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 7543e77 commit 5b06af7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

‎lib/internal/streams/legacy.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const{
44
ArrayIsArray,
55
ObjectSetPrototypeOf,
6+
ReflectOwnKeys,
67
}=primordials;
78

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

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

0 commit comments

Comments
(0)