◐ Shell
clean mode source ↗

fs: make mutating `options` in Callback `readdir()` not affect results · nodejs/node@4cc0493

Original file line numberDiff line numberDiff line change

@@ -1530,6 +1530,9 @@ function readdir(path, options, callback) {

15301530

}

15311531
15321532

if (options.recursive) {

1533+

// Make shallow copy to prevent mutating options from affecting results

1534+

options = copyObject(options);

1535+
15331536

readdirRecursive(path, options, callback);

15341537

return;

15351538

}

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 = { recursive: true, 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;