compression
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -0,1 +1,8 @@ | ||
1.0.9 / 2014-07-20 | ||
================== | ||
* Add `debug` messages | ||
* deps: accepts@~1.0.7 | ||
- deps: negotiator@0.4.7 | ||
1.0.8 / 2014-06-20 | ||
@@ -2,0 +9,0 @@ ================== |
59
index.js
@@ -5,2 +5,3 @@ /*! | ||
* Copyright(c) 2011 TJ Holowaychuk | ||
* Copyright(c) 2014 Jonathan Ong | ||
* MIT Licensed | ||
@@ -16,2 +17,3 @@ */ | ||
var bytes = require('bytes'); | ||
var debug = require('debug')('compression') | ||
var onHeaders = require('on-headers'); | ||
@@ -34,4 +36,11 @@ var compressible = require('compressible'); | ||
exports.filter = function(req, res){ | ||
return compressible(res.getHeader('Content-Type')); | ||
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 | ||
}; | ||
@@ -42,4 +51,2 @@ | ||
* | ||
* See README.md for documentation of options. | ||
* | ||
* @param {Object} options | ||
@@ -85,4 +92,4 @@ * @return {Function} middleware | ||
// than the threshold, don't compress | ||
var length = res.getHeader('content-length'); | ||
if (!isNaN(length) && length < threshold) compress = false; | ||
var len = Number(res.getHeader('Content-Length')) | ||
checkthreshold(len) | ||
this._implicitHeader(); | ||
@@ -105,3 +112,3 @@ } | ||
if (!this._header) { | ||
compress = len && len >= threshold | ||
checkthreshold(len) | ||
} | ||
@@ -133,3 +140,11 @@ | ||
function nocompress(){ | ||
function checkthreshold(len) { | ||
if (compress && len < threshold) { | ||
debug('size below threshold') | ||
compress = false | ||
} | ||
} | ||
function nocompress(msg) { | ||
debug('no compression' + (msg ? ': ' + msg : '')) | ||
addListeners(res, on, listeners) | ||
@@ -140,4 +155,7 @@ listeners = null | ||
onHeaders(res, function(){ | ||
// default request filter | ||
if (!filter(req, res)) return nocompress() | ||
// determine if request is filtered | ||
if (!filter(req, res)) { | ||
nocompress('filtered') | ||
return | ||
} | ||
@@ -147,3 +165,6 @@ // vary | ||
if (!compress) return nocompress() | ||
if (!compress) { | ||
nocompress() | ||
return | ||
} | ||
@@ -153,6 +174,12 @@ var encoding = res.getHeader('Content-Encoding') || 'identity'; | ||
// already encoded | ||
if ('identity' !== encoding) return nocompress() | ||
if ('identity' !== encoding) { | ||
nocompress('already encoded') | ||
return | ||
} | ||
// head | ||
if ('HEAD' === req.method) return nocompress() | ||
if ('HEAD' === req.method) { | ||
nocompress('HEAD request') | ||
return | ||
} | ||
@@ -164,5 +191,9 @@ // compression method | ||
// negotiation failed | ||
if (!method || method === 'identity') return nocompress() | ||
if (!method || method === 'identity') { | ||
nocompress('not acceptable') | ||
return | ||
} | ||
// compression stream | ||
debug('%s compression', method) | ||
stream = exports.methods[method](options); | ||
@@ -169,0 +200,0 @@ addListeners(stream, stream.on, listeners) |
{ | ||
"name": "compression", | ||
"description": "Compression middleware for connect and node.js", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -12,5 +12,6 @@ "contributors": [ | ||
"dependencies": { | ||
"accepts": "~1.0.5", | ||
"accepts": "~1.0.7", | ||
"bytes": "1.0.0", | ||
"compressible": "1.1.0", | ||
"debug": "1.0.4", | ||
"on-headers": "0.0.0", | ||
@@ -20,3 +21,3 @@ "vary": "0.1.0" | ||
"devDependencies": { | ||
"istanbul": "0.2.10", | ||
"istanbul": "0.3.0", | ||
"mocha": "~1.20.1", | ||
@@ -30,3 +31,3 @@ "supertest": "~0.13.0", | ||
"scripts": { | ||
"test": "mocha --check-leaks --reporter dot", | ||
"test": "mocha --check-leaks --reporter spec --bail", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --reporter dot", | ||
@@ -33,0 +34,0 @@ "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --check-leaks --reporter spec" |
@@ -9,2 +9,8 @@ # compression | ||
## Install | ||
```bash | ||
$ npm install compression | ||
``` | ||
## API | ||
@@ -37,2 +43,47 @@ | ||
### res.flush | ||
This module adds a `res.flush()` method to force the partially-compressed | ||
response to be flushed to the client. | ||
## Examples | ||
### Server-Sent Events | ||
Because of the nature of compression this module does not work out of the box | ||
with server-sent events. To compress content, a window of the output needs to | ||
be buffered up in order to get good compression. Typically when using server-sent | ||
events, there are certain block of data that need to reach the client. | ||
You can achieve this by calling `res.flush()` when you need the data written to | ||
actually make it to the client. | ||
```js | ||
var compression = require('compression') | ||
var express = require('express') | ||
var app = express() | ||
// compress responses | ||
app.use(compression()) | ||
// server-sent event stream | ||
app.get('/events', function (req, res) { | ||
res.setHeader('Content-Type', 'text/event-stream') | ||
res.setHeader('Cache-Control', 'no-cache') | ||
// send a ping approx eveny 2 seconds | ||
var timer = setInterval(function () { | ||
res.write('data: ping\n\n') | ||
// !!! this is the important part | ||
res.flush() | ||
}, 2000) | ||
res.on('close', function () { | ||
clearInterval(timer) | ||
}) | ||
}) | ||
``` | ||
## License | ||
@@ -39,0 +90,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10402
185
110
6
+ Addeddebug@1.0.4
+ Addeddebug@1.0.4(transitive)
+ Addedms@0.6.2(transitive)
Updatedaccepts@~1.0.7