@fastify/compress 9.2.1 → 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/index.js +22 -7
- package/package.json +1 -1
- package/test/global-compress.test.js +49 -0
package/index.js
CHANGED
|
@@ -570,15 +570,30 @@ function compress (params) {
|
|
|
570
570
|
}
|
|
571
571
|
|
|
572
572
|
function setVaryHeader (reply) {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
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
|
|
578
593
|
}
|
|
579
|
-
} else {
|
|
580
|
-
reply.header('Vary', 'accept-encoding')
|
|
581
594
|
}
|
|
595
|
+
|
|
596
|
+
reply.header('vary', value + ', accept-encoding')
|
|
582
597
|
}
|
|
583
598
|
|
|
584
599
|
function onEnd (err) {
|
package/package.json
CHANGED
|
@@ -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
|
|