From c87d82f99604d94e416a1cb84d30470263192037 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 27 Sep 2024 18:39:24 -0500 Subject: [PATCH 1/7] add test for default encoding --- test/compression.js | 67 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/test/compression.js b/test/compression.js index 6975ea0b..fe6d4ec1 100644 --- a/test/compression.js +++ b/test/compression.js @@ -657,6 +657,73 @@ 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) + }) + }) }) function createServer (opts, fn) { From b1644d96b6929d3e0abecc5234779a26011d32e9 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 27 Sep 2024 18:56:29 -0500 Subject: [PATCH 2/7] add functionality --- index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/index.js b/index.js index 1d089427..65037e4a 100644 --- a/index.js +++ b/index.js @@ -37,6 +37,8 @@ module.exports.filter = shouldCompress var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/ +var encodingSupported = ['gzip', 'deflate', 'identity'] + /** * Compress response data with gzip / deflate. * @@ -51,6 +53,7 @@ function compression (options) { // options var filter = opts.filter || shouldCompress var threshold = bytes.parse(opts.threshold) + var defaultEncoding = opts.defaultEncoding || 'identity' if (threshold == null) { threshold = 1024 @@ -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'].split(',')[0] === '') { + method = defaultEncoding + } + // negotiation failed if (!method || method === 'identity') { nocompress('not acceptable') return } + if (opts.defaultEncoding) { + opts.defaultEncoding = undefined + } + // compression stream debug('%s compression', method) stream = method === 'gzip' From 7b5825789391a35b8c6a7d4eb2093dd2132a1936 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 27 Sep 2024 19:19:24 -0500 Subject: [PATCH 3/7] support * --- index.js | 4 ++-- test/compression.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 65037e4a..5c8ae827 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ module.exports.filter = shouldCompress var cacheControlNoTransformRegExp = /(?:^|,)\s*?no-transform\s*?(?:,|$)/ -var encodingSupported = ['gzip', 'deflate', 'identity'] +var encodingSupported = ['*', 'gzip', 'deflate', 'identity'] /** * Compress response data with gzip / deflate. @@ -187,7 +187,7 @@ function compression (options) { // if no method is found, use the default encoding if (encodingSupported.indexOf(defaultEncoding) !== -1 && req.headers['accept-encoding'].split(',')[0] === '') { - method = defaultEncoding + method = defaultEncoding === '*' ? 'gzip' : defaultEncoding } // negotiation failed diff --git a/test/compression.js b/test/compression.js index fe6d4ec1..6d1bb25a 100644 --- a/test/compression.js +++ b/test/compression.js @@ -723,6 +723,19 @@ describe('compression()', function () { .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) + }) }) }) From 5d6a82795908b8defa90d34a3661f31431423f34 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 27 Sep 2024 19:29:02 -0500 Subject: [PATCH 4/7] add docs --- HISTORY.md | 2 +- README.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index b038e53f..e5fb31b1 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/README.md b/README.md index eb4d8878..a862c819 100644 --- a/README.md +++ b/README.md @@ -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 From 24609a63726a6d66ea89f9b35f4b3d1786b28ed6 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sat, 28 Sep 2024 10:11:32 -0500 Subject: [PATCH 5/7] fix logic --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 5c8ae827..69e72872 100644 --- a/index.js +++ b/index.js @@ -186,7 +186,7 @@ function compression (options) { } // if no method is found, use the default encoding - if (encodingSupported.indexOf(defaultEncoding) !== -1 && req.headers['accept-encoding'].split(',')[0] === '') { + if (encodingSupported.indexOf(defaultEncoding) !== -1 && !req.headers['accept-encoding']) { method = defaultEncoding === '*' ? 'gzip' : defaultEncoding } From 7a3f9ef9e5ebf49719e1fcb3216b6dacd0325f22 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Tue, 5 Nov 2024 22:10:58 -0500 Subject: [PATCH 6/7] change defaultEncoding to enforceEnconding --- HISTORY.md | 2 +- README.md | 2 +- index.js | 10 +++------- test/compression.js | 24 ++++++++++++------------ 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index fe4e94b8..00193aef 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ unreleased ========== - * Add the defaultEncoding option for requests without `Accept-Encoding` header + * Add the enforceEncoding option for requests without `Accept-Encoding` header 1.7.5 / 2024-10-31 ========== diff --git a/README.md b/README.md index 91591b67..d32645e2 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ 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 +##### 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. diff --git a/index.js b/index.js index 6889b3f4..bb420e46 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ function compression (options) { // options var filter = opts.filter || shouldCompress var threshold = bytes.parse(opts.threshold) - var defaultEncoding = opts.defaultEncoding || 'identity' + var enforceEncoding = opts.enforceEncoding || 'identity' if (threshold == null) { threshold = 1024 @@ -181,8 +181,8 @@ function compression (options) { var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip']) // if no method is found, use the default encoding - if (encodingSupported.indexOf(defaultEncoding) !== -1 && !req.headers['accept-encoding']) { - method = defaultEncoding === '*' ? 'gzip' : defaultEncoding + if (encodingSupported.indexOf(enforceEncoding) !== -1 && !req.headers['accept-encoding']) { + method = enforceEncoding === '*' ? 'gzip' : enforceEncoding } // negotiation failed @@ -191,10 +191,6 @@ function compression (options) { return } - if (opts.defaultEncoding) { - opts.defaultEncoding = undefined - } - // compression stream debug('%s compression', method) stream = method === 'gzip' diff --git a/test/compression.js b/test/compression.js index 6d1bb25a..ad2b1472 100644 --- a/test/compression.js +++ b/test/compression.js @@ -658,9 +658,9 @@ describe('compression()', function () { }) }) - describe('defaultEncoding', function () { + describe('enforceEncoding', function () { it('should compress the provided encoding and not the default encoding', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: 'deflate' }, function (req, res) { + var server = createServer({ threshold: 0, enforceEncoding: 'deflate' }, function (req, res) { res.setHeader('Content-Type', 'text/plain') res.end('hello, world') }) @@ -672,8 +672,8 @@ describe('compression()', function () { .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) { + 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') }) @@ -685,8 +685,8 @@ describe('compression()', function () { .expect(200, 'hello, world', done) }) - it('should compress when defaultEncoding is gzip', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: 'gzip' }, function (req, res) { + 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') }) @@ -698,8 +698,8 @@ describe('compression()', function () { .expect(200, 'hello, world', done) }) - it('should compress when defaultEncoding is deflate', function (done) { - var server = createServer({ threshold: 0, defaultEncoding: 'deflate' }, function (req, res) { + 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') }) @@ -711,8 +711,8 @@ describe('compression()', function () { .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) { + 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') }) @@ -724,8 +724,8 @@ describe('compression()', function () { .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) { + 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') }) From 7e00d42978bd96f702b6af4a780646a2f686ccb8 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Sat, 16 Nov 2024 14:44:56 -0500 Subject: [PATCH 7/7] sort conditional --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d9f650c5..70d3491f 100644 --- a/index.js +++ b/index.js @@ -181,7 +181,7 @@ function compression (options) { var method = negotiator.encoding(['gzip', 'deflate', 'identity'], ['gzip']) // if no method is found, use the default encoding - if (encodingSupported.indexOf(enforceEncoding) !== -1 && !req.headers['accept-encoding']) { + if (!req.headers['accept-encoding'] && encodingSupported.indexOf(enforceEncoding) !== -1) { method = enforceEncoding === '*' ? 'gzip' : enforceEncoding }