Skip to content

Commit 1d08b4c

Browse files
authored
fix: ensure that compress stream stays open (#375)
1 parent 05588b9 commit 1d08b4c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ function compress (params) {
447447

448448
stream = zipStream(params.compressStream, encoding)
449449
pump(payload, stream, onEnd.bind(this))
450-
this.send(stream)
450+
return this.send(stream)
451451
}
452452
}
453453

test/regression/issue-350.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict'
2+
3+
const { test } = require('node:test')
4+
const zlib = require('node:zlib')
5+
const Fastify = require('fastify')
6+
const fastifyCompress = require('../..')
7+
8+
const largeJsonString = JSON.stringify({
9+
text: 'Lorem ipsum dolor sit amet. '.repeat(2048),
10+
})
11+
12+
async function routes (fastify) {
13+
fastify.route({
14+
method: 'GET',
15+
url: '/api/compress-test',
16+
handler: async function (_request, reply) {
17+
reply.type('application/json')
18+
return reply.compress(largeJsonString)
19+
}
20+
})
21+
}
22+
23+
test('should compress large payload without premature close', async (t) => {
24+
const fastify = Fastify()
25+
await fastify.register(fastifyCompress, { encodings: ['gzip'], global: true })
26+
await fastify.register(routes)
27+
28+
const response = await fastify.inject({
29+
url: '/api/compress-test',
30+
method: 'GET',
31+
headers: { 'accept-encoding': 'gzip' }
32+
})
33+
34+
t.assert.equal(response.statusCode, 200)
35+
t.assert.equal(response.headers['content-encoding'], 'gzip')
36+
t.assert.ok(
37+
response.rawPayload.length > 0,
38+
`Expected compressed response body, got ${response.rawPayload.length} bytes`
39+
)
40+
41+
const decompressed = zlib.gunzipSync(response.rawPayload)
42+
t.assert.equal(decompressed.toString('utf-8'), largeJsonString)
43+
})

0 commit comments

Comments
 (0)