koa-compress 4.0.0 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +6 -0
- package/README.md +20 -4
- package/lib/encodings.js +8 -0
- package/lib/index.js +20 -6
- package/package.json +12 -12
package/HISTORY.md
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
|
|
2
|
+
4.0.1 / 2020-04-29
|
|
3
|
+
==================
|
|
4
|
+
|
|
5
|
+
* fix issue with state being preserved between requests
|
|
6
|
+
|
|
2
7
|
4.0.0 / 2020-04-27
|
|
3
8
|
==================
|
|
4
9
|
|
|
@@ -8,6 +13,7 @@
|
|
|
8
13
|
* added compression disabling option: set `options.br = false`
|
|
9
14
|
|
|
10
15
|
3.1.0 / 2020-04-15
|
|
16
|
+
==================
|
|
11
17
|
|
|
12
18
|
* support no-transform @Pawda
|
|
13
19
|
|
package/README.md
CHANGED
|
@@ -18,15 +18,20 @@ app.use(compress({
|
|
|
18
18
|
},
|
|
19
19
|
threshold: 2048,
|
|
20
20
|
gzip: {
|
|
21
|
-
flush: require('zlib').Z_SYNC_FLUSH
|
|
21
|
+
flush: require('zlib').constants.Z_SYNC_FLUSH
|
|
22
22
|
},
|
|
23
23
|
deflate: {
|
|
24
|
-
flush: require('zlib').Z_SYNC_FLUSH,
|
|
24
|
+
flush: require('zlib').constants.Z_SYNC_FLUSH,
|
|
25
25
|
},
|
|
26
26
|
br: false // disable brotli
|
|
27
27
|
}))
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
+
## Maintainers
|
|
31
|
+
|
|
32
|
+
- Lead: @jonathanong [@jongleberry](https://twitter.com/jongleberry)
|
|
33
|
+
- Team: @koajs/compress
|
|
34
|
+
|
|
30
35
|
## Options
|
|
31
36
|
|
|
32
37
|
### filter\<Function\>
|
|
@@ -51,9 +56,20 @@ The current encodings are, in order of preference: `br`, `gzip`, `deflate`.
|
|
|
51
56
|
Setting `options[encoding] = {}` will pass those options to the encoding function.
|
|
52
57
|
Setting `options[encoding] = false` will disable that encoding.
|
|
53
58
|
|
|
54
|
-
|
|
59
|
+
#### options<span></span>.br
|
|
60
|
+
|
|
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.
|
|
63
|
+
|
|
64
|
+
### options.defaultEncoding\<String\>
|
|
65
|
+
|
|
66
|
+
An optional string, which specifies what encoders to use for requests without
|
|
67
|
+
[Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding).
|
|
68
|
+
Default `identity`.
|
|
55
69
|
|
|
56
|
-
|
|
70
|
+
The standard dictates to treat such requests as `*` meaning that all compressions are permissible,
|
|
71
|
+
yet it causes very practical problems when debugging servers with manual tools like `curl`, `wget`, and so on.
|
|
72
|
+
If you want to enable the standard behavior, just set `defaultEncoding` to `*`.
|
|
57
73
|
|
|
58
74
|
## Manually turning compression on and off
|
|
59
75
|
|
package/lib/encodings.js
CHANGED
|
@@ -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
|
package/lib/index.js
CHANGED
|
@@ -26,16 +26,27 @@ const NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
|
|
|
26
26
|
*/
|
|
27
27
|
|
|
28
28
|
module.exports = (options = {}) => {
|
|
29
|
-
let { filter = compressible, threshold = 1024 } = options
|
|
29
|
+
let { filter = compressible, threshold = 1024, defaultEncoding = 'identity' } = options
|
|
30
30
|
if (typeof threshold === 'string') threshold = bytes(threshold)
|
|
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
|
|
35
|
-
|
|
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
|
|
36
45
|
})
|
|
37
46
|
|
|
38
|
-
return
|
|
47
|
+
return compressMiddleware
|
|
48
|
+
|
|
49
|
+
async function compressMiddleware (ctx, next) {
|
|
39
50
|
ctx.vary('Accept-Encoding')
|
|
40
51
|
|
|
41
52
|
await next()
|
|
@@ -61,7 +72,10 @@ module.exports = (options = {}) => {
|
|
|
61
72
|
if (threshold && ctx.response.length < threshold) return
|
|
62
73
|
|
|
63
74
|
// get the preferred content encoding
|
|
64
|
-
encodings
|
|
75
|
+
const encodings = new Encodings({
|
|
76
|
+
preferredEncodings
|
|
77
|
+
})
|
|
78
|
+
encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || defaultEncoding)
|
|
65
79
|
const encoding = encodings.getPreferredContentEncoding()
|
|
66
80
|
|
|
67
81
|
// identity === no compression
|
|
@@ -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(
|
|
93
|
+
const stream = ctx.body = compress(encodingOptions[encoding])
|
|
80
94
|
|
|
81
95
|
if (body instanceof Stream) {
|
|
82
96
|
body.pipe(stream)
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koa-compress",
|
|
3
3
|
"description": "Compress middleware for koa",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.1.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Jonathan Ong",
|
|
7
7
|
"email": "me@jongleberry.com",
|
|
@@ -13,24 +13,24 @@
|
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"bytes": "^3.0.0",
|
|
15
15
|
"compressible": "^2.0.0",
|
|
16
|
-
"http-errors": "^1.
|
|
16
|
+
"http-errors": "^1.8.0",
|
|
17
17
|
"koa-is-json": "^1.0.0",
|
|
18
|
-
"statuses": "^2.0.
|
|
18
|
+
"statuses": "^2.0.1"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"eslint": "^
|
|
22
|
-
"eslint-config-standard": "^
|
|
23
|
-
"eslint-plugin-import": "^2.
|
|
21
|
+
"eslint": "^7.32.0",
|
|
22
|
+
"eslint-config-standard": "^16.0.3",
|
|
23
|
+
"eslint-plugin-import": "^2.24.0",
|
|
24
24
|
"eslint-plugin-node": "^11.0.0",
|
|
25
|
-
"eslint-plugin-promise": "^
|
|
26
|
-
"eslint-plugin-standard": "^
|
|
27
|
-
"jest": "^
|
|
28
|
-
"koa": "^2.
|
|
29
|
-
"supertest": "^
|
|
25
|
+
"eslint-plugin-promise": "^5.1.0",
|
|
26
|
+
"eslint-plugin-standard": "^5.0.0",
|
|
27
|
+
"jest": "^27.0.6",
|
|
28
|
+
"koa": "^2.13.1",
|
|
29
|
+
"supertest": "^6.1.6"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"eslint": "eslint --ignore-path .gitignore .",
|
|
33
|
-
"test": "jest
|
|
33
|
+
"test": "jest"
|
|
34
34
|
},
|
|
35
35
|
"files": [
|
|
36
36
|
"lib",
|