Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
5.2.0
Plugin version
8.0.3
Node.js version
20.8.1
Operating system
macOS
Operating system version:
sonoma(14.1.1)
Description
After upgrading Fastify from v3 to v5, we encountered an issue where the @fastify/compress plugin prematurely closes the response when handling large payloads . This results in a "premature close" error before the response is fully sent to the client.
error:
{"type":"Error","message":"premature close","stack":"Error: premature close\n at onclosenexttick (/Users/4911462/Documents/testBmService/baymax-service/node_modules/end-of-stream/index.js:54:86)\n at process.processTicksAndRejections (node:internal/process/task_queues:77:11)"},"msg":"premature close"}
Actual Behavior
The response is never fully sent, and Fastify logs a "premature close" error.
Additional Context
Fastify version: 5.2.0
@fastify/compress version: 8.0.2/8.0.1
Node.js version: 20.x
Deployed environment: Kubernetes pods (though reproducible locally)
### Link to code that reproduces the bug
Ex: myHandler.js
const myHandler = {
async getAllLiveData(request, response){
...rest of the code goes here
const res = await serviceLayer.getDataMethod(this, );
response.header('Cache-Control', `s-maxage=------`);
response.type('application/json');
if(res){
res.config = getConfig();
res.config.enableSDKLogs = shouldAllowSDKLogging(vault.conf, requestDetails)
setNodeCacheIfEnabled(this, res, request, 'v2Cache')
response.compress(res)
} else {
response.status(204);
}
},
compress plugin initialization
const fp = require('fastify-plugin');
const compress = require('@fastify/compress');
module.exports = fp((fastify) => {
fastify.register(compress, { encodings: ['gzip'], global: false });
});
Potential Workarounds
We temporarily removed @fastify/compress and directly returned the response. However, this is not ideal for production as compression is essential for reducing payload sizes.
Open Questions
Is this a known issue in Fastify v5?
Are there alternative configurations for handling large payloads without hitting this limitation?
Should we handle compression manually instead of using @fastify/compress?
Link to code that reproduces the bug
None
Expected Behavior
Fastify should successfully compress and send large responses without prematurely closing the connection.