Skip to content

Commit

Permalink
fs: handle result of access binding directly in fs.existsSync
Browse files Browse the repository at this point in the history
Instead of throwing errors in fs.accessSync and then catching it,
handle the result from the binding directly in fs.existsSync.

Note that the argument validation errors still needs to be caught
until we properly deprecate the don't-thrown-on-invalid-arguments
behavior.

PR-URL: #24015
Fixes: #24008
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information
joyeecheung authored and MylesBorins committed Dec 3, 2018
1 parent 1b6c00e commit 765a3a9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,14 @@ Object.defineProperty(exists, internalUtil.promisify.custom, {
// TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior
function existsSync(path) {
try {
fs.accessSync(path, F_OK);
return true;
path = toPathIfFileURL(path);
validatePath(path);
} catch (e) {
return false;
}
const ctx = { path };
binding.access(pathModule.toNamespacedPath(path), F_OK, undefined, ctx);
return ctx.errno === undefined;
}

function readFileAfterOpen(err, fd) {
Expand Down

0 comments on commit 765a3a9

Please sign in to comment.