Skip to content

Commit 0bb98f4

Browse files
author
Greg Femec
committed
add option to specify available encodings
closes expressjs#25
1 parent 4423fb0 commit 0bb98f4

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ the response.
4646
The default filter function uses the [compressible](https://www.npmjs.com/package/compressible)
4747
module to determine if `res.getHeader('Content-Type')` is compressible.
4848

49+
##### available
50+
51+
The set of encodings to make available for compressing responses. This is an
52+
array of strings accepted by the `encoding` function from the
53+
[accepts](https://www.npmjs.com/package/accepts) module.
54+
55+
The default available array is `['gzip', 'deflate', 'identity']`.
56+
4957
##### threshold
5058

5159
The byte threshold for the response body size before compression is considered

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function compression(options) {
3939
var opts = options || {}
4040

4141
var filter = opts.filter || shouldCompress
42+
var available = opts.available || ['gzip', 'deflate', 'identity']
4243
var threshold = typeof opts.threshold === 'string'
4344
? bytes(opts.threshold)
4445
: opts.threshold
@@ -157,7 +158,7 @@ function compression(options) {
157158

158159
// compression method
159160
var accept = accepts(req)
160-
var method = accept.encoding(['gzip', 'deflate', 'identity'])
161+
var method = accept.encoding(available)
161162

162163
// negotiation failed
163164
if (!method || method === 'identity') {

test/compression.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,33 @@ describe('compression()', function(){
544544
.end()
545545
})
546546
})
547+
548+
describe('available', function () {
549+
it('should limit the encodings used', function(done){
550+
var server = createServer({ available: ['deflate'], threshold: 0 }, function (req, res) {
551+
res.setHeader('Content-Type', 'text/plain')
552+
res.end('hello, world')
553+
})
554+
555+
request(server)
556+
.get('/')
557+
.set('Accept-Encoding', 'gzip')
558+
.expect(shouldNotHaveHeader('Content-Encoding'))
559+
.expect(200, done)
560+
})
561+
562+
it('should not prevent available encodings from being used', function(done){
563+
var server = createServer({ available: ['gzip'], threshold: 0 }, function (req, res) {
564+
res.setHeader('Content-Type', 'text/plain')
565+
res.end('hello, world')
566+
})
567+
568+
request(server)
569+
.get('/')
570+
.set('Accept-Encoding', 'gzip')
571+
.expect('Content-Encoding', 'gzip', done)
572+
})
573+
})
547574
})
548575

549576
function createServer(opts, fn) {

0 commit comments

Comments
 (0)