Skip to content

Commit

Permalink
add option to specify available encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Femec committed Jan 1, 2015
1 parent 4423fb0 commit 7c7a9f3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ 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.

##### available

The set of encodings to make available for responses. This is an array of
strings passed to the `encoding` function of the [accepts](https://www.npmjs.com/package/accepts)
module to determine the method of compression used.

The default `available` array is `['gzip', 'deflate', 'identity']`.

##### threshold

The byte threshold for the response body size before compression is considered
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function compression(options) {
var opts = options || {}

var filter = opts.filter || shouldCompress
var available = opts.available || ['gzip', 'deflate', 'identity']
var threshold = typeof opts.threshold === 'string'
? bytes(opts.threshold)
: opts.threshold
Expand Down Expand Up @@ -157,7 +158,7 @@ function compression(options) {

// compression method
var accept = accepts(req)
var method = accept.encoding(['gzip', 'deflate', 'identity'])
var method = accept.encoding(available)

// negotiation failed
if (!method || method === 'identity') {
Expand Down
39 changes: 39 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,45 @@ describe('compression()', function(){
.end()
})
})

describe('available', function () {
it('should limit the encodings used', function(done){
var server = createServer({ available: ['gzip'], threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

request(server)
.get('/')
.set('Accept-Encoding', 'deflate;q=1.000, gzip;q=0.001')
.expect('Content-Encoding', 'gzip', done)
})

it('should prevent compression if no encoding available', function(done){
var server = createServer({ available: ['gzip'], threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

it('should not prevent available encodings from being used', function(done){
var server = createServer({ available: ['deflate'], threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

function createServer(opts, fn) {
Expand Down

0 comments on commit 7c7a9f3

Please sign in to comment.