Skip to content

Commit

Permalink
use listenerCount immediatly (#13)
Browse files Browse the repository at this point in the history
* use listenerCount immediatly

* like before
  • Loading branch information
Uzlopak authored Jan 13, 2023
1 parent 5b263c2 commit 3c18fc1
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 3c18fc1

Please sign in to comment.