Skip to content

Commit c95c62f

Browse files
committed
feat: allow enabling and disabling compression and decompression separately
1 parent 331457b commit c95c62f

File tree

2 files changed

+153
-2
lines changed

2 files changed

+153
-2
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ function processCompressParams (opts) {
124124
}
125125

126126
const params = {
127-
global: (typeof opts.global === 'boolean') ? opts.global : true
127+
global: (typeof opts.globalCompression === 'boolean') ? opts.globalCompression :
128+
(typeof opts.global === 'boolean') ? opts.global : true
128129
}
129130

130131
params.removeContentLengthHeader = typeof opts.removeContentLengthHeader === 'boolean' ? opts.removeContentLengthHeader : true
@@ -173,7 +174,8 @@ function processDecompressParams (opts) {
173174
const customZlib = opts.zlib || zlib
174175

175176
const params = {
176-
global: (typeof opts.global === 'boolean') ? opts.global : true,
177+
global: (typeof opts.globalDecompression === 'boolean') ? opts.globalDecompression :
178+
(typeof opts.global === 'boolean') ? opts.global : true,
177179
onUnsupportedRequestEncoding: opts.onUnsupportedRequestEncoding,
178180
onInvalidRequestPayload: opts.onInvalidRequestPayload,
179181
decompressStream: {

test/global-compress.test.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,3 +3298,152 @@ for (const contentType of notByDefaultSupportedContentTypes) {
32983298
t.assert.equal(response.rawPayload.toString('utf-8'), file)
32993299
})
33003300
}
3301+
3302+
test('It should support separate globalCompression and globalDecompression settings', async (t) => {
3303+
t.plan(3)
3304+
3305+
// Test case: disable compression but enable decompression globally
3306+
const fastify = Fastify()
3307+
await fastify.register(compressPlugin, {
3308+
globalCompression: false,
3309+
globalDecompression: true
3310+
})
3311+
3312+
const testData = { message: 'test data for decompression only' }
3313+
3314+
fastify.get('/', (request, reply) => {
3315+
reply.send(testData)
3316+
})
3317+
3318+
// Test that compression is disabled
3319+
const getResponse = await fastify.inject({
3320+
url: '/',
3321+
method: 'GET',
3322+
headers: {
3323+
'accept-encoding': 'gzip, deflate, br'
3324+
}
3325+
})
3326+
3327+
t.assert.equal(getResponse.statusCode, 200)
3328+
// No compression should happen since globalCompression is false
3329+
t.assert.equal(getResponse.headers['content-encoding'], undefined)
3330+
t.assert.deepEqual(getResponse.json(), testData)
3331+
})
3332+
3333+
test('It should enable decompression when globalDecompression is true', async (t) => {
3334+
t.plan(2)
3335+
3336+
// Test case: enable decompression globally
3337+
const fastify = Fastify()
3338+
await fastify.register(compressPlugin, {
3339+
globalCompression: false,
3340+
globalDecompression: true
3341+
})
3342+
3343+
// Create a compressed request payload to test decompression
3344+
const compressedPayload = zlib.gzipSync(JSON.stringify({ input: 'compressed data' }))
3345+
3346+
fastify.post('/', (request, reply) => {
3347+
// This should be decompressed automatically
3348+
reply.send({ received: request.body })
3349+
})
3350+
3351+
// Test that decompression is enabled
3352+
const postResponse = await fastify.inject({
3353+
url: '/',
3354+
method: 'POST',
3355+
headers: {
3356+
'content-encoding': 'gzip',
3357+
'content-type': 'application/json'
3358+
},
3359+
payload: compressedPayload
3360+
})
3361+
3362+
t.assert.equal(postResponse.statusCode, 200)
3363+
// Decompression should work since globalDecompression is true
3364+
t.assert.deepEqual(postResponse.json(), { received: { input: 'compressed data' } })
3365+
})
3366+
3367+
test('It should fall back to global option when specific options are not provided', async (t) => {
3368+
t.plan(2)
3369+
3370+
// Test that global: false affects both compression and decompression
3371+
const fastify = Fastify()
3372+
await fastify.register(compressPlugin, {
3373+
global: false
3374+
})
3375+
3376+
const testData = { message: 'test data' }
3377+
3378+
fastify.get('/', (request, reply) => {
3379+
reply.send(testData)
3380+
})
3381+
3382+
const response = await fastify.inject({
3383+
url: '/',
3384+
method: 'GET',
3385+
headers: {
3386+
'accept-encoding': 'gzip, deflate, br'
3387+
}
3388+
})
3389+
3390+
t.assert.equal(response.statusCode, 200)
3391+
// No compression should happen since global is false
3392+
t.assert.equal(response.headers['content-encoding'], undefined)
3393+
})
3394+
3395+
test('It should demonstrate globalCompression overrides global setting', async (t) => {
3396+
t.plan(2)
3397+
3398+
// Test that globalCompression: true overrides global: false
3399+
const fastify = Fastify()
3400+
await fastify.register(compressPlugin, {
3401+
global: false,
3402+
globalCompression: true,
3403+
threshold: 0
3404+
})
3405+
3406+
fastify.get('/', (request, reply) => {
3407+
reply.send({ message: 'globalCompression overrides global' })
3408+
})
3409+
3410+
const response = await fastify.inject({
3411+
url: '/',
3412+
method: 'GET',
3413+
headers: { 'accept-encoding': 'gzip' }
3414+
})
3415+
3416+
t.assert.equal(response.statusCode, 200)
3417+
t.assert.equal(response.headers['content-encoding'], 'gzip') // Compression enabled despite global: false
3418+
})
3419+
3420+
test('It should demonstrate globalDecompression controls decompression independently', async (t) => {
3421+
t.plan(2)
3422+
3423+
// Test globalDecompression: false disables decompression even with global: true
3424+
const fastify = Fastify()
3425+
await fastify.register(compressPlugin, {
3426+
global: true,
3427+
globalDecompression: false
3428+
})
3429+
3430+
const compressedPayload = zlib.gzipSync(JSON.stringify({ test: 'decompression' }))
3431+
3432+
fastify.post('/', (request, reply) => {
3433+
reply.send({ received: request.body })
3434+
})
3435+
3436+
const response = await fastify.inject({
3437+
url: '/',
3438+
method: 'POST',
3439+
headers: {
3440+
'content-encoding': 'gzip',
3441+
'content-type': 'application/json'
3442+
},
3443+
payload: compressedPayload
3444+
})
3445+
3446+
// Should get 400 error since decompression is disabled and compressed data can't be parsed
3447+
t.assert.equal(response.statusCode, 400)
3448+
t.assert.ok(response.body.includes('Content-Length') || response.body.includes('Bad Request'))
3449+
})

0 commit comments

Comments
 (0)