stream-pkg
A simple tool to compose package for stream based API, such as tcp socket.
Last updated 7 years ago by changchang .
Repository · Original npm · Tarball · package.json
$ cnpm install stream-pkg 
SYNC missed versions from official npm registry.

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

Current Tags

  • 0.0.5                                ...           latest (7 years ago)

3 Versions

  • 0.0.5                                ...           7 years ago
  • 0.0.4                                ...           8 years ago
  • 0.0.1                                ...           8 years ago

Copyright 2014 - 2017 © taobao.org |