Skip to content

Commit 4bfee0d

Browse files
authored
When compressing add accept-encoding to the Vary header, if the header exists concatenate it. (#120)
* When compressing add the Vary header, if exists concat to it * support multiple headers * updated tests * preveted accept-encoding from being added twice * shortened the code
1 parent ef4ceca commit 4bfee0d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

index.js

+9
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ function compress (params) {
371371
payload = intoStream(payload)
372372
}
373373

374+
if (this.hasHeader('Vary')) {
375+
const varyHeader = Array.isArray(this.getHeader('Vary')) ? this.getHeader('Vary') : [this.getHeader('Vary')]
376+
if (!varyHeader.some(h => h === 'accept-encoding')) {
377+
this.header('Vary', [...varyHeader, 'accept-encoding'])
378+
}
379+
} else {
380+
this.header('Vary', 'accept-encoding')
381+
}
382+
374383
this
375384
.header('Content-Encoding', encoding)
376385
.removeHeader('content-length')

test/test-global-compress.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Fastify = require('fastify')
1212
const compressPlugin = require('../index')
1313

1414
test('should send a deflated data', t => {
15-
t.plan(4)
15+
t.plan(5)
1616
const fastify = Fastify()
1717
fastify.register(compressPlugin, { global: false })
1818

@@ -29,6 +29,7 @@ test('should send a deflated data', t => {
2929
}, (err, res) => {
3030
t.error(err)
3131
t.strictEqual(res.headers['content-encoding'], 'deflate')
32+
t.strictEqual(res.headers.vary, 'accept-encoding')
3233
t.notOk(res.headers['content-length'], 'no content length')
3334
const file = readFileSync('./package.json', 'utf8')
3435
const payload = zlib.inflateSync(res.rawPayload)
@@ -1735,3 +1736,42 @@ test('stream onEnd handler should log an error if exists', t => {
17351736
t.equal(actual.msg, expect.message)
17361737
})
17371738
})
1739+
1740+
test('should concat accept-encoding to vary header if present', t => {
1741+
t.plan(4)
1742+
const fastify = Fastify()
1743+
1744+
fastify.register(compressPlugin, { global: false })
1745+
1746+
fastify.get('/', (req, reply) => {
1747+
reply.header('vary', 'different-header')
1748+
reply.type('text/plain').compress(createReadStream('./package.json'))
1749+
})
1750+
1751+
fastify.get('/foo', (req, reply) => {
1752+
reply.header('vary', ['different-header', 'my-header'])
1753+
reply.type('text/plain').compress(createReadStream('./package.json'))
1754+
})
1755+
1756+
fastify.inject({
1757+
url: '/',
1758+
method: 'GET',
1759+
headers: {
1760+
'accept-encoding': 'deflate'
1761+
}
1762+
}, (err, res) => {
1763+
t.error(err)
1764+
t.deepEqual(res.headers.vary, ['different-header', 'accept-encoding'])
1765+
})
1766+
1767+
fastify.inject({
1768+
url: '/foo',
1769+
method: 'GET',
1770+
headers: {
1771+
'accept-encoding': 'deflate'
1772+
}
1773+
}, (err, res) => {
1774+
t.error(err)
1775+
t.deepEqual(res.headers.vary, ['different-header', 'my-header', 'accept-encoding'])
1776+
})
1777+
})

0 commit comments

Comments
 (0)