Skip to content

protobuf wrong behavior on bytes #1482

@papirosko

Description

@papirosko

protobuf.js version: 6.10.1

when i generare js from this proto:

message UUIDProto  {
    bytes value = 1;
}

with commands:

pbjs -t static-module --no-convert  -w commonjs -o ...
pbts -o ...

i get the following js:

...
                UUIDProto.prototype.value = $util.newBuffer([]);
...

and ts:

...
            interface IUUIDProto {

                /** UUIDProto value */
                value?: (Uint8Array|null);
            }
...

as you can see the result type is Uint8Array, which can be passed to, e.g. uuid parser.

After i execute grpc service method, as described in docs:

        const Client = grpc.makeGenericClientConstructor({}, null);
        const client = new Client(
            addr,
            grpc.credentials.createInsecure()
        );

        const rpcImpl = function(method, requestData, callback) {
            client.makeUnaryRequest(
                `/package.${this.constructor.name}/${method.name}`,
                arg => arg,
                arg => arg,
                requestData,
                {
                    deadline: new Date().getTime() + 3000
                },
                callback
            )
        };

Under node the real value of uuid proto after deserialization is Buffer, and it can't be processed by uuid parser:

{"value":{"type":"Buffer","data":[54,158,93,160,17,34,66,37,168,136,1,115,145,20,49,21]}}

The runtime object should have the same type as it has in .d.ts file. If you want optimization for node (such as base64, buffer or array), it should be applied to generated sources, not to the runtime objects.

BTW the same is for enums. I was shocked, then i saw enum value as string, which should be number

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions