connectrpc / connect-es

The TypeScript implementation of Connect: Protobuf RPC that works.

Home Page:https://connectrpc.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[connect-fastify] issue running in google cloud / firebase functions

Azuka opened this issue · comments

Is your feature request related to a problem? Please describe.
I've been using connect-fastify with Firebase functions (which are a wrapper around Google Cloud Functions) for a while now. There's a known issue (and workaround) for fastify: https://fastify.dev/docs/v4.15.x/Guides/Serverless/#add-custom-contenttypeparser-to-fastify-instance when working with json request bodies, which I've been solving by just copying the contents of the connect-fastify plugin and modifying the noopContentTypeParser.

Describe the solution you'd like
This is a trivial change to check if the incoming body is a buffer/readable stream.

function noopContentTypeParser(
  _req: unknown,
  _payload: { body?: Buffer|Readable },
  done: (err: null, body?: unknown) => void,
) {
  let {body} = _payload;

  if (body instanceof Buffer) {
    body = Readable.from(body);
  }

  done(null, body);
}

Describe alternatives you've considered
Documenting the issue and leaving the implementation up to users, or allowing users to specify their own noopContentTypeParser implementation.

Additional context
N/A

Hey Azuka, thanks for raising the issue.

When you call done with a Readable, I expect fastify to set this value as the body property on the fastify request object. If I follow correctly, the Readable is passed as a parsedJsonBody: JsonValue to universalRequestFromNodeRequest() here.

I think to properly support this, we have to update the signature of universalRequestFromNodeRequest to explicitly handle this input, so that tests can be added.

I am not sure that we can modify the noop-parser without breaking other use cases. Adding good test coverage is not trivial, unfortunately. Maybe the best solution would be to allow you to bring your own content-type parser for firebase (in combination with updating the function signature).

We are currently switching our tests to https://github.com/connectrpc/conformance. This will possibly give us better options to add test coverage. For now, vendoring the plugin in your project seems like a good workaround.

Thanks @timostamm.

I hadn't used fastify outside of firebase, but I agree about not breaking it for others without thorough tests.

I did go looking in the conformance repo before opening an issue (because I didn't see tests here), but glad to hear that there are plans to cover more use cases there.