stream-pkg
v0.0.5
Published
A simple tool to compose package for stream based API, such as tcp socket.
Downloads
743
Readme
#Stream-pkg
Stream-pkg is a simple tool for packages that transfered on stream-based API. As we known, stream API is a good thing of Node.js and we could compose different applications by stream-based API conventionally. But package may be splited into small chunks when it transfered on stream-based API such as socket. We have to recompose the chunks into package when we receive them. And stream-pkg could do it for you.
##Installation
npm install stream-pkg
##Usage
var Composer = require('stream-pkg');
var src = 'Hello world.';
var comp = new Composer();
// package to data
var res = comp.compose(src);
// data to package
comp.on('data', function(data) {
var str = data.toString('utf-8');
str.should.equal(src);
done();
});
comp.feed(res);
##API ###composer.compose(pkg) Compose package into byte data. ####Arguments
- pkg - String or Buffer. Package data.
- return - Buffer that fill with package data.
###composer.feed(data, [offset], [end]) Feed data into composer. ####Arguments
- data - Buffer. Next chunnk of data receive from stream.
- offset - Number. Optional. Offset index of buffer that start to feed. Default is 0.
- end - Number. Optional. End index (not includ) of buffer that stop to feed. Default is data.length.
##Event ###'data'(pkg) Emit package by data event when the package has finished. ###'length_limit'(composer, data, offset)