Coder Social home page Coder Social logo

node-stream-tk's Introduction

stream-tk

stream-tk is a small experimental toolkit for handling data streams in Node.js.

It supports the "stream3" APIs from Node.js v0.12 and later versions.

Installation

npm install stream-tk

test methods API

stream-tk provides many test methods.

.isStream( obj )

Returns true if object is a data stream.

var stream = require('stream'),
    stk = require('stream-tk');
    
var myStream = new stream.PassThrough();

stk.isStream(myStream); // -> true
stk.isStream(process.stdout); // -> true
stk.isStream("hello"); // -> false

.isReadable( obj )

Returns true if object is a Readable data stream.

var readable = new stream.Readable();
readable._read = function(){}; // implement _read method

stk.isReadable(readable); // -> true
stk.isReadable(process.stdin); // -> true

.isWritable( obj )

Returns true if object is a Writable data stream.

var writable = new stream.Writable();

stk.isWritable(writable); // -> true
stk.isWritable(process.stdout); // -> true

.isTransform( obj )

Returns true if object is a Transform data stream.

var transform = new stream.Transform();

stk.isTransform(transform); // -> true
stk.isTransform(require('zlib').createGzip()); // -> true

.isFlowing( obj )

Returns true if object is a Readable data stream in flowing mode.

readable.pause();

stk.isFlowing(readable); // -> false

readable.resume();

stk.isFlowing(readable); // -> true

.isPipeOn( source, destination )

Returns true if Readable stream source in piped on Writable stream destination.

stk.isPipeOn(readable, writable); // -> false

readable.pipe(writable);

stk.isPipeOn(readable, writable); // -> true

.isEnded( obj )

Returns true if object is either a Readable or Writable data stream that has been ended.

writable.end("no more data");

stk.isEnded(writable); // -> true

readable.push(null);

stk.isEnded(readable); // -> true

.isCorked( obj )

Returns true if object is a Writable data stream that has been corked using the .cork() method.

stk.isCorked(writable); // -> false

writable.cork();

stk.isCorked(writable); // -> true

writable.uncork();

stk.isCorked(writable); // -> false

.bufferize( readable, buffer|length [, callback] )

Fills a buffer from a Readable data stream, and returns the destination buffer being filled. Either a buffer or a byte length must be provided. A callback may also be provided, and will be called when the buffer is full. Note that the buffer will accept flowing data(using readable.resume()) or explicitely read data(using readable.read()).

var buf = stk.bufferize(readable, new Buffer(100), function(err, buf) {
  console.log("filled buffer contains: ", buf.toString());
});

Extending stream objects using .extend( )

The .extend() method allows you to extend stream objects with methods from the stream-tk API:

var myStream = new stream.PassThrough();

stk.extend(myStream);

myStream.isReadable(); // -> true
myStream.isWritable(); // -> true
myStream.isTransform(); // -> true
myStream.isFlowing(); // -> false
myStream.isPipeOn(process.stdout); // -> false

myStream.pipe(process.stdout);

myStream.isFlowing(); // -> true
myStream.isPipeOn(process.stdout); // -> true

UNIX pseudo devices API

stream-tk provides data stream mappings to UNIX pseudo devices(/dev/null, /dev/zero, /dev/full, /dev/urandom). (Also works on the Windows platform, however UNIX pseudo devices are simulated.)

.createNull( mode )

Creates a data stream mapped to the /dev/null pseudo device. Either 'read' or 'write' must be provided as mode parameter.

var sink = stk.createNull('write');
anyReadable.pipe(sink);

.createZero( mode, length )

Creates a data stream mapped to the /dev/zero pseudo device. Either 'read' or 'write' must be provided as mode parameter. The length parameter is also mandatory in 'read' mode.

// create a 1k file filled with 0 bits
var zeroSource = stk.createZero('read', 1024);
var file = require('fs').createWriteStream('zeros.txt');
zeroSource.pipe(file);

var sink = stk.createZero('write');
anyReadable.pipe(sink);

.createFull( mode, length )

Creates a data stream mapped to the /dev/full pseudo device. Either 'read' or 'write' must be provided as mode parameter. The length parameter is also mandatory in 'read' mode.

// create a 1k file filled with 0 bits
var zeroSource = stk.createFull('read', 1024);
var file = require('fs').createWriteStream('zeros.txt');
zeroSource.pipe(file);

.createRandom( mode, length )

Creates a data stream mapped to the /dev/urandom pseudo device. Either 'read' or 'write' must be provided as mode parameter. The length parameter is also mandatory in 'read' mode.

// create a 1k file filled with pseudo-random binary
var randomSource = stk.createRandom('read', 1024);
var file = require('fs').createWriteStream('random.txt');
randomSource.pipe(file);

// mix random data into the pool
stk.createRandom('write').write("anything");

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.