Eomm / fastify-raw-body

Request raw body
MIT License
44 stars 10 forks source link

raw body is invoked too late? #15

Open phhoef opened 2 years ago

phhoef commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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?