Skip to content

Commit da9833d

Browse files
uhopdobesv
andauthored
PR 110: new default (#120)
* Use identity as default value for Accept-Encoding if it is blank or not provided * Updated the lock file. * Added defaultEncoding. The code, the tests, the docs. * Stupid ESLint fix. Co-authored-by: Dobes Vandermeer <dobesv@gmail.com>
1 parent 0139443 commit da9833d

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,19 @@ The current encodings are, in order of preference: `br`, `gzip`, `deflate`.
5151
Setting `options[encoding] = {}` will pass those options to the encoding function.
5252
Setting `options[encoding] = false` will disable that encoding.
5353

54-
### options.br
54+
#### options<span></span>.br
5555

56-
[Brotli compression](https://en.wikipedia.org/wiki/Brotli) is supported in node v11.7.0+, which includes it natively.
56+
[Brotli compression](https://en.wikipedia.org/wiki/Brotli) is supported in node v11.7.0+, which includes it natively.
57+
58+
### options.defaultEncoding\<String\>
59+
60+
An optional string, which specifies what encoders to use for requests without
61+
[Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding).
62+
Default `idenity`.
63+
64+
The standard dictates to treat such requests as `*` meaning that all compressions are permissible,
65+
yet it causes very practical problems when debugging servers with manual tools like `curl`, `wget`, and so on.
66+
If you want to enable the standard behavior, just set `defaultEncoding` to `*`.
5767

5868
## Manually turning compression on and off
5969

__tests__/index.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,55 @@ describe('Compress', () => {
245245
.expect(200, done)
246246
})
247247

248-
it('should not crash if no accept-encoding is sent', (done) => {
248+
it('should not compress if no accept-encoding is sent (with the default)', (done) => {
249249
const app = new Koa()
250+
app.use(compress({
251+
threshold: 0
252+
}))
253+
app.use((ctx) => {
254+
ctx.type = 'text'
255+
ctx.body = buffer
256+
})
257+
server = app.listen()
250258

251-
app.use(compress())
252-
app.use(sendBuffer)
259+
request(server)
260+
.get('/')
261+
.set('Accept-Encoding', '')
262+
.end((err, res) => {
263+
if (err) { return done(err) }
264+
265+
assert(!res.headers['content-encoding'])
266+
assert(!res.headers['transfer-encoding'])
267+
assert.equal(res.headers['content-length'], '1024')
268+
assert.equal(res.headers.vary, 'Accept-Encoding')
269+
270+
done()
271+
})
272+
})
273+
274+
it('should be gzip if no accept-encoding is sent (with the standard default)', (done) => {
275+
const app = new Koa()
276+
app.use(compress({
277+
threshold: 0,
278+
defaultEncoding: '*'
279+
}))
280+
app.use((ctx) => {
281+
ctx.type = 'text'
282+
ctx.body = buffer
283+
})
253284
server = app.listen()
254285

255286
request(server)
256287
.get('/')
257-
.expect(200, done)
288+
.set('Accept-Encoding', '')
289+
.end((err, res) => {
290+
if (err) { return done(err) }
291+
292+
assert.equal(res.headers['content-encoding'], 'gzip')
293+
assert.equal(res.headers.vary, 'Accept-Encoding')
294+
295+
done()
296+
})
258297
})
259298

260299
it('should not crash if a type does not pass the filter', (done) => {

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
2626
*/
2727

2828
module.exports = (options = {}) => {
29-
let { filter = compressible, threshold = 1024 } = options
29+
let { filter = compressible, threshold = 1024, defaultEncoding = 'identity' } = options
3030
if (typeof threshold === 'string') threshold = bytes(threshold)
3131

3232
// `options.br = false` would remove it as a preferred encoding
@@ -61,7 +61,7 @@ module.exports = (options = {}) => {
6161
const encodings = new Encodings({
6262
preferredEncodings
6363
})
64-
encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || undefined)
64+
encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || defaultEncoding)
6565
const encoding = encodings.getPreferredContentEncoding()
6666

6767
// identity === no compression

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)