Giter Club home page Giter Club logo

node-lei-proto's Introduction

lei-proto

简单的Buffer编码/解析模块

安装

npm install lei-proto

使用

const parseProto = require('lei-proto');

const p = parseProto([
  ['a', 'uint', 1],
  ['b', 'int', 1],
  ['c', 'float'],
  ['d', 'double'],
  ['e', 'string', 1],
  ['f', 'buffer', 1]
]);

const b = p.encode(1, 2, 3.3, 4.4, 'a', new Buffer('b'));
// 或者
// const b = p.encodeEx({a: 1, b: 2, c: 3.3, d: 4.4, e: 'a', f: new Buffer('b')});
const c = p.decode(b);
console.log(b);
// => <Buffer 01 02 40 53 33 33 40 11 99 99 99 99 99 9a 61 62>
console.log(c);
// => { a: 1, b: 2, c: 3.299999952316284, d: 4.4, e: 'a', f: <Buffer 62> }

规则:['名称', '数据类型', 长度, '字节顺序BE/LE']

支持的数据类型:

  • uint 需要指定长度,字节顺序默认BE
  • int 需要指定长度,字节顺序默认BE
  • float 长度为4
  • double 长度为4
  • string 需要指定长度,当其为最后一项时可省略长度(表示不定长)
  • buffer 需要指定长度,当其为最后一项时可省略长度(表示不定长)

具体使用方法参考单元测试。

性能提示

  • Buffer.from(str) 接口比较慢,所以最后一项尽量不要使用不定长的 stringbuffer
  • 如果可能,尽量使用 buffer 而不使用 string 类型
  • 尽量缓存 Buffer 对象,避免多次创建

The MIT License

The MIT License (MIT)

Copyright (c) 2015-2018 Zongmin Lei <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

node-lei-proto's People

Contributors

leizongmin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-lei-proto's Issues

支持不定长的string和buffer

存储结构:

存储长度 uint(n) | 数据
----------------|------------
03              | "abc"

通过第三个参数指定用于存储长度的字节数,存储时数据第N个字段表示其长度,然后才是数据本身

[ "a", "vstring", 1 ]
[ "b", "vbuffer", 2 ]

LV格式不定长buffer

怎么支持不定长buffer格式的呢?
那种LV格式的,前面一个字节表示后面buffer长度。在拼数据的时候才能确定buffer的长度。

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.