Socket
Socket
Sign inDemoInstall

@dxos/codec-protobuf

Package Overview
Dependencies
Maintainers
8
Versions
2980
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dxos/codec-protobuf

Value encoding for protobuf.


Version published
Weekly downloads
5.3K
increased by62.43%
Maintainers
8
Weekly downloads
 
Created
Source

Codec Protobuf

Codec for protobuf to use in libraries that follows the valueEncoding API of leveldb, like hypercore.

Build Status Coverage Status Greenkeeper badge js-semistandard-style standard-readme compliant

This module use a custom implementation of protobuf.js to support decode Buffers in the browser.

Install

$ npm install @dxos/codec-protobuf

Usage

syntax = "proto3";

message Task {
  string id = 1;
  string title = 2;
}

Using .proto files

import protobufjs from 'protobufjs';
import { Codec } from '@dxos/codec-protobuf';

const codec = new Codec('Task');

(async () => {
  // Load from a protobufjs root.
  codec
    .addJson(await protobufjs.load('schema.proto'))
    .build();

  const buffer = codec.encode({ id: 'task-0', title: 'test' });

  codec.decode(buffer); // { id: 'task-0', title: 'test' }
})();

Using JSON descriptors

import protobufjs from 'protobufjs';
import { Codec } from '@dxos/codec-protobuf';

const codec = new Codec('Task');

// Load from a JSON compiled proto schema.
codec
  .addJson(require('./schema.json'))
  .build();

const buffer = codec.encode({ id: 'task-0', title: 'test' });

codec.decode(buffer); // { id: 'task-0', title: 'test' }

API

const codec = new Codec(rootTypeUrl, [options])

Create a new CodecProtobuf instance.

The options are:

  • rootTypeUrl: string: Defines the root type message to encode/decode messages. Required.
  • options: Object
    • verify: boolean: Verify the objects before to encode. Default: true.
    • recursive: boolean: Recursively decode the buffer. Default: true.
    • strict: boolean: Throw an exception if the type is not found. Default: true.
codec.addJson(json) -> codec

Add the given JSON schema to the type dictionary.

  • json: Object
codec.getType(type) -> (Type|null)

Get a type from the loaded schemas.

  • type: string: Fully qualified type name.
codec.encode(data) -> Buffer

Encode an object using a specific protobuf type.

  • data: Object
codec.decode(buffer) -> Object
  • buffer: Buffer

Contributing

PRs accepted.

License

GPL-3.0 © dxos

FAQs

Package last updated on 02 Jun 2020

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