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

raw body is invoked too late? #15

Open
phhoef opened this issue Jan 8, 2022 · 4 comments
Open

raw body is invoked too late? #15

phhoef opened this issue Jan 8, 2022 · 4 comments

Comments

@phhoef
Copy link

phhoef commented Jan 8, 2022

Hi,

I've a strange problem.
I've built a small web app with NestJS and Fastify.
The app is receiving rest calls from an 3rd party app.
That's why I do not have influence on the requests. I've to take handle the requests as they come.

There're several requests, that store documents in an s3 storage.
The files can be sent as body of a PUT request.

I've a controller, that handles the request and stores the document.
For some requests, the raw body is called AFTER the controller has been invoked.
That said, the rawBody property is undefined in the controller.

I've attached the debugger in the preparsingRawBody and my controller.

  app.register(fastifyRawBody, {
    field: 'rawBody',
    global: true,
    encoding: false,
    runFirst: true,
  });

I do have attached an additional contentTypeParser as catch-all parser.
Could that be a problem?

  fastifyAdapter.getInstance().addContentTypeParser('*', (req, body, done) => {
    done(null, body);
  });
@Eomm
Copy link
Owner

Eomm commented Jan 8, 2022

Could that be a problem?

Yes, it could be: why did you add an empty parser?
Just one is executed by fastify, so I think sometimes it execute this plugin's parser and sometime it executes your parser instead

@phhoef
Copy link
Author

phhoef commented Jan 8, 2022

The 3rd party app does not add the content-type header. If i do not add the wildcard parser, fastify would deny the request due to unsupported media type.

I might have found a workaround, now. I read the request.raw stream into a buffer and do not use the fastify-raw-body plugin.
Is that a bad idea from a performance perspective?

@Eomm
Copy link
Owner

Eomm commented Jan 8, 2022

Is that a bad idea from a performance perspective?

No, from the performance side.
I would check the error case (such as client disconnected): if you don't handle it you may cause some memory leak

@phhoef
Copy link
Author

phhoef commented Jan 8, 2022

Could you give me a hint, how to properly handle the error case?
I am quite new to the NodeJS development.

Why would a disconnect cause a memory leak?
I‘ve created a async method reading the stream like this:

var bufs = [];
stdout.on('data', function(d){ bufs.push(d); });
stdout.on('end', function(){
  var buf = Buffer.concat(bufs);
})

The stream and the buffer are local variables, and should be freed by the GC when the method finished, right?

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

2 participants