@fastify/compress 7.0.1 → 7.0.3
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 +19 -20
- package/lib/utils.js +42 -1
- package/package.json +6 -6
- package/test/global-compress.test.js +152 -80
- package/test/regression/issue-288.test.js +62 -0
- package/test/routes-compress.test.js +21 -10
- package/test/utils.test.js +43 -1
- package/types/index.d.ts +14 -7
package/index.js
CHANGED
|
@@ -7,12 +7,12 @@ const fp = require('fastify-plugin')
|
|
|
7
7
|
const encodingNegotiator = require('@fastify/accept-negotiator')
|
|
8
8
|
const pump = require('pump')
|
|
9
9
|
const mimedb = require('mime-db')
|
|
10
|
-
const intoStream = require('into-stream')
|
|
11
10
|
const peek = require('peek-stream')
|
|
12
11
|
const { Minipass } = require('minipass')
|
|
13
12
|
const pumpify = require('pumpify')
|
|
13
|
+
const { Readable } = require('readable-stream')
|
|
14
14
|
|
|
15
|
-
const { isStream, isGzip, isDeflate } = require('./lib/utils')
|
|
15
|
+
const { isStream, isGzip, isDeflate, intoAsyncIterator } = require('./lib/utils')
|
|
16
16
|
|
|
17
17
|
const InvalidRequestEncodingError = createError('FST_CP_ERR_INVALID_CONTENT_ENCODING', 'Unsupported Content-Encoding: %s', 415)
|
|
18
18
|
const InvalidRequestCompressedPayloadError = createError('FST_CP_ERR_INVALID_CONTENT', 'Could not decompress the request payload using the provided encoding', 400)
|
|
@@ -242,7 +242,6 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
|
|
|
242
242
|
// response is already compressed
|
|
243
243
|
return next()
|
|
244
244
|
}
|
|
245
|
-
setVaryHeader(reply)
|
|
246
245
|
|
|
247
246
|
let stream, encoding
|
|
248
247
|
const noCompress =
|
|
@@ -277,14 +276,14 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
|
|
|
277
276
|
if (Buffer.byteLength(payload) < params.threshold) {
|
|
278
277
|
return next()
|
|
279
278
|
}
|
|
280
|
-
payload =
|
|
279
|
+
payload = Readable.from(intoAsyncIterator(payload))
|
|
281
280
|
}
|
|
282
281
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
282
|
+
setVaryHeader(reply)
|
|
283
|
+
reply.header('Content-Encoding', encoding)
|
|
284
|
+
if (params.removeContentLengthHeader) {
|
|
285
|
+
reply.removeHeader('content-length')
|
|
286
|
+
}
|
|
288
287
|
|
|
289
288
|
stream = zipStream(params.compressStream, encoding)
|
|
290
289
|
pump(payload, stream, onEnd.bind(reply))
|
|
@@ -360,7 +359,6 @@ function compress (params) {
|
|
|
360
359
|
return
|
|
361
360
|
}
|
|
362
361
|
|
|
363
|
-
setVaryHeader(this)
|
|
364
362
|
let stream, encoding
|
|
365
363
|
const noCompress =
|
|
366
364
|
// don't compress on x-no-compression header
|
|
@@ -402,14 +400,14 @@ function compress (params) {
|
|
|
402
400
|
if (Buffer.byteLength(payload) < params.threshold) {
|
|
403
401
|
return this.send(payload)
|
|
404
402
|
}
|
|
405
|
-
payload =
|
|
403
|
+
payload = Readable.from(intoAsyncIterator(payload))
|
|
406
404
|
}
|
|
407
405
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
406
|
+
setVaryHeader(this)
|
|
407
|
+
this.header('Content-Encoding', encoding)
|
|
408
|
+
if (params.removeContentLengthHeader) {
|
|
409
|
+
this.removeHeader('content-length')
|
|
410
|
+
}
|
|
413
411
|
|
|
414
412
|
stream = zipStream(params.compressStream, encoding)
|
|
415
413
|
pump(payload, stream, onEnd.bind(this))
|
|
@@ -419,9 +417,10 @@ function compress (params) {
|
|
|
419
417
|
|
|
420
418
|
function setVaryHeader (reply) {
|
|
421
419
|
if (reply.hasHeader('Vary')) {
|
|
422
|
-
const
|
|
423
|
-
|
|
424
|
-
|
|
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(', '))
|
|
425
424
|
}
|
|
426
425
|
} else {
|
|
427
426
|
reply.header('Vary', 'accept-encoding')
|
|
@@ -510,7 +509,7 @@ function maybeUnzip (payload, serialize) {
|
|
|
510
509
|
// handle case where serialize doesn't return a string or Buffer
|
|
511
510
|
if (!Buffer.isBuffer(buf)) return result
|
|
512
511
|
if (isCompressed(buf) === 0) return result
|
|
513
|
-
return
|
|
512
|
+
return Readable.from(intoAsyncIterator(result))
|
|
514
513
|
}
|
|
515
514
|
|
|
516
515
|
function zipStream (deflate, encoding) {
|
package/lib/utils.js
CHANGED
|
@@ -35,4 +35,45 @@ function isStream (stream) {
|
|
|
35
35
|
return stream !== null && typeof stream === 'object' && typeof stream.pipe === 'function'
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Provide a async iteratable for Readable.from
|
|
40
|
+
*/
|
|
41
|
+
async function * intoAsyncIterator (payload) {
|
|
42
|
+
if (typeof payload === 'object') {
|
|
43
|
+
if (Buffer.isBuffer(payload)) {
|
|
44
|
+
yield payload
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (
|
|
49
|
+
// ArrayBuffer
|
|
50
|
+
payload instanceof ArrayBuffer ||
|
|
51
|
+
// NodeJS.TypedArray
|
|
52
|
+
ArrayBuffer.isView(payload)
|
|
53
|
+
) {
|
|
54
|
+
yield Buffer.from(payload)
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Iterator
|
|
59
|
+
if (Symbol.iterator in payload) {
|
|
60
|
+
for (const chunk of payload) {
|
|
61
|
+
yield chunk
|
|
62
|
+
}
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Async Iterator
|
|
67
|
+
if (Symbol.asyncIterator in payload) {
|
|
68
|
+
for await (const chunk of payload) {
|
|
69
|
+
yield chunk
|
|
70
|
+
}
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// string
|
|
76
|
+
yield payload
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = { isGzip, isDeflate, isStream, intoAsyncIterator }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/compress",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.3",
|
|
4
4
|
"description": "Fastify compression utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -8,24 +8,24 @@
|
|
|
8
8
|
"dependencies": {
|
|
9
9
|
"@fastify/accept-negotiator": "^1.1.0",
|
|
10
10
|
"fastify-plugin": "^4.5.0",
|
|
11
|
-
"into-stream": "^6.0.0",
|
|
12
11
|
"mime-db": "^1.52.0",
|
|
13
12
|
"minipass": "^7.0.2",
|
|
14
13
|
"peek-stream": "^1.1.3",
|
|
15
14
|
"pump": "^3.0.0",
|
|
16
|
-
"pumpify": "^2.0.1"
|
|
15
|
+
"pumpify": "^2.0.1",
|
|
16
|
+
"readable-stream": "^4.5.2"
|
|
17
17
|
},
|
|
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",
|
|
26
26
|
"standard": "^17.1.0",
|
|
27
27
|
"tap": "^16.3.7",
|
|
28
|
-
"tsd": "^0.
|
|
28
|
+
"tsd": "^0.31.0",
|
|
29
29
|
"typescript": "^5.1.6"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|