@fastify/compress 6.5.0 → 7.0.1

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.
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![NPM version](https://img.shields.io/npm/v/@fastify/compress.svg?style=flat)](https://www.npmjs.com/package/@fastify/compress)
5
5
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
6
6
 
7
- Adds compression utils to [the Fastify `reply` object](https://www.fastify.io/docs/latest/Reference/Reply/#reply) and a hook to decompress requests payloads.
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
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.
@@ -169,7 +169,7 @@ You can tune compression by setting the `brotliOptions` and `zlibOptions` proper
169
169
  brotliOptions: {
170
170
  params: {
171
171
  [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT, // useful for APIs that primarily return text
172
- [zlib.constants.BROTLI_PARAM_QUALITY]: 4, // default is 11, max is 11, min is 0
172
+ [zlib.constants.BROTLI_PARAM_QUALITY]: 4, // default is 4, max is 11, min is 0
173
173
  },
174
174
  },
175
175
  zlibOptions: {
package/index.js CHANGED
@@ -109,6 +109,13 @@ function fastifyCompress (fastify, opts, next) {
109
109
  }
110
110
 
111
111
  const defaultCompressibleTypes = /^text\/(?!event-stream)|(?:\+|\/)json(?:;|$)|(?:\+|\/)text(?:;|$)|(?:\+|\/)xml(?:;|$)|octet-stream(?:;|$)/u
112
+ const recommendedDefaultBrotliOptions = {
113
+ params: {
114
+ // Default of 4 as 11 has a heavy impact on performance.
115
+ // https://blog.cloudflare.com/this-is-brotli-from-origin#testing
116
+ [zlib.constants.BROTLI_PARAM_QUALITY]: 4
117
+ }
118
+ }
112
119
 
113
120
  function processCompressParams (opts) {
114
121
  /* istanbul ignore next */
@@ -121,7 +128,9 @@ function processCompressParams (opts) {
121
128
  }
122
129
 
123
130
  params.removeContentLengthHeader = typeof opts.removeContentLengthHeader === 'boolean' ? opts.removeContentLengthHeader : true
124
- params.brotliOptions = opts.brotliOptions
131
+ params.brotliOptions = params.global
132
+ ? { ...recommendedDefaultBrotliOptions, ...opts.brotliOptions }
133
+ : opts.brotliOptions
125
134
  params.zlibOptions = opts.zlibOptions
126
135
  params.onUnsupportedEncoding = opts.onUnsupportedEncoding
127
136
  params.inflateIfDeflated = opts.inflateIfDeflated === true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/compress",
3
- "version": "6.5.0",
3
+ "version": "7.0.1",
4
4
  "description": "Fastify compression utils",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -25,7 +25,7 @@
25
25
  "jsonstream": "^1.0.3",
26
26
  "standard": "^17.1.0",
27
27
  "tap": "^16.3.7",
28
- "tsd": "^0.29.0",
28
+ "tsd": "^0.30.0",
29
29
  "typescript": "^5.1.6"
30
30
  },
31
31
  "scripts": {
@@ -2534,36 +2534,69 @@ test('When `encodings` option is set, it should only use the registered value',
2534
2534
  t.equal(response.statusCode, 200)
2535
2535
  })
2536
2536
 
2537
- test('It should send data compressed according to `brotliOptions`', async (t) => {
2538
- t.plan(2)
2539
- const brotliOptions = {
2540
- params: {
2541
- [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
2542
- [zlib.constants.BROTLI_PARAM_QUALITY]: 4
2537
+ test('It should send data compressed according to `brotliOptions` :', async (t) => {
2538
+ t.test('when using br encoding', async (t) => {
2539
+ t.plan(3)
2540
+ const brotliOptions = {
2541
+ params: {
2542
+ [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
2543
+ [zlib.constants.BROTLI_PARAM_QUALITY]: 8
2544
+ }
2543
2545
  }
2544
- }
2545
2546
 
2546
- const fastify = Fastify()
2547
- await fastify.register(compressPlugin, { global: true, brotliOptions })
2547
+ const fastify = Fastify()
2548
+ await fastify.register(compressPlugin, { global: true, brotliOptions })
2548
2549
 
2549
- fastify.get('/', (request, reply) => {
2550
- reply
2551
- .type('text/plain')
2552
- .compress(createReadStream('./package.json'))
2550
+ fastify.get('/', (request, reply) => {
2551
+ reply
2552
+ .type('text/plain')
2553
+ .compress(createReadStream('./package.json'))
2554
+ })
2555
+
2556
+ const response = await fastify.inject({
2557
+ url: '/',
2558
+ method: 'GET',
2559
+ headers: {
2560
+ 'accept-encoding': 'br'
2561
+ }
2562
+ })
2563
+
2564
+ const file = readFileSync('./package.json', 'utf8')
2565
+ const payload = zlib.brotliDecompressSync(response.rawPayload, brotliOptions)
2566
+ t.equal(response.headers['content-encoding'], 'br')
2567
+ t.equal(payload.toString('utf-8'), file)
2568
+
2569
+ const compressedPayload = zlib.brotliCompressSync(file, brotliOptions)
2570
+ t.same(response.rawPayload, compressedPayload)
2553
2571
  })
2554
2572
 
2555
- const response = await fastify.inject({
2556
- url: '/',
2557
- method: 'GET',
2558
- headers: {
2559
- 'accept-encoding': 'br'
2573
+ t.test('default BROTLI_PARAM_QUALITY to be 4', async (t) => {
2574
+ t.plan(1)
2575
+
2576
+ const fastify = Fastify()
2577
+ await fastify.register(compressPlugin, { global: true })
2578
+
2579
+ const file = readFileSync('./package.json', 'utf8')
2580
+ fastify.get('/', (request, reply) => {
2581
+ reply
2582
+ .type('text/plain')
2583
+ .compress(file)
2584
+ })
2585
+
2586
+ const response = await fastify.inject({
2587
+ url: '/',
2588
+ method: 'GET',
2589
+ headers: {
2590
+ 'accept-encoding': 'br'
2591
+ }
2592
+ })
2593
+
2594
+ const defaultBrotliOptions = {
2595
+ params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4 }
2560
2596
  }
2597
+ const compressedPayload = zlib.brotliCompressSync(file, defaultBrotliOptions)
2598
+ t.same(response.rawPayload, compressedPayload)
2561
2599
  })
2562
-
2563
- const file = readFileSync('./package.json', 'utf8')
2564
- const payload = zlib.brotliDecompressSync(response.rawPayload, brotliOptions)
2565
- t.equal(response.headers['content-encoding'], 'br')
2566
- t.equal(payload.toString('utf-8'), file)
2567
2600
  })
2568
2601
 
2569
2602
  test('It should send data compressed according to `zlibOptions` :', async (t) => {
@@ -40,7 +40,7 @@ app.get('/test-two', async (request, reply) => {
40
40
  expectError(reply.compress())
41
41
  })
42
42
 
43
- // Instanciation of an app without global
43
+ // Instantiation of an app without global
44
44
  const appWithoutGlobal: FastifyInstance = fastify()
45
45
  appWithoutGlobal.register(fastifyCompress, { global: false })
46
46
 
@@ -107,11 +107,11 @@ appWithoutGlobal.inject(
107
107
  }
108
108
  },
109
109
  (err) => {
110
- expectType<Error>(err)
110
+ expectType<Error | undefined>(err)
111
111
  }
112
112
  )
113
113
 
114
- // Instanciation of an app that should trigger a typescript error
114
+ // Instantiation of an app that should trigger a typescript error
115
115
  const appThatTriggerAnError = fastify()
116
116
  expectError(appThatTriggerAnError.register(fastifyCompress, {
117
117
  global: true,