| [9af201e] | 1 | # compression
|
|---|
| 2 |
|
|---|
| 3 | [![NPM Version][npm-image]][npm-url]
|
|---|
| 4 | [![NPM Downloads][downloads-image]][downloads-url]
|
|---|
| 5 | [![Build Status][github-actions-ci-image]][github-actions-ci-url]
|
|---|
| 6 | [![OpenSSF Scorecard Badge][ossf-scorecard-badge]][ossf-scorecard-visualizer]
|
|---|
| 7 | [![Funding][funding-image]][funding-url]
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | Node.js compression middleware.
|
|---|
| 11 |
|
|---|
| 12 | The following compression codings are supported:
|
|---|
| 13 |
|
|---|
| 14 | - deflate
|
|---|
| 15 | - gzip
|
|---|
| 16 | - br (brotli)
|
|---|
| 17 |
|
|---|
| 18 | **Note** Brotli is supported only since Node.js versions v11.7.0 and v10.16.0.
|
|---|
| 19 |
|
|---|
| 20 | ## Install
|
|---|
| 21 |
|
|---|
| 22 | This is a [Node.js](https://nodejs.org/en/) module available through the
|
|---|
| 23 | [npm registry](https://www.npmjs.com/). Installation is done using the
|
|---|
| 24 | [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|---|
| 25 |
|
|---|
| 26 | ```bash
|
|---|
| 27 | $ npm install compression
|
|---|
| 28 | ```
|
|---|
| 29 |
|
|---|
| 30 | ## API
|
|---|
| 31 |
|
|---|
| 32 | ```js
|
|---|
| 33 | var compression = require('compression')
|
|---|
| 34 | ```
|
|---|
| 35 |
|
|---|
| 36 | ### compression([options])
|
|---|
| 37 |
|
|---|
| 38 | Returns the compression middleware using the given `options`. The middleware
|
|---|
| 39 | will attempt to compress response bodies for all requests that traverse through
|
|---|
| 40 | the middleware, based on the given `options`.
|
|---|
| 41 |
|
|---|
| 42 | This middleware will never compress responses that include a `Cache-Control`
|
|---|
| 43 | header with the [`no-transform` directive](https://tools.ietf.org/html/rfc7234#section-5.2.2.4),
|
|---|
| 44 | as compressing will transform the body.
|
|---|
| 45 |
|
|---|
| 46 | #### Options
|
|---|
| 47 |
|
|---|
| 48 | `compression()` accepts these properties in the options object. In addition to
|
|---|
| 49 | those listed below, [zlib](https://nodejs.org/api/zlib.html) options may be
|
|---|
| 50 | passed in to the options object or
|
|---|
| 51 | [brotli](https://nodejs.org/api/zlib.html#zlib_class_brotlioptions) options.
|
|---|
| 52 |
|
|---|
| 53 | ##### chunkSize
|
|---|
| 54 |
|
|---|
| 55 | Type: `Number`<br>
|
|---|
| 56 | Default: `zlib.constants.Z_DEFAULT_CHUNK`, or `16384`.
|
|---|
| 57 |
|
|---|
| 58 | See [Node.js documentation](https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
|
|---|
| 59 | regarding the usage.
|
|---|
| 60 |
|
|---|
| 61 | ##### filter
|
|---|
| 62 |
|
|---|
| 63 | Type: `Function`
|
|---|
| 64 |
|
|---|
| 65 | A function to decide if the response should be considered for compression.
|
|---|
| 66 | This function is called as `filter(req, res)` and is expected to return
|
|---|
| 67 | `true` to consider the response for compression, or `false` to not compress
|
|---|
| 68 | the response.
|
|---|
| 69 |
|
|---|
| 70 | The default filter function uses the [compressible](https://www.npmjs.com/package/compressible)
|
|---|
| 71 | module to determine if `res.getHeader('Content-Type')` is compressible.
|
|---|
| 72 |
|
|---|
| 73 | ##### level
|
|---|
| 74 |
|
|---|
| 75 | Type: `Number`<br>
|
|---|
| 76 | Default: `zlib.constants.Z_DEFAULT_COMPRESSION`, or `-1`
|
|---|
| 77 |
|
|---|
| 78 | The level of zlib compression to apply to responses. A higher level will result
|
|---|
| 79 | in better compression, but will take longer to complete. A lower level will
|
|---|
| 80 | result in less compression, but will be much faster.
|
|---|
| 81 |
|
|---|
| 82 | This is an integer in the range of `0` (no compression) to `9` (maximum
|
|---|
| 83 | compression). The special value `-1` can be used to mean the "default
|
|---|
| 84 | compression level", which is a default compromise between speed and
|
|---|
| 85 | compression (currently equivalent to level 6).
|
|---|
| 86 |
|
|---|
| 87 | - `-1` Default compression level (also `zlib.constants.Z_DEFAULT_COMPRESSION`).
|
|---|
| 88 | - `0` No compression (also `zlib.constants.Z_NO_COMPRESSION`).
|
|---|
| 89 | - `1` Fastest compression (also `zlib.constants.Z_BEST_SPEED`).
|
|---|
| 90 | - `2`
|
|---|
| 91 | - `3`
|
|---|
| 92 | - `4`
|
|---|
| 93 | - `5`
|
|---|
| 94 | - `6` (currently what `zlib.constants.Z_DEFAULT_COMPRESSION` points to).
|
|---|
| 95 | - `7`
|
|---|
| 96 | - `8`
|
|---|
| 97 | - `9` Best compression (also `zlib.constants.Z_BEST_COMPRESSION`).
|
|---|
| 98 |
|
|---|
| 99 | **Note** in the list above, `zlib` is from `zlib = require('zlib')`.
|
|---|
| 100 |
|
|---|
| 101 | ##### memLevel
|
|---|
| 102 |
|
|---|
| 103 | Type: `Number`<br>
|
|---|
| 104 | Default: `zlib.constants.Z_DEFAULT_MEMLEVEL`, or `8`
|
|---|
| 105 |
|
|---|
| 106 | This specifies how much memory should be allocated for the internal compression
|
|---|
| 107 | state and is an integer in the range of `1` (minimum level) and `9` (maximum
|
|---|
| 108 | level).
|
|---|
| 109 |
|
|---|
| 110 | See [Node.js documentation](https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
|
|---|
| 111 | regarding the usage.
|
|---|
| 112 |
|
|---|
| 113 | ##### brotli
|
|---|
| 114 |
|
|---|
| 115 | Type: `Object`
|
|---|
| 116 |
|
|---|
| 117 | This specifies the options for configuring Brotli. See [Node.js documentation](https://nodejs.org/api/zlib.html#class-brotlioptions) for a complete list of available options.
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | ##### strategy
|
|---|
| 121 |
|
|---|
| 122 | Type: `Number`<br>
|
|---|
| 123 | Default: `zlib.constants.Z_DEFAULT_STRATEGY`
|
|---|
| 124 |
|
|---|
| 125 | This is used to tune the compression algorithm. This value only affects the
|
|---|
| 126 | compression ratio, not the correctness of the compressed output, even if it
|
|---|
| 127 | is not set appropriately.
|
|---|
| 128 |
|
|---|
| 129 | - `zlib.constants.Z_DEFAULT_STRATEGY` Use for normal data.
|
|---|
| 130 | - `zlib.constants.Z_FILTERED` Use for data produced by a filter (or predictor).
|
|---|
| 131 | Filtered data consists mostly of small values with a somewhat random
|
|---|
| 132 | distribution. In this case, the compression algorithm is tuned to
|
|---|
| 133 | compress them better. The effect is to force more Huffman coding and less
|
|---|
| 134 | string matching; it is somewhat intermediate between `zlib.constants.Z_DEFAULT_STRATEGY`
|
|---|
| 135 | and `zlib.constants.Z_HUFFMAN_ONLY`.
|
|---|
| 136 | - `zlib.constants.Z_FIXED` Use to prevent the use of dynamic Huffman codes, allowing
|
|---|
| 137 | for a simpler decoder for special applications.
|
|---|
| 138 | - `zlib.constants.Z_HUFFMAN_ONLY` Use to force Huffman encoding only (no string match).
|
|---|
| 139 | - `zlib.constants.Z_RLE` Use to limit match distances to one (run-length encoding).
|
|---|
| 140 | This is designed to be almost as fast as `zlib.constants.Z_HUFFMAN_ONLY`, but give
|
|---|
| 141 | better compression for PNG image data.
|
|---|
| 142 |
|
|---|
| 143 | **Note** in the list above, `zlib` is from `zlib = require('zlib')`.
|
|---|
| 144 |
|
|---|
| 145 | ##### threshold
|
|---|
| 146 |
|
|---|
| 147 | Type: `Number` or `String`<br>
|
|---|
| 148 | Default: `1kb`
|
|---|
| 149 |
|
|---|
| 150 | The byte threshold for the response body size before compression is considered
|
|---|
| 151 | for the response. This is a number of bytes or any string
|
|---|
| 152 | accepted by the [bytes](https://www.npmjs.com/package/bytes) module.
|
|---|
| 153 |
|
|---|
| 154 | **Note** this is only an advisory setting; if the response size cannot be determined
|
|---|
| 155 | at the time the response headers are written, then it is assumed the response is
|
|---|
| 156 | _over_ the threshold. To guarantee the response size can be determined, be sure
|
|---|
| 157 | set a `Content-Length` response header.
|
|---|
| 158 |
|
|---|
| 159 | ##### windowBits
|
|---|
| 160 |
|
|---|
| 161 | Type: `Number`<br>
|
|---|
| 162 | Default: `zlib.constants.Z_DEFAULT_WINDOWBITS`, or `15`
|
|---|
| 163 |
|
|---|
| 164 | See [Node.js documentation](https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
|
|---|
| 165 | regarding the usage.
|
|---|
| 166 |
|
|---|
| 167 | ##### enforceEncoding
|
|---|
| 168 |
|
|---|
| 169 | Type: `String`<br>
|
|---|
| 170 | Default: `identity`
|
|---|
| 171 |
|
|---|
| 172 | This is the default encoding to use when the client does not specify an encoding in the request's [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) header.
|
|---|
| 173 |
|
|---|
| 174 | #### .filter
|
|---|
| 175 |
|
|---|
| 176 | The default `filter` function. This is used to construct a custom filter
|
|---|
| 177 | function that is an extension of the default function.
|
|---|
| 178 |
|
|---|
| 179 | ```js
|
|---|
| 180 | var compression = require('compression')
|
|---|
| 181 | var express = require('express')
|
|---|
| 182 |
|
|---|
| 183 | var app = express()
|
|---|
| 184 |
|
|---|
| 185 | app.use(compression({ filter: shouldCompress }))
|
|---|
| 186 |
|
|---|
| 187 | function shouldCompress (req, res) {
|
|---|
| 188 | if (req.headers['x-no-compression']) {
|
|---|
| 189 | // don't compress responses with this request header
|
|---|
| 190 | return false
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | // fallback to standard filter function
|
|---|
| 194 | return compression.filter(req, res)
|
|---|
| 195 | }
|
|---|
| 196 | ```
|
|---|
| 197 |
|
|---|
| 198 | ### res.flush
|
|---|
| 199 |
|
|---|
| 200 | This module adds a `res.flush()` method to force the partially-compressed
|
|---|
| 201 | response to be flushed to the client.
|
|---|
| 202 |
|
|---|
| 203 | ## Examples
|
|---|
| 204 |
|
|---|
| 205 | ### express
|
|---|
| 206 |
|
|---|
| 207 | When using this module with express, simply `app.use` the module as
|
|---|
| 208 | high as you like. Requests that pass through the middleware will be compressed.
|
|---|
| 209 |
|
|---|
| 210 | ```js
|
|---|
| 211 | var compression = require('compression')
|
|---|
| 212 | var express = require('express')
|
|---|
| 213 |
|
|---|
| 214 | var app = express()
|
|---|
| 215 |
|
|---|
| 216 | // compress all responses
|
|---|
| 217 | app.use(compression())
|
|---|
| 218 |
|
|---|
| 219 | // add all routes
|
|---|
| 220 | ```
|
|---|
| 221 |
|
|---|
| 222 | ### Node.js HTTP server
|
|---|
| 223 |
|
|---|
| 224 | ```js
|
|---|
| 225 | var compression = require('compression')({ threshold: 0 })
|
|---|
| 226 | var http = require('http')
|
|---|
| 227 |
|
|---|
| 228 | function createServer (fn) {
|
|---|
| 229 | return http.createServer(function (req, res) {
|
|---|
| 230 | compression(req, res, function (err) {
|
|---|
| 231 | if (err) {
|
|---|
| 232 | res.statusCode = err.status || 500
|
|---|
| 233 | res.end(err.message)
|
|---|
| 234 | return
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | fn(req, res)
|
|---|
| 238 | })
|
|---|
| 239 | })
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | var server = createServer(function (req, res) {
|
|---|
| 243 | res.setHeader('Content-Type', 'text/plain')
|
|---|
| 244 | res.end('hello world!')
|
|---|
| 245 | })
|
|---|
| 246 |
|
|---|
| 247 | server.listen(3000, () => {
|
|---|
| 248 | console.log('> Listening at http://localhost:3000')
|
|---|
| 249 | })
|
|---|
| 250 | ```
|
|---|
| 251 |
|
|---|
| 252 | ### Server-Sent Events
|
|---|
| 253 |
|
|---|
| 254 | Because of the nature of compression this module does not work out of the box
|
|---|
| 255 | with server-sent events. To compress content, a window of the output needs to
|
|---|
| 256 | be buffered up in order to get good compression. Typically when using server-sent
|
|---|
| 257 | events, there are certain block of data that need to reach the client.
|
|---|
| 258 |
|
|---|
| 259 | You can achieve this by calling `res.flush()` when you need the data written to
|
|---|
| 260 | actually make it to the client.
|
|---|
| 261 |
|
|---|
| 262 | ```js
|
|---|
| 263 | var compression = require('compression')
|
|---|
| 264 | var express = require('express')
|
|---|
| 265 |
|
|---|
| 266 | var app = express()
|
|---|
| 267 |
|
|---|
| 268 | // compress responses
|
|---|
| 269 | app.use(compression())
|
|---|
| 270 |
|
|---|
| 271 | // server-sent event stream
|
|---|
| 272 | app.get('/events', function (req, res) {
|
|---|
| 273 | res.setHeader('Content-Type', 'text/event-stream')
|
|---|
| 274 | res.setHeader('Cache-Control', 'no-cache')
|
|---|
| 275 |
|
|---|
| 276 | // send a ping approx every 2 seconds
|
|---|
| 277 | var timer = setInterval(function () {
|
|---|
| 278 | res.write('data: ping\n\n')
|
|---|
| 279 |
|
|---|
| 280 | // !!! this is the important part
|
|---|
| 281 | res.flush()
|
|---|
| 282 | }, 2000)
|
|---|
| 283 |
|
|---|
| 284 | res.on('close', function () {
|
|---|
| 285 | clearInterval(timer)
|
|---|
| 286 | })
|
|---|
| 287 | })
|
|---|
| 288 | ```
|
|---|
| 289 |
|
|---|
| 290 | ## Contributing
|
|---|
| 291 |
|
|---|
| 292 | The Express.js project welcomes all constructive contributions. Contributions take many forms,
|
|---|
| 293 | from code for bug fixes and enhancements, to additions and fixes to documentation, additional
|
|---|
| 294 | tests, triaging incoming pull requests and issues, and more!
|
|---|
| 295 |
|
|---|
| 296 | See the [Contributing Guide](https://github.com/expressjs/express/blob/master/Contributing.md) for more technical details on contributing.
|
|---|
| 297 |
|
|---|
| 298 | ## License
|
|---|
| 299 |
|
|---|
| 300 | [MIT](LICENSE)
|
|---|
| 301 |
|
|---|
| 302 | [npm-image]: https://badgen.net/npm/v/compression
|
|---|
| 303 | [npm-url]: https://npmjs.org/package/compression
|
|---|
| 304 | [downloads-image]: https://badgen.net/npm/dm/compression
|
|---|
| 305 | [downloads-url]: https://npmcharts.com/compare/compression?minimal=true
|
|---|
| 306 | [github-actions-ci-image]: https://badgen.net/github/checks/expressjs/compression/master?label=CI
|
|---|
| 307 | [github-actions-ci-url]: https://github.com/expressjs/compression/actions?query=workflow%3Aci
|
|---|
| 308 | [ossf-scorecard-badge]: https://api.scorecard.dev/projects/github.com/expressjs/compression/badge
|
|---|
| 309 | [ossf-scorecard-visualizer]: https://ossf.github.io/scorecard-visualizer/#/projects/github.com/expressjs/compression
|
|---|
| 310 | [funding-url]: https://opencollective.com/express
|
|---|
| 311 | [funding-image]: https://badgen.net/badge/icon/sponsor/pink?icon=github&label=Open%20Collective |
|---|