Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Protobuf to ts transpiler
protons
is a high performance implementation of Protocol Buffers v3.
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 Uint8Array
s during application execution.
Please ensure you declare them as the correct type of dependencies:
$ npm install --save-dev protons
$ npm install --save protons-runtime
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'
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:
proto3
semantics onlyBigInt
s and not Long
s (e.g. int64
, uint64
, sint64
etc)optional
fields are set on the deserialized object forms as undefined
instead of the default valuessingular
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 featuremap
fields can have keys of any type - protobufs.js only supports stringsmap
fields are deserialized as ES6 Map
s - protobuf.js uses Object
sTo 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:
message MyMessage {
// repeatedField cannot have more than 10 entries
repeated uint32 repeatedField = 1 [(protons.options).limit = 10];
// stringMap cannot have more than 10 keys
map<string, string> stringMap = 2 [(protons.options).limit = 10];
}
Or at runtime by passing objects to the .decode
function of your message:
const message = MyMessage.decode(buf, {
limits: {
repeatedField: 10,
stringMap: 10
}
})
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:
message MyMessage {
repeated int64 bigintField = 1;
repeated int64 numberField = 2 [jstype = JS_NUMBER];
repeated int64 stringField = 3 [jstype = JS_STRING];
}
const message = MyMessage.decode(buf)
console.info(typeof message.bigintField) // bigint
console.info(typeof message.numberField) // number
console.info(typeof message.stringField) // string
Some features are missing OneOf
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
.
$ npm i protons
Licensed under either of
Contributions welcome! Please check out the issues.
Also see our contributing document for more information on how we work, and about contributing in general.
Please be aware that all interactions related to this repo are subject to the IPFS Code of Conduct.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
FAQs
Protobuf to ts transpiler
The npm package protons receives a total of 13,636 weekly downloads. As such, protons popularity was classified as popular.
We found that protons demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.