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: add default option #191

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
unreleased
==========

* Add the defaultEncoding option for requests without `Accept-Encoding` header
* deps: accepts@~1.3.8
- Fix sorting encoding with extra parameters
- deps: mime-types@~2.1.34
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ The default value is `zlib.Z_DEFAULT_WINDOWBITS`, or `15`.
See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
regarding the usage.

##### defaultEncoding

This is the default encoding to use when the client does not specify an encoding in the request's [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) header.

The default value is `identity`.

#### .filter

The default `filter` function. This is used to construct a custom filter
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ module.exports.filter = shouldCompress

var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/

var encodingSupported = ['*', 'gzip', 'deflate', 'identity']
bjohansebas marked this conversation as resolved.
Show resolved Hide resolved

/**
* Compress response data with gzip / deflate.
*
Expand All @@ -51,6 +53,7 @@ function compression (options) {
// options
var filter = opts.filter || shouldCompress
var threshold = bytes.parse(opts.threshold)
var defaultEncoding = opts.defaultEncoding || 'identity'
bjohansebas marked this conversation as resolved.
Show resolved Hide resolved

if (threshold == null) {
threshold = 1024
Expand Down Expand Up @@ -182,12 +185,21 @@ function compression (options) {
method = accept.encoding(['gzip', 'identity'])
}

// if no method is found, use the default encoding
if (encodingSupported.indexOf(defaultEncoding) !== -1 && !req.headers['accept-encoding']) {
method = defaultEncoding === '*' ? 'gzip' : defaultEncoding
}

// negotiation failed
if (!method || method === 'identity') {
nocompress('not acceptable')
return
}

if (opts.defaultEncoding) {
bjohansebas marked this conversation as resolved.
Show resolved Hide resolved
opts.defaultEncoding = undefined
}

// compression stream
debug('%s compression', method)
stream = method === 'gzip'
Expand Down
80 changes: 80 additions & 0 deletions test/compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,86 @@ describe('compression()', function () {
.end()
})
})

describe('defaultEncoding', function () {
it('should compress the provided encoding and not the default encoding', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'deflate' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

it('should not compress when defaultEncoding is identity', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'identity' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

it('should compress when defaultEncoding is gzip', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'gzip' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

it('should compress when defaultEncoding is deflate', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'deflate' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

it('should not compress when defaultEncoding is unknown', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: 'bogus' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

it('should be gzip if no accept-encoding is sent when defaultEncoding is *', function (done) {
var server = createServer({ threshold: 0, defaultEncoding: '*' }, function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello, world')
})

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

function createServer (opts, fn) {
Expand Down
Loading