Created
March 14, 2013 16:36
-
-
Save siygle/5162875 to your computer and use it in GitHub Desktop.
example of stream2 new class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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