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 9be130e..f3c62b5 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -245,16 +245,55 @@ 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 (with the default)', (done) => { const app = new Koa() + app.use(compress({ + threshold: 0 + })) + app.use((ctx) => { + ctx.type = 'text' + ctx.body = buffer + }) + server = app.listen() - app.use(compress()) - app.use(sendBuffer) + request(server) + .get('/') + .set('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 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('/') - .expect(200, done) + .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) => { diff --git a/lib/index.js b/lib/index.js index 582a912..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'] || undefined) + encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || defaultEncoding) const encoding = encodings.getPreferredContentEncoding() // identity === no compression 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": {