My goal is to manipulate the response payload after the response already returned to the client, but it looks impossible doing it with fastify + fastify-reply-from without affecting the response time. Here is the only way I could get the response body, and do something with it.
fastify.addHook('onSend', async (req, reply, payload) => {
return new Promise((resolve, reject) => {
const chunks = [];
payload.on('error', reject);
payload.on('data', (chunk) => {
chunks.push(chunk);
});
payload.on('end', () => {
const body = Buffer.concat(chunks);
doSomethingWithBody(body);
resolve(body);
});
});
Are there other ways to do it, without handling manually the stream (payload is "IncomingMessage" instance)?