It transpiles code to TypeScript and supports BigInts for 64 bit types.
The protons module contains the code to compile .proto files to .ts files and protons-runtime contains the code to do serialization/deserialization to Uint8Arrays during application execution.
Please ensure you declare them as the correct type of dependencies:
This module uses the internal reader/writer from protobuf.js as it is highly optimized and there's no point reinventing the wheel.
It does have one or two differences:
Supports proto3 semantics only
All 64 bit values are represented as BigInts and not Longs (e.g. int64, uint64, sint64 etc)
Unset optional fields are set on the deserialized object forms as undefined instead of the default values
singular fields set to default values are not serialized and are set to default values when deserialized if not set - protobuf.js diverges from the language guide around this feature
constmessage = MyMessage.decode(buf, { limits: { messages:5// max 5x SubMessages messages$: { repeatedField:5// no SubMessage can have more than 5 repeatedField entries } } })
Limiting repeating fields of map entries at runtime
Repeating fields in map entries can be limited by appending $value to the field name in the runtime limit options:
constmessage = MyMessage.decode(buf, { limits: { messages:5// max 5x SubMessages in the map messages$value: { repeatedField:5// no SubMessage in the map can have more than 5 repeatedField entries } } })
Overriding 64 bit types
By default 64 bit types are implemented as BigInts.
Sometimes this is undesirable due to performance issues or code legibility.
It's possible to override the JavaScript type 64 bit fields will deserialize
to, including repeated fields and map key/values:
protonsis a high performance implementation of Protocol Buffers v3.It transpiles code to TypeScript and supports BigInts for 64 bit types.
The
protonsmodule contains the code to compile.protofiles to.tsfiles andprotons-runtimecontains the code to do serialization/deserialization toUint8Arrays during application execution.Please ensure you declare them as the correct type of dependencies:
Usage
First generate your
.tsfiles:Then run tsc over them as normal:
In your code import the generated classes and use them to transform to/from bytes:
Differences from protobuf.js
This module uses the internal reader/writer from
protobuf.jsas it is highly optimized and there's no point reinventing the wheel.It does have one or two differences:
proto3semantics onlyBigInts and notLongs (e.g.int64,uint64,sint64etc)optionalfields are set on the deserialized object forms asundefinedinstead of the default valuessingularfields set to default values are not serialized and are set to default values when deserialized if not set - protobuf.js diverges from the language guide around this featuremapfields can have keys of any type - protobufs.js only supports stringsmapfields are deserialized as ES6Maps - protobuf.js usesObjectsExtra features
Limiting the size of repeated/map elements
To protect decoders from malicious payloads, it's possible to limit the maximum size of repeated/map elements.
You can either do this at compile time by using the protons.options extension:
Or at runtime by passing objects to the
.decodefunction of your message:Limiting repeating fields of nested messages at runtime
Sub messages with repeating elements can be limited in a similar way:
Limiting repeating fields of repeating messages at runtime
Sub messages defined in repeating elements can be limited by appending
$to the field name in the runtime limit options:Limiting repeating fields of map entries at runtime
Repeating fields in map entries can be limited by appending
$valueto the field name in the runtime limit options:Overriding 64 bit types
By default 64 bit types are implemented as BigInts.
Sometimes this is undesirable due to performance issues or code legibility.
It's possible to override the JavaScript type 64 bit fields will deserialize to, including repeated fields and map key/values:
Missing features
Some features may be missing due to them not being needed in ipfs/libp2p so far.
If these features are important to you, please open PRs implementing them along with tests comparing the generated bytes to
protobuf.jsandpbjs.