From 9d12a5fabd766cb70f3b395a390a43fb0b69bbaa Mon Sep 17 00:00:00 2001 From: Feng Yu Date: Fri, 3 Jun 2022 16:24:08 +0800 Subject: [PATCH] fs: export constants from `fs/promises` PR-URL: https://github.com/nodejs/node/pull/43177 Reviewed-By: LiviaMedeiros Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel --- doc/api/fs.md | 17 ++++++++++++----- lib/internal/fs/promises.js | 5 ++++- test/parallel/test-fs-promises-exists.js | 5 ++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index d47e6ef5c2c179..3969d5d6f8282a 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -740,8 +740,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); @@ -843,8 +842,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'); @@ -1595,6 +1593,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 @@ -6615,7 +6621,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 @@ -7320,6 +7326,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 diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 346771c506ca3a..ee41318d3b1eb1 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -16,13 +16,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'); @@ -847,6 +849,7 @@ module.exports = { appendFile, readFile, watch, + constants, }, FileHandle, diff --git a/test/parallel/test-fs-promises-exists.js b/test/parallel/test-fs-promises-exists.js index d56308257e6b17..b9bbeee1de97fd 100644 --- a/test/parallel/test-fs-promises-exists.js +++ b/test/parallel/test-fs-promises-exists.js @@ -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);