protobuf-ts icon indicating copy to clipboard operation
protobuf-ts copied to clipboard

Serializing stream to binary.

Open singerxt opened this issue 2 years ago • 2 comments

Hello!

I have a React application that uses msw.js for mocking requests. I'm able to to knock simple responses by serializing proto message. However things are getting complicating when the response is stream and I'm unable to find any method which would be helpful to serialize array of objects to binary which I could use as response body.

Do you know how I can serialize objects to stream?

For reference proto file looks like this.

syntax = "proto3"

message Point {
  int32 latitude = 1;
  int32 longitude = 2;
}

service RouteGuide {
   rpc streamPoints() returns (stream Point) {}
}

singerxt avatar Oct 24 '23 18:10 singerxt

I assume you're using gRPC-web? It encodes messages and final trailers in the response body with a simple framing described in the spec.

protobuf-ts implements reading messages and a final trailer from a response body (here), but it only implements encoding a single message for a request body (here), because that's the only required case for clients.

To mock a gRPC-web response with MSW, a couple of parts are missing:

  • encoding trailers following the spec, with at least grpc-status and grpc-message fields
  • encoding 0 or more messages to support unary and server-streaming RPCs
  • conditionally base64 encoding the body based on the request content-type (only for the gRPC-web text encoding)

I do not have the spare time to implement those parts, but with a bit of patience, it's definitely doable 🙂

timostamm avatar Oct 24 '23 23:10 timostamm

@timostamm Thank you!

Do you think that kind feature should part of your library? If so I'm more then happy to work on it.

singerxt avatar Oct 25 '23 02:10 singerxt