stephenh/ts-proto

Support grpc-web services

stephenh opened this issue · 3 comments

Currently we only support twirp RPC services.

(This was hence been implemented for grpcjs and grpc-web.)

Added "help wanted" label b/c I don't personally use grpc services right now.

I managed to make this work with grpc-web with some minor changes to methodInfo in the generated grpc-web code.

The trick is to turn this piece of the generated code:

methodInfoEcho = new grpcWeb.AbstractClientBase.MethodInfo(
    EchoResponse,
    (request: EchoRequest) => {
      return request.serializeBinary();
    },
    EchoResponse.deserializeBinary
  );

into that:

methodInfoEcho = new grpcWeb.AbstractClientBase.MethodInfo(
    EchoResponse,
    (request: EchoRequest) => {
        return EchoRequest.encode(request).finish();
    },
    (response: Uint8Array) => {
      return EchoResponse.decode(response);
    },
);

Since this library is using interfaces, there's no clean way I could imagine to make this work by just modifying ts-proto because I can't implement (de-)serialize on the interfaces. My current plan is to port some of the grpc-web codegen over to this repo so the ts-proto gen can also generate grpc-web compatible services.

@stephenh what are your thoughts on this? If you'd be ok with having some grpc service generation code (togglable between twirp and grpc) I'd try to contribute that once I have some time to spare.

@hendrikhofstadt sorry for the really late follow up here; I hadn't / generally don't use grpc-web, so didn't have anything really useful to say. :-)

That said, I had a client request grpc-web support, so I just released v1.28.0 that adds a outputClientImpl=grpc-web flag that will a) use the improbable-eng/grpc-web runtime library but b) only use ts-proto's code generation output (i.e. doesn't require anything from the ts-protoc-gen output).

In retrospect, your serializeBinary/deserializeBinary suspicions were exactly right.

Would be great if you could check out the latest version and see if it works for your use case / setup. Would be great to get a 2nd set of eyes and users on it, I've admittedly only done a few trivial unary-only RPC calls so far.

Thanks!