Skip to content

Commit ff33adc

Browse files
committed
fs: make mutating options in Callback readdir() not affect results
1 parent 779031f commit ff33adc

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/fs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,9 @@ function readdir(path, options, callback) {
15311531
}
15321532

15331533
if (options.recursive) {
1534+
// Make shallow copy to prevent mutating options from affecting results
1535+
options = copyObject(options);
1536+
15341537
readdirRecursive(path, options, callback);
15351538
return;
15361539
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ fs.readdir(readdirDir, {
8686
assertDirents(await direntsPromise);
8787
})().then(common.mustCall());
8888

89+
{
90+
const options = { withFileTypes: true };
91+
fs.readdir(readdirDir, options, common.mustSucceed((dirents) => {
92+
assertDirents(dirents);
93+
}));
94+
options.withFileTypes = false;
95+
}
96+
8997
// Check for correct types when the binding returns unknowns
9098
const UNKNOWN = constants.UV_DIRENT_UNKNOWN;
9199
const oldReaddir = binding.readdir;

0 commit comments

Comments
 (0)