Skip to content

Commit

Permalink
perf: simplify threshold detection
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 10, 2015
1 parent d18edf5 commit 2403c20
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -16,6 +16,7 @@ unreleased
- perf: hoist regex declaration
- perf: use regex to extract mime
* perf: enable strict mode
* perf: simplify threshold detection

1.4.4 / 2015-05-11
==================
Expand Down
56 changes: 28 additions & 28 deletions index.js
Expand Up @@ -49,7 +49,7 @@ function compression(options) {
}

return function compression(req, res, next){
var compress = true
var length
var listeners = []
var write = res.write
var on = res.on
Expand All @@ -68,34 +68,26 @@ function compression(options) {

res.write = function(chunk, encoding){
if (!this._header) {
// if content-length is set and is lower
// than the threshold, don't compress
var len = Number(res.getHeader('Content-Length'))
checkthreshold(len)
this._implicitHeader();
this._implicitHeader()
}

return stream
? stream.write(new Buffer(chunk, encoding))
: write.call(res, chunk, encoding);
: write.call(this, chunk, encoding)
};

res.end = function(chunk, encoding){
var len

if (chunk) {
len = Buffer.isBuffer(chunk)
? chunk.length
: Buffer.byteLength(chunk, encoding)
}

if (!this._header) {
len = Number(this.getHeader('Content-Length')) || len
checkthreshold(len)
// estimate the length
if (!this.getHeader('Content-Length')) {
length = chunkLength(chunk, encoding)
}

this._implicitHeader()
}

if (!stream) {
return end.call(res, chunk, encoding)
return end.call(this, chunk, encoding)
}

// write Buffer for Node.js 0.8
Expand All @@ -119,15 +111,8 @@ function compression(options) {
return this
}

function checkthreshold(len) {
if (compress && len < threshold) {
debug('size below threshold')
compress = false
}
}

function nocompress(msg) {
debug('no compression' + (msg ? ': ' + msg : ''))
debug('no compression: %s', msg)
addListeners(res, on, listeners)
listeners = null
}
Expand All @@ -142,8 +127,9 @@ function compression(options) {
// vary
vary(res, 'Accept-Encoding')

if (!compress) {
nocompress()
// content-length below threshold
if (Number(res.getHeader('Content-Length')) < threshold || length < threshold) {
nocompress('size below threshold')
return
}

Expand Down Expand Up @@ -225,6 +211,20 @@ function addListeners(stream, on, listeners) {
}
}

/**
* Get the length of a given chunk
*/

function chunkLength(chunk, encoding) {
if (!chunk) {
return
}

return !Buffer.isBuffer(chunk)
? Buffer.byteLength(chunk, encoding)
: chunk.length
}

/**
* No-operation function
* @private
Expand Down
12 changes: 12 additions & 0 deletions test/compression.js
Expand Up @@ -394,6 +394,18 @@ describe('compression()', function(){
.expect(shouldNotHaveHeader('Content-Encoding'))
.expect(200, '....', done)
})

it('should work with res.end(null)', function (done) {
var server = createServer({ threshold: 1000 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end(null)
})

request(server)
.get('/')
.set('Accept-Encoding', 'gzip')
.expect(200, '', done)
})
})

describe('when "Accept-Encoding: gzip"', function () {
Expand Down

0 comments on commit 2403c20

Please sign in to comment.