Skip to content

Commit

Permalink
Add option to skip compression
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Jan 31, 2015
1 parent 4423fb0 commit 17a5782
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -76,6 +76,11 @@ function shouldCompress(req, res) {
This module adds a `res.flush()` method to force the partially-compressed
response to be flushed to the client.

### res.compress = false

If you'd like to opt out of compression for a particular request for whatever reason,
set `res.compress = false` and this middleware will be skipped.

## Examples

### express/connect
Expand Down
6 changes: 6 additions & 0 deletions index.js
Expand Up @@ -127,6 +127,12 @@ function compression(options) {
}

onHeaders(res, function(){
// user opts out of compression
if (res.compress === false) {
nocompress('out-out')
return
}

// determine if request is filtered
if (!filter(req, res)) {
nocompress('filtered')
Expand Down
19 changes: 19 additions & 0 deletions test/compression.js
Expand Up @@ -432,6 +432,25 @@ describe('compression()', function(){
})
})

describe('res.compress = false', function () {
it('should opt out of compression', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.compress = false
res.setHeader('Content-Type', 'text/plain')
res.end('abcdefg')
})

request(server)
.get('/')
.expect('abcdefg')
.expect(200, function (err, res) {
assert.ifError(err)
assert(!('content-encoding' in res.headers))
done()
})
})
})

describe('res.flush()', function () {
it('should always be present', function (done) {
var server = createServer(null, function (req, res) {
Expand Down

0 comments on commit 17a5782

Please sign in to comment.