fastify/fastify-reply-from

Async onResponse

Open

#380 opened on Sep 9, 2024

 (3 comments) (0 reactions) (0 assignees)JavaScript (97 forks)github user discovery
good first issue

Repository metrics

Stars
 (167 stars)
PR merge metrics
 (Avg merge 1d 21h) (5 merged PRs in 30d)

Description

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Hello! Can someone clarify is it possible or not using async onResponse hook with @fastify/reply-from plugin? Sometimes with async onResponse in my logs I see error:

"stack":"FastifyError: Reply was already sent, did you forget to return reply
 at Reply.send (/node_modules/fastify/lib/reply.js:151:26)
 at /node_modules/@fastify/reply-from/index.js:205:14
 at /node_modules/@fastify/reply-from/index.js:284:9
 at /node_modules/@fastify/reply-from/lib/request.js:177:7
 at RequestHandler.runInAsyncScope (node:async_hooks:203:9)
 at RequestHandler.onHeaders (/node_modules/@fastify/reply-from/node_modules/undici/lib/api/api-request.js:104:14)
 at Request.onHeaders (/node_modules/@fastify/reply-from/node_modules/undici/lib/core/request.js:275:29)
 at Parser.onHeadersComplete (/node_modules/@fastify/reply-from/node_modules/undici/lib/client.js:920:27)
 at wasm_on_headers_complete (/node_modules/@fastify/reply-from/node_modules/undici/lib/client.js:536:30)
 at wasm://wasm/0003626a:wasm-function[11]:0x494
 at wasm://wasm/0003626a:wasm-function[51]:0x1003
 at wasm://wasm/0003626a:wasm-function[68]:0x6e8e
 at wasm://wasm/0003626a:wasm-function[67]:0x1568
 at wasm://wasm/0003626a:wasm-function[21]:0x552
 at Parser.execute (/node_modules/@fastify/reply-from/node_modules/undici/lib/client.js:677:22)
 at Parser.readMore (/node_modules/@fastify/reply-from/node_modules/undici/lib/client.js:646:12)","code":"FST_ERR_REP_ALREADY_SENT","name":"FastifyError","statusCode":500},"msg":"Reply was already sent, did you forget to return reply

My goal is changing a response body:

    // Proxy to another Api
    return reply.from(proxyPath, {
      rewriteRequestHeaders() {
        return proxyHeaders;
      },
      rewriteHeaders(headers) {
        return createProxyHeaders(proxyUrl, headers);
      },
      async onResponse(_req, reply, res) {
        const resBody = await new Promise((resolve, reject) => {
          let buffer = Buffer.from('', 'utf8');
        
          res.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));
        
          res.on('end', async () => {
            resolve(buffer.toString());
          });
        
          res.on('error', (error) => {
            reject(error);
          });
        });

        // do something with resBody

       return reply.send(resBody);
     }

Contributor guide