Created
October 18, 2015 02:11
-
-
Save cbogart/ea8e6cbc87d04c66a385 to your computer and use it in GitHub Desktop.
Scan from many files, opening only one at a time.
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 scangz = function (fname) { | |
var rawread = fs.createReadStream(fname); | |
var unzipped = rawread.pipe(zlib.createUnzip()); | |
return unzipped; | |
} | |
var FileScanner = function() { | |
var self = this; | |
events.EventEmitter.call(self); | |
self.results = new Readable({objectMode: true}); | |
self.results._read = function noop() {}; | |
self.on('getFiles', function(files, filter) { | |
file = files.shift(); | |
var scanner = scangz(file); | |
var scannerp = scanner.pipe(jsonstream()); | |
console.log("\nopened", file); | |
scannerp.on('data', function(chunk) { | |
try { | |
if (filter(chunk)) { | |
self.results.push(chunk); | |
} | |
} catch (ev) { | |
console.log("ERROR: ", ev); | |
console.log(ev.stack); | |
} | |
}); | |
scannerp.on('end', function() { | |
console.log("END OF STREAM"); | |
if (files.length > 0) { | |
self.emit('getFiles', files, filter); | |
} | |
}); | |
}); | |
} | |
util.inherits(FileScanner, events.EventEmitter); | |
// Return a stream | |
module.exports.scanner = function() { | |
var files = ...get list of files... | |
var filescanner = new FileScanner(); | |
filescanner.emit('getFiles', files, filter); | |
return filescanner.results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment