Skip to content

Commit

Permalink
support res.removeListener('drain'), res.once('drain')
Browse files Browse the repository at this point in the history
Fixes #152
  • Loading branch information
zbjornson committed Mar 7, 2019
1 parent becc1c0 commit a75e2d2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
21 changes: 21 additions & 0 deletions index.js
Expand Up @@ -64,6 +64,7 @@ function compression (options) {

var _end = res.end
var _on = res.on
var _removeListener = res.removeListener
var _write = res.write

// flush
Expand Down Expand Up @@ -131,6 +132,26 @@ function compression (options) {
return this
}

res.removeListener = function removeListener (type, listener) {
if (!listeners || type !== 'drain') {
return _removeListener.call(this, type, listener)
}

if (stream) {
return stream.removeListener(type, listener)
}

// remove buffered listener
for (var i = 0; i < listeners.length; i++) {
if (listeners[i][0] === type && listeners[i][1] === listener) {
listeners.splice(i, 1)
break
}
}

return this
}

function nocompress (msg) {
debug('no compression: %s', msg)
addListeners(res, _on, listeners)
Expand Down
60 changes: 60 additions & 0 deletions test/compression.js
Expand Up @@ -301,6 +301,66 @@ describe('compression()', function () {
.expect(200, done)
})

it('should support removeListener("drain"); stream present', function (done) {
// compression doesn't proxy listenerCount() to the compression stream, so
// instead watch for a MaxListenersExceededWarning
var hasWarned = false
var onWarning = function (warning) {
hasWarned = true
}
process.on('warning', onWarning)
var server = createServer({threshold: 0}, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
var len = bytes('40kb')
var buf = Buffer.alloc(len, '.')
res.write(buf)
// (12 is greater than the default number of max listeners)
for (var times = 0; times < 12; times++) {
var listener = function () {}
res.on('drain', listener)
res.removeListener('drain', listener)
}
res.end()
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect(function () {
process.removeListener('warning', onWarning)
assert.ok(!hasWarned)
})
.expect(200, done)
})

it('should support removeListener("drain"); buffered', function (done) {
// Variant of above test for scenario when the listener is buffered (stream
// is not yet present)
var hasWarned = false
var onWarning = function (warning) {
hasWarned = true
}
process.on('warning', onWarning)
var server = createServer({threshold: 0}, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
for (var times = 0; times < 12; times++) {
var listener = function () {}
res.on('drain', listener)
res.removeListener('drain', listener)
}
res.end()
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect(function () {
process.removeListener('warning', onWarning)
assert.ok(!hasWarned)
})
.expect(200, done)
})

describe('threshold', function () {
it('should not compress responses below the threshold size', function (done) {
var server = createServer({ threshold: '1kb' }, function (req, res) {
Expand Down

0 comments on commit a75e2d2

Please sign in to comment.