Skip to content

Commit

Permalink
fs: make mutating options in Callback readdir() not affect results
Browse files Browse the repository at this point in the history
PR-URL: #56057
Reviewed-By: Rafael Gonzaga <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
LiviaMedeiros authored and targos committed Dec 13, 2024
1 parent 2df8257 commit c21a9e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,9 @@ function readdir(path, options, callback) {
}

if (options.recursive) {
// Make shallow copy to prevent mutating options from affecting results
options = copyObject(options);

readdirRecursive(path, options, callback);
return;
}
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-fs-readdir-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ fs.readdir(readdirDir, {
assertDirents(await direntsPromise);
})().then(common.mustCall());

{
const options = { recursive: true, withFileTypes: true };
fs.readdir(readdirDir, options, common.mustSucceed((dirents) => {
assertDirents(dirents);
}));
options.withFileTypes = false;
}

// Check for correct types when the binding returns unknowns
const UNKNOWN = constants.UV_DIRENT_UNKNOWN;
const oldReaddir = binding.readdir;
Expand Down

0 comments on commit c21a9e0

Please sign in to comment.