From 7c7a9f3e34394700dc86be16790314a630d0aec6 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 | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ef71577..e5342add 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 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 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..6206efd4 100644 --- a/test/compression.js +++ b/test/compression.js @@ -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) {