fs: make mutating options in Promises readdir() not affect results

PR-URL: https://github.com/nodejs/node/pull/56057
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
LiviaMedeiros
2024-11-29 00:52:15 +08:00
committed by Node.js GitHub Bot
parent 743eacc48f
commit b5e25aa2ce
2 changed files with 12 additions and 0 deletions

View File

@@ -944,6 +944,10 @@ async function readdirRecursive(originalPath, options) {
async function readdir(path, options) {
options = getOptions(options);
// Make shallow copy to prevent mutating options from affecting results
options = copyObject(options);
path = getValidatedPath(path);
if (options.recursive) {
return readdirRecursive(path, options);

View File

@@ -78,6 +78,14 @@ fs.readdir(readdirDir, {
assertDirents(dirents);
})().then(common.mustCall());
// Check that mutating options doesn't affect results
(async () => {
const options = { withFileTypes: true };
const direntsPromise = fs.promises.readdir(readdirDir, options);
options.withFileTypes = false;
assertDirents(await direntsPromise);
})().then(common.mustCall());
// Check for correct types when the binding returns unknowns
const UNKNOWN = constants.UV_DIRENT_UNKNOWN;
const oldReaddir = binding.readdir;