GeezerVison is showing github.com — fs: drop duplicate API in promises mode · nodejs/node@2ffb9d6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/fs/bench-stat-promise.js +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,12 +9,12 @@ const bench = common.createBenchmark(main, { | 9 | 9 | }); | 10 | 10 |
| 11 | 11 | 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; | 14 | 14 | let remaining = n; | 15 | 15 | bench.start(); | 16 | 16 | while (remaining-- > 0) | 17 | | - await fsPromises[statType](arg); | | 17 | + await (handleMode ? arg.stat() : fsPromises[statType](arg)); | 18 | 18 | bench.end(n); | 19 | 19 |
| 20 | 20 | if (typeof arg.close === 'function') | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.md -184Lines changed: 0 additions & 184 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3800,128 +3800,6 @@ fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL) | 3800 | 3800 | .catch(() => console.log('The file could not be copied')); | 3801 | 3801 | ``` | 3802 | 3802 |
| 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 | | - | 3925 | 3803 | ### fsPromises.lchmod(path, mode) | 3926 | 3804 | <!-- YAML | 3927 | 3805 | deprecated: v10.0.0 | @@ -4030,35 +3908,6 @@ by [Naming Files, Paths, and Namespaces][]. Under NTFS, if the filename contains | 4030 | 3908 | a colon, Node.js will open a file system stream, as described by | 4031 | 3909 | [this MSDN page][MSDN-Using-Streams]. | 4032 | 3910 |
| 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 | | - | 4062 | 3911 | ### fsPromises.readdir(path[, options]) | 4063 | 3912 | <!-- YAML | 4064 | 3913 | added: v10.0.0 | @@ -4243,39 +4092,6 @@ The `atime` and `mtime` arguments follow these rules: | 4243 | 4092 | - If the value can not be converted to a number, or is `NaN`, `Infinity` or | 4244 | 4093 | `-Infinity`, an `Error` will be thrown. | 4245 | 4094 |
| 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 | | - | 4279 | 4095 | ### fsPromises.writeFile(file, data[, options]) | 4280 | 4096 | <!-- YAML | 4281 | 4097 | added: v10.0.0 | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js -9Lines changed: 0 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -466,31 +466,22 @@ module.exports = { | 466 | 466 | access, | 467 | 467 | copyFile, | 468 | 468 | open, | 469 | | - read, | 470 | | - write, | 471 | 469 | rename, | 472 | 470 | truncate, | 473 | | - ftruncate, | 474 | 471 | rmdir, | 475 | | - fdatasync, | 476 | | - fsync, | 477 | 472 | mkdir, | 478 | 473 | readdir, | 479 | 474 | readlink, | 480 | 475 | symlink, | 481 | | - fstat, | 482 | 476 | lstat, | 483 | 477 | stat, | 484 | 478 | link, | 485 | 479 | unlink, | 486 | | - fchmod, | 487 | 480 | chmod, | 488 | 481 | lchmod, | 489 | 482 | lchown, | 490 | | - fchown, | 491 | 483 | chown, | 492 | 484 | utimes, | 493 | | - futimes, | 494 | 485 | realpath, | 495 | 486 | mkdtemp, | 496 | 487 | writeFile, | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises.js +5-18Lines changed: 5 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,27 +11,19 @@ const { | 11 | 11 | access, | 12 | 12 | chmod, | 13 | 13 | copyFile, | 14 | | - fchmod, | 15 | | - fdatasync, | 16 | | - fstat, | 17 | | - fsync, | 18 | | - ftruncate, | 19 | | - futimes, | 20 | 14 | link, | 21 | 15 | lchmod, | 22 | 16 | lstat, | 23 | 17 | mkdir, | 24 | 18 | mkdtemp, | 25 | 19 | open, | 26 | | - read, | 27 | 20 | readdir, | 28 | 21 | readlink, | 29 | 22 | realpath, | 30 | 23 | rename, | 31 | 24 | rmdir, | 32 | 25 | stat, | 33 | 26 | symlink, | 34 | | - write, | 35 | 27 | unlink, | 36 | 28 | utimes | 37 | 29 | } = fsPromises; | @@ -75,13 +67,13 @@ function verifyStatObject(stat) { | 75 | 67 | const handle = await open(dest, 'r+'); | 76 | 68 | assert.strictEqual(typeof handle, 'object'); | 77 | 69 |
| 78 | | - let stats = await fstat(handle); | | 70 | + let stats = await handle.stat(); | 79 | 71 | verifyStatObject(stats); | 80 | 72 | assert.strictEqual(stats.size, 35); | 81 | 73 |
| 82 | | - await ftruncate(handle, 1); | | 74 | + await handle.truncate(1); | 83 | 75 |
| 84 | | - stats = await fstat(handle); | | 76 | + stats = await handle.stat(); | 85 | 77 | verifyStatObject(stats); | 86 | 78 | assert.strictEqual(stats.size, 1); | 87 | 79 |
| @@ -91,15 +83,13 @@ function verifyStatObject(stat) { | 91 | 83 | stats = await handle.stat(); | 92 | 84 | verifyStatObject(stats); | 93 | 85 |
| 94 | | - await fdatasync(handle); | 95 | 86 | await handle.datasync(); | 96 | | - await fsync(handle); | 97 | 87 | await handle.sync(); | 98 | 88 |
| 99 | 89 | const buf = Buffer.from('hello fsPromises'); | 100 | 90 | 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); | 103 | 93 | assert.strictEqual(ret.bytesRead, bufLen); | 104 | 94 | assert.deepStrictEqual(ret.buffer, buf); | 105 | 95 |
| @@ -111,18 +101,15 @@ function verifyStatObject(stat) { | 111 | 101 | assert.deepStrictEqual(ret2.buffer, buf2); | 112 | 102 |
| 113 | 103 | await chmod(dest, 0o666); | 114 | | - await fchmod(handle, 0o666); | 115 | 104 | await handle.chmod(0o666); | 116 | 105 |
| 117 | 106 | // Mode larger than 0o777 should be masked off. | 118 | 107 | await chmod(dest, (0o777 + 1)); | 119 | | - await fchmod(handle, 0o777 + 1); | 120 | 108 | await handle.chmod(0o777 + 1); | 121 | 109 |
| 122 | 110 | await utimes(dest, new Date(), new Date()); | 123 | 111 |
| 124 | 112 | try { | 125 | | - await futimes(handle, new Date(), new Date()); | 126 | 113 | await handle.utimes(new Date(), new Date()); | 127 | 114 | } catch (err) { | 128 | 115 | // Some systems do not have futimes. If there is an error, | |
|