stream-wrapper
Create streams from read/write functions
Last updated 11 years ago by mafintosh .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install stream-wrapper 
SYNC missed versions from official npm registry.

stream-wrapper

Drop-in replacement for the core stream that allows you to create streams from read/write functions.

npm install stream-wrapper

All streams create are stream2 streams

Readable Stream

var stream = require('stream-wrapper');

var rs = stream.readable(function read(size) {
	this.push(new Buffer('hello world'));
});

If you don't have a read function just omit it

var rs = stream.readable();
rs.push(new Buffer('hello world'));

The Readable prototype is exposed through stream.Readable.

Writable Stream

var stream = require('stream-wrapper');

var ws = stream.writable(function writable(chunk, enc, callback) {
	console.log('writing', chunk);
	callback();
});

The Writable prototype is exposed through stream.Writable.

Duplex Stream

var stream = require('stream-wrapper');

var ds = stream.duplex(function read() {
	this.push(new Buffer('hello world'));
}, function write(chunk, enc, callback) {
	console.log('writing', chunk);
	callback();
});

The Duplex prototype is exposed through stream.Duplex

Transform Stream

var stream = require('stream-wrapper');

var ts = stream.transform(function transform(chunk, enc, callback) {
	this.push(chunk);
	callback();
});

If you want to add a flush function pass it as the second parameter

var ts = stream.transform(function transform() {
	...
}, function flush(callback) {
	console.log('now flushing...');
	callback();
});

The Transform prototype is exposed through stream.Transform

Stream options

If you want to pass stream options (like objectMode) pass them as the first parameter to readable, writable, duplex or transform

var rs = stream.readable({objectMode:true}, function read() {
	this.push({message:'i am an object'});
});

Stream defaults

You can change the default options for the stream by calling defaults

// all streams created have objectMode enabled
stream = stream.defaults({objectMode:true});

Stream.destroy

All streams have a .destroy method implemented per default that when called emits close and sets stream.destroyed = true.

var rs = stream.readable();

rs.on('close', function() {
	// rs.destroyed === true
	console.log('someone called destroy');
});

rs.destroy();

License

MIT

Current Tags

  • 0.1.2                                ...           latest (11 years ago)

3 Versions

  • 0.1.2                                ...           11 years ago
  • 0.1.1                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |