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

pomelo-protobuf

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pomelo-protobuf - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

.npmignore

52

lib/client/protobuf.js

@@ -87,3 +87,2 @@ /* ProtocolBuffer client 0.1.0*/

if(isNaN(n) || n < 0){
console.log(n);
return null;

@@ -93,3 +92,3 @@ }

var result = [];
while(n !== 0){
do{
var tmp = n % 128;

@@ -103,3 +102,3 @@ var next = Math.floor(n/128);

n = next;
}
}while(n !== 0);

@@ -114,6 +113,4 @@ return result;

}
//console.log('n : %j, n<0 : %j, -n*2 : %j, -1 : %j',n, n<0, (-n)*2-1);
n = n<0?(Math.abs(n)*2-1):n*2;
//console.log(n);
return Codec.encodeUInt32(n);

@@ -207,13 +204,8 @@ };

//console.log('decode bytes : [%j]', bytes[offset]);
offset += 1;
}else if(bytes[offset] < 224){
code = ((bytes[offset] & 0x3f)<<6) + (bytes[offset+1] & 0x3f);
//console.log('decode bytes : [%j, %j]', bytes[offset], bytes[offset+1]);
offset += 2;
}else{
code = ((bytes[offset] & 0x0f)<<12) + ((bytes[offset+1] & 0x3f)<<6) + (bytes[offset+2] & 0x3f);
//console.log('decode bytes : [%j, %j, %j]', bytes[offset], bytes[offset+1], bytes[offset + 2]);
offset += 3;

@@ -226,3 +218,2 @@ }

//console.log('array : %j, length : %j', array, array.length);
var str = '';

@@ -260,9 +251,6 @@ for(var i = 0; i < array.length;){

if(charCode <= 0x7f){
//console.log('code :%j, length : %j', charCode, 1);
return [charCode];
}else if(charCode <= 0x7ff){
//console.log('code :%j, length : %j', charCode, 2);
return [0xc0|(charCode>>6), 0x80|(charCode & 0x3f)];
}else{
//console.log('code :%j, length : %j', charCode, 3);
return [0xe0|(charCode>>12), 0x80|((charCode & 0xfc0)>>6), 0x80|(charCode & 0x3f)];

@@ -305,3 +293,2 @@ }

if(!checkMsg(msg, protos)){
console.warn('check msg failed! msg : %j, proto : %j', msg, protos);
return null;

@@ -318,3 +305,2 @@ }

console.log('length : %j', length);
if(!!protos){

@@ -327,3 +313,2 @@ offset = encodeMsg(uInt8Array, offset, protos, msg);

console.log('offset : %j', offset);
return null;

@@ -347,7 +332,6 @@ };

if(typeof(msg[name]) === 'undefined'){
//console.log('no property msg : %j, name : %j', msg[name], name);
return false;
}
case 'optional' :
if(!!protos.__messages[proto.type]){
if(typeof(msg[name]) !== 'undefined'){
if(!!protos.__messages[proto.type]){

@@ -379,10 +363,7 @@ checkMsg(msg[name], protos.__messages[proto.type]);

//console.error('encode proto : %j', proto);
switch(proto.option){
case 'required' :
case 'optional' :
//console.log('encode tag');
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = encodeProp(msg[name], proto.type, offset, buffer, protos);
//console.log('encode tag finish, value : %j', msg[name]);
break;

@@ -426,7 +407,5 @@ case 'repeated' :

offset += length;
//console.log('encode string length : %j, str : %j', length, value);
break;
default :
if(!!protos.__messages[type]){
//console.log('msg encode start, type :%j, value : %j, start : %j', type, value, offset);
//Use a tmp buffer to build an internal msg

@@ -444,4 +423,2 @@ var tmpBuffer = new ArrayBuffer(codec.byteLength(JSON.stringify(value)));

}
//console.log('msg encode finish, offset : %j', offset);
}

@@ -467,6 +444,4 @@ break;

}else{
//console.log('encode array : %j', array);
for(i = 0; i < array.length; i++){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
//console.log('encode array value : %j', array[i]);
offset = encodeProp(array[i], proto.type, offset, buffer, protos);

@@ -480,4 +455,2 @@ }

function writeBytes(buffer, offset, bytes){
//console.log('wirte bytes : %j', bytes);
for(var i = 0; i < bytes.length; i++, offset++){

@@ -534,16 +507,7 @@ buffer[offset] = bytes[i];

while(offset<length){
//console.log('offset : %j, length : %j, head bytes : %j', offset, length, peekBytes());
var head = getHead();
var type = head.type;
var tag = head.tag;
var name = protos.__tags[tag];
// console.log('decode bytes : %j', peekBytes());
// console.log('tag : %j, tags : %j', tag, protos.__tags);
if(!protos[name]){
var a = 111;
console.log('msg ' + msg + 'protos' + protos);
}
switch(protos[name].option){

@@ -555,3 +519,2 @@ case 'optional' :

case 'repeated' :
//console.log('decode array');
if(!msg[name]){

@@ -572,3 +535,2 @@ msg[name] = [];

function isFinish(msg, protos){
//console.log('head : %j, tags : %j, result : %j', peekHead(), protos.__tags, !!protos.__tags[peekHead().tag]);
return (!protos.__tags[peekHead().tag]);

@@ -601,3 +563,2 @@ }

function decodeProp(type, protos){
//console.log('type : %j, protos : %j', type, protos);
switch(type){

@@ -614,3 +575,3 @@ case 'uInt32':

case 'double' :
var double = codec.decodeDouble(buffer, offset)
var double = codec.decodeDouble(buffer, offset);
offset += 8;

@@ -626,3 +587,2 @@ return double;

default :
//console.log('object type : %j, protos: %j', type, protos);
if(!!protos && !!protos.__messages[type]){

@@ -655,4 +615,6 @@ var length = codec.decodeUInt32(getBytes());

var b;
do{
var b = buffer[pos];
b = buffer[pos];
bytes.push(b);

@@ -659,0 +621,0 @@ pos++;

@@ -30,6 +30,4 @@ var Encoder = module.exports;

}
//console.log('n : %j, n<0 : %j, -n*2 : %j, -1 : %j',n, n<0, (-n)*2-1);
n = n<0?(Math.abs(n)*2-1):n*2;
//console.log(n);
return Encoder.encodeUInt32(n);

@@ -36,0 +34,0 @@ }

@@ -35,12 +35,7 @@ var protoParser = require('./protoParser');

while(offset<length){
//console.log('offset : %j, length : %j, head bytes : %j', offset, length, peekBytes());
var head = getHead();
var type = head.type;
var tag = head.tag;
var name = protos.__tags[tag];
// console.log('decode bytes : %j', peekBytes());
// console.log('tag : %j, tags : %j', tag, protos.__tags);
switch(protos[name].option){

@@ -52,3 +47,2 @@ case 'optional' :

case 'repeated' :
//console.log('decode array');
if(!msg[name]){

@@ -69,3 +63,2 @@ msg[name] = [];

function isFinish(msg, protos){
//console.log('head : %j, tags : %j, result : %j', peekHead(), protos.__tags, !!protos.__tags[peekHead().tag]);
return (!protos.__tags[peekHead().tag]);

@@ -98,3 +91,2 @@ }

function decodeProp(type, protos){
//console.log('type : %j, protos : %j', type, protos);
switch(type){

@@ -122,3 +114,2 @@ case 'uInt32':

default :
//console.log('object type : %j, protos: %j', type, protos);
if(!!protos && !!protos.__messages[type]){

@@ -125,0 +116,0 @@ var length = encoder.decodeUInt32(getBytes());

@@ -39,4 +39,2 @@ var protoParser = require('./protoParser');

}
//console.error('encode fail ! offset : %j', offset);
}

@@ -51,4 +49,4 @@

function checkMsg(msg, protos){
if(!protos){
console.warn('no protos exist for msg : %j', msg);
if(!protos || !msg){
console.warn('no protos or msg exist! msg : %j, protos : %j', msg, protos);
return false;

@@ -92,14 +90,10 @@ }

for(var name in msg){
//console.warn('name : %j, protos : %j', name, protos[name]);
if(!!protos[name]){
var proto = protos[name];
//console.error('encode proto : %j', proto);
switch(proto.option){
case 'required' :
case 'optional' :
//console.log('encode tag');
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = encodeProp(msg[name], proto.type, offset, buffer, protos);
//console.log('encode tag finish, value : %j', msg[name]);
break;

@@ -119,2 +113,4 @@ case 'repeated' :

function encodeProp(value, type, offset, buffer, protos){
var length = 0;
switch(type){

@@ -137,3 +133,3 @@ case 'uInt32':

case 'string':
var length = Buffer.byteLength(value);
length = Buffer.byteLength(value);

@@ -145,10 +141,8 @@ //Encode length

offset += length;
//console.log('encode string length : %j, str : %j', length, value);
break;
default :
if(!!protos.__messages[type]){
//console.log('msg encode start, type :%j, value : %j, start : %j', type, value, offset);
//Use a tmp buffer to build an internal msg
var tmpBuffer = new Buffer(Buffer.byteLength(JSON.stringify(value)));
var length = 0;
length = 0;

@@ -162,3 +156,2 @@ length = encodeMsg(tmpBuffer, length, protos.__messages[type], value);

offset += length;
//console.log('msg encode finish, offset : %j', offset);
}

@@ -175,13 +168,13 @@ break;

function encodeArray(array, proto, offset, buffer, protos){
var i = 0;
if(util.isSimpleType(proto.type)){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
offset = writeBytes(buffer, offset, encoder.encodeUInt32(array.length));
for(var i = 0; i < array.length; i++){
for(i = 0; i < array.length; i++){
offset = encodeProp(array[i], proto.type, offset, buffer);
}
}else{
//console.log('encode array : %j', array);
for(var i = 0; i < array.length; i++){
for(i = 0; i < array.length; i++){
offset = writeBytes(buffer, offset, encodeTag(proto.type, proto.tag));
//console.log('encode array value : %j', array[i]);
offset = encodeProp(array[i], proto.type, offset, buffer, protos);

@@ -195,4 +188,2 @@ }

function writeBytes(buffer, offset, bytes){
//console.log('wirte bytes : %j', bytes);
for(var i = 0; i < bytes.length; i++){

@@ -199,0 +190,0 @@ buffer.writeUInt8(bytes[i], offset);

{
"name": "pomelo-protobuf",
"version": "0.3.0",
"version": "0.3.1",
"main": "./lib/protobuf",

@@ -5,0 +5,0 @@ "dependencies": {},

@@ -1,2 +0,115 @@

#Protobuf protocol implement for javascript
#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.
##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](http://pomelo.netease.com/resource/documentImage/protocol/Protobuf_pomelo.png)
##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.
###Server side and Client side
Pomelo-protobuf has server code and client code for js.
- The server code run in Node.JS environment, use Buffer to represent the binary data.
- The client side code run on browser, use Buffer to represent the binary data.
On average the Server version can be 60% higher than client version, with less memory usage. So we highly recommend that use the server code on Node.JS for better performance.
### Example message
```
var key = 'onMove';
var msg = {
entityId : 14,
path : [{x : 128,y : 796},{x : 677,y : 895}],
speed : 160
};
```
### Server side encode/decode
```
//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.
### Client side encode/decode
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.

@@ -8,32 +8,19 @@ var protobuf = require('../../lib/client/protobuf');

var should = require('should');
var tc = require('../testMsg');
describe('msgEncoderTest', function(){
var msg = {
entityId : 1,
paths : [{x : 1, y : 2, c : {a : 1}},
{x : 3, y : 4, c : {a : 2}, tests : [{a:1}, {a:2}]}],
speed : 110,
speed1 : 'ss',
a : 111.1111
};
var protos = parser.parse(require('../protos.json'));
var protos = parser.parse(require('../example.json'));
encoder.init(protos);
decoder.init(protos);
//protobuf.init({encoderProtos:protos, decoderProtos:protos});
protobuf.init({encoderProtos:protos, decoderProtos:protos});
describe('encodeTest', function(){
var buffer = encoder.encode('onMove', msg);
describe('protobufTest', function(){
for(var route in tc){
var msg = tc[route];
var buffer = protobuf.encode(route, msg);
var decodeMsg = protobuf.decode(route, buffer);
console.log(buffer);
var decodeMsg = decoder.decode('onMove', buffer);
//msg.should.equal(decodeMsg);
util.equal(msg, decodeMsg).should.equal(true);
var l1 = codec.byteLength(JSON.stringify(msg));
var l2 = buffer.length;
console.log('old Length : %j, new length : %j, compress rate : %j%', l1, l2, Math.floor(l2/l1*10000)/100);
util.equal(msg, decodeMsg).should.equal(true);
}
});
});

@@ -14,3 +14,3 @@ var tc = module.exports;

{'collisions':[
{'start':0,'length':3},
{'start':1,'length':3},
{'start':79,'length':3}

@@ -17,0 +17,0 @@ ]},

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