@fastify/compress 7.0.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/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 */
@@ -117,16 +124,13 @@ function processCompressParams (opts) {
117
124
  }
118
125
 
119
126
  const params = {
120
- global: (typeof opts.global === 'boolean') ? opts.global : true,
121
- /**
122
- * Default of 4 as 11 has a heavy impact on performance.
123
- * @see {@link https://blog.cloudflare.com/this-is-brotli-from-origin#testing}
124
- */
125
- brotliOptions: { [zlib.constants.BROTLI_PARAM_QUALITY]: 4, ...opts.brotliOptions }
127
+ global: (typeof opts.global === 'boolean') ? opts.global : true
126
128
  }
127
129
 
128
130
  params.removeContentLengthHeader = typeof opts.removeContentLengthHeader === 'boolean' ? opts.removeContentLengthHeader : true
129
- params.brotliOptions = opts.brotliOptions
131
+ params.brotliOptions = params.global
132
+ ? { ...recommendedDefaultBrotliOptions, ...opts.brotliOptions }
133
+ : opts.brotliOptions
130
134
  params.zlibOptions = opts.zlibOptions
131
135
  params.onUnsupportedEncoding = opts.onUnsupportedEncoding
132
136
  params.inflateIfDeflated = opts.inflateIfDeflated === true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/compress",
3
- "version": "7.0.0",
3
+ "version": "7.0.1",
4
4
  "description": "Fastify compression utils",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -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,