UNPKG

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