You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds compression utils to the Fastify `reply` object.
8
-
Support`gzip`, `deflate` and `brotli`.
7
+
Adds compression utils to [the Fastify `reply` object](https://www.fastify.io/docs/master/Reply/).
8
+
Supports`gzip`, `deflate`, and `brotli`.
9
9
10
10
## Install
11
11
```
12
-
npm i fastify-compress --save
12
+
npm i fastify-compress
13
13
```
14
14
15
15
## Usage
16
-
This plugins adds two functionalities to Fastify, a compress utility and a global compression hook.
16
+
This plugin adds two functionalities to Fastify: a compress utility and a global compression hook.
17
17
18
-
Currently the following headers are supported:
19
-
-`'deflate'`
20
-
-`'gzip'`
21
-
-`'br'`
22
-
-`'*'`
18
+
Currently, the following encoding tokens are supported, using the first acceptable token in this order:
23
19
24
-
If the `'accept-encoding'` header specifies no preferred encoding with an asterisk `*` the payload will be compressed with `gzip`.
20
+
1.`br`
21
+
2.`gzip`
22
+
3.`deflate`
23
+
4.`*` (no preference — `fastify-compress` will use `gzip`)
24
+
5.`identity` (no compression)
25
25
26
-
If an unsupported encoding is received or if the `'accept-encoding'` header is missing, it will not compress the payload.
26
+
If the `accept-encoding` header is missing or contains no supported encodings, it will not compress the payload.
27
27
28
-
It automatically defines if a payload should be compressed or not based on its `Content-Type`, if no content type is present, it will assume is`application/json`.
28
+
The plugin automatically decides if a payload should be compressed based on its `content-type`; if no content type is present, it will assume `application/json`.
29
29
30
30
### Global hook
31
-
The global compression hook is enabled by default if you want to disable it, pass the option `{ global: false }`.
31
+
The global compression hook is enabled by default. To disable it, pass the option `{ global: false }`:
32
32
```javascript
33
33
fastify.register(
34
34
require('fastify-compress'),
35
35
{ global:false }
36
36
)
37
37
```
38
-
Remember that thanks to the Fastify encapsulation model, you can set a global compression, but running it only in a subset of routes is you wrap them inside a plugin.
38
+
Remember that thanks to the Fastify encapsulation model, you can set a global compression, but run it only in a subset of routes if you wrap them inside a plugin.
39
39
40
40
### `reply.compress`
41
-
This plugin add a `compress`function to `reply` that accepts a stream or a string and compress it based on the `'accept-encoding'` header. If a js object is passed in, will be stringified as json.
41
+
This plugin adds a `compress`method to `reply` that accepts a stream or a string, and compresses it based on the `accept-encoding` header. If a JS object is passed in, it will be stringified to JSON.
42
42
43
43
```javascript
44
44
constfs=require('fs')
@@ -59,25 +59,25 @@ fastify.listen(3000, function (err) {
59
59
```
60
60
## Options
61
61
### Threshold
62
-
You can set a custom threshold below which it will not be made compression, default to `1024`.
62
+
The minimum byte size for a response to be compressed. Defaults to `1024`.
63
63
```javascript
64
64
fastify.register(
65
65
require('fastify-compress'),
66
66
{ threshold:2048 }
67
67
)
68
68
```
69
69
### customTypes
70
-
[mime-db](https://github.com/jshttp/mime-db) is used to determine if a `Content-Type` should be compressed. You can compress additional content types via regular expression.
70
+
[mime-db](https://github.com/jshttp/mime-db) is used to determine if a `content-type` should be compressed. You can compress additional content types via regular expression.
71
71
```javascript
72
72
fastify.register(
73
73
require('fastify-compress'),
74
74
{ customTypes:/x-protobuf$/ }
75
75
)
76
76
```
77
77
### Brotli
78
-
Brotli compression is enabled by default if your Node.js(>= v11.7.0) supports it natively.
78
+
Brotli compression is enabled by default if your Node.js supports it natively (≥ v11.7.0).
79
79
80
-
For the Node.js versions that not support brotli natively, it's not enabled by default, if you need it we recommend to install [`iltorb`](https://www.npmjs.com/package/iltorb) and pass it as option.
80
+
For Node.js versions that don’t natively support Brotli, it's not enabled by default. If you need it, we recommend installing [`iltorb`](https://www.npmjs.com/package/iltorb) and passing it to the `brotli`option:
81
81
82
82
```javascript
83
83
fastify.register(
@@ -87,10 +87,10 @@ fastify.register(
87
87
```
88
88
89
89
### Disable compression by header
90
-
You can selectively disable the response compression by using the `x-no-compression` header in the request.
90
+
You can selectively disable response compression by using the `x-no-compression` header in the request.
91
91
92
92
### Inflate pre-compressed bodies for clients that do not support compression
93
-
Optional feature to inflate pre-compressed data if the client doesn't include one of the supported compression types in its `Accept-Encoding` header.
93
+
Optional feature to inflate pre-compressed data if the client doesn't include one of the supported compression types in its `accept-encoding` header.
By default, `fastify-compress` prioritizes compression as described [at the beginning of §Usage](#usage). You can change that by passing an array of compression tokens to the `encodings` option:
109
+
110
+
```javascript
111
+
fastify.register(
112
+
require('fastify-compress'),
113
+
// Only support gzip and deflate, and prefer deflate to gzip
114
+
{ encodings: ['deflate', 'gzip'] }
115
+
)
116
+
```
117
+
106
118
## Note
107
-
Please have in mind that in largescale scenarios, you should use a proxy like Nginx to handle response-compression.
119
+
Please note that in large-scale scenarios, you should use a proxy like Nginx to handle responsecompression.
0 commit comments