UNPKG

2.23 kBMarkdownView Raw
1# Koa Compress [![Build Status](https://travis-ci.org/koajs/compress.png)](https://travis-ci.org/koajs/compress)
2
3Compress middleware for Koa
4
5## Example
6
7```js
8var compress = require('koa-compress')
9var koa = require('koa')
10
11var app = koa()
12app.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
23The options are passed to `zlib`: http://nodejs.org/api/zlib.html#zlib_options
24
25### filter
26
27An optional function that checks the response content type to decide whether to compress.
28By default, it uses [compressible](https://github.com/expressjs/compressible).
29
30### threshold
31
32Minimum response size in bytes to compress.
33Default `1024` bytes or `1kb`.
34
35## Manually turning compression on and off
36
37You can always enable compression by setting `this.compress = true`.
38You can always disable compression by setting `this.compress = false`.
39This bypasses the filter check.
40
41```js
42app.use(function (next) {
43 return function *() {
44 this.compress = true
45 this.body = fs.createReadStream(file)
46 }
47})
48```
49
50## License
51
52The MIT License (MIT)
53
54Copyright (c) 2013 Jonathan Ong me@jongleberry.com
55
56Permission is hereby granted, free of charge, to any person obtaining a copy
57of this software and associated documentation files (the "Software"), to deal
58in the Software without restriction, including without limitation the rights
59to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
60copies of the Software, and to permit persons to whom the Software is
61furnished to do so, subject to the following conditions:
62
63The above copyright notice and this permission notice shall be included in
64all copies or substantial portions of the Software.
65
66THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
71OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
72THE SOFTWARE.