Skip to content

Commit 4eb2bf5

Browse files
committed
fix: race conditions
1 parent 96d24f4 commit 4eb2bf5

File tree

1 file changed

+73
-28
lines changed
  • lib/node_modules/@stdlib/fs/resolve-parent-paths-by/lib

1 file changed

+73
-28
lines changed

lib/node_modules/@stdlib/fs/resolve-parent-paths-by/lib/main.js

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,7 @@ function some( paths, dir, predicate, done ) {
185185
if ( bool ) {
186186
out.push( spath );
187187
}
188-
}
189-
190-
/**
191-
* Callback invoked after checking for path existence.
192-
*
193-
* @private
194-
* @param {(Error|null)} error - error object
195-
* @param {boolean} bool - boolean indicating if a path exists
196-
* @returns {void}
197-
*/
198-
function onExists( error, bool ) { // eslint-disable-line node/handle-callback-err
199-
if ( bool ) {
200-
predicate( spath, onPredicate );
201-
}
188+
// Increment flag after `test` completes:
202189
FLG += 1;
203190
if ( FLG === paths.length ) {
204191
// Check if we have resolved any paths...
@@ -220,6 +207,41 @@ function some( paths, dir, predicate, done ) {
220207
return next( dir );
221208
}
222209
}
210+
211+
/**
212+
* Callback invoked after checking for path existence.
213+
*
214+
* @private
215+
* @param {(Error|null)} error - error object
216+
* @param {boolean} bool - boolean indicating if a path exists
217+
* @returns {void}
218+
*/
219+
function onExists( error, bool ) { // eslint-disable-line node/handle-callback-err
220+
if ( bool ) {
221+
predicate( spath, onPredicate );
222+
} else {
223+
FLG += 1;
224+
if ( FLG === paths.length ) {
225+
// Check if we have resolved any paths...
226+
if ( out.length > 0 ) {
227+
return done( null, out );
228+
}
229+
// Resolve a parent directory:
230+
child = dir;
231+
dir = resolve( dir, '..' );
232+
233+
// Reset flag:
234+
FLG = 0;
235+
236+
// If we have already reached root, we cannot resolve any higher directories...
237+
if ( child === dir ) {
238+
return done( null, out );
239+
}
240+
// Resolve paths at next directory level:
241+
return next( dir );
242+
}
243+
}
244+
}
223245
}
224246
}
225247

@@ -286,20 +308,6 @@ function all( paths, dir, predicate, done ) {
286308
}
287309
if ( bool ) {
288310
out[ idx ] = spath;
289-
}
290-
}
291-
292-
/**
293-
* Callback invoked after checking for path existence.
294-
*
295-
* @private
296-
* @param {(Error|null)} error - error object
297-
* @param {boolean} bool - boolean indicating if a path exists
298-
* @returns {void}
299-
*/
300-
function onExists( error, bool ) { // eslint-disable-line node/handle-callback-err
301-
if ( bool ) {
302-
predicate( spath, onPredicate );
303311
count += 1;
304312
}
305313
FLG += 1;
@@ -325,6 +333,43 @@ function all( paths, dir, predicate, done ) {
325333
return next( dir );
326334
}
327335
}
336+
337+
/**
338+
* Callback invoked after checking for path existence.
339+
*
340+
* @private
341+
* @param {(Error|null)} error - error object
342+
* @param {boolean} bool - boolean indicating if a path exists
343+
* @returns {void}
344+
*/
345+
function onExists( error, bool ) { // eslint-disable-line node/handle-callback-err
346+
if ( bool ) {
347+
predicate( spath, onPredicate );
348+
} else {
349+
FLG += 1;
350+
if ( FLG === paths.length ) {
351+
// Check if we have resolved any path...
352+
if ( count === paths.length ) {
353+
return done( null, out );
354+
}
355+
// Resolve a parent directory:
356+
child = dir;
357+
dir = resolve( dir, '..' );
358+
359+
// Reset flag and buffers:
360+
FLG = 0;
361+
out = [];
362+
count = 0;
363+
364+
// If we have already reached root, we cannot resolve any higher directories...
365+
if ( child === dir ) {
366+
return done( null, out );
367+
}
368+
// Resolve paths at next directory level:
369+
return next( dir );
370+
}
371+
}
372+
}
328373
}
329374
}
330375

0 commit comments

Comments
 (0)