Skip to content

Commit 5dcdd50

Browse files
committed
fs: make mutating options in readdir() not affect results
1 parent 1919560 commit 5dcdd50

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/internal/fs/promises.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,10 @@ async function readdirRecursive(originalPath, options) {
944944

945945
async function readdir(path, options) {
946946
options = getOptions(options);
947+
948+
// Make shallow copy to prevent mutating options from affecting results
949+
options = copyObject(options);
950+
947951
path = getValidatedPath(path);
948952
if (options.recursive) {
949953
return readdirRecursive(path, options);

test/parallel/test-fs-readdir-types.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ fs.readdir(readdirDir, {
7878
assertDirents(dirents);
7979
})().then(common.mustCall());
8080

81+
// Check that mutating options doesn't affect results
82+
{
83+
const options = { withFileTypes: true };
84+
fs.readdir(readdirDir, options, common.mustSucceed((dirents) => {
85+
assertDirents(dirents);
86+
}));
87+
options.withFileTypes = false;
88+
}
89+
90+
(async () => {
91+
const options = { withFileTypes: true };
92+
const direntsPromise = fs.promises.readdir(readdirDir, options);
93+
options.withFileTypes = false;
94+
assertDirents(await direntsPromise);
95+
})().then(common.mustCall());
96+
8197
// Check for correct types when the binding returns unknowns
8298
const UNKNOWN = constants.UV_DIRENT_UNKNOWN;
8399
const oldReaddir = binding.readdir;

0 commit comments

Comments
 (0)