Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix brotli responses being double-compressed #167

Merged
merged 3 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
if (payload == null) {
return next()
}
const responseEncoding = reply.getHeader('Content-Encoding')
if (responseEncoding && responseEncoding !== 'identity') {
// response is already compressed
return next()
}
setVaryHeader(reply)

let stream, encoding
Expand Down
54 changes: 46 additions & 8 deletions test/test-global-compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,21 @@ test('should support quality syntax', t => {
})
})

test('onSend hook should not double-compress Stream if already zipped', t => {
t.plan(3)
test('onSend hook should not double-compress Stream if already gzipped', t => {
t.plan(4)
const fastify = Fastify()
fastify.register(compressPlugin, { global: true })
fastify.register(compressPlugin, {
global: true,
threshold: 0
})

const file = readFileSync('./package.json', 'utf8')
fastify.get('/', (req, reply) => {
reply.type('text/plain').compress(
createReadStream('./package.json')
.pipe(zlib.createGzip())
)
const payload = zlib.gzipSync(file)
reply.type('application/json')
.header('content-encoding', 'gzip')
.header('content-length', payload.length)
.send(payload)
})

fastify.inject({
Expand All @@ -216,12 +221,44 @@ test('onSend hook should not double-compress Stream if already zipped', t => {
}, (err, res) => {
t.error(err)
t.equal(res.headers['content-encoding'], 'gzip')
const file = readFileSync('./package.json', 'utf8')
t.equal(res.headers['content-length'], res.rawPayload.length)
const payload = zlib.gunzipSync(res.rawPayload)
t.equal(payload.toString('utf-8'), file)
})
})

test('onSend hook should not double-compress Stream if already brotli compressed', t => {
t.plan(4)
const fastify = Fastify()
fastify.register(compressPlugin, {
global: true,
threshold: 0
})

const file = readFileSync('./package.json', 'utf8')
fastify.get('/', (req, reply) => {
const payload = zlib.brotliCompressSync(file)
reply.type('application/json')
.header('content-encoding', 'br')
.header('content-length', payload.length)
.send(payload)
})

fastify.inject({
url: '/',
method: 'GET',
headers: {
'accept-encoding': 'br,gzip,deflate'
}
}, (err, res) => {
t.error(err)
t.equal(res.headers['content-encoding'], 'br')
t.equal(res.headers['content-length'], res.rawPayload.length)
const payload = zlib.brotliDecompressSync(res.rawPayload)
t.equal(payload.toString('utf-8'), file)
})
})

test('should send a gzipped data for * header', t => {
t.plan(3)
const fastify = Fastify()
Expand Down Expand Up @@ -1732,6 +1769,7 @@ test('stream onEnd handler should log an error if exists', t => {
const logger = new Writable({
write (chunk, encoding, callback) {
actual = JSON.parse(chunk.toString())
callback()
}
})

Expand Down