@fastify/compress 7.0.0 → 7.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.
- package/index.js +25 -22
- package/package.json +3 -3
- package/test/global-compress.test.js +207 -102
- package/test/routes-compress.test.js +21 -10
- package/types/index.test-d.ts +3 -3
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 =
|
|
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
|
|
@@ -238,7 +242,6 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
|
|
|
238
242
|
// response is already compressed
|
|
239
243
|
return next()
|
|
240
244
|
}
|
|
241
|
-
setVaryHeader(reply)
|
|
242
245
|
|
|
243
246
|
let stream, encoding
|
|
244
247
|
const noCompress =
|
|
@@ -276,11 +279,11 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
|
|
|
276
279
|
payload = intoStream(payload)
|
|
277
280
|
}
|
|
278
281
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
282
|
+
setVaryHeader(reply)
|
|
283
|
+
reply.header('Content-Encoding', encoding)
|
|
284
|
+
if (params.removeContentLengthHeader) {
|
|
285
|
+
reply.removeHeader('content-length')
|
|
286
|
+
}
|
|
284
287
|
|
|
285
288
|
stream = zipStream(params.compressStream, encoding)
|
|
286
289
|
pump(payload, stream, onEnd.bind(reply))
|
|
@@ -356,7 +359,6 @@ function compress (params) {
|
|
|
356
359
|
return
|
|
357
360
|
}
|
|
358
361
|
|
|
359
|
-
setVaryHeader(this)
|
|
360
362
|
let stream, encoding
|
|
361
363
|
const noCompress =
|
|
362
364
|
// don't compress on x-no-compression header
|
|
@@ -401,11 +403,11 @@ function compress (params) {
|
|
|
401
403
|
payload = intoStream(payload)
|
|
402
404
|
}
|
|
403
405
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
406
|
+
setVaryHeader(this)
|
|
407
|
+
this.header('Content-Encoding', encoding)
|
|
408
|
+
if (params.removeContentLengthHeader) {
|
|
409
|
+
this.removeHeader('content-length')
|
|
410
|
+
}
|
|
409
411
|
|
|
410
412
|
stream = zipStream(params.compressStream, encoding)
|
|
411
413
|
pump(payload, stream, onEnd.bind(this))
|
|
@@ -415,9 +417,10 @@ function compress (params) {
|
|
|
415
417
|
|
|
416
418
|
function setVaryHeader (reply) {
|
|
417
419
|
if (reply.hasHeader('Vary')) {
|
|
418
|
-
const
|
|
419
|
-
|
|
420
|
-
|
|
420
|
+
const rawHeaderValue = reply.getHeader('Vary')
|
|
421
|
+
const headerValueArray = Array.isArray(rawHeaderValue) ? rawHeaderValue : [rawHeaderValue]
|
|
422
|
+
if (!headerValueArray.some((h) => h.includes('accept-encoding'))) {
|
|
423
|
+
reply.header('Vary', headerValueArray.concat('accept-encoding').join(', '))
|
|
421
424
|
}
|
|
422
425
|
} else {
|
|
423
426
|
reply.header('Vary', 'accept-encoding')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/compress",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.2",
|
|
4
4
|
"description": "Fastify compression utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@fastify/pre-commit": "^2.0.2",
|
|
20
20
|
"@types/node": "^20.4.2",
|
|
21
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
22
|
-
"@typescript-eslint/parser": "^
|
|
21
|
+
"@typescript-eslint/eslint-plugin": "^7.1.0",
|
|
22
|
+
"@typescript-eslint/parser": "^7.1.0",
|
|
23
23
|
"adm-zip": "^0.5.10",
|
|
24
24
|
"fastify": "^4.19.2",
|
|
25
25
|
"jsonstream": "^1.0.3",
|