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

feat: don't compress on x-no-compression header #208

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ function chunkLength (chunk, encoding) {

function shouldCompress (req, res) {
var type = res.getHeader('Content-Type')
var noCompressionHeader = res.getHeader('x-no-compression')

if (noCompressionHeader) {
debug('%s not compressed', type)
return false
}

if (type === undefined || !compressible(type)) {
debug('%s not compressible', type)
Expand Down
32 changes: 31 additions & 1 deletion test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ describe('compression()', function () {
.expect(200, 'hello, world', done)
})

it('should not set Vary headerh', function (done) {
it('should not set Vary header', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('Cache-Control', 'no-transform')
res.setHeader('Content-Type', 'text/plain')
Expand All @@ -570,6 +570,36 @@ describe('compression()', function () {
})
})

describe('when "X-No-Compression" request header', function () {
it('should not compress response', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('X-No-Compression', '0')
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

it('should not set Vary header', function (done) {
var server = createServer({ threshold: 0 }, function (req, res) {
res.setHeader('X-No-Compression', '1')
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

describe('.filter', function () {
it('should be a function', function () {
assert.strictEqual(typeof compression.filter, 'function')
Expand Down
Loading