Skip to content

Commit

Permalink
Fix: Use lazystream for streamFile to avoid fd exhaustion
Browse files Browse the repository at this point in the history
  • Loading branch information
Klowner authored and phated committed Nov 28, 2017
1 parent bcf0a45 commit 542771a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/src/getContents/streamFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

var fs = require('graceful-fs');
var stripBom = require('strip-bom-stream');
var lazystream = require('lazystream');

function streamFile(file, opt, cb) {
file.contents = fs.createReadStream(file.path);
var filePath = file.path;

file.contents = new lazystream.Readable(function() {
return fs.createReadStream(filePath);
});

if (opt.stripBOM) {
file.contents = file.contents.pipe(stripBom());
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"graceful-fs": "^4.0.0",
"gulp-sourcemaps": "^1.5.2",
"is-valid-glob": "^0.3.0",
"lazystream": "^1.0.0",
"merge-stream": "^1.0.0",
"mkdirp": "^0.5.0",
"object-assign": "^4.0.0",
Expand Down
33 changes: 33 additions & 0 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var should = require('should');
require('mocha');

var wipeOut = function() {
this.timeout(20000);
spies.setError('false');
statSpy.reset();
chmodSpy.reset();
Expand Down Expand Up @@ -1366,4 +1367,36 @@ describe('dest stream', function() {
.once('finish', done);
});

it('should not exhaust available file descriptors when streaming thousands of files', function(done) {
// This can be a very slow test on boxes with slow disk i/o
this.timeout(0);

// Make a ton of hard links
var numFiles = 6000;
var srcFile = path.join(__dirname, './fixtures/test.coffee');
fs.mkdirSync(path.join(__dirname, './out-fixtures'));
fs.mkdirSync(path.join(__dirname, './out-fixtures/in/'));

for (var idx = 0; idx < numFiles; idx++) {
fs.linkSync(srcFile, path.join(__dirname, './out-fixtures/in/test' + idx + '.coffee'));
}

var srcStream = vfs.src(path.join(__dirname, './out-fixtures/in/*.coffee'), { buffer: false });
var destStream = vfs.dest('./out-fixtures/out/', { cwd: __dirname });

var fileCount = 0;

srcStream
.pipe(through.obj(function(file, enc, cb) {
fileCount++;

cb(null, file);
}))
.pipe(destStream)
.once('finish', function() {
fileCount.should.equal(numFiles);
done();
});
});

});

0 comments on commit 542771a

Please sign in to comment.