From 9cd4ff7235c7c5c9f10a3edc8f3332e1e72701c3 Mon Sep 17 00:00:00 2001 From: Thiago Date: Mon, 18 Dec 2017 16:15:15 -0200 Subject: [PATCH 1/4] Added readStreamOptions --- lib/filewalker.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/filewalker.js b/lib/filewalker.js index 0a21c89..ee2a376 100644 --- a/lib/filewalker.js +++ b/lib/filewalker.js @@ -12,7 +12,7 @@ if (global.setImmediate !== undefined) { var lstat = process.platform === 'win32' ? 'stat' : 'lstat'; -function Filewalker(root, options) { +function Filewalker(root, options, readStreamOptions) { if(!(this instanceof Filewalker)) return new Filewalker(root, options); FunctionQueue.call(this, options); @@ -20,10 +20,23 @@ function Filewalker(root, options) { var self = this; this.matchRegExp = null; - + this.recursive = true; - options = options || {}; + this.readStreamOptions = { + flags: 'r', + encoding: null, + fd: null, + mode: 0666, + autoClose: true, + highWaterMark: 64 * 1024 + }; + + for ( var i in readStreamOptions ) { + this.readStreamOptions[ i ] = readStreamOptions[ i ]; + } + + options = options || {}; Object.keys(options).forEach(function(k) { if(self.hasOwnProperty(k)) { self[k] = options[k]; @@ -85,7 +98,7 @@ Filewalker.prototype._emitStream = function(p, s, fullPath) { this.open += 1; - var rs = fs.ReadStream(fullPath); + var rs = fs.ReadStream(fullPath, this.readStreamOptions); // retry on any error rs.on('error', function(err) { From 68e1b290187ff94a5105794f7b1aa188036188f5 Mon Sep 17 00:00:00 2001 From: Thiago Date: Thu, 21 Dec 2017 01:14:57 -0200 Subject: [PATCH 2/4] Now options may have 'readStream'. --- lib/filewalker.js | 24 ++++++++++++++++-------- test/basic-test.js | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/filewalker.js b/lib/filewalker.js index ee2a376..52a58c1 100644 --- a/lib/filewalker.js +++ b/lib/filewalker.js @@ -12,7 +12,7 @@ if (global.setImmediate !== undefined) { var lstat = process.platform === 'win32' ? 'stat' : 'lstat'; -function Filewalker(root, options, readStreamOptions) { +function Filewalker(root, options) { if(!(this instanceof Filewalker)) return new Filewalker(root, options); FunctionQueue.call(this, options); @@ -22,8 +22,10 @@ function Filewalker(root, options, readStreamOptions) { this.matchRegExp = null; this.recursive = true; + + options = options || {}; - this.readStreamOptions = { + var readStreamOptions = { flags: 'r', encoding: null, fd: null, @@ -32,13 +34,19 @@ function Filewalker(root, options, readStreamOptions) { highWaterMark: 64 * 1024 }; - for ( var i in readStreamOptions ) { - this.readStreamOptions[ i ] = readStreamOptions[ i ]; + 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; } - - options = options || {}; + + // Copy all the options properties to this, except 'readStream' Object.keys(options).forEach(function(k) { - if(self.hasOwnProperty(k)) { + if( k !== 'readStream' && self.hasOwnProperty(k) ) { self[k] = options[k]; } }); @@ -98,7 +106,7 @@ Filewalker.prototype._emitStream = function(p, s, fullPath) { this.open += 1; - var rs = fs.ReadStream(fullPath, this.readStreamOptions); + 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..3eaeadc 100644 --- a/test/basic-test.js +++ b/test/basic-test.js @@ -369,4 +369,24 @@ 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( rs.highWaterMark, fw.highWaterMark ); + reallyReadTheReadStream(rs); + }); + fw.on('done', done); + fw.walk(); + }); + + }); }); From b442870755ae7ca69ed766c662826b94c3b1a4a0 Mon Sep 17 00:00:00 2001 From: Thiago Date: Thu, 21 Dec 2017 02:43:26 -0200 Subject: [PATCH 3/4] Fixed. --- lib/filewalker.js | 4 +++- test/basic-test.js | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/filewalker.js b/lib/filewalker.js index 52a58c1..fa1aa77 100644 --- a/lib/filewalker.js +++ b/lib/filewalker.js @@ -43,10 +43,12 @@ function Filewalker(root, options) { } options.readStream = readStreamOptions; } + + this.readStream = options.readStream; // Copy all the options properties to this, except 'readStream' Object.keys(options).forEach(function(k) { - if( k !== 'readStream' && self.hasOwnProperty(k) ) { + if( self.hasOwnProperty(k) ) { self[k] = options[k]; } }); diff --git a/test/basic-test.js b/test/basic-test.js index 3eaeadc..9fb865a 100644 --- a/test/basic-test.js +++ b/test/basic-test.js @@ -381,7 +381,8 @@ describe('Filewalker', function() { }); it('when "stream", it should be using the given readStream option', function(done) { fw.on('stream', function(rs, p, s, fullPath) { - assert.strictEqual( rs.highWaterMark, fw.highWaterMark ); + assert.strictEqual( fw.readStream.highWaterMark, myHighWaterMark ); + assert.strictEqual( fw.readStream.highWaterMark, rs._readableState.highWaterMark ); reallyReadTheReadStream(rs); }); fw.on('done', done); From c53091a3e245465f8ff8490fdf3b28fa5c5ca804 Mon Sep 17 00:00:00 2001 From: Thiago Date: Thu, 21 Dec 2017 02:49:06 -0200 Subject: [PATCH 4/4] .gitignore added. --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore 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