grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.56k stars 762 forks source link

Cannot seem to use deserializeBinary manually #1201

Closed BBoldenow closed 2 years ago

BBoldenow commented 2 years ago

I'm having an issue in gRPC that's been driving me crazy. I have a .NET gRPC service that my ReactJS client is connecting to - this works fine. I'm subscribing to my stream, and I get data over the stream as expected. I'm having an issue deserializing "bytes" or "google.protobuf.Any" types that exist in my messages. The one I'm receiving looks like this:

message PointNotification {
  Point.KindsOfPointNotification Kind = 1;
  bytes NotificationData = 2;
}

In this case the bytes represent the following proto message, but it could be a variety of other messages:

message NotificationPointRemoved
{
    PointEntity Point = 1;
}

message PointEntity {
    Core.Types.EntityId Identifier = 1;    
    Core.Types.EntityLocation Location = 2;
    google.protobuf.Timestamp LastModifiedTime = 3;
}

Inside of the ReactJS client I am doing the following:

    useEffect(() => {
        console.log("Subscribing to point stream");
        var pointStream = client.subscribeToNotificationStream(new Empty(), {});
        pointStream.on("data", (response) => {
            switch (response.getKind())
            {
                case KindsOfPointNotification.KINDS_OF_POINT_NOTIFICATION_POINT_ADDED_OR_UPDATED:
                    console.log(NotificationPointAddedOrUpdated.deserializeBinary(response.getNotificationdata()));
                    break;
                case KindsOfPointNotification.KINDS_OF_POINT_NOTIFICATION_POINT_REMOVED:
                    console.log("Point removed");
                    break;
            }
        });

When I call deserializeBinary I get an uncaught exception in the browser that says:

AssertionError: Assertion failed
    at new __webpack_modules__../node_modules/google-protobuf/google-protobuf.js.goog.asserts.AssertionError (http://localhost:3000/static/js/bundle.js:10922:20)
    at Object.__webpack_modules__../node_modules/google-protobuf/google-protobuf.js.goog.asserts.doAssertFailure_ (http://localhost:3000/static/js/bundle.js:10951:7)
    at Object.__webpack_modules__../node_modules/google-protobuf/google-protobuf.js.goog.asserts.assert (http://localhost:3000/static/js/bundle.js:10960:53)
    at __webpack_modules__../node_modules/google-protobuf/google-protobuf.js.jspb.BinaryDecoder.advance (http://localhost:3000/static/js/bundle.js:14845:16)
    at __webpack_modules__../node_modules/google-protobuf/google-protobuf.js.jspb.BinaryReader.skipFixed64Field (http://localhost:3000/static/js/bundle.js:15240:161)
    at __webpack_modules__../node_modules/google-protobuf/google-protobuf.js.jspb.BinaryReader.skipField (http://localhost:3000/static/js/bundle.js:15269:12)
    at Function.proto.Point.NotificationPointAddedOrUpdated.deserializeBinaryFromReader (http://localhost:3000/static/js/bundle.js:6568:16)
    at Function.proto.Point.NotificationPointAddedOrUpdated.deserializeBinary (http://localhost:3000/static/js/bundle.js:6541:59)
    at Array.<anonymous> (http://localhost:3000/static/js/bundle.js:234:107)
    at qc.<anonymous> (http://localhost:3000/static/js/bundle.js:20338:58)

I've also tried this with the google.protobuf.Any type with similar, but slightly different results that you can read about here:

https://stackoverflow.com/questions/70988812/grpc-any-type-uncaught-typeerror-deserialize-is-not-a-function

This seems like an actual issue in grpc-web to me so I thought it prudent to bring to people's attention here. Does anyone know how to resolve this issue?

BBoldenow commented 2 years ago

The following issue led me to the solution: protocolbuffers/protobuf-javascript#57

The previous dev was not encoding data in base64, and that was causing deserialize issues in JavaScript.