Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
pomelo-protobuf
Advanced tools
Protobuf protocol is a high efficient binary protocol for data encode, this module implement the protobuf protocol, and used in [pomelo](https://github.com/NetEase/pomelo) for data transfer. Of course, pomelo-protobuf can also be used independently in other projects. ##Architecture Unlike the google protobuf, we provide a universal encoder and decoder in pomelo-protobuf. We use protos file as meta data to encode/decode messages, so you do not need to add any code to your project, instead , what you need is to add a protos.json (or two for different encoder and decoder messages) files to define the message need to encode by protobuf.The architecture of pomelo-protobuf is as follow:
#Pomelo-protobuf Protobuf protocol is a high efficient binary protocol for data encode, this module implement the protobuf protocol, and used in pomelo for data transfer. Of course, pomelo-protobuf can also be used independently in other projects. ##Architecture Unlike the google protobuf, we provide a universal encoder and decoder in pomelo-protobuf. We use protos file as meta data to encode/decode messages, so you do not need to add any code to your project, instead , what you need is to add a protos.json (or two for different encoder and decoder messages) files to define the message need to encode by protobuf.The architecture of pomelo-protobuf is as follow:
##Usage ###Define protos To use pomelo-protobuf, you need to write a JSON file to define the message format. The syntax of the file is as the same as the .proto file in protobuf, but in JSON format, here is the example protos.json:
{
"onMove" : {
"required uInt32 entityId" : 1,
"message Path": {
"required uInt32 x" : 1,
"required uInt32 y" : 2
},
"repeated Path path" : 2,
"required uInt32 speed" : 3
},
"onAttack" : {
"required uInt32 attacker" : 1,
"required uInt32 target" : 2,
"message AttackResult" : {
"required uInt32 result" : 1,
"required uInt32 damage" : 2,
"optional uInt32 exp" : 3
},
"required AttackResult result" : 3
}
}
Unlike the google protobuf, we write all the protos in the same file, with a unique key to define the message.
To use the protos, we use a parser to parse the protos file into more machine friendly format, which is also a json format, then you can use the result to decode/encode messages.
###RootMessage support you can write rootMessage in protos for global usage
{
"message Path": {
"required double x" : 1,
"required double y" : 2
},
"message Equipment" : {
"required uInt32 entityId" : 1,
"required uInt32 kindId" : 2
},
"onMove" : {
"required uInt32 entityId" : 1,
"repeated Path path" : 2,
"required float speed" : 3
},
"area.playerHandler.enterScene" : {
"message Player" : {
"message Bag" : {
"message Item" : {
"required uInt32 id" : 1,
"optional string type" : 2
},
"repeated Item items" : 1
},
"required uInt32 entityId" : 1,
"required uInt32 kindId" : 2,
"required Bag bag" : 3,
"repeated Equipment equipments" : 4
},
"optional Player curPlayer" : 2
}
}
###Server side and Client side Pomelo-protobuf has server code and client code for js.
On average, the encode/decode speed of Server version is 60% faster than client version, with less memory usage. So we highly recommend that use the server code on Node.JS for better performance.
var key = 'onMove';
var msg = {
entityId : 14,
path : [{x : 128,y : 796},{x : 677,y : 895}],
speed : 160
};
//Require proto buf module
var protobuf = require('protobuf');
//Set encode protos and decode protos
var protos = protobuf.parse(require('./protos.json'));
protobuf.init({encoderProtos:protos, decoderProtos:protos});
//Encode msg to binary Buffer
var buffer = protobuf.encode(key, msg);
//Decode a msg from binary buffer
var decodeMsg = protobuf.decode(key, buffer);
At server side, the encode result will be a Buffer. The encoderProtos and decodeProtos can be different, in this case we use the same protos for encoder and decoder.
To use the protbuf as browser, you need to include the /client/protobuf.js in your html.
//Require proto buf
var protobuf = require('protobuf');
//Get parsed protos from server
var protos = getProtos();
//Init protobuf
protobuf.init({encoderProtos:protos, decoderProtos:protos});
//Encode msg to binary Buffer
var buffer = protobuf.encode(key, msg);
//Decode a msg from binary buffer
var decodeMsg = protobuf.decode(key, buffer);
The protobuf will be a global variable, and you need to get the parsed protos from server. The others are the same as in server side, except the encoder result will by a ByteArray instead of Buffer.
###Compatibility For the same message and proto, the encode results are the same for pomelo-protobuf and google protobuf .This means you can exchange binary data with google-protobuf.
Some how we has some changes in the proto file, and there are some features we do not support, there are the different:
package : The array with simple content (integer, float) are packaged by default.And the complex content(message, string) are not packaged.
long : Pomelo protocol do not support long type, because there are no long int in javascript.All the integer bigger than 32 bits will be translate to a 64bit float, which has only has 52 bits significant figure. It will lost presion for any integer has more than 52 bits significant figures.
default : Pomelo-protobuf do not support default keyword, for the default value is only used to initialized the element at the decoder side, which can be done by the constructor.
enum : Pomelo-protobuf do not support the enum keyword.
FAQs
Protobuf protocol is a high efficient binary protocol for data encode, this module implement the protobuf protocol, and used in [pomelo](https://github.com/NetEase/pomelo) for data transfer. Of course, pomelo-protobuf can also be used independently in other projects. ##Architecture Unlike the google protobuf, we provide a universal encoder and decoder in pomelo-protobuf. We use protos file as meta data to encode/decode messages, so you do not need to add any code to your project, instead , what you need is to add a protos.json (or two for different encoder and decoder messages) files to define the message need to encode by protobuf.The architecture of pomelo-protobuf is as follow:
We found that pomelo-protobuf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.