koa-compress 1.0.9 → 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.
Files changed (3) hide show
  1. package/README.md +5 -7
  2. package/index.js +33 -31
  3. package/package.json +2 -2
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 koa = require('koa')
9
+ var Koa = require('koa')
10
10
 
11
- var app = koa()
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,10 +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(function (next) {
43
- return function *() {
44
- this.compress = true
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
  ```
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 = function (options) {
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* compress(next) {
40
- this.vary('Accept-Encoding')
41
-
42
- yield* next
41
+ return function compress(ctx, next) {
42
+ ctx.vary('Accept-Encoding')
43
43
 
44
- var body = this.body
45
- if (!body) return
46
- if (this.compress === false) return
47
- if (this.request.method === 'HEAD') return
48
- if (status.empty[this.response.status]) return
49
- if (this.response.get('Content-Encoding')) return
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
- // forced compression or implied
52
- if (!(this.compress === true || filter(this.response.type))) return
52
+ // forced compression or implied
53
+ if (!(ctx.compress === true || filter(ctx.response.type))) return
53
54
 
54
- // identity
55
- var encoding = this.acceptsEncodings('gzip', 'deflate', 'identity')
56
- if (!encoding) this.throw(406, 'supported encodings: gzip, deflate, identity')
57
- if (encoding === 'identity') return
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
- // json
60
- if (isJSON(body)) body = this.body = JSON.stringify(body)
60
+ // json
61
+ if (isJSON(body)) body = ctx.body = JSON.stringify(body)
61
62
 
62
- // threshold
63
- if (threshold && this.response.length < threshold) return
63
+ // threshold
64
+ if (threshold && ctx.response.length < threshold) return
64
65
 
65
- this.set('Content-Encoding', encoding)
66
- this.res.removeHeader('Content-Length')
66
+ ctx.set('Content-Encoding', encoding)
67
+ ctx.res.removeHeader('Content-Length')
67
68
 
68
- var stream =
69
- this.body = encodingMethods[encoding](options)
69
+ var stream =
70
+ ctx.body = encodingMethods[encoding](options)
70
71
 
71
- if (body instanceof Stream) {
72
- body.pipe(stream)
73
- } else {
74
- stream.end(body)
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": "1.0.9",
4
+ "version": "2.0.0",
5
5
  "author": {
6
6
  "name": "Jonathan Ong",
7
7
  "email": "me@jongleberry.com",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "devDependencies": {
20
20
  "istanbul": "^0.4.2",
21
- "koa": "^1.0.0",
21
+ "koa": "^2.0.0-alpha.3",
22
22
  "mocha": "^2.4.1",
23
23
  "should": "^3.0.0",
24
24
  "supertest": "^1.0.0"