koa-compress 2.0.0 → 3.0.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 +13 -1
- package/README.md +5 -1
- package/index.js +46 -52
- package/package.json +20 -11
package/HISTORY.md
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
|
|
2
|
+
3.0.0 / 2018-04-14
|
|
3
|
+
==================
|
|
4
|
+
|
|
5
|
+
* republish 2.1.0 as it switches to async functions
|
|
6
|
+
|
|
7
|
+
2.1.0 / 2018-04-13 (unpublished)
|
|
8
|
+
==================
|
|
9
|
+
|
|
10
|
+
* do not compress if the response is not writable
|
|
11
|
+
* switch testing frameworks to jest
|
|
12
|
+
* implement linting
|
|
13
|
+
|
|
2
14
|
1.0.8 / 2014-09-14
|
|
3
15
|
==================
|
|
4
16
|
|
|
5
17
|
* bump compressible
|
|
6
|
-
|
|
18
|
+
|
|
7
19
|
1.0.7 / 2014-05-13
|
|
8
20
|
==================
|
|
9
21
|
|
package/README.md
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
# Koa Compress
|
|
1
|
+
# Koa Compress
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.org/koajs/compress)
|
|
4
|
+
[](https://codecov.io/gh/koajs/compress)
|
|
5
|
+
[](https://greenkeeper.io/)
|
|
2
6
|
|
|
3
7
|
Compress middleware for Koa
|
|
4
8
|
|
package/index.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Module dependencies.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
const compressible = require('compressible')
|
|
8
|
+
const isJSON = require('koa-is-json')
|
|
9
|
+
const status = require('statuses')
|
|
10
|
+
const Stream = require('stream')
|
|
11
|
+
const bytes = require('bytes')
|
|
12
|
+
const zlib = require('zlib')
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Encoding methods supported.
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
const encodingMethods = {
|
|
19
19
|
gzip: zlib.createGzip,
|
|
20
20
|
deflate: zlib.createDeflate
|
|
21
21
|
}
|
|
@@ -28,52 +28,46 @@ var encodingMethods = {
|
|
|
28
28
|
* @api public
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
|
-
module.exports = (options) => {
|
|
32
|
-
|
|
31
|
+
module.exports = (options = {}) => {
|
|
32
|
+
let { filter = compressible, threshold = 1024 } = options
|
|
33
|
+
if (typeof threshold === 'string') threshold = bytes(threshold)
|
|
33
34
|
|
|
34
|
-
|
|
35
|
+
return async (ctx, next) => {
|
|
36
|
+
ctx.vary('Accept-Encoding')
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
: typeof options.threshold === 'number' ? options.threshold
|
|
38
|
-
: typeof options.threshold === 'string' ? bytes(options.threshold)
|
|
39
|
-
: 1024
|
|
38
|
+
await next()
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
let { body } = ctx
|
|
41
|
+
if (!body) return
|
|
42
|
+
if (ctx.res.headersSent || !ctx.writable) return
|
|
43
|
+
if (ctx.compress === false) return
|
|
44
|
+
if (ctx.request.method === 'HEAD') return
|
|
45
|
+
if (status.empty[ctx.response.status]) return
|
|
46
|
+
if (ctx.response.get('Content-Encoding')) return
|
|
47
|
+
|
|
48
|
+
// forced compression or implied
|
|
49
|
+
if (!(ctx.compress === true || filter(ctx.response.type))) return
|
|
50
|
+
|
|
51
|
+
// identity
|
|
52
|
+
const encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity')
|
|
53
|
+
if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')
|
|
54
|
+
if (encoding === 'identity') return
|
|
55
|
+
|
|
56
|
+
// json
|
|
57
|
+
if (isJSON(body)) body = ctx.body = JSON.stringify(body)
|
|
58
|
+
|
|
59
|
+
// threshold
|
|
60
|
+
if (threshold && ctx.response.length < threshold) return
|
|
61
|
+
|
|
62
|
+
ctx.set('Content-Encoding', encoding)
|
|
63
|
+
ctx.res.removeHeader('Content-Length')
|
|
64
|
+
|
|
65
|
+
const stream = ctx.body = encodingMethods[encoding](options)
|
|
43
66
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (ctx.response.get('Content-Encoding')) return
|
|
51
|
-
|
|
52
|
-
// forced compression or implied
|
|
53
|
-
if (!(ctx.compress === true || filter(ctx.response.type))) return
|
|
54
|
-
|
|
55
|
-
// identity
|
|
56
|
-
var encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity')
|
|
57
|
-
if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')
|
|
58
|
-
if (encoding === 'identity') return
|
|
59
|
-
|
|
60
|
-
// json
|
|
61
|
-
if (isJSON(body)) body = ctx.body = JSON.stringify(body)
|
|
62
|
-
|
|
63
|
-
// threshold
|
|
64
|
-
if (threshold && ctx.response.length < threshold) return
|
|
65
|
-
|
|
66
|
-
ctx.set('Content-Encoding', encoding)
|
|
67
|
-
ctx.res.removeHeader('Content-Length')
|
|
68
|
-
|
|
69
|
-
var stream =
|
|
70
|
-
ctx.body = encodingMethods[encoding](options)
|
|
71
|
-
|
|
72
|
-
if (body instanceof Stream) {
|
|
73
|
-
body.pipe(stream)
|
|
74
|
-
} else {
|
|
75
|
-
stream.end(body)
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
};
|
|
67
|
+
if (body instanceof Stream) {
|
|
68
|
+
body.pipe(stream)
|
|
69
|
+
} else {
|
|
70
|
+
stream.end(body)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
79
73
|
}
|
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": "3.0.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Jonathan Ong",
|
|
7
7
|
"email": "me@jongleberry.com",
|
|
@@ -11,26 +11,35 @@
|
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"repository": "koajs/compress",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"bytes": "^
|
|
14
|
+
"bytes": "^3.0.0",
|
|
15
15
|
"compressible": "^2.0.0",
|
|
16
16
|
"koa-is-json": "^1.0.0",
|
|
17
17
|
"statuses": "^1.0.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
20
|
+
"eslint": "^4.19.0",
|
|
21
|
+
"eslint-config-standard": "^11.0.0",
|
|
22
|
+
"eslint-config-standard-react": "^6.0.0",
|
|
23
|
+
"eslint-plugin-flowtype": "^2.46.1",
|
|
24
|
+
"eslint-plugin-import": "^2.9.0",
|
|
25
|
+
"eslint-plugin-node": "^6.0.1",
|
|
26
|
+
"eslint-plugin-promise": "^3.7.0",
|
|
27
|
+
"eslint-plugin-react": "^7.7.0",
|
|
28
|
+
"eslint-plugin-standard": "^3.0.1",
|
|
29
|
+
"jest": "^22.4.3",
|
|
30
|
+
"koa": "^2.0.1",
|
|
31
|
+
"supertest": "^3.0.0"
|
|
25
32
|
},
|
|
26
33
|
"scripts": {
|
|
27
|
-
"
|
|
28
|
-
"test
|
|
29
|
-
"test-travis": "NODE_ENV=test node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should"
|
|
34
|
+
"eslint": "eslint --ignore-path .gitignore .",
|
|
35
|
+
"test": "jest --forceExit"
|
|
30
36
|
},
|
|
31
37
|
"files": [
|
|
32
38
|
"index.js",
|
|
33
39
|
"LICENSE",
|
|
34
40
|
"HISTORY.md"
|
|
35
|
-
]
|
|
41
|
+
],
|
|
42
|
+
"jest": {
|
|
43
|
+
"testEnvironment": "node"
|
|
44
|
+
}
|
|
36
45
|
}
|