Package Exports
- protons
- protons/status
Readme
protons
Protobuf to ts transpiler
Table of contents
Install
$ npm i protons
protons
contains the code to compile .proto
files to .ts
files and protons-runtime
contains the code to do serialization/deserialization to Uint8Array
s during application execution.
$ npm install --save-dev protons
$ npm install --save protons-runtime
Usage
First generate your .ts
files:
$ protons ./path/to/foo.proto ./path/to/output.ts
Then run tsc over them as normal:
$ tsc
In your code import the generated classes and use them to transform to/from bytes:
import { Foo } from './foo.js'
const foo = {
message: 'hello world'
}
const encoded = Foo.encode(foo)
const decoded = Foo.decode(encoded)
console.info(decoded.message)
// 'hello world'
Differences from protobuf.js
This module uses the internal reader/writer from protobuf.js
as it is highly optimised 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
BigInt
s and notLong
s (e.g.int64
,uint64
,sint64
etc) - Unset
optional
fields are set on the deserialized object forms asundefined
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
Missing features
Some features are missing OneOf
, Map
s, etc due to them not being needed so far in ipfs/libp2p. If these features are important to you, please open PRs implementing them along with tests comparing the generated bytes to protobuf.js
and pbjs
.
Contribute
Feel free to join in. All welcome. Open an issue!
This repository falls under the IPFS Code of Conduct.
License
Licensed under either of
- Apache 2.0, (LICENSE-APACHE / http://www.apache.org/licenses/LICENSE-2.0)
- MIT (LICENSE-MIT / http://opensource.org/licenses/MIT)
Contribute
Feel free to join in. All welcome. Open an issue!
This repository falls under the IPFS Code of Conduct.