npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pomelo-protobuf-ts

v0.0.1

Published

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 oth

Downloads

2

Readme

#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:

pomelo protobuf

##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.

  • The server code run in Node.JS environment, use Buffer to represent the binary data.
  • The client side code run on browser, use ByteArray to represent the binary data.

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.

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.