cut

cutter

Cut buffer to package

Showing:

Popularity

Downloads/wk

8

Maintenance

No Maintenance Data Available

Package

Dependencies

0

Size (min+gzip)

0.6KB

License

Type Definitions

Tree-Shakeable

No?

Categories

Readme

cuttertravis-ci

A cutter for head-body buffer. Many network protocol will use packets composed of a fixed length "head" and a variable length "body", the "data" event emitted by node socket probably not a complete packet, but part of several packets. So this cutter will help to get packets.

Usage

var Cutter = require('cutter');
var net = require('net');

/**
 * must return length of head + body
 */
function packetLength(data) {
  return 4 + data[0] + (data[1] << 8) + (data[2] << 16);
}

var server = net.createServer(function(socket) {
  var cutter = new Cutter(4, packetLength);
  cutter.on('packet', function(packet) {
    var head = packet.slice(0, 4);
    var body = packet.slice(4, packet.length);
  });
  socket.on('data', function(data) {
    cutter.handleData(data);
  });

  cutter.on('error', function(err) {
    console.log(err.stack);
  });
});

server.listen(12345);

Error Event

When cutter parse packet error, it will emit an error event.

Installation

$ npm install cutter --save

License

The MIT license

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100