Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR 110: new default #120

Merged
merged 4 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah interesting workaround.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unset() didn't work, but setting the empty value did the trick.

.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.