Skip to content

Commit 5661ab3

Browse files
authored
Merge pull request #1436 from bids-standard/readDir-stack-fix
fix: Avoid putting large file entry arrays on the getFilesFromFs call stack
2 parents 9c3baf3 + ddb94fc commit 5661ab3

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

bids-validator/utils/files/readDir.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,9 @@ async function getFilesFromGitTree(dir, ig, options) {
260260
/**
261261
* Recursive helper function for 'preprocessNode'
262262
*/
263-
async function getFilesFromFs(dir, rootPath, ig, options) {
263+
async function getFilesFromFs(dir, rootPath, ig, options, parent = []) {
264264
const files = await fs.promises.readdir(dir, { withFileTypes: true })
265-
const filesAccumulator = []
266-
// Closure to merge the next file depth into this one
267-
const recursiveMerge = async nextRoot => {
268-
Array.prototype.push.apply(
269-
filesAccumulator,
270-
await getFilesFromFs(nextRoot, rootPath, ig, options),
271-
)
272-
}
265+
const filesAccumulator = parent
273266
for (const file of files) {
274267
const fullPath = path.join(dir, file.name)
275268
const relativePath = harmonizeRelativePath(
@@ -286,7 +279,7 @@ async function getFilesFromFs(dir, rootPath, ig, options) {
286279
}
287280
// Three cases to consider: directories, files, symlinks
288281
if (file.isDirectory()) {
289-
await recursiveMerge(fullPath)
282+
await getFilesFromFs(fullPath, rootPath, ig, options, filesAccumulator)
290283
} else if (file.isSymbolicLink()) {
291284
// Allow skipping symbolic links which lead to recursion
292285
// Disabling this is a big performance advantage on high latency
@@ -297,7 +290,13 @@ async function getFilesFromFs(dir, rootPath, ig, options) {
297290
const targetStat = await fs.promises.stat(targetPath)
298291
// Either add or recurse from the target depending
299292
if (targetStat.isDirectory()) {
300-
await recursiveMerge(targetPath)
293+
await getFilesFromFs(
294+
targetPath,
295+
rootPath,
296+
ig,
297+
options,
298+
filesAccumulator,
299+
)
301300
} else {
302301
filesAccumulator.push(fileObj)
303302
}

0 commit comments

Comments
 (0)