mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
stream: fix eventNames() to not return not defined events
PR-URL: https://github.com/nodejs/node/pull/51331 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
const {
|
||||
ArrayIsArray,
|
||||
ObjectSetPrototypeOf,
|
||||
ReflectOwnKeys,
|
||||
} = primordials;
|
||||
|
||||
const EE = require('events');
|
||||
@@ -93,6 +94,16 @@ Stream.prototype.pipe = function(dest, options) {
|
||||
return dest;
|
||||
};
|
||||
|
||||
Stream.prototype.eventNames = function eventNames() {
|
||||
const names = [];
|
||||
for (const key of ReflectOwnKeys(this._events)) {
|
||||
if (typeof this._events[key] === 'function' || (ArrayIsArray(this._events[key]) && this._events[key].length > 0)) {
|
||||
names.push(key);
|
||||
}
|
||||
}
|
||||
return names;
|
||||
};
|
||||
|
||||
function prependListener(emitter, event, fn) {
|
||||
// Sadly this is not cacheable as some libraries bundle their own
|
||||
// event emitter implementation with them.
|
||||
|
||||
42
test/parallel/test-stream-event-names.js
Normal file
42
test/parallel/test-stream-event-names.js
Normal file
@@ -0,0 +1,42 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const { Readable, Writable, Duplex } = require('stream');
|
||||
|
||||
{
|
||||
const stream = new Readable();
|
||||
assert.strictEqual(stream.eventNames().length, 0);
|
||||
}
|
||||
|
||||
{
|
||||
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.strictEqual(stream.eventNames().length, 0);
|
||||
}
|
||||
|
||||
{
|
||||
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.strictEqual(stream.eventNames().length, 0);
|
||||
}
|
||||
|
||||
{
|
||||
const stream = new Duplex();
|
||||
stream.on('foo', () => {});
|
||||
stream.on('finish', () => {});
|
||||
assert.deepStrictEqual(stream.eventNames(), ['finish', 'foo']);
|
||||
}
|
||||
Reference in New Issue
Block a user