Skip to content

Commit 1bf80a0

Browse files
calvinmetcalfevanlucas
authored andcommitted
stream: avoid caching prepend check
This removes the cached check for EE.prototype.prependListener because we can't have nice things. More specifically some libraries will bundle their own event emitter implementation. PR-URL: #8018 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent c26b9af commit 1bf80a0

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

lib/_stream_readable.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ var StringDecoder;
1313

1414
util.inherits(Readable, Stream);
1515

16-
var prependListener;
17-
if (typeof EE.prototype.prependListener === 'function') {
18-
prependListener = function prependListener(emitter, event, fn) {
16+
function prependListener(emitter, event, fn) {
17+
// Sadly this is not cacheable as some libraries bundle their own
18+
// event emitter implementation with them.
19+
if (typeof emitter.prependListener === 'function') {
1920
return emitter.prependListener(event, fn);
20-
};
21-
} else {
22-
prependListener = function prependListener(emitter, event, fn) {
21+
} else {
2322
// This is a hack to make sure that our error handler is attached before any
2423
// userland ones. NEVER DO THIS. This is here only because this code needs
2524
// to continue to work with older versions of Node.js that do not include
@@ -30,7 +29,7 @@ if (typeof EE.prototype.prependListener === 'function') {
3029
emitter._events[event].unshift(fn);
3130
else
3231
emitter._events[event] = [fn, emitter._events[event]];
33-
};
32+
}
3433
}
3534

3635
function ReadableState(options, stream) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const common = require('../common');
3+
const stream = require('stream');
4+
const util = require('util');
5+
6+
function Writable() {
7+
this.writable = true;
8+
stream.Writable.call(this);
9+
this.prependListener = undefined;
10+
}
11+
util.inherits(Writable, stream.Writable);
12+
Writable.prototype._write = function(chunk, end, cb) {
13+
cb();
14+
};
15+
16+
function Readable() {
17+
this.readable = true;
18+
stream.Readable.call(this);
19+
}
20+
util.inherits(Readable, stream.Readable);
21+
Readable.prototype._read = function() {
22+
this.push(null);
23+
};
24+
25+
const w = new Writable();
26+
w.on('pipe', common.mustCall(function() {}));
27+
28+
const r = new Readable();
29+
r.pipe(w);

0 commit comments

Comments
 (0)