Socket
Socket
Sign inDemoInstall

protons

Package Overview
Dependencies
Maintainers
5
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

protons

Protobuf to ts transpiler


Version published
Weekly downloads
16K
decreased by-2.86%
Maintainers
5
Weekly downloads
 
Created
Source

protons

ipfs.tech Discuss codecov CI

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

  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

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.

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.

FAQs

Package last updated on 08 Feb 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc