From 482b56ae60bb14588ee57dd92b48158d5f7c2f23 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 23 Oct 2018 22:26:57 +0200 Subject: fs: default open/openSync flags argument to 'r' Make fs.open() and fs.openSync() more economic to use by making the flags argument optional. You can now write: fs.open(file, cb) Instead of the more verbose: fs.open(file, 'r', cb) This idiom is already supported by functions like fs.readFile(). PR-URL: https://github.com/nodejs/node/pull/23767 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Colin Ihrig Reviewed-By: Refael Ackermann Reviewed-By: Trivikram Kamat --- lib/fs.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'lib/fs.js') diff --git a/lib/fs.js b/lib/fs.js index bf964f5e99..89c005375f 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -345,7 +345,7 @@ function tryReadSync(fd, isUserFd, buffer, pos, len) { function readFileSync(path, options) { options = getOptions(options, { flag: 'r' }); const isUserFd = isFd(path); // file descriptor ownership - const fd = isUserFd ? path : fs.openSync(path, options.flag || 'r', 0o666); + const fd = isUserFd ? path : fs.openSync(path, options.flag, 0o666); const stats = tryStatSync(fd, isUserFd); const size = isFileType(stats, S_IFREG) ? stats[8] : 0; @@ -411,14 +411,19 @@ function closeSync(fd) { function open(path, flags, mode, callback) { path = toPathIfFileURL(path); validatePath(path); - const flagsNumber = stringToFlags(flags); - if (arguments.length < 4) { - callback = makeCallback(mode); + if (arguments.length < 3) { + callback = flags; + flags = 'r'; mode = 0o666; - } else { + } else if (arguments.length === 3) { + callback = mode; + mode = 0o666; + } + const flagsNumber = stringToFlags(flags); + if (arguments.length >= 4) { mode = validateMode(mode, 'mode', 0o666); - callback = makeCallback(callback); } + callback = makeCallback(callback); const req = new FSReqCallback(); req.oncomplete = callback; @@ -433,7 +438,7 @@ function open(path, flags, mode, callback) { function openSync(path, flags, mode) { path = toPathIfFileURL(path); validatePath(path); - const flagsNumber = stringToFlags(flags); + const flagsNumber = stringToFlags(flags || 'r'); mode = validateMode(mode, 'mode', 0o666); const ctx = { path }; -- cgit v1.2.1