
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
protocol-buffers
Advanced tools
Encode/decode protocol buffers in node with stream support
npm install protocol-buffers
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'
}]
}]);
MIT
FAQs
Protocol Buffers for Node.js
The npm package protocol-buffers receives a total of 35,821 weekly downloads. As such, protocol-buffers popularity was classified as popular.
We found that protocol-buffers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.