bufbuild / protobuf-es

Protocol Buffers for ECMAScript. The only JavaScript Protobuf library that is fully-compliant with Protobuf conformance tests.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

question: looking for a way to load a message of unknown type

nohehf opened this issue · comments

Hello, I was wondering if there is any way to load a serialized protobuf message (string or bytes) without knowing its type beforehand. Some sort of mapping from typeName to the actual generated TS type?
Thank you!

Hey @nohehf, the protobuf binary format is not fully self-describing. To deserialize a message, you need to know it's type (i.e. the field numbers and field types).

You don't necessarily need generated code though. For example, you could use a registry to create message types at runtime from a set of descriptors, and use the dynamic message type to parse.

Some sort of mapping from typeName to the actual generated TS type?

Sounds like you may want to take a look at Any. It's a protobuf message that holds an arbitrary protobuf message (serialized), plus a type name. See here for an example with protobuf-es.

Oh, thank you, I didn't know about the registry, it definitely sounds like a solution, I'll look into it!
Maybe Any + reflection could be a solution too?
Thank you for the insight