Giter Club home page Giter Club logo

node-stream-lib's Introduction

node-stream-lib

A library with stream utilities for node.js

How to install

npm install -s stream-lib

How to use

In general

var streamLib = require('stream-lib');

// your streamlib module like:
// new streamLib.Random.Alphanumeric();

Please take also a look into the JSDoc

Buffer

Buffers a stream in memory with a duplex stream. You are able to save the content in memory if the destination works slower then the source. You are able to close the destination earlier.

You can also use it in object mode, when you set the first paramerter to true;

var streamLib = require('stream-lib');

// You need some source and destination streams
var fs = require('fs');

var sourceStream = fs.createReadStream('/path/to/your/source.file');
var destinationStream = fs.createWriteStream('/path/to/your/destination.file');

// You need the buffer stream
var objectMode = false;                    // This is optional
var bufferStream = new streamLib.Buffer({objectMode: objectMode});

sourceStream.pipe(decoderStream)
    .pipe(destinationStream);

Concat

Concat multiple streams in given order.

var streamLib = require('stream-lib');

// You need some source and destination streams
var fs = require('fs');

var firstStream = fs.createReadStream('/path/to/your/first.file');
var secondStream = fs.createReadStream('/path/to/your/second.file');
var thirdStream = fs.createReadStream('/path/to/your/third.file');
var destinationStream = fs.createWriteStream('/path/to/your/destination.file');

// You need the concat stream
var concatStream = new streamLib.Concat();

firstStream
    .pipe(concatStream);
secondStream
    .pipe(concatStream);
thirdStream
    .pipe(concatStream);

concatStream.pipe(destinationStream);

Delay

Delay the flow of a stream.

There are a lot of other pipes. Please take a look in API-doc

var streamLib = require('stream-lib');

// You need some source and destination streams
var fs = require('fs');

var sourceStream = fs.createReadStream('/path/to/your/source.file');
var destinationStream = fs.createWriteStream('/path/to/your/destination.file');

// You need the buffer stream
var delay = 500;                                  // Delay in milli-seconds (Zero makes it just asynchronous)
var objectMode = false;                           // This is optional
var delayStream = new streamLib.Pipe.Delay({objectMode: objectMode});

sourceStream.pipe(delayStream)
    .pipe(destinationStream);

Event

An event stream works like a normal event emitter but works with streams.

You are also able to augment an existing event emitter with the eventStream.

var streamLib = require('stream-lib');

// Create an event stream

var eventStream = new streamLib.Event();


// For sending and getting Events
eventStream.receive('test', function (data) {
    console.log('Test received:', data);
});

eventStream.send('test', 'Hello!');


var EventEmitter = require('events').EventEmitter;

// anotherEventStream

var anotherEventStream = new streamLib.Event();

anotherEventStream.receive('test', function () {});

eventStr.pipe(anotherEventStream);

Unit

Create a unit of different pipes.

var streamLib = require('stream-lib');

// Create an unit

var unit = new streamLib.Unit();

// Create streams for the unit

var hexDecoder = new streamLib.HexEncoder();
var toUpperCaseStream = new streamLib.UpperCase();

// Combine to an unit

HexEncoder
    .pipe(toUpperCaseStream);

unit.setWritableStream(HexEncoder);
unit.setReadableStream(toUpperCaseStream);

// Now the unit create an upper case hex string

Sequence

Streams a never ending recorded sequence.

var streamLib = require('stream-lib');

// Create an sequence

var sequence = new streamLib.Sequence();

sequence.write('1');
sequence.write('2');
sequence.write('4');
sequence.write('8');
sequence.write('16');
sequence.end();

sequence.on('data', function (chunk) {
    console.log(chunk.toString());
});

// Outputs 1 2 4 8 16 1 2 4 8 16 1 2 4 8 16 1 2 4 8 16 1 2 4 8 16 1 2 4 8 16 1 2 4 8 16 1 2 4 8 16 ...

Measure

Makes measures on a stream

var streamLib = require('stream-lib');

// Create a measure

var measure = new streamLib.Measure.Capacity();

// Create some asynchronous pipes

var firstPipe = new streamLib.Pipe.Async();
var secondPipe = new streamLib.Pipe.Async();
var thirdPipe = new streamLib.Pipe.Async();

measure.measureInlet
    .pipe(firstPipe)
    .pipe(secondPipe)
    .pipe(thirdPipe)
    .pipe(measure.measureOutlet);

measure.measureInlet.write('1');
measure.measureInlet.write('2');
measure.measureInlet.write('3');
measure.measureInlet.write('4');
measure.measureInlet.write('5');
measure.measureInlet.end();

measure.on('data', function (chunk) {
    console.log(chunk.capacity);       // Now you got the measured capacity of the streams
});

node-stream-lib's People

Contributors

atd-schubert avatar

Stargazers

Raphael Syed avatar

Watchers

James Cloos avatar  avatar

node-stream-lib's Issues

Typos in readme

It should be named as buffer-Stream not as decoder stream in Buffer example

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.