Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@wirelineio/codec-protobuf

wirelineio8GPL-3.00.3.2

Value encoding for protobuf.

readme

Wireline Codec Protobuf

CircleCI npm version

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

Requirement

CodecProtobuf only works with protocol-buffers since it's the only module that allows you to encode/decode type bytes to Buffer giving you the benefit of building universal apps working in Node and the Browser. (Most of the current bundle tooling for the Browser implements Buffer).

Install

$ npm install @wirelineio/codec-protobuf protocol-buffers

Usage

syntax = "proto3";

message Task {
  required string id = 1;
  string value = 2;
}
import protobuf from 'protocol-buffers';
import hypercore from 'hypercore';
import codecProtobuf from '@wirelineio/codec-protobuf';

const root = protobuf(fs.readFileSync('schema.proto'))

const codec = codecProtobuf(root);

const obj = { type: 'Task', message: { id: 'task-0', value: 'test' } };

const buffer = codec.encode(obj);

codec.decode(buffer); // { type: 'Task', message: { id: 'task-0', value: 'test' } }

// It's compatible with the valueEncoding option of hypercore
const feed = hypercore('./log', { valueEncoding: codec });

feed.append(obj, () => {
  feed.head(console.log) // { type: 'Task', message: { id: 'task-0', value: 'test' } }
});

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

Added

  • Initial packages.