Adding { parseAs: 'string } to addContentTypeParser prevents parsing from finishing

27 views Asked by At

I am using Firebase with Node and Fastify. Fastify version 4, Node 18. I have two parsers.

This one works just fine, it completes and forwards the request to my route.

server.addContentTypeParser('application/json', require('./lib/json-parser'))

This one starts but never finishes, and it times out. If I add any parseAs (buffer or string), or if I add the raw-body or fasitfy-raw-body plugin, it times out.

server.addContentTypeParser('application/json', { parseAs: 'buffer' }, function (req, body, done) {
  try {
    const newBody = {
      rawBody: body,
    }
    done(null, newBody)
  } catch (error) {
    error.statusCode = 400
    done(error, undefined)
  }
})

What could be the issue? It's not clear to me what Fastify is doing or what I am doing that would possibly cause this issue.

Update: this also times out.

server.addContentTypeParser('*', function (req, body, done) {
  var data = ''
  payload.on('data', chunk => { data += chunk })
  payload.on('end', () => { 
    done(null, data)
  })
}) 
1

There are 1 answers

0
Matt Hudson On

Apparently the issue is that I was using this with a Firebase function which was was trying to parse the response and causing some sort of conflict. I removed parseAs and I used the firebase raw.rawBody (which in Fastify is the firebase output and rawBody is the unparsed buffer from the firebase function).