summaryrefslogtreecommitdiff
path: root/lib/fs.js
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2018-10-23 22:26:57 +0200
committerRich Trott <rtrott@gmail.com>2018-10-26 06:35:21 -0700
commit482b56ae60bb14588ee57dd92b48158d5f7c2f23 (patch)
tree7a3cefd98c2657486819cf601e4d7b88c27b358c /lib/fs.js
parent6223236151f25975e380eef8470243ff8cf3f61d (diff)
downloadnode-new-482b56ae60bb14588ee57dd92b48158d5f7c2f23.tar.gz
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 <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'lib/fs.js')
-rw-r--r--lib/fs.js19
1 files changed, 12 insertions, 7 deletions
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 };