koa-compress 1.0.6 → 2.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 +11 -1
- package/LICENSE +22 -0
- package/README.md +5 -31
- package/index.js +33 -31
- package/package.json +19 -11
- package/.npmignore +0 -1
- package/.travis.yml +0 -3
- package/Makefile +0 -11
package/HISTORY.md
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
|
|
2
|
+
1.0.8 / 2014-09-14
|
|
3
|
+
==================
|
|
4
|
+
|
|
5
|
+
* bump compressible
|
|
6
|
+
|
|
7
|
+
1.0.7 / 2014-05-13
|
|
8
|
+
==================
|
|
9
|
+
|
|
10
|
+
* bump bytes
|
|
11
|
+
|
|
2
12
|
1.0.6 / 2014-04-24
|
|
3
13
|
==================
|
|
4
14
|
|
|
5
15
|
* refactor
|
|
6
16
|
* use statuses
|
|
7
|
-
|
|
17
|
+
|
|
8
18
|
1.0.5 / 2014-04-24
|
|
9
19
|
==================
|
|
10
20
|
|
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
|
@@ -6,9 +6,9 @@ Compress middleware for Koa
|
|
|
6
6
|
|
|
7
7
|
```js
|
|
8
8
|
var compress = require('koa-compress')
|
|
9
|
-
var
|
|
9
|
+
var Koa = require('koa')
|
|
10
10
|
|
|
11
|
-
var app =
|
|
11
|
+
var app = new Koa()
|
|
12
12
|
app.use(compress({
|
|
13
13
|
filter: function (content_type) {
|
|
14
14
|
return /text/i.test(content_type)
|
|
@@ -39,34 +39,8 @@ You can always disable compression by setting `this.compress = false`.
|
|
|
39
39
|
This bypasses the filter check.
|
|
40
40
|
|
|
41
41
|
```js
|
|
42
|
-
app.use(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.body = fs.createReadStream(file)
|
|
46
|
-
}
|
|
42
|
+
app.use((ctx, next) => {
|
|
43
|
+
ctx.compress = true
|
|
44
|
+
ctx.body = fs.createReadStream(file)
|
|
47
45
|
})
|
|
48
46
|
```
|
|
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,3 +1,5 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Module dependencies.
|
|
3
5
|
*/
|
|
@@ -26,7 +28,7 @@ var encodingMethods = {
|
|
|
26
28
|
* @api public
|
|
27
29
|
*/
|
|
28
30
|
|
|
29
|
-
module.exports =
|
|
31
|
+
module.exports = (options) => {
|
|
30
32
|
options = options || {}
|
|
31
33
|
|
|
32
34
|
var filter = options.filter || compressible
|
|
@@ -36,42 +38,42 @@ module.exports = function (options) {
|
|
|
36
38
|
: typeof options.threshold === 'string' ? bytes(options.threshold)
|
|
37
39
|
: 1024
|
|
38
40
|
|
|
39
|
-
return function
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
yield* next
|
|
41
|
+
return function compress(ctx, next) {
|
|
42
|
+
ctx.vary('Accept-Encoding')
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
return next().then(() => {
|
|
45
|
+
var body = ctx.body
|
|
46
|
+
if (!body) return
|
|
47
|
+
if (ctx.compress === false) return
|
|
48
|
+
if (ctx.request.method === 'HEAD') return
|
|
49
|
+
if (status.empty[ctx.response.status]) return
|
|
50
|
+
if (ctx.response.get('Content-Encoding')) return
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
// forced compression or implied
|
|
53
|
+
if (!(ctx.compress === true || filter(ctx.response.type))) return
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
|
58
59
|
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
// json
|
|
61
|
+
if (isJSON(body)) body = ctx.body = JSON.stringify(body)
|
|
61
62
|
|
|
62
|
-
|
|
63
|
-
|
|
63
|
+
// threshold
|
|
64
|
+
if (threshold && ctx.response.length < threshold) return
|
|
64
65
|
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
ctx.set('Content-Encoding', encoding)
|
|
67
|
+
ctx.res.removeHeader('Content-Length')
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
var stream =
|
|
70
|
+
ctx.body = encodingMethods[encoding](options)
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
72
|
+
if (body instanceof Stream) {
|
|
73
|
+
body.pipe(stream)
|
|
74
|
+
} else {
|
|
75
|
+
stream.end(body)
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
};
|
|
77
79
|
}
|
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": "2.0.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Jonathan Ong",
|
|
7
7
|
"email": "me@jongleberry.com",
|
|
@@ -11,18 +11,26 @@
|
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"repository": "koajs/compress",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
14
|
+
"bytes": "^2.3.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
|
+
"istanbul": "^0.4.2",
|
|
21
|
+
"koa": "^2.0.0-alpha.3",
|
|
22
|
+
"mocha": "^2.4.1",
|
|
23
|
+
"should": "^3.0.0",
|
|
24
|
+
"supertest": "^1.0.0"
|
|
24
25
|
},
|
|
25
26
|
"scripts": {
|
|
26
|
-
"test": "
|
|
27
|
-
|
|
27
|
+
"test": "NODE_ENV=test mocha --require should --reporter spec",
|
|
28
|
+
"test-cov": "NODE_ENV=test node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should",
|
|
29
|
+
"test-travis": "NODE_ENV=test node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"index.js",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"HISTORY.md"
|
|
35
|
+
]
|
|
28
36
|
}
|
package/.npmignore
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
test
|
package/.travis.yml
DELETED