diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/lib/filewalker.js b/lib/filewalker.js index 0a21c89..fa1aa77 100644 --- a/lib/filewalker.js +++ b/lib/filewalker.js @@ -20,12 +20,35 @@ function Filewalker(root, options) { var self = this; this.matchRegExp = null; - + this.recursive = true; - + options = options || {}; + + var readStreamOptions = { + flags: 'r', + encoding: null, + fd: null, + mode: 0666, + autoClose: true, + highWaterMark: 64 * 1024 + }; + + if ( ! options.readStream ) { + options.readStream = readStreamOptions; + } else { + // Overwrite only the specified properties + for ( var i in options.readStream ) { + readStreamOptions[ i ] = options.readStream[ i ]; + } + options.readStream = readStreamOptions; + } + + this.readStream = options.readStream; + + // Copy all the options properties to this, except 'readStream' Object.keys(options).forEach(function(k) { - if(self.hasOwnProperty(k)) { + if( self.hasOwnProperty(k) ) { self[k] = options[k]; } }); @@ -85,7 +108,7 @@ Filewalker.prototype._emitStream = function(p, s, fullPath) { this.open += 1; - var rs = fs.ReadStream(fullPath); + var rs = fs.ReadStream(fullPath, this.readStream); // retry on any error rs.on('error', function(err) { diff --git a/test/basic-test.js b/test/basic-test.js index 501d6a1..9fb865a 100644 --- a/test/basic-test.js +++ b/test/basic-test.js @@ -369,4 +369,25 @@ describe('Filewalker', function() { fw.walk(); }); }); + + describe('feature: use given readStream options', function() { + var myHighWaterMark = 1024*1024; + var fw; + before(function() { + fw = filewalker(examplesFile, { readStream: { highWaterMark: myHighWaterMark } } ); + }); + after(function() { + fw = null; + }); + it('when "stream", it should be using the given readStream option', function(done) { + fw.on('stream', function(rs, p, s, fullPath) { + assert.strictEqual( fw.readStream.highWaterMark, myHighWaterMark ); + assert.strictEqual( fw.readStream.highWaterMark, rs._readableState.highWaterMark ); + reallyReadTheReadStream(rs); + }); + fw.on('done', done); + fw.walk(); + }); + + }); });