Skip to content
Draft
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
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,15 @@ function buildRouteCompress (_fastify, params, routeOptions, decorateOnly) {
return next()
}

let stream, encoding
let stream
const encoding = getEncodingHeader(params.encodings, req)
const noCompress =
// don't compress on x-no-compression header
(req.headers['x-no-compression'] !== undefined) ||
// don't compress if not one of the indicated compressible types
(shouldCompress(reply.getHeader('Content-Type') || 'application/json', params.compressibleTypes) === false) ||
// don't compress on missing or identity `accept-encoding` header
((encoding = getEncodingHeader(params.encodings, req)) == null || encoding === 'identity')
(encoding == null || encoding === 'identity')

if (encoding == null && params.onUnsupportedEncoding != null) {
const encodingHeader = req.headers['accept-encoding']
Expand Down Expand Up @@ -391,14 +392,15 @@ function compress (params) {
return
}

let stream, encoding
let stream
const encoding = getEncodingHeader(params.encodings, this.request)
const noCompress =
// don't compress on x-no-compression header
(this.request.headers['x-no-compression'] !== undefined) ||
// don't compress if not one of the indicated compressible types
(shouldCompress(this.getHeader('Content-Type') || 'application/json', params.compressibleTypes) === false) ||
// don't compress on missing or identity `accept-encoding` header
((encoding = getEncodingHeader(params.encodings, this.request)) == null || encoding === 'identity')
(encoding == null || encoding === 'identity')

if (encoding == null && params.onUnsupportedEncoding != null) {
const encodingHeader = this.request.headers['accept-encoding']
Expand Down
69 changes: 69 additions & 0 deletions test/global-compress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2401,6 +2401,41 @@ describe('It should add hooks correctly: ', async () => {
})

describe('When `Accept-Encoding` request header values are not supported and `onUnsupportedEncoding` is defined :', async () => {
test('it should not call `onUnsupportedEncoding()` when supported encoding is skipped by x-no-compression', async (t) => {
t.plan(4)

let unsupportedCalls = 0
const fastify = Fastify()
await fastify.register(compressPlugin, {
global: true,
onUnsupportedEncoding: (encoding, _request, reply) => {
unsupportedCalls++
reply.code(406)
return JSON.stringify({ hello: encoding })
}
})

fastify.get('/', (_request, reply) => {
reply
.header('Content-Type', 'text/plain')
.send('hello')
})

const response = await fastify.inject({
url: '/',
method: 'GET',
headers: {
'accept-encoding': 'gzip',
'x-no-compression': true
}
})

t.assert.equal(response.statusCode, 200)
t.assert.equal(response.payload, 'hello')
t.assert.ok(!response.headers['content-encoding'])
t.assert.equal(unsupportedCalls, 0)
})

test('it should call the defined `onUnsupportedEncoding()` method', async (t) => {
t.plan(2)

Expand Down Expand Up @@ -3216,6 +3251,40 @@ describe('It should uncompress data when `Accept-Encoding` request header is mis
})

describe('When `onUnsupportedEncoding` is set and the `Accept-Encoding` request header value is an unsupported encoding', async () => {
test('it should not call `onUnsupportedEncoding()` when supported encoding is skipped by content type', async (t) => {
t.plan(4)

let unsupportedCalls = 0
const fastify = Fastify()
await fastify.register(compressPlugin, {
global: true,
onUnsupportedEncoding: (encoding, _request, reply) => {
unsupportedCalls++
reply.code(406)
return JSON.stringify({ hello: encoding })
}
})

fastify.get('/', (_request, reply) => {
reply
.type('image/png')
.compress(Buffer.from('hello'))
})

const response = await fastify.inject({
url: '/',
method: 'GET',
headers: {
'accept-encoding': 'gzip'
}
})

t.assert.equal(response.statusCode, 200)
t.assert.equal(response.rawPayload.toString(), 'hello')
t.assert.ok(!response.headers['content-encoding'])
t.assert.equal(unsupportedCalls, 0)
})

test('it should call the defined `onUnsupportedEncoding()` method', async (t) => {
t.plan(3)

Expand Down