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 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
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
unreleased
==========
* Use `res.headersSent` when available
* Add the enforceEncoding option for requests without `Accept-Encoding` header

1.7.5 / 2024-10-31
==========

* deps: Replace accepts with negotiator@~0.6.4
- Add preference option
* deps: [email protected]
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.

##### enforceEncoding

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
8 changes: 8 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 enforceEncoding = opts.enforceEncoding || 'identity'

if (threshold == null) {
threshold = 1024
Expand Down Expand Up @@ -177,6 +180,11 @@ function compression (options) {
var negotiator = new Negotiator(req)
var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip'])

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see * referenced anywhere but here. Can you describe why this is necessary when you could drop this check and just have folks pass gzip directly for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If * is not checked and changed, the deflate encoding would be applied as it’s the last one in the check (

compression/index.js

Lines 196 to 198 in 66c0cb7

stream = method === 'gzip'
? zlib.createGzip(opts)
: zlib.createDeflate(opts)
), and deflate is not the encoding we want to be applied, it should be gzip when it’s *.

}

// negotiation failed
if (!method || method === 'identity') {
nocompress('not acceptable')
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('enforceEncoding', function () {
it('should compress the provided encoding and not the default encoding', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: '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 enforceEncoding is identity', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: '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 enforceEncoding is gzip', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: '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 enforceEncoding is deflate', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: '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 enforceEncoding is unknown', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: '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 enforceEncoding is *', function (done) {
var server = createServer({ threshold: 0, enforceEncoding: '*' }, 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