From 3c18fc1874d7c3821f2421f5a5c56800be1d5878 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Fri, 13 Jan 2023 14:37:41 +0100 Subject: [PATCH] use listenerCount immediatly (#13) * use listenerCount immediatly * like before --- index.js | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 54ed8e5..d2b7709 100644 --- a/index.js +++ b/index.js @@ -251,14 +251,16 @@ SendStream.prototype.maxage = deprecate.function(function maxage (maxAge) { /** * Emit error with `status`. * + * @memberof SendStream * @param {number} status * @param {Error} [err] + * @this {Stream} * @private */ SendStream.prototype.error = function error (status, err) { // emit if listeners instead of responding - if (hasListeners(this, 'error')) { + if (this.listenerCount('error') > 0) { return this.emit('error', createHttpError(status, err)) } @@ -463,7 +465,7 @@ SendStream.prototype.isRangeFresh = function isRangeFresh () { SendStream.prototype.redirect = function redirect (path) { const res = this.res - if (hasListeners(this, 'directory')) { + if (this.listenerCount('directory') > 0) { this.emit('directory', res, path) return } @@ -1014,26 +1016,6 @@ function getHeaderNames (res) { : res.getHeaderNames() } -/** - * Determine if emitter has listeners of a given type. - * - * The way to do this check is done three different ways in Node.js >= 0.8 - * so this consolidates them into a minimal set using instance methods. - * - * @param {EventEmitter} emitter - * @param {string} type - * @returns {boolean} - * @private - */ - -function hasListeners (emitter, type) { - const count = typeof emitter.listenerCount !== 'function' - ? emitter.listeners(type).length - : emitter.listenerCount(type) - - return count > 0 -} - /** * Determine if the response headers have been sent. *