Skip to content

Commit daf6c53

Browse files
Fishrock123rvagg
authored andcommitted
events,lib: don't require EE#listenerCount()
Now parts of our public and public-ish APIs fall back to old-style listenerCount() if the emitter does not have a listenerCount function. Fixes: #2655 Refs: 8f58fb9 PR-URL: #2661 Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5a51edd commit daf6c53

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

lib/_http_server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ function connectionListener(socket) {
374374
parser = null;
375375

376376
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
377-
if (self.listenerCount(eventName) > 0) {
377+
if (EventEmitter.listenerCount(self, eventName) > 0) {
378378
debug('SERVER have listener for %s', eventName);
379379
var bodyHead = d.slice(bytesParsed, d.length);
380380

@@ -497,7 +497,7 @@ function connectionListener(socket) {
497497
(req.httpVersionMajor == 1 && req.httpVersionMinor == 1) &&
498498
continueExpression.test(req.headers['expect'])) {
499499
res._expect_continue = true;
500-
if (self.listenerCount('checkContinue') > 0) {
500+
if (EventEmitter.listenerCount(self, 'checkContinue') > 0) {
501501
self.emit('checkContinue', req, res);
502502
} else {
503503
res.writeContinue();

lib/_stream_readable.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
537537
debug('onerror', er);
538538
unpipe();
539539
dest.removeListener('error', onerror);
540-
if (dest.listenerCount('error') === 0)
540+
if (EE.listenerCount(dest, 'error') === 0)
541541
dest.emit('error', er);
542542
}
543543
// This is a brutally ugly hack to make sure that our error handler
@@ -586,7 +586,7 @@ function pipeOnDrain(src) {
586586
debug('pipeOnDrain', state.awaitDrain);
587587
if (state.awaitDrain)
588588
state.awaitDrain--;
589-
if (state.awaitDrain === 0 && src.listenerCount('data')) {
589+
if (state.awaitDrain === 0 && EE.listenerCount(src, 'data')) {
590590
state.flowing = true;
591591
flow(src);
592592
}

lib/events.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -395,10 +395,15 @@ EventEmitter.prototype.listeners = function listeners(type) {
395395
};
396396

397397
EventEmitter.listenerCount = function(emitter, type) {
398-
return emitter.listenerCount(type);
398+
if (typeof emitter.listenerCount === 'function') {
399+
return emitter.listenerCount(type);
400+
} else {
401+
return listenerCount.call(emitter, type);
402+
}
399403
};
400404

401-
EventEmitter.prototype.listenerCount = function listenerCount(type) {
405+
EventEmitter.prototype.listenerCount = listenerCount;
406+
function listenerCount(type) {
402407
const events = this._events;
403408

404409
if (events) {

lib/stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Stream.prototype.pipe = function(dest, options) {
7070
// don't leave dangling pipes when there are errors.
7171
function onerror(er) {
7272
cleanup();
73-
if (this.listenerCount('error') === 0) {
73+
if (EE.listenerCount(this, 'error') === 0) {
7474
throw er; // Unhandled stream error in pipe.
7575
}
7676
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
const common = require('../common');
3+
const stream = require('stream');
4+
5+
const r = new stream.Stream();
6+
r.listenerCount = undefined;
7+
8+
const w = new stream.Stream();
9+
w.listenerCount = undefined;
10+
11+
w.on('pipe', function() {
12+
r.emit('error', new Error('Readable Error'));
13+
w.emit('error', new Error('Writable Error'));
14+
});
15+
r.on('error', common.mustCall(noop));
16+
w.on('error', common.mustCall(noop));
17+
r.pipe(w);
18+
19+
function noop() {};

0 commit comments

Comments
 (0)