7.5.0 • Published 3 months ago

protons v7.5.0

Weekly downloads
28,219
License
Apache-2.0 OR MIT
Repository
github
Last release
3 months ago

protons

ipfs.tech Discuss codecov CI

Protobuf to ts transpiler

About

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 Uint8Arrays during application execution.

Please ensure you declare them as the correct type of dependencies:

$ 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:

  1. Supports proto3 semantics only
  2. All 64 bit values are represented as BigInts and not Longs (e.g. int64, uint64, sint64 etc)
  3. Unset optional fields are set on the deserialized object forms as undefined instead of the default values
  4. 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
  5. map fields can have keys of any type - protobufs.js only supports strings
  6. map fields are deserialized as ES6 Maps - protobuf.js uses Objects

Extra 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:

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
  }
})

Limiting repeating fields of nested messages at runtime

Sub messages with repeating elements can be limited in a similar way:

message SubMessage {
  repeated uint32 repeatedField = 1;
}

message MyMessage {
  SubMessage message = 1;
}
const message = MyMessage.decode(buf, {
  limits: {
    messages: {
      repeatedField: 5 // the SubMessage can not have more than 5 repeatedField entries
    }
  }
})

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:

message SubMessage {
 repeated uint32 repeatedField = 1;
}

message MyMessage {
  repeated SubMessage messages = 1;
}
const message = 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:

message SubMessage {
 repeated uint32 repeatedField = 1;
}

message MyMessage {
  map<string, SubMessage> messages = 1;
}
const message = 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:

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

Missing features

Some features are missing OneOfs, 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.

Install

$ npm i protons

API Docs

License

Licensed under either of

Contribute

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.

npm.io

tlg-manager@network-stackify/stack@everything-registry/sub-chunk-2485pulsarcastrepudiandaedoloresxs-js-ipfs-unixfs@jimpick/libp2p-floodsub@infinitebrahmanuniverse/nolb-protoss-ipfs-bitswapss-ipfs-bitswap-test@spacehq/mailbox@spacehq/users@spacehq/utilszeronet-nodetrustzeronet-protocolzeronet-relay@xstorage/xs-js-ipld-dag-pb@xstorage/xs-js-libp2p-circuit@xstorage/xs-js-libp2p-crypto@xstorage/xs-js-libp2p-identify@xstorage/xs-js-libp2p-record@xstorage/xs-js-libp2p-seciohedera-connecteth-libp2p-kad-dhteth-libp2p-secioeth-libp2p-circuiteth-libp2p-identifyjs-swrm-client-testfloodsub-streamlibp2p-exchange-directlibp2p-identifylibp2p-imlibp2p-circuitlibp2p-exchange-rendezvouslibp2p-mesh-rpclibp2p-nodetrustlibp2p-nodetrust-serverlibp2p-tlslibp2p-webrtc-circuitlibp2p-rendezvouslibp2p-seciolibp2p-stardust@digitalnative/p2pdisco-datadisco-dht-datadisco-imagedisco-messagedisco-peer-infodisco-roomdisco-filedisco-foldermyel-clientims-protonsincubedinstaller-test-malware-client-2@compendia/ipfs-bitswap-commonjs@compendia/libp2p-commonjscrdt-event-log-liteknightxv-libp2pipfs-hdipfs-ci-testipfs-vueipld-lfcipld-lfc-txjs-swrm-clientjs-swrm-client-betajs-swrm-client-videoperferendisautem@instadapp/iga-node@instadapp/interop-nodenot-a-thing-test@galtproject/js-ipfs@hashgraph/hashconnectpixiuswap-libs-sdkpixiu-swap-coretlg-manager-test@zippie/ipfs-bitswap@waku/vote-poll-sdk-coreuplexutquos@w3f/libp2p-cryptovitaerecusandaearbase@ark-us/chat-manager@ark-us/libp2p-transport@arve.knudsen/libp2p-circuit@arve.knudsen/libp2p-crypto@arve.knudsen/libp2p-daemon@arve.knudsen/libp2p-identify@arve.knudsen/libp2p-kad-dht@arve.knudsen/libp2p-secio
7.5.0

3 months ago

7.4.0

3 months ago

7.3.4

4 months ago

7.3.3

4 months ago

7.3.2

4 months ago

7.3.1

5 months ago

7.3.0

6 months ago

7.2.1

6 months ago

7.2.0

6 months ago

7.1.0

6 months ago

7.0.7

7 months ago

7.0.6

7 months ago

7.0.4

10 months ago

7.0.3

10 months ago

7.0.5

9 months ago

6.1.0

1 year ago

6.1.2

1 year ago

6.1.1

1 year ago

6.0.2

1 year ago

6.1.3

1 year ago

7.0.0

1 year ago

7.0.2

1 year ago

7.0.1

1 year ago

6.0.1

2 years ago

6.0.0

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

3.0.5

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.0.2

4 years ago

1.1.0

4 years ago

1.0.1

6 years ago

1.0.0

7 years ago