From 0bb98f4fa9c36fa482a9237fa00a364000777002 Mon Sep 17 00:00:00 2001 From: Greg Femec Date: Wed, 31 Dec 2014 21:32:40 -0800 Subject: [PATCH] add option to specify available encodings closes #25 --- README.md | 8 ++++++++ index.js | 3 ++- test/compression.js | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ef71577..0d6ca1ce 100644 --- a/README.md +++ b/README.md @@ -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 compressing responses. This is an +array of strings accepted by the `encoding` function from the +[accepts](https://www.npmjs.com/package/accepts) module. + +The default available array is `['gzip', 'deflate', 'identity']`. + ##### threshold The byte threshold for the response body size before compression is considered diff --git a/index.js b/index.js index b3ebda0f..7c246304 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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') { diff --git a/test/compression.js b/test/compression.js index 85d0c80e..e04ae5ea 100644 --- a/test/compression.js +++ b/test/compression.js @@ -544,6 +544,33 @@ describe('compression()', function(){ .end() }) }) + + describe('available', function () { + it('should limit the encodings 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', 'gzip') + .expect(shouldNotHaveHeader('Content-Encoding')) + .expect(200, done) + }) + + it('should not prevent available encodings from being 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', 'gzip') + .expect('Content-Encoding', 'gzip', done) + }) + }) }) function createServer(opts, fn) {