compression
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -0,1 +1,10 @@ | ||
1.3.0 / 2014-12-30 | ||
================== | ||
* Export the default `filter` function for wrapping | ||
* deps: accepts@~1.2.2 | ||
- deps: mime-types@~2.0.7 | ||
- deps: negotiator@0.5.0 | ||
* deps: debug@~2.1.1 | ||
1.2.2 / 2014-12-10 | ||
@@ -2,0 +11,0 @@ ================== |
93
index.js
@@ -12,37 +12,21 @@ /*! | ||
* Module dependencies. | ||
* @private | ||
*/ | ||
var zlib = require('zlib'); | ||
var accepts = require('accepts'); | ||
var bytes = require('bytes'); | ||
var accepts = require('accepts') | ||
var bytes = require('bytes') | ||
var compressible = require('compressible') | ||
var debug = require('debug')('compression') | ||
var onHeaders = require('on-headers'); | ||
var compressible = require('compressible'); | ||
var vary = require('vary'); | ||
var onHeaders = require('on-headers') | ||
var vary = require('vary') | ||
var zlib = require('zlib') | ||
/** | ||
* Supported content-encoding methods. | ||
* Module exports. | ||
*/ | ||
exports.methods = { | ||
gzip: zlib.createGzip | ||
, deflate: zlib.createDeflate | ||
}; | ||
module.exports = compression | ||
module.exports.filter = shouldCompress | ||
/** | ||
* Default filter function. | ||
*/ | ||
exports.filter = function filter(req, res) { | ||
var type = res.getHeader('Content-Type') | ||
if (type === undefined || !compressible(type)) { | ||
debug('%s not compressible', type) | ||
return false | ||
} | ||
return true | ||
}; | ||
/** | ||
* Compress response data with gzip / deflate. | ||
@@ -52,16 +36,15 @@ * | ||
* @return {Function} middleware | ||
* @api public | ||
* @public | ||
*/ | ||
module.exports = function compression(options) { | ||
options = options || {}; | ||
var filter = options.filter || exports.filter; | ||
var threshold; | ||
function compression(options) { | ||
var opts = options || {} | ||
if (false === options.threshold || 0 === options.threshold) { | ||
threshold = 0 | ||
} else if ('string' === typeof options.threshold) { | ||
threshold = bytes(options.threshold) | ||
} else { | ||
threshold = options.threshold || 1024 | ||
var filter = opts.filter || shouldCompress | ||
var threshold = typeof opts.threshold === 'string' | ||
? bytes(opts.threshold) | ||
: opts.threshold | ||
if (threshold == null) { | ||
threshold = 1024 | ||
} | ||
@@ -79,3 +62,3 @@ | ||
req.on('close', function(){ | ||
res.write = res.end = function(){}; | ||
res.write = res.end = noop | ||
}); | ||
@@ -179,4 +162,4 @@ | ||
// compression method | ||
var accept = accepts(req); | ||
var method = accept.encodings(['gzip', 'deflate', 'identity']); | ||
var accept = accepts(req) | ||
var method = accept.encoding(['gzip', 'deflate', 'identity']) | ||
@@ -191,3 +174,7 @@ // negotiation failed | ||
debug('%s compression', method) | ||
stream = exports.methods[method](options); | ||
stream = method === 'gzip' | ||
? zlib.createGzip(opts) | ||
: zlib.createDeflate(opts) | ||
// add bufferred listeners to stream | ||
addListeners(stream, stream.on, listeners) | ||
@@ -222,6 +209,7 @@ | ||
}; | ||
}; | ||
} | ||
/** | ||
* Add bufferred listeners to stream | ||
* @private | ||
*/ | ||
@@ -235,2 +223,23 @@ | ||
/** | ||
* No-operation function | ||
* @private | ||
*/ | ||
function noop(){} | ||
/** | ||
* Default filter function. | ||
* @private | ||
*/ | ||
function shouldCompress(req, res) { | ||
var type = res.getHeader('Content-Type') | ||
if (type === undefined || !compressible(type)) { | ||
debug('%s not compressible', type) | ||
return false | ||
} | ||
return true | ||
} |
{ | ||
"name": "compression", | ||
"description": "Compression middleware for connect and node.js", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -12,6 +12,6 @@ "contributors": [ | ||
"dependencies": { | ||
"accepts": "~1.1.4", | ||
"accepts": "~1.2.2", | ||
"bytes": "1.0.0", | ||
"compressible": "~2.0.1", | ||
"debug": "~2.1.0", | ||
"debug": "~2.1.1", | ||
"on-headers": "~1.0.0", | ||
@@ -21,4 +21,4 @@ "vary": "~1.0.0" | ||
"devDependencies": { | ||
"istanbul": "0.3.4", | ||
"mocha": "~2.0.1", | ||
"istanbul": "0.3.5", | ||
"mocha": "~2.1.0", | ||
"supertest": "~0.15.0" | ||
@@ -25,0 +25,0 @@ }, |
@@ -35,7 +35,41 @@ # compression | ||
- `threshold` `<1kb>` - response is only compressed if the byte size is at or above this threshold. | ||
- `filter` - a filtering callback function. Uses [Compressible](https://github.com/expressjs/compressible) by default. | ||
`compression()` accepts these properties in the options object. In addition to | ||
those listed below, [zlib](http://nodejs.org/api/zlib.html) options may be | ||
passed in to the options object. | ||
In addition to these, [zlib](http://nodejs.org/api/zlib.html) options may be passed in to the options object. | ||
##### filter | ||
A function to decide if the response should be considered for compression. | ||
This function is called as `filter(req, res)` and is expected to return | ||
`true` to consider the response for compression, or `false` to not compress | ||
the response. | ||
The default filter function uses the [compressible](https://www.npmjs.com/package/compressible) | ||
module to determine if `res.getHeader('Content-Type')` is compressible. | ||
##### threshold | ||
The byte threshold for the response body size before compression is considered | ||
for the response, defaults to `1kb`. This is a number of bytes, any string | ||
accepted by the [bytes](https://www.npmjs.com/package/bytes) module, or `false`. | ||
#### .filter | ||
The default `filter` function. This is used to construct a custom filter | ||
function that is an extension of the default function. | ||
```js | ||
app.use(compression({filter: shouldCompress})) | ||
function shouldCompress(req, res) { | ||
if (req.headers['x-no-compression']) { | ||
// don't compress responses with this request header | ||
return false | ||
} | ||
// fallback to standard filter function | ||
return compression.filter(req, res) | ||
} | ||
``` | ||
### res.flush | ||
@@ -42,0 +76,0 @@ |
13655
191
150
+ Addedaccepts@1.2.13(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addednegotiator@0.5.3(transitive)
- Removedaccepts@1.1.4(transitive)
- Removedmime-db@1.12.0(transitive)
- Removedmime-types@2.0.14(transitive)
- Removednegotiator@0.4.9(transitive)
Updatedaccepts@~1.2.2
Updateddebug@~2.1.1