From 765a3a946600236b4cf60e6bd8c9b32a23cdb591 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 1 Nov 2018 18:04:57 +0800 Subject: [PATCH] fs: handle result of access binding directly in fs.existsSync 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: https://github.com/nodejs/node/pull/24015 Fixes: https://github.com/nodejs/node/issues/24008 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Refael Ackermann --- lib/fs.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 9e74beee285f2d..56421924fd287a 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -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) {