@fastify/compress 8.0.1 → 8.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,5 +9,5 @@ updates:
9
9
  - package-ecosystem: "npm"
10
10
  directory: "/"
11
11
  schedule:
12
- interval: "weekly"
12
+ interval: "monthly"
13
13
  open-pull-requests-limit: 10
@@ -4,7 +4,6 @@ on:
4
4
  push:
5
5
  branches:
6
6
  - main
7
- - master
8
7
  - next
9
8
  - 'v*'
10
9
  paths-ignore:
@@ -15,9 +14,15 @@ on:
15
14
  - 'docs/**'
16
15
  - '*.md'
17
16
 
17
+ permissions:
18
+ contents: read
19
+
18
20
  jobs:
19
21
  test:
20
- uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5.0.0
22
+ permissions:
23
+ contents: write
24
+ pull-requests: write
25
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
21
26
  with:
22
27
  license-check: true
23
28
  lint: true
package/README.md CHANGED
@@ -1,19 +1,36 @@
1
1
  # @fastify/compress
2
2
 
3
- ![CI](https://github.com/fastify/fastify-compress/workflows/CI/badge.svg)
3
+ [![CI](https://github.com/fastify/fastify-compress/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-compress/actions/workflows/ci.yml)
4
4
  [![NPM version](https://img.shields.io/npm/v/@fastify/compress.svg?style=flat)](https://www.npmjs.com/package/@fastify/compress)
5
- [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
5
+ [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
6
6
 
7
7
  Adds compression utils to [the Fastify `reply` object](https://fastify.dev/docs/latest/Reference/Reply/#reply) and a hook to decompress requests payloads.
8
8
  Supports `gzip`, `deflate`, and `brotli`.
9
9
 
10
- > **Important note:** since `@fastify/compress` version 4.x payloads that are compressed using the `zip` algorithm are not automatically uncompressed anymore. `@fastify/compress` main feature is to provide response compression mechanism to your server, however the `zip` format does not appear in the [IANA maintained Table of Content Encodings](https://www.iana.org/assignments/http-parameters/http-parameters.xml#content-coding) and thus such behavior was out of the scope of this plugin.
10
+ > ℹ️ Note: In large-scale scenarios, use a proxy like Nginx to handle response compression.
11
+
12
+ > ⚠ Warning: Since `@fastify/compress` version 4.x, payloads compressed with the `zip` algorithm are not automatically uncompressed. This plugin focuses on response compression, and `zip` is not in the [IANA Table of Content Encodings](https://www.iana.org/assignments/http-parameters/http-parameters.xml#content-coding).
11
13
 
12
14
  ## Install
13
15
  ```
14
16
  npm i @fastify/compress
15
17
  ```
16
18
 
19
+ ### Compatibility
20
+ | Plugin version | Fastify version |
21
+ | ---------------|-----------------|
22
+ | `>=8.x` | `^5.x` |
23
+ | `>=6.x <8.x` | `^4.x` |
24
+ | `>=3.x <6.x` | `^3.x` |
25
+ | `^2.x` | `^2.x` |
26
+ | `>=0.x <2.x` | `^1.x` |
27
+
28
+
29
+ Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin
30
+ in the table above.
31
+ See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.
32
+
33
+
17
34
  ## Usage - Compress replies
18
35
 
19
36
  This plugin adds two functionalities to Fastify: a compress utility and a global compression hook.
@@ -26,25 +43,27 @@ Currently, the following encoding tokens are supported, using the first acceptab
26
43
  4. `*` (no preference — `@fastify/compress` will use `gzip`)
27
44
  5. `identity` (no compression)
28
45
 
29
- If an unsupported encoding is received or if the `'accept-encoding'` header is missing, it will not compress the payload. If an unsupported encoding is received and you would like to return an error, provide an `onUnsupportedEncoding` option.
46
+ If an unsupported encoding is received or the `'accept-encoding'` header is missing, the payload will not be compressed.
47
+ To return an error for unsupported encoding, use the `onUnsupportedEncoding` option.
30
48
 
31
- 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`.
49
+ The plugin compresses payloads based on `content-type`. If absent, it assumes `application/json`.
32
50
 
33
51
  ### Global hook
34
- The global compression hook is enabled by default. To disable it, pass the option `{ global: false }`:
35
- ```javascript
52
+ The global compression hook is enabled by default. To disable it, pass `{ global: false }`:
53
+ ```js
36
54
  await fastify.register(
37
55
  import('@fastify/compress'),
38
56
  { global: false }
39
57
  )
40
58
  ```
41
- 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.
59
+ Fastify encapsulation can be used to set global compression but run it only in a subset of routes by wrapping them inside a plugin.
42
60
 
43
- Important note! If you are using `@fastify/compress` plugin together with `@fastify/static` plugin, you must register the `@fastify/compress` (with *global hook*) **before** registering `@fastify/static`.
61
+ > ℹ️ Note: If using `@fastify/compress` plugin together with `@fastify/static` plugin, `@fastify/compress` must be registered (with *global hook*) **before** registering `@fastify/static`.
44
62
 
45
63
  ### Per Route options
46
- You can specify different options for compression per route by passing in the `compress` options on the route's configuration.
47
- ```javascript
64
+ Different compression options can be specified per route using the `compress` options in the route's configuration.
65
+ Setting `compress: false` on any route will disable compression on the route even if global compression is enabled.
66
+ ```js
48
67
  await fastify.register(
49
68
  import('@fastify/compress'),
50
69
  { global: false }
@@ -65,15 +84,13 @@ fastify.get('/custom-route', {
65
84
  })
66
85
  ```
67
86
 
68
- Note: Setting `compress = false` on any route will disable compression on the route even if global compression is enabled.
69
-
70
87
  ### `reply.compress`
71
- 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.
72
- Note that the compress method is configured with either the per route parameters if the route has a custom configuration or with the global parameters if the the route has no custom parameters but
73
- the plugin was defined as global.
88
+ This plugin adds a `compress` method to `reply` that compresses a stream or string based on the `accept-encoding` header. If a JS object is passed, it will be stringified to JSON.
74
89
 
75
- ```javascript
76
- import fs from 'fs'
90
+ The `compress` method uses per-route parameters if configured, otherwise it uses global parameters.
91
+
92
+ ```js
93
+ import fs from 'node:fs'
77
94
  import fastify from 'fastify'
78
95
 
79
96
  const app = fastify()
@@ -91,17 +108,17 @@ await app.listen({ port: 3000 })
91
108
  ## Compress Options
92
109
 
93
110
  ### threshold
94
- The minimum byte size for a response to be compressed. Defaults to `1024`.
95
- ```javascript
111
+ The minimum byte size for response compression. Defaults to `1024`.
112
+ ```js
96
113
  await fastify.register(
97
114
  import('@fastify/compress'),
98
115
  { threshold: 2048 }
99
116
  )
100
117
  ```
101
118
  ### customTypes
102
- [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 or by providing a function.
119
+ [mime-db](https://github.com/jshttp/mime-db) determines if a `content-type` should be compressed. Additional content types can be compressed via regex or a function.
103
120
 
104
- ```javascript
121
+ ```js
105
122
  await fastify.register(
106
123
  import('@fastify/compress'),
107
124
  { customTypes: /x-protobuf$/ }
@@ -110,7 +127,7 @@ await fastify.register(
110
127
 
111
128
  or
112
129
 
113
- ```javascript
130
+ ```js
114
131
  await fastify.register(
115
132
  import('@fastify/compress'),
116
133
  { customTypes: contentType => contentType.endsWith('x-protobuf') }
@@ -118,8 +135,9 @@ await fastify.register(
118
135
  ```
119
136
 
120
137
  ### onUnsupportedEncoding
121
- When the encoding is not supported, a custom error response can be sent in place of the uncompressed payload by setting the `onUnsupportedEncoding(encoding, request, reply)` option to be a function that can modify the reply and return a `string | Buffer | Stream | Error` payload.
122
- ```javascript
138
+ Set `onUnsupportedEncoding(encoding, request, reply)` to send a custom error response for unsupported encoding. The function can modify the reply and return a `string | Buffer | Stream | Error` payload.
139
+
140
+ ```js
123
141
  await fastify.register(
124
142
  import('@fastify/compress'),
125
143
  {
@@ -132,11 +150,11 @@ await fastify.register(
132
150
  ```
133
151
 
134
152
  ### Disable compression by header
135
- You can selectively disable response compression by using the `x-no-compression` header in the request.
153
+ Response compression can be disabled by an `x-no-compression` header in the request.
136
154
 
137
155
  ### Inflate pre-compressed bodies for clients that do not support compression
138
156
  Optional feature to inflate pre-compressed data if the client does not include one of the supported compression types in its `accept-encoding` header.
139
- ```javascript
157
+ ```js
140
158
  await fastify.register(
141
159
  import('@fastify/compress'),
142
160
  { inflateIfDeflated: true }
@@ -149,10 +167,9 @@ fastify.get('/file', (req, reply) =>
149
167
  ```
150
168
 
151
169
  ### Customize encoding priority
170
+ By default, `@fastify/compress` prioritizes compression as described [here](#usage). Change this by passing an array of compression tokens to the `encodings` option:
152
171
 
153
- By default, `@fastify/compress` prioritizes compression as described [at the beginning of §Usage - Compress replies](#usage). You can change that by passing an array of compression tokens to the `encodings` option:
154
-
155
- ```javascript
172
+ ```js
156
173
  await fastify.register(
157
174
  import('@fastify/compress'),
158
175
  // Only support gzip and deflate, and prefer deflate to gzip
@@ -161,10 +178,9 @@ await fastify.register(
161
178
  ```
162
179
 
163
180
  ### brotliOptions and zlibOptions
181
+ Compression can be tuned with `brotliOptions` and `zlibOptions`, which are passed directly to native node `zlib` methods. See [class definitions](https://nodejs.org/api/zlib.html#zlib_class_options).
164
182
 
165
- You can tune compression by setting the `brotliOptions` and `zlibOptions` properties. These properties are passed directly to native node `zlib` methods, so they should match the corresponding [class](https://nodejs.org/api/zlib.html#zlib_class_brotlioptions) [definitions](https://nodejs.org/api/zlib.html#zlib_class_options).
166
-
167
- ```javascript
183
+ ```js
168
184
  server.register(fastifyCompress, {
169
185
  brotliOptions: {
170
186
  params: {
@@ -179,13 +195,13 @@ You can tune compression by setting the `brotliOptions` and `zlibOptions` proper
179
195
  ```
180
196
 
181
197
  ### Manage `Content-Length` header removal with removeContentLengthHeader
182
- By default, `@fastify/compress` removes the reply `Content-Length` header. You can change that by setting the `removeContentLengthHeader` to `false` either on a global scope or on a route specific scope.
198
+ By default, `@fastify/compress` removes the reply `Content-Length` header. Change this by setting `removeContentLengthHeader` to `false` globally or per route.
183
199
 
184
- ```javascript
200
+ ```js
185
201
  // Global plugin scope
186
202
  await server.register(fastifyCompress, { global: true, removeContentLengthHeader: false });
187
203
 
188
- // Route specific scope
204
+ // Route-specific scope
189
205
  fastify.get('/file', {
190
206
  compress: { removeContentLengthHeader: false }
191
207
  }, (req, reply) =>
@@ -194,8 +210,7 @@ By default, `@fastify/compress` removes the reply `Content-Length` header. You c
194
210
  ```
195
211
 
196
212
  ## Usage - Decompress request payloads
197
-
198
- This plugin adds a `preParsing` hook that decompress the request payload according to the `content-encoding` request header.
213
+ This plugin adds a `preParsing` hook to decompress the request payload based on the `content-encoding` request header.
199
214
 
200
215
  Currently, the following encoding tokens are supported:
201
216
 
@@ -203,26 +218,26 @@ Currently, the following encoding tokens are supported:
203
218
  2. `gzip`
204
219
  3. `deflate`
205
220
 
206
- If an unsupported encoding or and invalid payload is received, the plugin will throw an error.
221
+ If an unsupported encoding or invalid payload is received, the plugin throws an error.
207
222
 
208
- If the request header is missing, the plugin will not do anything and yield to the next hook.
223
+ If the request header is missing, the plugin yields to the next hook.
209
224
 
210
225
  ### Global hook
211
226
 
212
- The global request decompression hook is enabled by default. To disable it, pass the option `{ global: false }`:
213
- ```javascript
227
+ The global request decompression hook is enabled by default. To disable it, pass `{ global: false }`:
228
+ ```js
214
229
  await fastify.register(
215
230
  import('@fastify/compress'),
216
231
  { global: false }
217
232
  )
218
233
  ```
219
234
 
220
- Remember that thanks to the Fastify encapsulation model, you can set a global decompression, but run it only in a subset of routes if you wrap them inside a plugin.
235
+ Fastify encapsulation can be used to set global decompression but run it only in a subset of routes by wrapping them inside a plugin.
221
236
 
222
237
  ### Per Route options
223
238
 
224
- You can specify different options for decompression per route by passing in the `decompress` options on the route's configuration.
225
- ```javascript
239
+ Specify different decompression options per route using the `decompress` options in the route's configuration.
240
+ ```js
226
241
  await fastify.register(
227
242
  import('@fastify/compress'),
228
243
  { global: false }
@@ -245,9 +260,9 @@ fastify.get('/custom-route', {
245
260
 
246
261
  ### requestEncodings
247
262
 
248
- By default, `@fastify/compress` accepts all encodings specified [at the beginning of §Usage - Decompress request payloads](#usage). You can change that by passing an array of compression tokens to the `requestEncodings` option:
263
+ By default, `@fastify/compress` accepts all encodings specified [here](#usage). Change this by passing an array of compression tokens to the `requestEncodings` option:
249
264
 
250
- ```javascript
265
+ ```js
251
266
  await fastify.register(
252
267
  import('@fastify/compress'),
253
268
  // Only support gzip
@@ -257,17 +272,17 @@ await fastify.register(
257
272
 
258
273
  ### forceRequestEncoding
259
274
 
260
- By default, `@fastify/compress` chooses the decompressing algorithm by looking at the `content-encoding` header, if present.
275
+ By default, `@fastify/compress` chooses the decompression algorithm based on the `content-encoding` header.
261
276
 
262
- You can force one algorithm and ignore the header at all by providing the `forceRequestEncoding` option.
277
+ One algorithm can be forced, and the header ignored, by providing the `forceRequestEncoding` option.
263
278
 
264
- Note that if the request payload is not compressed, `@fastify/compress` will try to decompress, resulting in an error.
279
+ If the request payload is not compressed, `@fastify/compress` will try to decompress, resulting in an error.
265
280
 
266
281
  ### onUnsupportedRequestEncoding
267
282
 
268
- When the request payload encoding is not supported, you can customize the response error by setting the `onUnsupportedEncoding(request, encoding)` option to be a function that returns an error.
283
+ The response error can be customized for unsupported request payload encoding by setting `onUnsupportedEncoding(request, encoding)` to a function that returns an error.
269
284
 
270
- ```javascript
285
+ ```js
271
286
  await fastify.register(
272
287
  import('@fastify/compress'),
273
288
  {
@@ -285,9 +300,9 @@ await fastify.register(
285
300
 
286
301
  ### onInvalidRequestPayload
287
302
 
288
- When the request payload cannot be decompressed using the detected algorithm, you can customize the response error setting the `onInvalidRequestPayload(request, encoding)` option to be a function that returns an error.
303
+ The response error can be customized for undetectable request payloads by setting `onInvalidRequestPayload(request, encoding)` to a function that returns an error.
289
304
 
290
- ```javascript
305
+ ```js
291
306
  await fastify.register(
292
307
  import('@fastify/compress'),
293
308
  {
@@ -303,10 +318,7 @@ await fastify.register(
303
318
  )
304
319
  ```
305
320
 
306
- ## Note
307
- Please note that in large-scale scenarios, you should use a proxy like Nginx to handle response compression.
308
-
309
- ## Acknowledgements
321
+ ## Acknowledgments
310
322
 
311
323
  Past sponsors:
312
324
 
@@ -0,0 +1,6 @@
1
+ 'use strict'
2
+
3
+ module.exports = require('neostandard')({
4
+ ignores: require('neostandard').resolveIgnoresFromGitignore(),
5
+ ts: true
6
+ })
package/index.js CHANGED
@@ -204,7 +204,7 @@ function processDecompressParams (opts) {
204
204
  return params
205
205
  }
206
206
 
207
- function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
207
+ function buildRouteCompress (_fastify, params, routeOptions, decorateOnly) {
208
208
  // In order to provide a compress method with the same parameter set as the route itself,
209
209
  // we decorate the reply at the start of the request
210
210
  if (Array.isArray(routeOptions.onRequest)) {
@@ -216,7 +216,7 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
216
216
  }
217
217
 
218
218
  const compressFn = compress(params)
219
- function onRequest (req, reply, next) {
219
+ function onRequest (_req, reply, next) {
220
220
  reply.compress = compressFn
221
221
  next()
222
222
  }
@@ -291,7 +291,7 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
291
291
  }
292
292
  }
293
293
 
294
- function buildRouteDecompress (fastify, params, routeOptions) {
294
+ function buildRouteDecompress (_fastify, params, routeOptions) {
295
295
  // Add our decompress handler in the preParsing hook
296
296
  if (Array.isArray(routeOptions.preParsing)) {
297
297
  routeOptions.preParsing.unshift(preParsing)
@@ -301,7 +301,7 @@ function buildRouteDecompress (fastify, params, routeOptions) {
301
301
  routeOptions.preParsing = [preParsing]
302
302
  }
303
303
 
304
- function preParsing (request, reply, raw, next) {
304
+ function preParsing (request, _reply, raw, next) {
305
305
  // Get the encoding from the options or from the headers
306
306
  let encoding = params.forceEncoding
307
307
 
@@ -321,7 +321,7 @@ function buildRouteDecompress (fastify, params, routeOptions) {
321
321
  if (params.onUnsupportedRequestEncoding) {
322
322
  try {
323
323
  errorPayload = params.onUnsupportedRequestEncoding(encoding, request)
324
- } catch (ex) {
324
+ } catch {
325
325
  errorPayload = undefined
326
326
  }
327
327
  }
@@ -448,7 +448,7 @@ function onDecompressError (request, params, encoding, error) {
448
448
  if (params.onInvalidRequestPayload) {
449
449
  try {
450
450
  errorPayload = params.onInvalidRequestPayload(encoding, request, error)
451
- } catch (ex) {
451
+ } catch {
452
452
  errorPayload = undefined
453
453
  }
454
454
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/compress",
3
- "version": "8.0.1",
3
+ "version": "8.0.2",
4
4
  "description": "Fastify compression utils",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -18,20 +18,18 @@
18
18
  "devDependencies": {
19
19
  "@fastify/pre-commit": "^2.1.0",
20
20
  "@types/node": "^22.0.0",
21
- "@typescript-eslint/eslint-plugin": "^7.6.0",
22
- "@typescript-eslint/parser": "^7.6.0",
23
21
  "adm-zip": "^0.5.12",
24
22
  "c8": "^10.1.2",
23
+ "eslint": "^9.17.0",
25
24
  "fastify": "^5.0.0",
26
25
  "jsonstream": "^1.0.3",
27
- "standard": "^17.1.0",
26
+ "neostandard": "^0.12.0",
28
27
  "tsd": "^0.31.0",
29
- "typescript": "^5.4.5"
28
+ "typescript": "~5.7.2"
30
29
  },
31
30
  "scripts": {
32
- "lint": "standard",
33
- "lint:fix": "npm run lint -- --fix",
34
- "lint:fix:typescript": "npm run lint:fix -- --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin \"**/*.d.ts\"",
31
+ "lint": "eslint",
32
+ "lint:fix": "eslint --fix",
35
33
  "test": "npm run test:unit && npm run test:typescript",
36
34
  "test:typescript": "tsd",
37
35
  "test:unit": "node --test",
@@ -46,11 +44,40 @@
46
44
  "brotli"
47
45
  ],
48
46
  "author": "Tomas Della Vedova - @delvedor (http://delved.org)",
47
+ "contributors": [
48
+ {
49
+ "name": "Matteo Collina",
50
+ "email": "hello@matteocollina.com"
51
+ },
52
+ {
53
+ "name": "Manuel Spigolon",
54
+ "email": "behemoth89@gmail.com"
55
+ },
56
+ {
57
+ "name": "Aras Abbasi",
58
+ "email": "aras.abbasi@gmail.com"
59
+ },
60
+ {
61
+ "name": "Frazer Smith",
62
+ "email": "frazer.dev@icloud.com",
63
+ "url": "https://github.com/fdawgs"
64
+ }
65
+ ],
49
66
  "license": "MIT",
50
67
  "bugs": {
51
68
  "url": "https://github.com/fastify/fastify-compress/issues"
52
69
  },
53
70
  "homepage": "https://github.com/fastify/fastify-compress#readme",
71
+ "funding": [
72
+ {
73
+ "type": "github",
74
+ "url": "https://github.com/sponsors/fastify"
75
+ },
76
+ {
77
+ "type": "opencollective",
78
+ "url": "https://opencollective.com/fastify"
79
+ }
80
+ ],
54
81
  "repository": {
55
82
  "type": "git",
56
83
  "url": "git+https://github.com/fastify/fastify-compress.git"