Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't encode responses with Cache-Control: no-transform #53

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ var zlib = require('zlib')
module.exports = compression
module.exports.filter = shouldCompress

/**
* Regex to match no-transform directive in a cache-control header
*/
var NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/

/**
* Compress response data with gzip / deflate.
*
Expand Down Expand Up @@ -135,6 +140,18 @@ function compression(options) {
return
}

// Don't compress for Cache-Control: no-transform
// https://tools.ietf.org/html/rfc7234#section-5.2.1.6
var cacheControl = res.getHeader('Cache-Control')
if (cacheControl) {
var noTransform = NO_TRANSFORM_REGEX.test(cacheControl)
if (noTransform) {
nocompress('no-transform')
return
}
}


// vary
vary(res, 'Accept-Encoding')

Expand Down
46 changes: 46 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,52 @@ describe('compression()', function(){
.expect(200, done)
})

describe("Cache-Control", function() {

[
'no-transform',
'public, no-transform',
'no-transform, private',
'no-transform , max-age=1000',
'max-age=1000 , no-transform'
].forEach(function(headerValue) {
it('should skip Cache-Control: ' + headerValue, function(done){
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.setHeader('Cache-Control', headerValue)
res.end('hello, world')
})

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

[
'not-no-transform',
'public',
'no-transform-thingy'
].forEach(function(headerValue) {
it('should not skip Cache-Control: ' + headerValue, function(done){
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.setHeader('Cache-Control', headerValue)
res.end('hello, world')
})

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

})

it('should skip unknown accept-encoding', function(done){
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
Expand Down