fastify-multipart icon indicating copy to clipboard operation
fastify-multipart copied to clipboard

Support firebase

Open ThomasKientz opened this issue 1 year ago • 6 comments

Currently we can't use this plugin with serverless GCP (or Firebase).

As explained here https://github.com/fastify/fastify/issues/946#issuecomment-766319521 and in the doc for firebase, we need to use req.rawBody in order to parse files from multipart.

ThomasKientz avatar Oct 08 '24 09:10 ThomasKientz

the tests are not happy

Uzlopak avatar Oct 08 '24 09:10 Uzlopak

the tests are not happy

@Uzlopak don't really understand why it's failing, looks good but return exit code 1

ThomasKientz avatar Oct 08 '24 12:10 ThomasKientz

The test coverage is Not 100%

Uzlopak avatar Oct 08 '24 12:10 Uzlopak

Well the problem is, that gcf is a glorified express router. So they preparse the body so that you dont have to. You get the body stream in rawBody. So maybe we need, like you suggest, a plugin which basically removes the default content type parsers and just assigns rawbody to body?!

Uzlopak avatar Oct 10 '24 08:10 Uzlopak

What's comes my mind is something like?

function gcp(fastify) {
  // add pass through content-type parser
  fastify.addContentTypeParser('application/json', {}, (req, body, done) => {
    done(null, body.body);
  });
  
  // add stream transform for multipart when neccessary
  if (fastify.hasContentTypeParser('multipart/form-data')) {
    fastify.addHook('preParsing', function(request, reply, payload, done) {
      if(request.headers['content-type'].startsWith('multipart/form-data')) {
        // we override the `.pipe` method and return rawBody as new payload
        const pipe = request.raw.pipe
        request.raw.pipe = function(stream) {
          Readable.from([rawBody]).pipe(stream)
        }
        request.raw.originalPipe = pipe
        done(null, payload.rawBody)
      } else {
        done(null, payload)
      }
    })
  }
}

climba03003 avatar Oct 10 '24 09:10 climba03003

Should I pursue the current implementation of this pull request and add test coverage? @Uzlopak

ThomasKientz avatar Oct 14 '24 08:10 ThomasKientz