Skip to content

Commit

Permalink
fs: make mutating options in Promises readdir() not affect results
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
LiviaMedeiros authored and aduh95 committed Dec 18, 2024
1 parent 8288f57 commit 8d485f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 @@ -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;
Expand Down

0 comments on commit 8d485f1

Please sign in to comment.