1 | # Koa Compress [![Build Status](https://travis-ci.org/koajs/compress.png)](https://travis-ci.org/koajs/compress)
|
2 |
|
3 | Compress middleware for Koa
|
4 |
|
5 | ## Example
|
6 |
|
7 | ```js
|
8 | var compress = require('koa-compress')
|
9 | var koa = require('koa')
|
10 |
|
11 | var app = koa()
|
12 | app.use(compress({
|
13 | filter: function (content_type) {
|
14 | return /text/i.test(content_type)
|
15 | },
|
16 | threshold: 2048,
|
17 | flush: require('zlib').Z_SYNC_FLUSH
|
18 | }))
|
19 | ```
|
20 |
|
21 | ## Options
|
22 |
|
23 | The options are passed to `zlib`: http://nodejs.org/api/zlib.html#zlib_options
|
24 |
|
25 | ### filter
|
26 |
|
27 | 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).
|
29 |
|
30 | ### threshold
|
31 |
|
32 | Minimum response size in bytes to compress.
|
33 | Default `1024` bytes or `1kb`.
|
34 |
|
35 | ## Manually turning compression on and off
|
36 |
|
37 | You can always enable compression by setting `this.compress = true`.
|
38 | You can always disable compression by setting `this.compress = false`.
|
39 | This bypasses the filter check.
|
40 |
|
41 | ```js
|
42 | app.use(function (next) {
|
43 | return function *() {
|
44 | this.compress = true
|
45 | this.body = fs.createReadStream(file)
|
46 | }
|
47 | })
|
48 | ```
|
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.
|