Skip to content

Instantly share code, notes, and snippets.

@siygle
Created March 14, 2013 16:36
Show Gist options
  • Select an option

  • Save siygle/5162875 to your computer and use it in GitHub Desktop.

Select an option

Save siygle/5162875 to your computer and use it in GitHub Desktop.
example of stream2 new class
var Duplex = require('stream').Duplex;
var util = require('util');
function both() {
if (!(this instanceof both)) return new both();
Duplex.call(this);
}
util.inherits(both, Duplex);
both.prototype._read = function(size) {
this.push('start read!');
this.push(null);
};
both.prototype._write = function(chunk, encoding, cb) {
if (chunk) {
this.push(chunk + ' write!');
} else {
this.push('empty write!');
}
cb();
};
var test = new both();
test.pipe(process.stdout);
test.on('end', function() {
console.log('finish read! Let try write');
process.stdin.pipe(test);
});
var Transform = require('stream').Transform;
var util = require('util');
function append() {
Transform.call(this);
}
util.inherits(append, Transform);
append.prototype._transform = function(chunk, encoding, cb) {
this.push(chunk + '[ROCK]');
cb();
};
var test = new append();
process.stdin.pipe(test).pipe(process.stdout);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment