Skip to content

Commit 3516a27

Browse files
bnoordhuisBethGriggs
authored andcommitted
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: #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> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent bda45a5 commit 3516a27

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

Diff for: doc/api/fs.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ this API: [`fs.mkdtemp()`][].
23042304
The optional `options` argument can be a string specifying an encoding, or an
23052305
object with an `encoding` property specifying the character encoding to use.
23062306

2307-
## fs.open(path, flags[, mode], callback)
2307+
## fs.open(path[, flags[, mode]], callback)
23082308
<!-- YAML
23092309
added: v0.0.2
23102310
changes:
@@ -2322,6 +2322,7 @@ changes:
23222322

23232323
* `path` {string|Buffer|URL}
23242324
* `flags` {string|number} See [support of file system `flags`][].
2325+
**Default:** `'r'`.
23252326
* `mode` {integer} **Default:** `0o666` (readable and writable)
23262327
* `callback` {Function}
23272328
* `err` {Error}
@@ -2343,7 +2344,7 @@ a colon, Node.js will open a file system stream, as described by
23432344
Functions based on `fs.open()` exhibit this behavior as well:
23442345
`fs.writeFile()`, `fs.readFile()`, etc.
23452346

2346-
## fs.openSync(path, flags[, mode])
2347+
## fs.openSync(path[, flags, mode])
23472348
<!-- YAML
23482349
added: v0.1.21
23492350
changes:
@@ -2360,7 +2361,8 @@ changes:
23602361
-->
23612362

23622363
* `path` {string|Buffer|URL}
2363-
* `flags` {string|number} See [support of file system `flags`][].
2364+
* `flags` {string|number} **Default:** `'r'`.
2365+
See [support of file system `flags`][].
23642366
* `mode` {integer} **Default:** `0o666`
23652367
* Returns: {number}
23662368

Diff for: lib/fs.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ function tryReadSync(fd, isUserFd, buffer, pos, len) {
340340
function readFileSync(path, options) {
341341
options = getOptions(options, { flag: 'r' });
342342
const isUserFd = isFd(path); // file descriptor ownership
343-
const fd = isUserFd ? path : fs.openSync(path, options.flag || 'r', 0o666);
343+
const fd = isUserFd ? path : fs.openSync(path, options.flag, 0o666);
344344

345345
const stats = tryStatSync(fd, isUserFd);
346346
const size = isFileType(stats, S_IFREG) ? stats[8] : 0;
@@ -406,14 +406,19 @@ function closeSync(fd) {
406406
function open(path, flags, mode, callback) {
407407
path = toPathIfFileURL(path);
408408
validatePath(path);
409-
const flagsNumber = stringToFlags(flags);
410-
if (typeof mode === 'function') {
411-
callback = makeCallback(mode);
409+
if (arguments.length < 3) {
410+
callback = flags;
411+
flags = 'r';
412412
mode = 0o666;
413-
} else {
413+
} else if (arguments.length === 3) {
414+
callback = mode;
415+
mode = 0o666;
416+
}
417+
const flagsNumber = stringToFlags(flags);
418+
if (arguments.length >= 4) {
414419
mode = validateMode(mode, 'mode', 0o666);
415-
callback = makeCallback(callback);
416420
}
421+
callback = makeCallback(callback);
417422

418423
const req = new FSReqWrap();
419424
req.oncomplete = callback;
@@ -428,7 +433,7 @@ function open(path, flags, mode, callback) {
428433
function openSync(path, flags, mode) {
429434
path = toPathIfFileURL(path);
430435
validatePath(path);
431-
const flagsNumber = stringToFlags(flags);
436+
const flagsNumber = stringToFlags(flags || 'r');
432437
mode = validateMode(mode, 'mode', 0o666);
433438

434439
const ctx = { path };

Diff for: lib/internal/fs/promises.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,12 @@ async function copyFile(src, dest, flags) {
195195
async function open(path, flags, mode) {
196196
path = toPathIfFileURL(path);
197197
validatePath(path);
198+
if (arguments.length < 2) flags = 'r';
199+
const flagsNumber = stringToFlags(flags);
198200
mode = validateMode(mode, 'mode', 0o666);
199201
return new FileHandle(
200202
await binding.openFileHandle(pathModule.toNamespacedPath(path),
201-
stringToFlags(flags),
202-
mode, kUsePromises));
203+
flagsNumber, mode, kUsePromises));
203204
}
204205

205206
async function read(handle, buffer, offset, length, position) {

Diff for: test/parallel/test-fs-open.js

+50
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ try {
3636
}
3737
assert.strictEqual(caughtException, true);
3838

39+
fs.openSync(__filename);
40+
41+
fs.open(__filename, common.mustCall((err) => {
42+
assert.ifError(err);
43+
}));
44+
3945
fs.open(__filename, 'r', common.mustCall((err) => {
4046
assert.ifError(err);
4147
}));
@@ -44,6 +50,39 @@ fs.open(__filename, 'rs', common.mustCall((err) => {
4450
assert.ifError(err);
4551
}));
4652

53+
fs.open(__filename, 'r', 0, common.mustCall((err) => {
54+
assert.ifError(err);
55+
}));
56+
57+
fs.open(__filename, 'r', null, common.mustCall((err) => {
58+
assert.ifError(err);
59+
}));
60+
61+
async function promise() {
62+
await fs.promises.open(__filename);
63+
await fs.promises.open(__filename, 'r');
64+
}
65+
66+
promise().then(common.mustCall()).catch(common.mustNotCall());
67+
68+
common.expectsError(
69+
() => fs.open(__filename, 'r', 'boom', common.mustNotCall()),
70+
{
71+
code: 'ERR_INVALID_ARG_VALUE',
72+
type: TypeError
73+
}
74+
);
75+
76+
for (const extra of [[], ['r'], ['r', 0], ['r', 0, 'bad callback']]) {
77+
common.expectsError(
78+
() => fs.open(__filename, ...extra),
79+
{
80+
code: 'ERR_INVALID_CALLBACK',
81+
type: TypeError
82+
}
83+
);
84+
}
85+
4786
[false, 1, [], {}, null, undefined].forEach((i) => {
4887
common.expectsError(
4988
() => fs.open(i, 'r', common.mustNotCall()),
@@ -59,4 +98,15 @@ fs.open(__filename, 'rs', common.mustCall((err) => {
5998
type: TypeError
6099
}
61100
);
101+
fs.promises.open(i, 'r')
102+
.then(common.mustNotCall())
103+
.catch(common.mustCall((err) => {
104+
common.expectsError(
105+
() => { throw err; },
106+
{
107+
code: 'ERR_INVALID_ARG_TYPE',
108+
type: TypeError
109+
}
110+
);
111+
}));
62112
});

0 commit comments

Comments
 (0)