Skip to content

Commit f4e596c

Browse files
authored
feat: add default option (#191)
* add test for default encoding * add functionality * support * * add docs * fix logic * change defaultEncoding to enforceEnconding * sort conditional
1 parent b7d5d77 commit f4e596c

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

HISTORY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
unreleased
22
==========
33
* Use `res.headersSent` when available
4+
* Add the enforceEncoding option for requests without `Accept-Encoding` header
45

56
1.7.5 / 2024-10-31
67
==========
7-
88
* deps: Replace accepts with negotiator@~0.6.4
99
- Add preference option
1010

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ The default value is `zlib.Z_DEFAULT_WINDOWBITS`, or `15`.
139139
See [Node.js documentation](http://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
140140
regarding the usage.
141141

142+
##### enforceEncoding
143+
144+
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.
145+
146+
The default value is `identity`.
147+
142148
#### .filter
143149

144150
The default `filter` function. This is used to construct a custom filter

index.js

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ module.exports.filter = shouldCompress
3737

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

40+
var encodingSupported = ['*', 'gzip', 'deflate', 'identity']
41+
4042
/**
4143
* Compress response data with gzip / deflate.
4244
*
@@ -51,6 +53,7 @@ function compression (options) {
5153
// options
5254
var filter = opts.filter || shouldCompress
5355
var threshold = bytes.parse(opts.threshold)
56+
var enforceEncoding = opts.enforceEncoding || 'identity'
5457

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

183+
// if no method is found, use the default encoding
184+
if (!req.headers['accept-encoding'] && encodingSupported.indexOf(enforceEncoding) !== -1) {
185+
method = enforceEncoding === '*' ? 'gzip' : enforceEncoding
186+
}
187+
180188
// negotiation failed
181189
if (!method || method === 'identity') {
182190
nocompress('not acceptable')

test/compression.js

+80
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,86 @@ describe('compression()', function () {
702702
.end()
703703
})
704704
})
705+
706+
describe('enforceEncoding', function () {
707+
it('should compress the provided encoding and not the default encoding', function (done) {
708+
var server = createServer({ threshold: 0, enforceEncoding: 'deflate' }, function (req, res) {
709+
res.setHeader('Content-Type', 'text/plain')
710+
res.end('hello, world')
711+
})
712+
713+
request(server)
714+
.get('/')
715+
.set('Accept-Encoding', 'gzip')
716+
.expect('Content-Encoding', 'gzip')
717+
.expect(200, 'hello, world', done)
718+
})
719+
720+
it('should not compress when enforceEncoding is identity', function (done) {
721+
var server = createServer({ threshold: 0, enforceEncoding: 'identity' }, function (req, res) {
722+
res.setHeader('Content-Type', 'text/plain')
723+
res.end('hello, world')
724+
})
725+
726+
request(server)
727+
.get('/')
728+
.set('Accept-Encoding', '')
729+
.expect(shouldNotHaveHeader('Content-Encoding'))
730+
.expect(200, 'hello, world', done)
731+
})
732+
733+
it('should compress when enforceEncoding is gzip', function (done) {
734+
var server = createServer({ threshold: 0, enforceEncoding: 'gzip' }, function (req, res) {
735+
res.setHeader('Content-Type', 'text/plain')
736+
res.end('hello, world')
737+
})
738+
739+
request(server)
740+
.get('/')
741+
.set('Accept-Encoding', '')
742+
.expect('Content-Encoding', 'gzip')
743+
.expect(200, 'hello, world', done)
744+
})
745+
746+
it('should compress when enforceEncoding is deflate', function (done) {
747+
var server = createServer({ threshold: 0, enforceEncoding: 'deflate' }, function (req, res) {
748+
res.setHeader('Content-Type', 'text/plain')
749+
res.end('hello, world')
750+
})
751+
752+
request(server)
753+
.get('/')
754+
.set('Accept-Encoding', '')
755+
.expect('Content-Encoding', 'deflate')
756+
.expect(200, 'hello, world', done)
757+
})
758+
759+
it('should not compress when enforceEncoding is unknown', function (done) {
760+
var server = createServer({ threshold: 0, enforceEncoding: 'bogus' }, function (req, res) {
761+
res.setHeader('Content-Type', 'text/plain')
762+
res.end('hello, world')
763+
})
764+
765+
request(server)
766+
.get('/')
767+
.set('Accept-Encoding', '')
768+
.expect(shouldNotHaveHeader('Content-Encoding'))
769+
.expect(200, 'hello, world', done)
770+
})
771+
772+
it('should be gzip if no accept-encoding is sent when enforceEncoding is *', function (done) {
773+
var server = createServer({ threshold: 0, enforceEncoding: '*' }, function (req, res) {
774+
res.setHeader('Content-Type', 'text/plain')
775+
res.end('hello, world')
776+
})
777+
778+
request(server)
779+
.get('/')
780+
.set('Accept-Encoding', '')
781+
.expect('Content-Encoding', 'gzip')
782+
.expect(200, 'hello, world', done)
783+
})
784+
})
705785
})
706786

707787
function createServer (opts, fn) {

0 commit comments

Comments
 (0)