Skip to content

Commit f752ac0

Browse files
committed
stream: remove legacy prependListener fallback
The prependListener method has been available on EventEmitter since Node.js v6.0.0 (April 2016). The fallback code that manipulated the internal _events object is no longer necessary. This also removes tests that explicitly tested the fallback behavior.
1 parent 0d7e4b1 commit f752ac0

File tree

3 files changed

+3
-43
lines changed

3 files changed

+3
-43
lines changed

lib/internal/streams/legacy.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,7 @@ Stream.prototype.eventNames = function eventNames() {
106106
};
107107

108108
function prependListener(emitter, event, fn) {
109-
// Sadly this is not cacheable as some libraries bundle their own
110-
// event emitter implementation with them.
111-
if (typeof emitter.prependListener === 'function')
112-
return emitter.prependListener(event, fn);
113-
114-
// This is a hack to make sure that our error handler is attached before any
115-
// userland ones. NEVER DO THIS. This is here only because this code needs
116-
// to continue to work with older versions of Node.js that do not include
117-
// the prependListener() method. The goal is to eventually remove this hack.
118-
if (!emitter._events || !emitter._events[event])
119-
emitter.on(event, fn);
120-
else if (ArrayIsArray(emitter._events[event]))
121-
emitter._events[event].unshift(fn);
122-
else
123-
emitter._events[event] = [fn, emitter._events[event]];
109+
emitter.prependListener(event, fn);
124110
}
125111

126112
module.exports = { Stream, prependListener };

test/parallel/test-event-emitter-prepend.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,3 @@ myEE.prependOnceListener('foo',
1717
common.mustCall(() => assert.strictEqual(m++, 0)));
1818

1919
myEE.emit('foo');
20-
21-
// Test fallback if prependListener is undefined.
22-
const stream = require('stream');
23-
24-
delete EventEmitter.prototype.prependListener;
25-
26-
function Writable() {
27-
this.writable = true;
28-
stream.Stream.call(this);
29-
}
30-
Object.setPrototypeOf(Writable.prototype, stream.Stream.prototype);
31-
Object.setPrototypeOf(Writable, stream.Stream);
32-
33-
function Readable() {
34-
this.readable = true;
35-
stream.Stream.call(this);
36-
}
37-
Object.setPrototypeOf(Readable.prototype, stream.Stream.prototype);
38-
Object.setPrototypeOf(Readable, stream.Stream);
39-
40-
const w = new Writable();
41-
const r = new Readable();
42-
r.pipe(w);

test/parallel/test-stream-events-prepend.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
const common = require('../common');
33
const stream = require('stream');
44

5-
class Writable extends stream.Writable {
6-
constructor() {
7-
super();
8-
this.prependListener = undefined;
9-
}
5+
// Test that pipe() correctly uses prependListener for error handlers.
106

7+
class Writable extends stream.Writable {
118
_write(chunk, end, cb) {
129
cb();
1310
}

0 commit comments

Comments
 (0)