Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite body #265

Open
advany opened this issue Aug 8, 2022 · 4 comments
Open

Rewrite body #265

advany opened this issue Aug 8, 2022 · 4 comments

Comments

@advany
Copy link

advany commented Aug 8, 2022

I use @fastify/http-proxy and it works wonderfully (and faster then http-proxy-middleware). The only thing I can't figure out how can I rewrite the body of a response? I need to change the html before is send back to the client.

@mcollina
Copy link
Member

mcollina commented Aug 9, 2022

use onResponse: https://github.com/fastify/fastify-reply-from#onresponserequest-reply-res

@advany
Copy link
Author

advany commented Aug 10, 2022

For anyone else having this problem. I created this function and it solved it:

function change_body(request, reply, res, call_back) {
  let headers = reply.getHeaders();
  let contenttype = headers['content-type'];
  let contentencoding = headers['content-encoding'];

  if(contenttype.includes('text')) {
    let decompress;

    switch (contentencoding) {
      case 'gzip':
        decompress = zlib.createGunzip();
        break;
      case 'br':
        decompress = zlib.createBrotliDecompress();
        break;
      case 'deflate':
        decompress = zlib.createInflate();
        break;
      default:
        break;
    }

    if(decompress) {
      res.pipe(decompress);
      let buffer = Buffer.from('', 'utf8');

      decompress.on('data', (chunk) => (buffer = Buffer.concat([buffer, chunk])));

      decompress.on('end', async () => {
        let body = buffer.toString();
        if(body) {
          reply.removeHeader('content-length');
          body = call_back.call(this, body);
        }
        reply.compress(body);
      });
    } else reply.send(res);
  } else reply.send(res);
}

You will also need:

  await server.register(
    import('@fastify/compress'),
    { global: false }
  )

And call it using:

  onResponse: async (request, reply, res) => {
        change_body(request, reply, res, function(body) {
          return body.replace('Search This', 'Replace with');
        })
      },

@advany
Copy link
Author

advany commented Aug 10, 2022

@mcollina would be great if this could be added to fastify like a hook or something. something like rewriteBody so please who need to change the body can easily do it

@advany advany closed this as completed Aug 10, 2022
@Eomm Eomm transferred this issue from fastify/help Aug 10, 2022
@Eomm
Copy link
Member

Eomm commented Aug 10, 2022

Reopened as a feature request

I think we could provide a feature for it, but as your example shows, there are stuff like compression and content type to deal with.

I think a good tread off between simplicity and usability could be:

reply.from('/', {
  onPayload(bufferPayload, reply) {
     // manage the buffer payload
     reply.send({ whatever })
  }
})

I'm assuming users do not want deal with stream here

@Eomm Eomm reopened this Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants