Last active
December 19, 2015 05:09
-
-
Save czzarr/5902531 to your computer and use it in GitHub Desktop.
Transform a Readable objects stream into a buffer stream with a Transform stream.
This file contains 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 stream = require('stream'); | |
var through = stream.Transform(); | |
through._writableState.objectMode = true; | |
through._transform = function (chunk, encoding, done) { | |
this.push(JSON.stringify(chunk)); | |
this.push('\n'); | |
done(); | |
}; | |
var Readable = ream.Readable; | |
var rs = Readable({ objectMode: true }); | |
var c = 97 - 1; | |
rs._read = function () { | |
if (c >= 'z'.charCodeAt(0)) return rs.push(null); | |
setTimeout(function () { | |
rs.push({ ops: String.fromCharCode(++c)}); | |
}, 100); | |
}; | |
rs.pipe(through).pipe(process.stdout); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment