Created
September 11, 2012 17:12
-
-
Save polotek/3699914 to your computer and use it in GitHub Desktop.
readable stream 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 readstream = require('fs').createReadStream('./data.json') | |
, buffer = [] | |
, len = 0; | |
readstream.on('data', function(data) { | |
buffer.push(data); | |
len += data.length; | |
}); | |
readstream.on('end', function() { | |
buffer = Buffer.concat(buffer, len); | |
console.log(buffer.toString()); | |
}); | |
var readstream = require('fs').createReadStream('./data.json') | |
, buffer = [] | |
, len = 0; | |
// hook to readable so you can restart your processing | |
readstream.on('readable', function process() { | |
var data; | |
// People are going to adopt this weird assign and check pattern | |
// from C code. Gross. | |
while((data = readstream.read()) !== null) { | |
buffer.push(data); | |
len += data.length; | |
} | |
}); | |
readstream.on('end', function() { | |
buffer = Buffer.concat(buffer, len); | |
console.log(buffer.toString()); | |
}); | |
var readstream = require('fs').createReadStream('./data.json') | |
, Collector = require('data-collector-stream') | |
, collector = new Collector(); | |
readstream.pipe(collector) | |
.on('end', function() { | |
console.log(collector.getData().toString()); | |
}); | |
var readstream = require('fs').createReadStream('./data.json') | |
, FilterStream = require('stream').FilterStream; | |
var filter = new FilterStream(function(data) { | |
if(data === null) { | |
this.emit('end'); | |
} | |
this.emit('data', data.toString().toUpperCase()); | |
}); | |
readstream.pipe(filter) | |
.on('end', function() { | |
}); | |
// If filtering was supported by stream, maybe something like this. | |
readstream.filter(gzipper); | |
// foo protocol = | |
// 2 bytes = msgpack length | |
// n bytes = filename (n = msgpack length as int) | |
// the rest = file contents | |
// end | |
function FooParser (options) { | |
// hook up to underlying stream somehow | |
Readable.call(this, options) | |
this.process() | |
this.state = 'start' | |
} | |
util.inherits(FooParser, Readable) | |
FooParser.prototype.process = function () { | |
var data; | |
switch (this.state) { | |
case 'start': | |
if((data = this.read(2)) !== null) { | |
this.length = parseInt(data.toString(), 16) | |
this.state = 'filename' | |
} | |
break | |
case 'filename': | |
if((data = this.read(this.length)) !== null) { | |
this.filename = data | |
this.state = 'file' | |
return | |
} | |
break | |
case 'file': | |
this.emit('ready') | |
break | |
} | |
} | |
var parser = new FooParser(); | |
req.pipe(parser).on('ready', function() { | |
var file = fs.createWriteStream(this.filename); | |
this.pipe(file); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment