Skip to content

Commit

Permalink
allow custom compression for custom Accept-Encoding Fixes expressjs#59
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Desaulniers committed Oct 27, 2015
1 parent 2b4549a commit 665c0cf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
19 changes: 15 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,14 @@ function compression(options) {

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

if (opts.compressor) {
acceptableEncodings =
Object.keys(opts.compressor).concat(acceptableEncodings)
}

var method = accept.encoding(acceptableEncodings)

// we really don't prefer deflate
if (method === 'deflate' && accept.encoding(['gzip'])) {
Expand All @@ -188,9 +195,13 @@ function compression(options) {

// compression stream
debug('%s compression', method)
stream = method === 'gzip'
? zlib.createGzip(opts)
: zlib.createDeflate(opts)
if (method === 'gzip') {
stream = zlib.createGzip(opts);
} else if (method === 'deflate') {
stream = zlib.createDeflate(opts);
} else if (opts.compressor && method in opts.compressor) {
stream = opts.compressor[method];
}

// add bufferred listeners to stream
addListeners(stream, stream.on, listeners)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"devDependencies": {
"istanbul": "0.3.21",
"mocha": "2.3.3",
"supertest": "1.1.0"
"supertest": "1.1.0",
"through": "^2.3.8"
},
"files": [
"LICENSE",
Expand Down
39 changes: 39 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var bytes = require('bytes');
var crypto = require('crypto');
var http = require('http');
var request = require('supertest');
var through = require('through');

var compression = require('..');

Expand Down Expand Up @@ -492,6 +493,44 @@ describe('compression()', function(){
})
})

describe('when "Accept-Encoding: custom"', function () {
it('should not use content encoding without a custom compressor function', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

it('should use content encoding with a custom compressor function', function (done) {
var compressor = through(function (d) {
this.queue(d)
}, function () {
this.queue(null)
})
var opts = {
threshold: 0,
compressor: {
'bingo': compressor
}
}
var server = createServer(opts, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

describe('when "Cache-Control: no-transform" response header', function () {
it('should not compress response', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
Expand Down

0 comments on commit 665c0cf

Please sign in to comment.