Giter VIP home page Giter VIP logo

lob-enc's Introduction

Length-Object-Binary (LOB) Packet Encoding (javascript)

This module will encode and decode LOB packets to/from JSON and Buffers.

Install: npm install lob-enc

Primary usage:

var lob = require('lob-enc');
var json = {
  "type":"test",
  "foo":["bar"]
};
var body = new Buffer("any binary!");
var bin = lob.encode(json, body));
// bin will be a buffer with json and body encoded

var packet = lob.decode(bin);
// packet.json == json, and packet.body == body

// do both encode and decode together, for convenience
var packet = lob.packet(json, body);

// object validator
var bool = lob.isPacket(packet);

Also supports reading a packet in a streaming mode:

var stream = lob.stream(function(packet, cbDone){
  // packet.json is the complete header
  // any packet.body will be subsequently streamed
  cbDone();
});

var es = require('event-stream');
stream.pipe(es.wait(function(err, body){
  // body is the body of the packet
}));

// test stream in fragments
var bin = new Buffer('001d7b2274797065223a2274657374222c22666f6f223a5b22626172225d7d616e792062696e61727921','hex');
es.readArray([bin.slice(0,10),bin.slice(10,20),bin.slice(20,30),bin.slice(30)]).pipe(stream);

Packets can be read and written in chunks:

// args.ack = true will only send one chunk until another is read, blocking/acking
var chunk = lob.chunking(args, function cbPacket(err, packet){ });

chunk.pipe(socket); // chunks are written to socket
socket.pipe(chunk); // incoming socket data is read back as chunks

chunk.send(packet); // send a packet as one or more chunks

Packets can be cloaked:

var cloaked = lob.cloak(packet);
var packet = lob.decloak(cloaked);
// packet.cloaked = # of rounds

lob-enc's People

Contributors

quartzjer avatar paulrd avatar

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.