Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: export constants from fs/promises #43177

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,7 @@ with an {Error} object. The following example checks if the file
`/etc/passwd` can be read and written by the current process.

```mjs
import { access } from 'node:fs/promises';
import { constants } from 'node:fs';
import { access, constants } from 'node:fs/promises';

try {
await access('/etc/passwd', constants.R_OK | constants.W_OK);
Expand Down Expand Up @@ -892,8 +891,7 @@ error occurs after the destination file has been opened for writing, an attempt
will be made to remove the destination.

```mjs
import { constants } from 'node:fs';
import { copyFile } from 'node:fs/promises';
import { copyFile, constants } from 'node:fs/promises';

try {
await copyFile('source.txt', 'destination.txt');
Expand Down Expand Up @@ -1624,6 +1622,14 @@ try {
Aborting an ongoing request does not abort individual operating
system requests but rather the internal buffering `fs.writeFile` performs.

### `fsPromises.constants`

* {Object}

Returns an object containing commonly used constants for file system
operations. The object is the same as `fs.constants`. See [FS constants][]
for more details.

## Callback API

The callback APIs perform all operations asynchronously, without blocking the
Expand Down Expand Up @@ -6885,7 +6891,7 @@ operations.

#### FS constants

The following constants are exported by `fs.constants`.
The following constants are exported by `fs.constants` and `fsPromises.constants`.

Not every constant will be available on every operating system;
this is especially important for Windows, where many of the POSIX specific
Expand Down Expand Up @@ -7590,6 +7596,7 @@ the file contents.

[#25741]: https://github.com/nodejs/node/issues/25741
[Common System Errors]: errors.md#common-system-errors
[FS constants]: #fs-constants
[File access constants]: #file-access-constants
[MDN-Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
[MDN-Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ const {
Uint8Array,
} = primordials;

const { fs: constants } = internalBinding('constants');
const {
F_OK,
O_SYMLINK,
O_WRONLY,
S_IFMT,
S_IFREG
} = internalBinding('constants').fs;
} = constants;

const binding = internalBinding('fs');
const { Buffer } = require('buffer');

Expand Down Expand Up @@ -899,6 +901,7 @@ module.exports = {
appendFile,
readFile,
watch,
constants,
},

FileHandle,
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-fs-promises-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

require('../common');
const assert = require('assert');
const fs = require('fs');
const fsPromises = require('fs/promises');

assert.strictEqual(require('fs/promises'), require('fs').promises);
assert.strictEqual(fsPromises, fs.promises);
assert.strictEqual(fsPromises.constants, fs.constants);