Skip to content

Commit

Permalink
Use fs.statSync and fs.lstatSync safely. (#84)
Browse files Browse the repository at this point in the history
Use fs.statSync and fs.lstatSync safely.
Fix #82
  • Loading branch information
yuanchuan authored Apr 9, 2019
1 parent a680e20 commit aa70433
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
29 changes: 23 additions & 6 deletions lib/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ function matchObject(item, str) {
=== '[object ' + str + ']';
}

function checkStat(name, fn) {
try {
return fn(name);
} catch (err) {
if (/^(ENOENT|EPERM|EACCES)$/.test(err.code)) {
if (err.code !== 'ENOENT') {
console.warn('Warning: Cannot access %s', name);
}
return false;
}
throw err;
}
}

var is = {
nil: function(item) {
return item == null;
Expand Down Expand Up @@ -39,19 +53,22 @@ var is = {
return fs.existsSync(name);
},
file: function(name) {
return is.exists(name)
? fs.statSync(name).isFile() : false;
return checkStat(name, function(n) {
return fs.statSync(n).isFile()
});
},
samePath: function(a, b) {
return path.resolve(a) === path.resolve(b);
},
directory: function(name) {
return is.exists(name)
? fs.statSync(name).isDirectory() : false;
return checkStat(name, function(n) {
return fs.statSync(n).isDirectory()
});
},
symbolicLink: function(name) {
return is.exists(name)
? fs.lstatSync(name).isSymbolicLink() : false;
return checkStat(name, function(n) {
return fs.lstatSync(n).isSymbolicLink();
});
},
windows: function() {
return os.platform() === 'win32';
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ describe('options', function() {
delay: 0,
recursive: true,
filter: function(name) {
return !/file1/.test(name);
return /bb\/file2/.test(name);
}
}

Expand Down

0 comments on commit aa70433

Please sign in to comment.