@fastify/compress 9.2.0 → 9.2.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/.c8rc CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "all": true,
3
- "exclude": ["test/", "coverage/", "types/", "eslint.config.js"],
3
+ "exclude": ["test/", "coverage/", "types/", "benchmark/", "eslint.config.js"],
4
4
  "clean": true,
5
5
  "check-coverage": true,
6
6
  "branches": 100,
@@ -0,0 +1,24 @@
1
+ name: Release the package
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ semver:
7
+ description: 'Release bump type'
8
+ required: true
9
+ type: choice
10
+ options:
11
+ - patch
12
+ - minor
13
+ - major
14
+
15
+ permissions: {}
16
+
17
+ jobs:
18
+ release:
19
+ permissions:
20
+ id-token: write # required for npm provenance via OIDC
21
+ contents: write # required to push the release commit and the tag
22
+ uses: fastify/workflows/.github/workflows/reusable-release.yml@b1a11bce713bc857e52b2231fec3c89071ad3135
23
+ with:
24
+ semver: ${{ inputs.semver }}
package/README.md CHANGED
@@ -480,7 +480,7 @@ await fastify.register(
480
480
 
481
481
  ## Acknowledgments
482
482
 
483
- Past sp
483
+ Past sponsors:
484
484
  - LetzDoIt
485
485
 
486
486
  ## License
package/index.js CHANGED
@@ -2,13 +2,11 @@
2
2
 
3
3
  const zlib = require('node:zlib')
4
4
  const { inherits, format } = require('node:util')
5
- const { pipeline, compose } = require('node:stream')
5
+ const { pipeline, compose, Readable, PassThrough } = require('node:stream')
6
6
 
7
7
  const fp = require('fastify-plugin')
8
8
  const encodingNegotiator = require('@fastify/accept-negotiator')
9
9
  const mimedb = require('mime-db')
10
- const { Minipass } = require('minipass')
11
- const { Readable } = require('readable-stream')
12
10
 
13
11
  const {
14
12
  computeSyncThreshold,
@@ -572,15 +570,30 @@ function compress (params) {
572
570
  }
573
571
 
574
572
  function setVaryHeader (reply) {
575
- if (reply.hasHeader('Vary')) {
576
- const rawHeaderValue = reply.getHeader('Vary')
577
- const headerValueArray = Array.isArray(rawHeaderValue) ? rawHeaderValue : [rawHeaderValue]
578
- if (!headerValueArray.some((h) => h.includes('accept-encoding'))) {
579
- reply.header('Vary', headerValueArray.concat('accept-encoding').join(', '))
573
+ const current = reply.getHeader('vary')
574
+
575
+ if (current === undefined) {
576
+ reply.header('vary', 'accept-encoding')
577
+ return
578
+ }
579
+
580
+ const value = Array.isArray(current) ? current.join(', ') : String(current)
581
+
582
+ // Walk the comma-separated tokens without splitting the string.
583
+ const headerLength = value.length
584
+ let start = 0
585
+ for (let i = 0; i <= headerLength; i++) {
586
+ if (i === headerLength || value[i] === ',') {
587
+ const token = value.slice(start, i).trim().toLowerCase()
588
+ // A `Vary: *` already covers every request header.
589
+ if (token === 'accept-encoding' || token === '*') {
590
+ return
591
+ }
592
+ start = i + 1
580
593
  }
581
- } else {
582
- reply.header('Vary', 'accept-encoding')
583
594
  }
595
+
596
+ reply.header('vary', value + ', accept-encoding')
584
597
  }
585
598
 
586
599
  function onEnd (err) {
@@ -681,8 +694,8 @@ function maybeUnzip (payload, serialize) {
681
694
  function zipStream (deflate, encoding) {
682
695
  return createPeekTransform(function (data) {
683
696
  switch (isCompressed(data)) {
684
- case 1: return new Minipass()
685
- case 2: return new Minipass()
697
+ case 1: return new PassThrough()
698
+ case 2: return new PassThrough()
686
699
  }
687
700
  return deflate[encoding]()
688
701
  })
@@ -698,7 +711,7 @@ function unzipStream (inflate, maxRecursion) {
698
711
  case 1: return compose(inflate.gzip(), unzipStream(inflate, maxRecursion - 1))
699
712
  case 2: return compose(inflate.deflate(), unzipStream(inflate, maxRecursion - 1))
700
713
  }
701
- return new Minipass()
714
+ return new PassThrough()
702
715
  })
703
716
  }
704
717
 
package/lib/utils.js CHANGED
@@ -1,8 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const { Readable: NodeReadable } = require('node:stream')
3
+ const { Readable: NodeReadable, Transform } = require('node:stream')
4
4
  const { availableParallelism } = require('node:os')
5
- const { Transform } = require('readable-stream')
6
5
 
7
6
  // Bounds of the computed default synchronous compression threshold
8
7
  const MIN_SYNC_THRESHOLD = 4096
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/compress",
3
- "version": "9.2.0",
3
+ "version": "9.2.2",
4
4
  "description": "Fastify compression utils",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -8,9 +8,7 @@
8
8
  "dependencies": {
9
9
  "@fastify/accept-negotiator": "^2.0.0",
10
10
  "fastify-plugin": "^6.0.0",
11
- "mime-db": "^1.52.0",
12
- "minipass": "^7.0.4",
13
- "readable-stream": "^4.5.2"
11
+ "mime-db": "^1.52.0"
14
12
  },
15
13
  "devDependencies": {
16
14
  "@types/node": "^26.0.1",
@@ -3167,6 +3167,55 @@ test('It should not add `accept-encoding` to `Vary` reply header if already pres
3167
3167
  })
3168
3168
  })
3169
3169
 
3170
+ test('It should handle case-insensitivity, substring safety, and wildcard in Vary header', async (t) => {
3171
+ t.plan(3)
3172
+
3173
+ const fastify = Fastify()
3174
+ await fastify.register(compressPlugin, { global: true })
3175
+
3176
+ fastify.get('/case', (_request, reply) => {
3177
+ reply
3178
+ .header('vary', 'Accept-Encoding')
3179
+ .type('text/plain')
3180
+ .compress(createReadStream('./package.json'))
3181
+ })
3182
+
3183
+ fastify.get('/substring', (_request, reply) => {
3184
+ reply
3185
+ .header('vary', 'x-accept-encoding')
3186
+ .type('text/plain')
3187
+ .compress(createReadStream('./package.json'))
3188
+ })
3189
+
3190
+ fastify.get('/wildcard', (_request, reply) => {
3191
+ reply
3192
+ .header('vary', '*')
3193
+ .type('text/plain')
3194
+ .compress(createReadStream('./package.json'))
3195
+ })
3196
+
3197
+ const res1 = await fastify.inject({
3198
+ url: '/case',
3199
+ method: 'GET',
3200
+ headers: { 'accept-encoding': 'deflate' }
3201
+ })
3202
+ t.assert.equal(res1.headers.vary, 'Accept-Encoding')
3203
+
3204
+ const res2 = await fastify.inject({
3205
+ url: '/substring',
3206
+ method: 'GET',
3207
+ headers: { 'accept-encoding': 'deflate' }
3208
+ })
3209
+ t.assert.equal(res2.headers.vary, 'x-accept-encoding, accept-encoding')
3210
+
3211
+ const res3 = await fastify.inject({
3212
+ url: '/wildcard',
3213
+ method: 'GET',
3214
+ headers: { 'accept-encoding': 'deflate' }
3215
+ })
3216
+ t.assert.equal(res3.headers.vary, '*')
3217
+ })
3218
+
3170
3219
  test('It should follow the `Accept-Encoding` request header encoding order', async (t) => {
3171
3220
  t.plan(3)
3172
3221