koa-compress 1.0.8 → 3.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.
Files changed (4) hide show
  1. package/HISTORY.md +22 -1
  2. package/README.md +12 -11
  3. package/index.js +39 -33
  4. package/package.json +23 -14
package/HISTORY.md CHANGED
@@ -1,9 +1,30 @@
1
1
 
2
+ 3.1.0 / 2020-04-15
3
+
4
+ * support no-transform @Pawda
5
+
6
+ 3.0.0 / 2018-04-14
7
+ ==================
8
+
9
+ * republish 2.1.0 as it switches to async functions
10
+
11
+ 2.1.0 / 2018-04-13 (unpublished)
12
+ ==================
13
+
14
+ * do not compress if the response is not writable
15
+ * switch testing frameworks to jest
16
+ * implement linting
17
+
18
+ 1.0.8 / 2014-09-14
19
+ ==================
20
+
21
+ * bump compressible
22
+
2
23
  1.0.7 / 2014-05-13
3
24
  ==================
4
25
 
5
26
  * bump bytes
6
-
27
+
7
28
  1.0.6 / 2014-04-24
8
29
  ==================
9
30
 
package/README.md CHANGED
@@ -1,4 +1,7 @@
1
- # Koa Compress [![Build Status](https://travis-ci.org/koajs/compress.png)](https://travis-ci.org/koajs/compress)
1
+ # Koa Compress
2
+
3
+ [![Node.js CI](https://github.com/koajs/compress/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/koajs/compress/actions?query=workflow%3A%22Node.js+CI%22+branch%3Amaster)
4
+ [![codecov](https://codecov.io/gh/koajs/compress/branch/master/graph/badge.svg)](https://codecov.io/gh/koajs/compress)
2
5
 
3
6
  Compress middleware for Koa
4
7
 
@@ -6,9 +9,9 @@ Compress middleware for Koa
6
9
 
7
10
  ```js
8
11
  var compress = require('koa-compress')
9
- var koa = require('koa')
12
+ var Koa = require('koa')
10
13
 
11
- var app = koa()
14
+ var app = new Koa()
12
15
  app.use(compress({
13
16
  filter: function (content_type) {
14
17
  return /text/i.test(content_type)
@@ -25,7 +28,7 @@ The options are passed to `zlib`: http://nodejs.org/api/zlib.html#zlib_options
25
28
  ### filter
26
29
 
27
30
  An optional function that checks the response content type to decide whether to compress.
28
- By default, it uses [compressible](https://github.com/expressjs/compressible).
31
+ By default, it uses [compressible](https://github.com/jshttp/compressible).
29
32
 
30
33
  ### threshold
31
34
 
@@ -34,15 +37,13 @@ Default `1024` bytes or `1kb`.
34
37
 
35
38
  ## Manually turning compression on and off
36
39
 
37
- You can always enable compression by setting `this.compress = true`.
38
- You can always disable compression by setting `this.compress = false`.
40
+ You can always enable compression by setting `ctx.compress = true`.
41
+ You can always disable compression by setting `ctx.compress = false`.
39
42
  This bypasses the filter check.
40
43
 
41
44
  ```js
42
- app.use(function (next) {
43
- return function *() {
44
- this.compress = true
45
- this.body = fs.createReadStream(file)
46
- }
45
+ app.use((ctx, next) => {
46
+ ctx.compress = true
47
+ ctx.body = fs.createReadStream(file)
47
48
  })
48
49
  ```
package/index.js CHANGED
@@ -1,23 +1,30 @@
1
+ 'use strict'
2
+
1
3
  /**
2
4
  * Module dependencies.
3
5
  */
4
6
 
5
- var compressible = require('compressible')
6
- var isJSON = require('koa-is-json')
7
- var status = require('statuses')
8
- var Stream = require('stream')
9
- var bytes = require('bytes')
10
- var zlib = require('zlib')
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
- var encodingMethods = {
18
+ const encodingMethods = {
17
19
  gzip: zlib.createGzip,
18
20
  deflate: zlib.createDeflate
19
21
  }
20
22
 
23
+ /**
24
+ * Regex to match no-transform directive in a cache-control header
25
+ */
26
+ const NO_TRANSFORM_REGEX = /(?:^|,)\s*?no-transform\s*?(?:,|$)/
27
+
21
28
  /**
22
29
  * Compress middleware.
23
30
  *
@@ -26,47 +33,46 @@ var encodingMethods = {
26
33
  * @api public
27
34
  */
28
35
 
29
- module.exports = function (options) {
30
- options = options || {}
36
+ module.exports = (options = {}) => {
37
+ let { filter = compressible, threshold = 1024 } = options
38
+ if (typeof threshold === 'string') threshold = bytes(threshold)
31
39
 
32
- var filter = options.filter || compressible
40
+ return async (ctx, next) => {
41
+ ctx.vary('Accept-Encoding')
33
42
 
34
- var threshold = !options.threshold ? 1024
35
- : typeof options.threshold === 'number' ? options.threshold
36
- : typeof options.threshold === 'string' ? bytes(options.threshold)
37
- : 1024
43
+ await next()
38
44
 
39
- return function* compress(next) {
40
- this.vary('Accept-Encoding')
41
-
42
- yield* next
43
-
44
- var body = this.body
45
+ let { body } = ctx
45
46
  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
47
+ if (ctx.res.headersSent || !ctx.writable) return
48
+ if (ctx.compress === false) return
49
+ if (ctx.request.method === 'HEAD') return
50
+ if (status.empty[ctx.response.status]) return
51
+ if (ctx.response.get('Content-Encoding')) return
50
52
 
51
53
  // forced compression or implied
52
- if (!(this.compress === true || filter(this.response.type))) return
54
+ if (!(ctx.compress === true || filter(ctx.response.type))) return
55
+
56
+ // Don't compress for Cache-Control: no-transform
57
+ // https://tools.ietf.org/html/rfc7234#section-5.2.1.6
58
+ const cacheControl = ctx.response.get('Cache-Control')
59
+ if (cacheControl && NO_TRANSFORM_REGEX.test(cacheControl)) return
53
60
 
54
61
  // identity
55
- var encoding = this.acceptsEncodings('gzip', 'deflate', 'identity')
56
- if (!encoding) this.throw(406, 'supported encodings: gzip, deflate, identity')
62
+ const encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity')
63
+ if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')
57
64
  if (encoding === 'identity') return
58
65
 
59
66
  // json
60
- if (isJSON(body)) body = this.body = JSON.stringify(body)
67
+ if (isJSON(body)) body = ctx.body = JSON.stringify(body)
61
68
 
62
69
  // threshold
63
- if (threshold && this.response.length < threshold) return
70
+ if (threshold && ctx.response.length < threshold) return
64
71
 
65
- this.set('Content-Encoding', encoding)
66
- this.res.removeHeader('Content-Length')
72
+ ctx.set('Content-Encoding', encoding)
73
+ ctx.res.removeHeader('Content-Length')
67
74
 
68
- var stream =
69
- this.body = encodingMethods[encoding](options)
75
+ const stream = ctx.body = encodingMethods[encoding](options)
70
76
 
71
77
  if (body instanceof Stream) {
72
78
  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": "1.0.8",
4
+ "version": "3.1.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
- "statuses": "1",
15
- "koa-is-json": "1",
16
- "compressible": "2",
17
- "bytes": "1"
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
- "istanbul-harmony": "0",
21
- "koa": "0",
22
- "mocha": "1",
23
- "should": "3",
24
- "supertest": "0"
20
+ "eslint": "^6.0.0",
21
+ "eslint-config-standard": "^14.1.1",
22
+ "eslint-plugin-import": "^2.9.0",
23
+ "eslint-plugin-node": "^11.0.0",
24
+ "eslint-plugin-promise": "^4.0.0",
25
+ "eslint-plugin-standard": "^4.0.1",
26
+ "jest": "^25.3.0",
27
+ "koa": "^2.0.1",
28
+ "supertest": "^4.0.2"
25
29
  },
26
30
  "scripts": {
27
- "test": "NODE_ENV=test mocha --harmony-generators --require should --reporter spec",
28
- "test-cov": "NODE_ENV=test node --harmony-generators ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should",
29
- "test-travis": "NODE_ENV=test node --harmony-generators ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should"
31
+ "eslint": "eslint --ignore-path .gitignore .",
32
+ "test": "jest --forceExit"
30
33
  },
31
34
  "files": [
32
35
  "index.js",
33
36
  "LICENSE",
34
37
  "HISTORY.md"
35
- ]
38
+ ],
39
+ "jest": {
40
+ "testEnvironment": "node"
41
+ },
42
+ "engines": {
43
+ "node": ">= 8.0.0"
44
+ }
36
45
  }