@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 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 = intoStream(payload)
279
+ payload = Readable.from(intoAsyncIterator(payload))
281
280
  }
282
281
 
283
- params.removeContentLengthHeader
284
- ? reply
285
- .header('Content-Encoding', encoding)
286
- .removeHeader('content-length')
287
- : reply.header('Content-Encoding', encoding)
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 = intoStream(payload)
403
+ payload = Readable.from(intoAsyncIterator(payload))
406
404
  }
407
405
 
408
- params.removeContentLengthHeader
409
- ? this
410
- .header('Content-Encoding', encoding)
411
- .removeHeader('content-length')
412
- : this.header('Content-Encoding', encoding)
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 varyHeader = Array.isArray(reply.getHeader('Vary')) ? reply.getHeader('Vary') : [reply.getHeader('Vary')]
423
- if (!varyHeader.some((h) => h.includes('accept-encoding'))) {
424
- reply.header('Vary', `${varyHeader.join(', ')}, accept-encoding`)
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 intoStream(result)
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
- module.exports = { isGzip, isDeflate, isStream }
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.1",
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": "^6.0.0",
22
- "@typescript-eslint/parser": "^6.0.0",
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.30.0",
28
+ "tsd": "^0.31.0",
29
29
  "typescript": "^5.1.6"
30
30
  },
31
31
  "scripts": {