Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

protocol-buffers

Package Overview
Dependencies
Maintainers
1
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

protocol-buffers

encode/decode protocol buffers in node with stream support

  • 0.4.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
34K
increased by11.7%
Maintainers
1
Weekly downloads
 
Created
Source

protocol-buffers

Encode/decode protocol buffers in node with stream support

npm install protocol-buffers

build status dat

Usage

You should pass raw a .proto file as a buffer (or you can specify the schema as json). 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.createDecodeStream();

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

In addition to passing in a raw proto file you can also specify the schema as JSON

var schema = protobuf([{
	name: 'num',
	type: 'float'
}, {
	name: 'payload',
	type: 'bytes'
}, {
	name: 'some_nested_thing',
	type: 'object',
	fields: [{
		name: 'another_prop',
		type: 'string'
	}]
}]);

License

MIT

FAQs

Package last updated on 17 May 2014

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