compression
Advanced tools
Comparing version 1.4.4 to 1.5.0
@@ -0,1 +1,24 @@ | ||
1.5.0 / 2015-06-09 | ||
================== | ||
* Fix return value from `.end` and `.write` after end | ||
* Improve detection of zero-length body without `Content-Length` | ||
* deps: accepts@~1.2.9 | ||
- deps: mime-types@~2.1.1 | ||
- perf: avoid argument reassignment & argument slice | ||
- perf: avoid negotiator recursive construction | ||
- perf: enable strict mode | ||
- perf: remove unnecessary bitwise operator | ||
* deps: bytes@2.1.0 | ||
- Slight optimizations | ||
- Units no longer case sensitive when parsing | ||
* deps: compressible@~2.0.3 | ||
- Fix regex fallback to work if type exists, but is undefined | ||
- deps: mime-db@'>= 1.13.0 < 2' | ||
- perf: hoist regex declaration | ||
- perf: use regex to extract mime | ||
* perf: enable strict mode | ||
* perf: remove flush reassignment | ||
* perf: simplify threshold detection | ||
1.4.4 / 2015-05-11 | ||
@@ -2,0 +25,0 @@ ================== |
90
index.js
@@ -6,6 +6,8 @@ /*! | ||
* Copyright(c) 2014 Jonathan Ong | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* Copyright(c) 2014-2015 Douglas Christopher Wilson | ||
* MIT Licensed | ||
*/ | ||
'use strict' | ||
/** | ||
@@ -42,6 +44,5 @@ * Module dependencies. | ||
// options | ||
var filter = opts.filter || shouldCompress | ||
var threshold = typeof opts.threshold === 'string' | ||
? bytes(opts.threshold) | ||
: opts.threshold | ||
var threshold = bytes.parse(opts.threshold) | ||
@@ -53,3 +54,4 @@ if (threshold == null) { | ||
return function compression(req, res, next){ | ||
var compress = true | ||
var ended = false | ||
var length | ||
var listeners = [] | ||
@@ -61,37 +63,36 @@ var write = res.write | ||
// see #8 | ||
req.on('close', function(){ | ||
res.write = res.end = noop | ||
}); | ||
// flush | ||
res.flush = function flush() { | ||
if (stream) { | ||
stream.flush() | ||
} | ||
} | ||
// flush is noop by default | ||
res.flush = noop; | ||
// proxy | ||
res.write = function(chunk, encoding){ | ||
if (ended) { | ||
return false | ||
} | ||
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 (ended) { | ||
return false | ||
} | ||
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() | ||
@@ -101,5 +102,8 @@ } | ||
if (!stream) { | ||
return end.call(res, chunk, encoding) | ||
return end.call(this, chunk, encoding) | ||
} | ||
// mark ended | ||
ended = true | ||
// write Buffer for Node.js 0.8 | ||
@@ -126,11 +130,4 @@ return chunk | ||
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) | ||
@@ -150,4 +147,5 @@ listeners = null | ||
if (!compress) { | ||
nocompress() | ||
// content-length below threshold | ||
if (Number(res.getHeader('Content-Length')) < threshold || length < threshold) { | ||
nocompress('size below threshold') | ||
return | ||
@@ -194,7 +192,2 @@ } | ||
// overwrite the flush method | ||
res.flush = function(){ | ||
stream.flush(); | ||
} | ||
// header fields | ||
@@ -236,8 +229,15 @@ res.setHeader('Content-Encoding', method); | ||
/** | ||
* No-operation function | ||
* @private | ||
* Get the length of a given chunk | ||
*/ | ||
function noop(){} | ||
function chunkLength(chunk, encoding) { | ||
if (!chunk) { | ||
return 0 | ||
} | ||
return !Buffer.isBuffer(chunk) | ||
? Buffer.byteLength(chunk, encoding) | ||
: chunk.length | ||
} | ||
/** | ||
@@ -244,0 +244,0 @@ * Default filter function. |
{ | ||
"name": "compression", | ||
"description": "Node.js compression middleware", | ||
"version": "1.4.4", | ||
"version": "1.5.0", | ||
"contributors": [ | ||
@@ -12,5 +12,5 @@ "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"dependencies": { | ||
"accepts": "~1.2.7", | ||
"bytes": "1.0.0", | ||
"compressible": "~2.0.2", | ||
"accepts": "~1.2.9", | ||
"bytes": "2.1.0", | ||
"compressible": "~2.0.3", | ||
"debug": "~2.2.0", | ||
@@ -21,5 +21,5 @@ "on-headers": "~1.0.0", | ||
"devDependencies": { | ||
"istanbul": "0.3.9", | ||
"mocha": "2.2.4", | ||
"supertest": "~0.15.0" | ||
"istanbul": "0.3.15", | ||
"mocha": "2.2.5", | ||
"supertest": "1.0.1" | ||
}, | ||
@@ -26,0 +26,0 @@ "files": [ |
@@ -121,2 +121,7 @@ # compression | ||
**Note** this is only an advisory setting; if the response size cannot be determined | ||
at the time the response headers are written, then it is assumed the response is | ||
_over_ the threshold. To guarantee the response size can be determined, be sure | ||
set a `Content-Length` response header. | ||
##### windowBits | ||
@@ -123,0 +128,0 @@ |
Sorry, the diff of this file is not supported yet
18572
228
197
+ Addedbytes@2.1.0(transitive)
- Removedbytes@1.0.0(transitive)
Updatedaccepts@~1.2.9
Updatedbytes@2.1.0
Updatedcompressible@~2.0.3