Skip to content

Commit 3d205cd

Browse files
committed
brotli: set default quality to 4 closes #121
1 parent 2271a41 commit 3d205cd

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Setting `options[encoding] = false` will disable that encoding.
5959
#### options<span></span>.br
6060

6161
[Brotli compression](https://en.wikipedia.org/wiki/Brotli) is supported in node v11.7.0+, which includes it natively.
62+
As of v5.1.0, the default quality level is 4 for performance reasons.
6263

6364
### options.defaultEncoding\<String\>
6465

__tests__/defaults.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
const assert = require('assert')
3+
const zlib = require('zlib')
4+
5+
const createCompressMiddleware = require('..')
6+
7+
test('default brotli param quality should be 4', () => {
8+
const middleware = createCompressMiddleware()
9+
assert(Array.isArray(middleware.preferredEncodings))
10+
assert(middleware.encodingOptions)
11+
assert.strictEqual(middleware.encodingOptions.br[zlib.constants.BROTLI_PARAM_QUALITY], 4)
12+
})

lib/encodings.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Encodings.encodingMethods = {
2121
br: zlib.createBrotliCompress
2222
}
2323

24+
Encodings.encodingMethodDefaultOptions = {
25+
gzip: {},
26+
deflate: {},
27+
br: {
28+
[zlib.constants.BROTLI_PARAM_QUALITY]: 4
29+
}
30+
}
31+
2432
// how we treat `Accept-Encoding: *`
2533
Encodings.wildcardAcceptEncoding = ['gzip', 'deflate']
2634
// our preferred encodings

lib/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,22 @@ module.exports = (options = {}) => {
3131

3232
// `options.br = false` would remove it as a preferred encoding
3333
const preferredEncodings = Encodings.preferredEncodings.filter((encoding) => options[encoding] !== false && options[encoding] !== null)
34+
const encodingOptions = {}
35+
preferredEncodings.forEach((encoding) => {
36+
encodingOptions[encoding] = {
37+
...Encodings.encodingMethodDefaultOptions[encoding],
38+
...(options[encoding] || {})
39+
}
40+
})
41+
42+
Object.assign(compressMiddleware, {
43+
preferredEncodings,
44+
encodingOptions
45+
})
46+
47+
return compressMiddleware
3448

35-
return async (ctx, next) => {
49+
async function compressMiddleware (ctx, next) {
3650
ctx.vary('Accept-Encoding')
3751

3852
await next()
@@ -76,7 +90,7 @@ module.exports = (options = {}) => {
7690
ctx.res.removeHeader('Content-Length')
7791

7892
const compress = Encodings.encodingMethods[encoding]
79-
const stream = ctx.body = compress(options[encoding])
93+
const stream = ctx.body = compress(encodingOptions[encoding])
8094

8195
if (body instanceof Stream) {
8296
body.pipe(stream)

0 commit comments

Comments
 (0)