Skip to content

Commit f765693

Browse files
authored
fs: export constants from fs/promises
PR-URL: nodejs#43177 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 06d8606 commit f765693

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

doc/api/fs.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,7 @@ with an {Error} object. The following example checks if the file
789789
`/etc/passwd` can be read and written by the current process.
790790
791791
```mjs
792-
import { access } from 'node:fs/promises';
793-
import { constants } from 'node:fs';
792+
import { access, constants } from 'node:fs/promises';
794793

795794
try {
796795
await access('/etc/passwd', constants.R_OK | constants.W_OK);
@@ -892,8 +891,7 @@ error occurs after the destination file has been opened for writing, an attempt
892891
will be made to remove the destination.
893892
894893
```mjs
895-
import { constants } from 'node:fs';
896-
import { copyFile } from 'node:fs/promises';
894+
import { copyFile, constants } from 'node:fs/promises';
897895

898896
try {
899897
await copyFile('source.txt', 'destination.txt');
@@ -1624,6 +1622,14 @@ try {
16241622
Aborting an ongoing request does not abort individual operating
16251623
system requests but rather the internal buffering `fs.writeFile` performs.
16261624
1625+
### `fsPromises.constants`
1626+
1627+
* {Object}
1628+
1629+
Returns an object containing commonly used constants for file system
1630+
operations. The object is the same as `fs.constants`. See [FS constants][]
1631+
for more details.
1632+
16271633
## Callback API
16281634
16291635
The callback APIs perform all operations asynchronously, without blocking the
@@ -6885,7 +6891,7 @@ operations.
68856891

68866892
#### FS constants
68876893

6888-
The following constants are exported by `fs.constants`.
6894+
The following constants are exported by `fs.constants` and `fsPromises.constants`.
68896895

68906896
Not every constant will be available on every operating system;
68916897
this is especially important for Windows, where many of the POSIX specific
@@ -7590,6 +7596,7 @@ the file contents.
75907596
75917597
[#25741]: https://github.com/nodejs/node/issues/25741
75927598
[Common System Errors]: errors.md#common-system-errors
7599+
[FS constants]: #fs-constants
75937600
[File access constants]: #file-access-constants
75947601
[MDN-Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
75957602
[MDN-Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type

lib/internal/fs/promises.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ const {
1717
Uint8Array,
1818
} = primordials;
1919

20+
const { fs: constants } = internalBinding('constants');
2021
const {
2122
F_OK,
2223
O_SYMLINK,
2324
O_WRONLY,
2425
S_IFMT,
2526
S_IFREG
26-
} = internalBinding('constants').fs;
27+
} = constants;
28+
2729
const binding = internalBinding('fs');
2830
const { Buffer } = require('buffer');
2931

@@ -899,6 +901,7 @@ module.exports = {
899901
appendFile,
900902
readFile,
901903
watch,
904+
constants,
902905
},
903906

904907
FileHandle,

test/parallel/test-fs-promises-exists.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22

33
require('../common');
44
const assert = require('assert');
5+
const fs = require('fs');
6+
const fsPromises = require('fs/promises');
57

6-
assert.strictEqual(require('fs/promises'), require('fs').promises);
8+
assert.strictEqual(fsPromises, fs.promises);
9+
assert.strictEqual(fsPromises.constants, fs.constants);

0 commit comments

Comments
 (0)