Skip to content

Commit

Permalink
PR 110: new default (#120)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
uhop and dobesv authored Jul 6, 2020
1 parent 0139443 commit da9833d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<span></span>.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\<String\>

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

Expand Down
47 changes: 43 additions & 4 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da9833d

Please sign in to comment.