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 targos committed Nov 5, 2018
1 parent 7ee0cea commit 424be28
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 @@ -228,11 +228,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 424be28

Please sign in to comment.