source: frontend/node_modules/compression/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 9.7 KB
RevLine 
[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
10Node.js compression middleware.
11
12The 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
22This 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
33var compression = require('compression')
34```
35
36### compression([options])
37
38Returns the compression middleware using the given `options`. The middleware
39will attempt to compress response bodies for all requests that traverse through
40the middleware, based on the given `options`.
41
42This middleware will never compress responses that include a `Cache-Control`
43header with the [`no-transform` directive](https://tools.ietf.org/html/rfc7234#section-5.2.2.4),
44as compressing will transform the body.
45
46#### Options
47
48`compression()` accepts these properties in the options object. In addition to
49those listed below, [zlib](https://nodejs.org/api/zlib.html) options may be
50passed in to the options object or
51[brotli](https://nodejs.org/api/zlib.html#zlib_class_brotlioptions) options.
52
53##### chunkSize
54
55Type: `Number`<br>
56Default: `zlib.constants.Z_DEFAULT_CHUNK`, or `16384`.
57
58See [Node.js documentation](https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
59regarding the usage.
60
61##### filter
62
63Type: `Function`
64
65A function to decide if the response should be considered for compression.
66This 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
68the response.
69
70The default filter function uses the [compressible](https://www.npmjs.com/package/compressible)
71module to determine if `res.getHeader('Content-Type')` is compressible.
72
73##### level
74
75Type: `Number`<br>
76Default: `zlib.constants.Z_DEFAULT_COMPRESSION`, or `-1`
77
78The level of zlib compression to apply to responses. A higher level will result
79in better compression, but will take longer to complete. A lower level will
80result in less compression, but will be much faster.
81
82This is an integer in the range of `0` (no compression) to `9` (maximum
83compression). The special value `-1` can be used to mean the "default
84compression level", which is a default compromise between speed and
85compression (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
103Type: `Number`<br>
104Default: `zlib.constants.Z_DEFAULT_MEMLEVEL`, or `8`
105
106This specifies how much memory should be allocated for the internal compression
107state and is an integer in the range of `1` (minimum level) and `9` (maximum
108level).
109
110See [Node.js documentation](https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
111regarding the usage.
112
113##### brotli
114
115Type: `Object`
116
117This 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
122Type: `Number`<br>
123Default: `zlib.constants.Z_DEFAULT_STRATEGY`
124
125This is used to tune the compression algorithm. This value only affects the
126compression ratio, not the correctness of the compressed output, even if it
127is 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
147Type: `Number` or `String`<br>
148Default: `1kb`
149
150The byte threshold for the response body size before compression is considered
151for the response. This is a number of bytes or any string
152accepted 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
155at 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
157set a `Content-Length` response header.
158
159##### windowBits
160
161Type: `Number`<br>
162Default: `zlib.constants.Z_DEFAULT_WINDOWBITS`, or `15`
163
164See [Node.js documentation](https://nodejs.org/api/zlib.html#zlib_memory_usage_tuning)
165regarding the usage.
166
167##### enforceEncoding
168
169Type: `String`<br>
170Default: `identity`
171
172This 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
176The default `filter` function. This is used to construct a custom filter
177function that is an extension of the default function.
178
179```js
180var compression = require('compression')
181var express = require('express')
182
183var app = express()
184
185app.use(compression({ filter: shouldCompress }))
186
187function 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
200This module adds a `res.flush()` method to force the partially-compressed
201response to be flushed to the client.
202
203## Examples
204
205### express
206
207When using this module with express, simply `app.use` the module as
208high as you like. Requests that pass through the middleware will be compressed.
209
210```js
211var compression = require('compression')
212var express = require('express')
213
214var app = express()
215
216// compress all responses
217app.use(compression())
218
219// add all routes
220```
221
222### Node.js HTTP server
223
224```js
225var compression = require('compression')({ threshold: 0 })
226var http = require('http')
227
228function 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
242var server = createServer(function (req, res) {
243 res.setHeader('Content-Type', 'text/plain')
244 res.end('hello world!')
245})
246
247server.listen(3000, () => {
248 console.log('> Listening at http://localhost:3000')
249})
250```
251
252### Server-Sent Events
253
254Because of the nature of compression this module does not work out of the box
255with server-sent events. To compress content, a window of the output needs to
256be buffered up in order to get good compression. Typically when using server-sent
257events, there are certain block of data that need to reach the client.
258
259You can achieve this by calling `res.flush()` when you need the data written to
260actually make it to the client.
261
262```js
263var compression = require('compression')
264var express = require('express')
265
266var app = express()
267
268// compress responses
269app.use(compression())
270
271// server-sent event stream
272app.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
292The Express.js project welcomes all constructive contributions. Contributions take many forms,
293from code for bug fixes and enhancements, to additions and fixes to documentation, additional
294tests, triaging incoming pull requests and issues, and more!
295
296See 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
Note: See TracBrowser for help on using the repository browser.