koa-compress 1.0.7 → 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 +18 -1
- package/LICENSE +22 -0
- package/README.md +10 -32
- package/index.js +29 -33
- package/package.json +27 -10
- package/.npmignore +0 -1
- package/.travis.yml +0 -3
- package/Makefile +0 -11
package/HISTORY.md
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
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
|
+
|
|
14
|
+
1.0.8 / 2014-09-14
|
|
15
|
+
==================
|
|
16
|
+
|
|
17
|
+
* bump compressible
|
|
18
|
+
|
|
2
19
|
1.0.7 / 2014-05-13
|
|
3
20
|
==================
|
|
4
21
|
|
|
5
22
|
* bump bytes
|
|
6
|
-
|
|
23
|
+
|
|
7
24
|
1.0.6 / 2014-04-24
|
|
8
25
|
==================
|
|
9
26
|
|
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
The MIT License (MIT)
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2014 Jonathan Ong me@jongleberry.com
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
|
14
|
+
all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
THE SOFTWARE.
|
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
|
|
|
@@ -6,9 +10,9 @@ Compress middleware for Koa
|
|
|
6
10
|
|
|
7
11
|
```js
|
|
8
12
|
var compress = require('koa-compress')
|
|
9
|
-
var
|
|
13
|
+
var Koa = require('koa')
|
|
10
14
|
|
|
11
|
-
var app =
|
|
15
|
+
var app = new Koa()
|
|
12
16
|
app.use(compress({
|
|
13
17
|
filter: function (content_type) {
|
|
14
18
|
return /text/i.test(content_type)
|
|
@@ -39,34 +43,8 @@ You can always disable compression by setting `this.compress = false`.
|
|
|
39
43
|
This bypasses the filter check.
|
|
40
44
|
|
|
41
45
|
```js
|
|
42
|
-
app.use(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.body = fs.createReadStream(file)
|
|
46
|
-
}
|
|
46
|
+
app.use((ctx, next) => {
|
|
47
|
+
ctx.compress = true
|
|
48
|
+
ctx.body = fs.createReadStream(file)
|
|
47
49
|
})
|
|
48
50
|
```
|
|
49
|
-
|
|
50
|
-
## License
|
|
51
|
-
|
|
52
|
-
The MIT License (MIT)
|
|
53
|
-
|
|
54
|
-
Copyright (c) 2013 Jonathan Ong me@jongleberry.com
|
|
55
|
-
|
|
56
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
57
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
58
|
-
in the Software without restriction, including without limitation the rights
|
|
59
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
60
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
61
|
-
furnished to do so, subject to the following conditions:
|
|
62
|
-
|
|
63
|
-
The above copyright notice and this permission notice shall be included in
|
|
64
|
-
all copies or substantial portions of the Software.
|
|
65
|
-
|
|
66
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
67
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
68
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
69
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
70
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
71
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
72
|
-
THE SOFTWARE.
|
package/index.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Module dependencies.
|
|
3
5
|
*/
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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')
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* Encoding methods supported.
|
|
14
16
|
*/
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
const encodingMethods = {
|
|
17
19
|
gzip: zlib.createGzip,
|
|
18
20
|
deflate: zlib.createDeflate
|
|
19
21
|
}
|
|
@@ -26,47 +28,41 @@ var encodingMethods = {
|
|
|
26
28
|
* @api public
|
|
27
29
|
*/
|
|
28
30
|
|
|
29
|
-
module.exports =
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
var filter = options.filter || compressible
|
|
33
|
-
|
|
34
|
-
var threshold = !options.threshold ? 1024
|
|
35
|
-
: typeof options.threshold === 'number' ? options.threshold
|
|
36
|
-
: typeof options.threshold === 'string' ? bytes(options.threshold)
|
|
37
|
-
: 1024
|
|
31
|
+
module.exports = (options = {}) => {
|
|
32
|
+
let { filter = compressible, threshold = 1024 } = options
|
|
33
|
+
if (typeof threshold === 'string') threshold = bytes(threshold)
|
|
38
34
|
|
|
39
|
-
return
|
|
40
|
-
|
|
35
|
+
return async (ctx, next) => {
|
|
36
|
+
ctx.vary('Accept-Encoding')
|
|
41
37
|
|
|
42
|
-
|
|
38
|
+
await next()
|
|
43
39
|
|
|
44
|
-
|
|
40
|
+
let { body } = ctx
|
|
45
41
|
if (!body) return
|
|
46
|
-
if (
|
|
47
|
-
if (
|
|
48
|
-
if (
|
|
49
|
-
if (
|
|
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
|
|
50
47
|
|
|
51
48
|
// forced compression or implied
|
|
52
|
-
if (!(
|
|
49
|
+
if (!(ctx.compress === true || filter(ctx.response.type))) return
|
|
53
50
|
|
|
54
51
|
// identity
|
|
55
|
-
|
|
56
|
-
if (!encoding)
|
|
52
|
+
const encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity')
|
|
53
|
+
if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')
|
|
57
54
|
if (encoding === 'identity') return
|
|
58
55
|
|
|
59
56
|
// json
|
|
60
|
-
if (isJSON(body)) body =
|
|
57
|
+
if (isJSON(body)) body = ctx.body = JSON.stringify(body)
|
|
61
58
|
|
|
62
59
|
// threshold
|
|
63
|
-
if (threshold &&
|
|
60
|
+
if (threshold && ctx.response.length < threshold) return
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
ctx.set('Content-Encoding', encoding)
|
|
63
|
+
ctx.res.removeHeader('Content-Length')
|
|
67
64
|
|
|
68
|
-
|
|
69
|
-
this.body = encodingMethods[encoding](options)
|
|
65
|
+
const stream = ctx.body = encodingMethods[encoding](options)
|
|
70
66
|
|
|
71
67
|
if (body instanceof Stream) {
|
|
72
68
|
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": "3.0.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Jonathan Ong",
|
|
7
7
|
"email": "me@jongleberry.com",
|
|
@@ -11,18 +11,35 @@
|
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"repository": "koajs/compress",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
14
|
+
"bytes": "^3.0.0",
|
|
15
|
+
"compressible": "^2.0.0",
|
|
16
|
+
"koa-is-json": "^1.0.0",
|
|
17
|
+
"statuses": "^1.0.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
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"
|
|
24
32
|
},
|
|
25
33
|
"scripts": {
|
|
26
|
-
"
|
|
34
|
+
"eslint": "eslint --ignore-path .gitignore .",
|
|
35
|
+
"test": "jest --forceExit"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"index.js",
|
|
39
|
+
"LICENSE",
|
|
40
|
+
"HISTORY.md"
|
|
41
|
+
],
|
|
42
|
+
"jest": {
|
|
43
|
+
"testEnvironment": "node"
|
|
27
44
|
}
|
|
28
45
|
}
|
package/.npmignore
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
test
|
package/.travis.yml
DELETED