From 48670e18d8329c2f3d4ee66dc7081135d97b8fec Mon Sep 17 00:00:00 2001 From: Dobes Vandermeer Date: Wed, 13 May 2020 23:59:36 -0700 Subject: [PATCH 1/4] Use identity as default value for Accept-Encoding if it is blank or not provided --- __tests__/index.js | 24 +++++++++++++++++++----- lib/index.js | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/__tests__/index.js b/__tests__/index.js index 9be130e..33562da 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -245,16 +245,30 @@ describe('Compress', () => { .expect(200, done) }) - it('should not crash if no accept-encoding is sent', (done) => { + it('should not compress if no accept-encoding is sent', (done) => { const app = new Koa() - - app.use(compress()) - app.use(sendBuffer) + app.use(compress({ + threshold: 0 + })) + app.use((ctx) => { + ctx.type = 'text' + ctx.body = buffer + }) server = app.listen() request(server) .get('/') - .expect(200, done) + .unset('accept-encoding') + .end((err, res) => { + if (err) { return done(err) } + + assert(!res.headers['content-encoding']) + assert(!res.headers['transfer-encoding']) + assert.equal(res.headers['content-length'], '1024') + assert.equal(res.headers.vary, 'Accept-Encoding') + + done() + }) }) it('should not crash if a type does not pass the filter', (done) => { diff --git a/lib/index.js b/lib/index.js index 582a912..a1841bb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -61,7 +61,7 @@ module.exports = (options = {}) => { const encodings = new Encodings({ preferredEncodings }) - encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || undefined) + encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || 'identity') const encoding = encodings.getPreferredContentEncoding() // identity === no compression From 28392b316fcba035786640db1649cd500f838268 Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Sun, 5 Jul 2020 16:33:54 -0500 Subject: [PATCH 2/4] Updated the lock file. --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 1ac0528..e3afd05 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "koa-compress", - "version": "4.0.0", + "version": "4.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { From 7256b9b69c6c4f78a6a5e9edcb537bcf0670f43c Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Sun, 5 Jul 2020 16:34:38 -0500 Subject: [PATCH 3/4] Added defaultEncoding. The code, the tests, the docs. --- README.md | 14 ++++++++++++-- __tests__/index.js | 31 ++++++++++++++++++++++++++++--- lib/index.js | 4 ++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c2929bc..8f08e41 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,19 @@ The current encodings are, in order of preference: `br`, `gzip`, `deflate`. Setting `options[encoding] = {}` will pass those options to the encoding function. Setting `options[encoding] = false` will disable that encoding. -### options.br +#### options.br -[Brotli compression](https://en.wikipedia.org/wiki/Brotli) is supported in node v11.7.0+, which includes it natively. +[Brotli compression](https://en.wikipedia.org/wiki/Brotli) is supported in node v11.7.0+, which includes it natively. + +### options.defaultEncoding\ + +An optional string, which specifies what encoders to use for requests without +[Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding). +Default `idenity`. + +The standard dictates to treat such requests as `*` meaning that all compressions are permissible, +yet it causes very practical problems when debugging servers with manual tools like `curl`, `wget`, and so on. +If you want to enable the standard behavior, just set `defaultEncoding` to `*`. ## Manually turning compression on and off diff --git a/__tests__/index.js b/__tests__/index.js index 33562da..740e979 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -245,7 +245,7 @@ describe('Compress', () => { .expect(200, done) }) - it('should not compress if no accept-encoding is sent', (done) => { + it('should not compress if no accept-encoding is sent (with the default)', (done) => { const app = new Koa() app.use(compress({ threshold: 0 @@ -257,8 +257,8 @@ describe('Compress', () => { server = app.listen() request(server) - .get('/') - .unset('accept-encoding') + .get('/') + .set('Accept-Encoding', '') .end((err, res) => { if (err) { return done(err) } @@ -271,6 +271,31 @@ describe('Compress', () => { }) }) + it('should be gzip if no accept-encoding is sent (with the standard default)', (done) => { + const app = new Koa() + app.use(compress({ + threshold: 0, + defaultEncoding: '*' + })) + app.use((ctx) => { + ctx.type = 'text' + ctx.body = buffer + }) + server = app.listen() + + request(server) + .get('/') + .set('Accept-Encoding', '') + .end((err, res) => { + if (err) { return done(err) } + + assert.equal(res.headers['content-encoding'], 'gzip') + assert.equal(res.headers.vary, 'Accept-Encoding') + + done() + }) + }) + it('should not crash if a type does not pass the filter', (done) => { const app = new Koa() diff --git a/lib/index.js b/lib/index.js index a1841bb..1849e90 100644 --- a/lib/index.js +++ b/lib/index.js @@ -26,7 +26,7 @@ const NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/ */ module.exports = (options = {}) => { - let { filter = compressible, threshold = 1024 } = options + let { filter = compressible, threshold = 1024, defaultEncoding = 'identity' } = options if (typeof threshold === 'string') threshold = bytes(threshold) // `options.br = false` would remove it as a preferred encoding @@ -61,7 +61,7 @@ module.exports = (options = {}) => { const encodings = new Encodings({ preferredEncodings }) - encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || 'identity') + encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || defaultEncoding) const encoding = encodings.getPreferredContentEncoding() // identity === no compression From 9a37801cfef64fad787018688d8fe623d8c65c18 Mon Sep 17 00:00:00 2001 From: Eugene Lazutkin Date: Sun, 5 Jul 2020 16:41:40 -0500 Subject: [PATCH 4/4] Stupid ESLint fix. --- __tests__/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/index.js b/__tests__/index.js index 740e979..f3c62b5 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -257,7 +257,7 @@ describe('Compress', () => { server = app.listen() request(server) - .get('/') + .get('/') .set('Accept-Encoding', '') .end((err, res) => { if (err) { return done(err) }