Skip to content

Commit 2ffb9d6

Browse files
ChALkeRtargos
authored andcommitted
fs: drop duplicate API in promises mode
This drops exporting duplicate methods that accept FileHandle as the first argument (to mirror callback-based methods accepting 'fd'). Those methods were not adding actual value to the API because all of those are already present as FileHandle methods, and they would probably be confusing to the new users and making docs harder to read. Also, the API was a bit inconsistent and lacked .close(handle). Fixes: #20548 PR-URL: #20559 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Backport-PR-URL: #21172
1 parent a5c5714 commit 2ffb9d6

File tree

4 files changed

+8
-214
lines changed

4 files changed

+8
-214
lines changed

benchmark/fs/bench-stat-promise.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const bench = common.createBenchmark(main, {
99
});
1010

1111
async function run(n, statType) {
12-
const arg = statType === 'fstat' ?
13-
await fsPromises.open(__filename, 'r') : __filename;
12+
const handleMode = statType === 'fstat';
13+
const arg = handleMode ? await fsPromises.open(__filename, 'r') : __filename;
1414
let remaining = n;
1515
bench.start();
1616
while (remaining-- > 0)
17-
await fsPromises[statType](arg);
17+
await (handleMode ? arg.stat() : fsPromises[statType](arg));
1818
bench.end(n);
1919

2020
if (typeof arg.close === 'function')

doc/api/fs.md

-184
Original file line numberDiff line numberDiff line change
@@ -3800,128 +3800,6 @@ fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL)
38003800
.catch(() => console.log('The file could not be copied'));
38013801
```
38023802

3803-
### fsPromises.fchmod(filehandle, mode)
3804-
<!-- YAML
3805-
added: v10.0.0
3806-
-->
3807-
3808-
* `filehandle` {FileHandle}
3809-
* `mode` {integer}
3810-
* Returns: {Promise}
3811-
3812-
Asynchronous fchmod(2). The `Promise` is resolved with no arguments upon
3813-
success.
3814-
3815-
### fsPromises.fchown(filehandle, uid, gid)
3816-
<!-- YAML
3817-
added: v10.0.0
3818-
-->
3819-
3820-
* `filehandle` {FileHandle}
3821-
* `uid` {integer}
3822-
* `gid` {integer}
3823-
* Returns: {Promise}
3824-
3825-
Changes the ownership of the file represented by `filehandle` then resolves
3826-
the `Promise` with no arguments upon success.
3827-
3828-
### fsPromises.fdatasync(filehandle)
3829-
<!-- YAML
3830-
added: v10.0.0
3831-
-->
3832-
3833-
* `filehandle` {FileHandle}
3834-
* Returns: {Promise}
3835-
3836-
Asynchronous fdatasync(2). The `Promise` is resolved with no arguments upon
3837-
success.
3838-
3839-
### fsPromises.fstat(filehandle)
3840-
<!-- YAML
3841-
added: v10.0.0
3842-
-->
3843-
3844-
* `filehandle` {FileHandle}
3845-
* Returns: {Promise}
3846-
3847-
Retrieves the [`fs.Stats`][] for the given `filehandle`.
3848-
3849-
### fsPromises.fsync(filehandle)
3850-
<!-- YAML
3851-
added: v10.0.0
3852-
-->
3853-
3854-
* `filehandle` {FileHandle}
3855-
* Returns: {Promise}
3856-
3857-
Asynchronous fsync(2). The `Promise` is resolved with no arguments upon
3858-
success.
3859-
3860-
### fsPromises.ftruncate(filehandle[, len])
3861-
<!-- YAML
3862-
added: v10.0.0
3863-
-->
3864-
3865-
* `filehandle` {FileHandle}
3866-
* `len` {integer} **Default:** `0`
3867-
* Returns: {Promise}
3868-
3869-
Truncates the file represented by `filehandle` then resolves the `Promise`
3870-
with no arguments upon success.
3871-
3872-
If the file referred to by the `FileHandle` was larger than `len` bytes, only
3873-
the first `len` bytes will be retained in the file.
3874-
3875-
For example, the following program retains only the first four bytes of the
3876-
file:
3877-
3878-
```js
3879-
console.log(fs.readFileSync('temp.txt', 'utf8'));
3880-
// Prints: Node.js
3881-
3882-
async function doTruncate() {
3883-
const fd = await fsPromises.open('temp.txt', 'r+');
3884-
await fsPromises.ftruncate(fd, 4);
3885-
console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node
3886-
}
3887-
3888-
doTruncate().catch(console.error);
3889-
```
3890-
3891-
If the file previously was shorter than `len` bytes, it is extended, and the
3892-
extended part is filled with null bytes (`'\0'`). For example,
3893-
3894-
```js
3895-
console.log(fs.readFileSync('temp.txt', 'utf8'));
3896-
// Prints: Node.js
3897-
3898-
async function doTruncate() {
3899-
const fd = await fsPromises.open('temp.txt', 'r+');
3900-
await fsPromises.ftruncate(fd, 10);
3901-
console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints Node.js\0\0\0
3902-
}
3903-
3904-
doTruncate().catch(console.error);
3905-
```
3906-
3907-
The last three bytes are null bytes (`'\0'`), to compensate the over-truncation.
3908-
3909-
### fsPromises.futimes(filehandle, atime, mtime)
3910-
<!-- YAML
3911-
added: v10.0.0
3912-
-->
3913-
3914-
* `filehandle` {FileHandle}
3915-
* `atime` {number|string|Date}
3916-
* `mtime` {number|string|Date}
3917-
* Returns: {Promise}
3918-
3919-
Change the file system timestamps of the object referenced by the supplied
3920-
`FileHandle` then resolves the `Promise` with no arguments upon success.
3921-
3922-
This function does not work on AIX versions before 7.1, it will resolve the
3923-
`Promise` with an error using code `UV_ENOSYS`.
3924-
39253803
### fsPromises.lchmod(path, mode)
39263804
<!-- YAML
39273805
deprecated: v10.0.0
@@ -4030,35 +3908,6 @@ by [Naming Files, Paths, and Namespaces][]. Under NTFS, if the filename contains
40303908
a colon, Node.js will open a file system stream, as described by
40313909
[this MSDN page][MSDN-Using-Streams].
40323910

4033-
### fsPromises.read(filehandle, buffer, offset, length, position)
4034-
<!-- YAML
4035-
added: v10.0.0
4036-
-->
4037-
4038-
* `filehandle` {FileHandle}
4039-
* `buffer` {Buffer|Uint8Array}
4040-
* `offset` {integer}
4041-
* `length` {integer}
4042-
* `position` {integer}
4043-
* Returns: {Promise}
4044-
4045-
Read data from the file specified by `filehandle`.
4046-
4047-
`buffer` is the buffer that the data will be written to.
4048-
4049-
`offset` is the offset in the buffer to start writing at.
4050-
4051-
`length` is an integer specifying the number of bytes to read.
4052-
4053-
`position` is an argument specifying where to begin reading from in the file.
4054-
If `position` is `null`, data will be read from the current file position,
4055-
and the file position will be updated.
4056-
If `position` is an integer, the file position will remain unchanged.
4057-
4058-
Following successful read, the `Promise` is resolved with an object with a
4059-
`bytesRead` property specifying the number of bytes read, and a `buffer`
4060-
property that is a reference to the passed in `buffer` argument.
4061-
40623911
### fsPromises.readdir(path[, options])
40633912
<!-- YAML
40643913
added: v10.0.0
@@ -4243,39 +4092,6 @@ The `atime` and `mtime` arguments follow these rules:
42434092
- If the value can not be converted to a number, or is `NaN`, `Infinity` or
42444093
`-Infinity`, an `Error` will be thrown.
42454094

4246-
### fsPromises.write(filehandle, buffer[, offset[, length[, position]]])
4247-
<!-- YAML
4248-
added: v10.0.0
4249-
-->
4250-
4251-
* `filehandle` {FileHandle}
4252-
* `buffer` {Buffer|Uint8Array}
4253-
* `offset` {integer}
4254-
* `length` {integer}
4255-
* `position` {integer}
4256-
* Returns: {Promise}
4257-
4258-
Write `buffer` to the file specified by `filehandle`.
4259-
4260-
The `Promise` is resolved with an object containing a `bytesWritten` property
4261-
identifying the number of bytes written, and a `buffer` property containing
4262-
a reference to the `buffer` written.
4263-
4264-
`offset` determines the part of the buffer to be written, and `length` is
4265-
an integer specifying the number of bytes to write.
4266-
4267-
`position` refers to the offset from the beginning of the file where this data
4268-
should be written. If `typeof position !== 'number'`, the data will be written
4269-
at the current position. See pwrite(2).
4270-
4271-
It is unsafe to use `fsPromises.write()` multiple times on the same file
4272-
without waiting for the `Promise` to be resolved (or rejected). For this
4273-
scenario, `fs.createWriteStream` is strongly recommended.
4274-
4275-
On Linux, positional writes do not work when the file is opened in append mode.
4276-
The kernel ignores the position argument and always appends the data to
4277-
the end of the file.
4278-
42794095
### fsPromises.writeFile(file, data[, options])
42804096
<!-- YAML
42814097
added: v10.0.0

lib/internal/fs/promises.js

-9
Original file line numberDiff line numberDiff line change
@@ -466,31 +466,22 @@ module.exports = {
466466
access,
467467
copyFile,
468468
open,
469-
read,
470-
write,
471469
rename,
472470
truncate,
473-
ftruncate,
474471
rmdir,
475-
fdatasync,
476-
fsync,
477472
mkdir,
478473
readdir,
479474
readlink,
480475
symlink,
481-
fstat,
482476
lstat,
483477
stat,
484478
link,
485479
unlink,
486-
fchmod,
487480
chmod,
488481
lchmod,
489482
lchown,
490-
fchown,
491483
chown,
492484
utimes,
493-
futimes,
494485
realpath,
495486
mkdtemp,
496487
writeFile,

test/parallel/test-fs-promises.js

+5-18
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,19 @@ const {
1111
access,
1212
chmod,
1313
copyFile,
14-
fchmod,
15-
fdatasync,
16-
fstat,
17-
fsync,
18-
ftruncate,
19-
futimes,
2014
link,
2115
lchmod,
2216
lstat,
2317
mkdir,
2418
mkdtemp,
2519
open,
26-
read,
2720
readdir,
2821
readlink,
2922
realpath,
3023
rename,
3124
rmdir,
3225
stat,
3326
symlink,
34-
write,
3527
unlink,
3628
utimes
3729
} = fsPromises;
@@ -75,13 +67,13 @@ function verifyStatObject(stat) {
7567
const handle = await open(dest, 'r+');
7668
assert.strictEqual(typeof handle, 'object');
7769

78-
let stats = await fstat(handle);
70+
let stats = await handle.stat();
7971
verifyStatObject(stats);
8072
assert.strictEqual(stats.size, 35);
8173

82-
await ftruncate(handle, 1);
74+
await handle.truncate(1);
8375

84-
stats = await fstat(handle);
76+
stats = await handle.stat();
8577
verifyStatObject(stats);
8678
assert.strictEqual(stats.size, 1);
8779

@@ -91,15 +83,13 @@ function verifyStatObject(stat) {
9183
stats = await handle.stat();
9284
verifyStatObject(stats);
9385

94-
await fdatasync(handle);
9586
await handle.datasync();
96-
await fsync(handle);
9787
await handle.sync();
9888

9989
const buf = Buffer.from('hello fsPromises');
10090
const bufLen = buf.length;
101-
await write(handle, buf);
102-
const ret = await read(handle, Buffer.alloc(bufLen), 0, bufLen, 0);
91+
await handle.write(buf);
92+
const ret = await handle.read(Buffer.alloc(bufLen), 0, bufLen, 0);
10393
assert.strictEqual(ret.bytesRead, bufLen);
10494
assert.deepStrictEqual(ret.buffer, buf);
10595

@@ -111,18 +101,15 @@ function verifyStatObject(stat) {
111101
assert.deepStrictEqual(ret2.buffer, buf2);
112102

113103
await chmod(dest, 0o666);
114-
await fchmod(handle, 0o666);
115104
await handle.chmod(0o666);
116105

117106
// Mode larger than 0o777 should be masked off.
118107
await chmod(dest, (0o777 + 1));
119-
await fchmod(handle, 0o777 + 1);
120108
await handle.chmod(0o777 + 1);
121109

122110
await utimes(dest, new Date(), new Date());
123111

124112
try {
125-
await futimes(handle, new Date(), new Date());
126113
await handle.utimes(new Date(), new Date());
127114
} catch (err) {
128115
// Some systems do not have futimes. If there is an error,

0 commit comments

Comments
 (0)