JSPM

  • Created
  • Published
  • Downloads 66696
  • Score
    100M100P100Q195304F

encode/decode protocol buffers in node with stream support

Package Exports

  • protocol-buffers

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (protocol-buffers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

protocol-buffers

Encode/decode protocol buffers in node with stream support

npm install protocol-buffers

build status dat

Usage

You should pass a parsed .proto file (using proto2json) or a raw buffer/string consisting of the proto messages. Per default the first message is used as the main one. Add an optional second argument to set it to a specific message

Assuming the following test.proto file exists

message Test {
  required float num  = 1;
  required string payload = 2;
}
var protobuf = require('protocol-buffers');
var schema = protobuf(fs.readFileSync('test.proto'));

var buf = schema.encode({
    num: 42,
    payload: 'hello world'
});

console.log(buf); // should print a buffer

var obj = schema.decode(buf);

console.log(obj); // should print an object similar to above

You can also stream the encoding/decodings

var encoder = schema.createEncodeStream();

encoder.write({
    num: 42,
    payload: 'hello world'
});

encoder.write({
    num: 43,
    payload: 'hello another world'
});

...

encoder.on('data', function(buf) {
    // buf is a buffer with the encoded object
});

And similarly if you wanted to decode

var decoder = schema.createEncodeStream();

decoder.write(buf);
decoder.on('data', function(obj) {
    // obj is an unpacked object
});

Note that each buffer passed to decoder.write should contain a full protobuf object so make sure you do some sort of delimiting/length-prefixing first

License

MIT