File tree Expand file tree Collapse file tree 4 files changed +37
-2
lines changed Expand file tree Collapse file tree 4 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,7 @@ Setting `options[encoding] = false` will disable that encoding.
59
59
#### options<span ></span >.br
60
60
61
61
[ 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.
62
63
63
64
### options.defaultEncoding\< String\>
64
65
Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change @@ -21,6 +21,14 @@ Encodings.encodingMethods = {
21
21
br : zlib . createBrotliCompress
22
22
}
23
23
24
+ Encodings . encodingMethodDefaultOptions = {
25
+ gzip : { } ,
26
+ deflate : { } ,
27
+ br : {
28
+ [ zlib . constants . BROTLI_PARAM_QUALITY ] : 4
29
+ }
30
+ }
31
+
24
32
// how we treat `Accept-Encoding: *`
25
33
Encodings . wildcardAcceptEncoding = [ 'gzip' , 'deflate' ]
26
34
// our preferred encodings
Original file line number Diff line number Diff line change @@ -31,8 +31,22 @@ module.exports = (options = {}) => {
31
31
32
32
// `options.br = false` would remove it as a preferred encoding
33
33
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
34
48
35
- return async ( ctx , next ) => {
49
+ async function compressMiddleware ( ctx , next ) {
36
50
ctx . vary ( 'Accept-Encoding' )
37
51
38
52
await next ( )
@@ -76,7 +90,7 @@ module.exports = (options = {}) => {
76
90
ctx . res . removeHeader ( 'Content-Length' )
77
91
78
92
const compress = Encodings . encodingMethods [ encoding ]
79
- const stream = ctx . body = compress ( options [ encoding ] )
93
+ const stream = ctx . body = compress ( encodingOptions [ encoding ] )
80
94
81
95
if ( body instanceof Stream ) {
82
96
body . pipe ( stream )
You can’t perform that action at this time.
0 commit comments