@wirelineio/codec-protobuf
v0.3.2
Published
Value encoding for protobuf.
Downloads
51
Keywords
Readme
Wireline Codec Protobuf
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' } }
});