Skip to content

Commit 558ecf1

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

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-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 responses. This is an array of
52+
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,45 @@ 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: ['gzip'], 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', 'deflate;q=1.000, gzip;q=0.001')
558+
.expect('Content-Encoding', 'gzip', done)
559+
})
560+
561+
it('should prevent compression if no encoding available', function(done){
562+
var server = createServer({ available: ['gzip'], threshold: 0 }, function (req, res) {
563+
res.setHeader('Content-Type', 'text/plain')
564+
res.end('hello, world')
565+
})
566+
567+
request(server)
568+
.get('/')
569+
.set('Accept-Encoding', 'deflate')
570+
.expect(shouldNotHaveHeader('Content-Encoding'))
571+
.expect(200, done)
572+
})
573+
574+
it('should not prevent available encodings from being used', function(done){
575+
var server = createServer({ available: ['deflate'], threshold: 0 }, function (req, res) {
576+
res.setHeader('Content-Type', 'text/plain')
577+
res.end('hello, world')
578+
})
579+
580+
request(server)
581+
.get('/')
582+
.set('Accept-Encoding', 'deflate')
583+
.expect('Content-Encoding', 'deflate', done)
584+
})
585+
})
547586
})
548587

549588
function createServer(opts, fn) {

0 commit comments

Comments
 (0)