Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protobuf wrong behavior on bytes #1482

Open
papirosko opened this issue Aug 25, 2020 · 2 comments
Open

protobuf wrong behavior on bytes #1482

papirosko opened this issue Aug 25, 2020 · 2 comments

Comments

@papirosko
Copy link

papirosko commented Aug 25, 2020

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

@papirosko
Copy link
Author

workaround for enums (they can be deserialized as string and as number in consequent requests (really WTF???)). I really hate this solution.

const value = fixEnumNumberValue<package.SomeEnumProto>(res.enumField, package.SomeEnumProto);


function fixEnumNumberValue<T>(v: string | number, en: any): T {
    const raw = v as any;
    if (Number.isInteger(raw)) {
        return raw as T;
    } else {
        return en[raw.toString()] as any as T;
    }

}

@papirosko
Copy link
Author

examining runtime object with bytes field, it actually is not a buffer, but some weird json presentation of a buffer....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant