Skip to content

Commit

Permalink
fix: Only traverse directories that can match the glob base
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Apr 7, 2024
1 parent 6dfc6bf commit 76bcccd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
57 changes: 46 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function walkdir() {

var ee = new EventEmitter();

var queue = fastq(readdir, 1);
var queue = fastq(process, 1);
queue.drain = function () {
ee.emit('end');
};
Expand All @@ -52,20 +52,39 @@ function walkdir() {
ee.end = function () {
queue.kill();
};
ee.walk = function (filepath) {
queue.push(filepath);
};
ee.walk = walk;
ee.exists = exists;

function isDefined(value) {
return typeof value !== 'undefined';
}

function queuePush(value) {
queue.push(value);
function walk(path) {
queue.push({ action: 'walk', path: path });
}

function exists(path) {
queue.push({ action: 'exists', path: path });
}

function readdir(filepath, cb) {
fs.readdir(filepath, readdirOpts, onReaddir);
function process(data, cb) {
if (data.action === 'walk') {
fs.readdir(data.path, readdirOpts, onReaddir);
} else {
fs.stat(data.path, onStat);
}

function onStat(err, stat) {
if (err) {
// Ignore errors but also don't emit the path
return cb();
}

// `stat` has `isDirectory()` which is what we use from Dirent
ee.emit('path', data.path, stat);

cb();
}

function onReaddir(err, dirents) {
if (err) {
Expand All @@ -77,14 +96,14 @@ function walkdir() {
return cb(err);
}

dirs.filter(isDefined).forEach(queuePush);
dirs.filter(isDefined).forEach(walk);

cb();
});
}

function processDirents(dirent, key, cb) {
var nextpath = path.join(filepath, dirent.name);
var nextpath = path.join(data.path, dirent.name);
ee.emit('path', nextpath, dirent);

if (dirent.isDirectory()) {
Expand Down Expand Up @@ -138,6 +157,10 @@ function validateGlobs(globs) {
}
}

function isPositiveGlob(glob) {
return !isNegatedGlob(glob).negated;
}

function validateOptions(opts) {
if (typeof opts.cwd !== 'string') {
throw new Error('The `cwd` option must be a string');
Expand Down Expand Up @@ -251,7 +274,19 @@ function globStream(globs, opt) {
walker.on('path', onPath);
walker.once('end', onEnd);
walker.once('error', onError);
walker.walk(ourOpt.cwd);
ourGlobs.forEach(function (glob) {
if (isGlob(glob)) {
// We only want to walk the glob-parent directories of any positive glob
// to reduce the amount of files have to check.
if (isPositiveGlob(glob)) {
var base = globParent(glob);
walker.walk(base);
}
} else {
// If the strig is not a glob, we just check for the existence of it.
walker.exists(glob);
}
});

function read(cb) {
walker.resume();
Expand Down
53 changes: 52 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var sinon = require('sinon');

// Need to wrap this to cause walker to emit an error
var fs = require('fs');
var os = require('os');

var globStream = require('../');

Expand Down Expand Up @@ -370,6 +371,9 @@ function suite(moduleName) {
});

it('emits all objects (unordered) when given multiple absolute paths and no cwd', function (done) {
var testFile = path.join(os.tmpdir(), "glob-stream-test.txt");
fs.writeFileSync(testFile, "test");

var expected = [
{
cwd: cwd,
Expand All @@ -387,24 +391,71 @@ function suite(moduleName) {
base: dir + '/fixtures/whatsgoingon',
path: dir + '/fixtures/whatsgoingon/test.js',
},
{
cwd: cwd,
base: deWindows(os.tmpdir()),
path: deWindows(os.tmpdir()) + '/glob-stream-test.txt',
}
];

var paths = [
dir + '/fixtures/whatsgoingon/hey/isaidhey/whatsgoingon/test.txt',
dir + '/fixtures/test.coffee',
dir + '/fixtures/whatsgoingon/test.js',
os.tmpdir() + '/glob-stream-*.txt',
];

function assert(pathObjs) {
expect(pathObjs.length).toEqual(3);
fs.unlinkSync(testFile, "test");
expect(pathObjs.length).toEqual(4);
expect(pathObjs).toContainEqual(expected[0]);
expect(pathObjs).toContainEqual(expected[1]);
expect(pathObjs).toContainEqual(expected[2]);
expect(pathObjs).toContainEqual(expected[3]);
}

stream.pipeline([globStream(paths), concat(assert)], done);
});

it('resolves globs when process.chdir() changes the cwd', function (done) {
var prevCwd = process.cwd();
process.chdir('test/fixtures/stuff');

var expected = {
cwd: dir + '/fixtures/stuff',
base: dir + '/fixtures',
path: dir + '/fixtures/test.coffee',
};

function assert(pathObjs) {
process.chdir(prevCwd);

expect(pathObjs.length).toEqual(1);
expect(pathObjs[0]).toEqual(expected);
}

stream.pipeline([globStream(['../*.coffee']), concat(assert)], done);
});

// https://github.com/gulpjs/glob-stream/issues/129
it('does not take a long time when when checking a singular glob in the project root', function (done) {
// Extremely short timeout to ensure we aren't traversing node_modules
this.timeout(10);

var expected = {
cwd: cwd,
base: cwd,
path: cwd + '/package.json',
};

function assert(pathObjs) {
expect(pathObjs.length).toEqual(1);
expect(pathObjs[0]).toEqual(expected);
}

stream.pipeline([globStream(['./package.json']), concat(assert)], done);
});

it('removes duplicate objects from the stream using default (path) filter', function (done) {
var expected = {
cwd: dir,
Expand Down

0 comments on commit 76bcccd

Please sign in to comment.