@@ -84,7 +84,7 @@ function patcher(roots) {
8484 if ( process . env . VERBOSE_LOGS ) {
8585 console . error ( 'fs patcher called without any valid root paths ' + __filename ) ;
8686 }
87- return ;
87+ return function ( ) { } ;
8888 }
8989 const origLstat = fs . lstat . bind ( fs ) ;
9090 const origLstatSync = fs . lstatSync . bind ( fs ) ;
@@ -242,11 +242,11 @@ function patcher(roots) {
242242 return origReadlink ( ...args ) ;
243243 }
244244 const cb = once ( args [ args . length - 1 ] ) ;
245- args [ args . length - 1 ] = function readlinkCb ( err , str ) {
245+ args [ args . length - 1 ] = function readlinkCb ( err , p ) {
246246 if ( err )
247247 return cb ( err ) ;
248248 const resolved = resolvePathLike ( args [ 0 ] ) ;
249- str = path . resolve ( path . dirname ( resolved ) , str ) ;
249+ const str = path . resolve ( path . dirname ( resolved ) , p ) ;
250250 const escapedRoot = isEscape ( resolved , str ) ;
251251 if ( escapedRoot ) {
252252 return nextHop ( str , readlinkNextHopCb ) ;
@@ -261,10 +261,12 @@ function patcher(roots) {
261261 return cb ( einval ( 'readlink' , args [ 0 ] ) ) ;
262262 }
263263 }
264- next = path . resolve ( path . dirname ( resolved ) , path . relative ( path . dirname ( str ) , next ) ) ;
265- if ( next != resolved &&
266- ! isEscape ( resolved , next , [ escapedRoot ] ) ) {
267- return cb ( null , next ) ;
264+ const resolvedNext = path . resolve ( path . dirname ( resolved ) , path . relative ( path . dirname ( str ) , next ) ) ;
265+ if ( resolvedNext != resolved &&
266+ ! isEscape ( resolved , resolvedNext , [
267+ escapedRoot ,
268+ ] ) ) {
269+ return cb ( null , resolvedNext ) ;
268270 }
269271 // The escape from the root is not mappable back into the root; throw EINVAL
270272 return cb ( einval ( 'readlink' , args [ 0 ] ) ) ;
@@ -282,19 +284,14 @@ function patcher(roots) {
282284 const escapedRoot = isEscape ( resolved , str ) ;
283285 if ( escapedRoot ) {
284286 let next = nextHopSync ( str ) ;
285- if ( ! next ) {
286- if ( next == undefined ) {
287- // The escape from the root is not mappable back into the root; throw EINVAL
288- throw enoent ( 'readlink' , args [ 0 ] ) ;
289- }
290- else {
291- // The escape from the root is not mappable back into the root; throw EINVAL
292- throw einval ( 'readlink' , args [ 0 ] ) ;
293- }
287+ if ( next === false ) {
288+ // The escape from the root is not mappable back into the root; throw EINVAL
289+ throw enoent ( 'readlink' , args [ 0 ] ) ;
294290 }
295- next = path . resolve ( path . dirname ( resolved ) , path . relative ( path . dirname ( str ) , next ) ) ;
296- if ( next != resolved && ! isEscape ( resolved , next , [ escapedRoot ] ) ) {
297- return next ;
291+ const resolvedNext = path . resolve ( path . dirname ( resolved ) , path . relative ( path . dirname ( str ) , next ) ) ;
292+ if ( resolvedNext != resolved &&
293+ ! isEscape ( resolved , resolvedNext , [ escapedRoot ] ) ) {
294+ return resolvedNext ;
298295 }
299296 // The escape from the root is not mappable back into the root; throw EINVAL
300297 throw einval ( 'readlink' , args [ 0 ] ) ;
@@ -315,7 +312,7 @@ function patcher(roots) {
315312 if ( err )
316313 return cb ( err ) ;
317314 // user requested withFileTypes
318- if ( result [ 0 ] && result [ 0 ] . isSymbolicLink ) {
315+ if ( result [ 0 ] && 'isSymbolicLink' in result [ 0 ] ) {
319316 Promise . all ( result . map ( ( v ) => handleDirent ( p , v ) ) )
320317 . then ( ( ) => {
321318 cb ( null , result ) ;
@@ -452,7 +449,7 @@ function patcher(roots) {
452449 } ) ;
453450 } ;
454451 const origRead = dir . read . bind ( dir ) ;
455- dir . read = async function handleDirRead ( cb ) {
452+ dir . read = function handleDirRead ( cb ) {
456453 if ( typeof cb === 'function' ) {
457454 origRead ( function handleDirReadCb ( err , entry ) {
458455 if ( err )
@@ -461,13 +458,10 @@ function patcher(roots) {
461458 cb ( null , entry ) ;
462459 } , ( err ) => cb ( err , null ) ) ;
463460 } ) ;
461+ return undefined ;
464462 }
465463 else {
466- const entry = await origRead ( ) ;
467- if ( entry ) {
468- await handleDirent ( p , entry ) ;
469- }
470- return entry ;
464+ return origRead ( ) . then ( ( entry ) => entry && handleDirent ( p , entry ) ) ;
471465 }
472466 } ;
473467 const origReadSync = dir . readSync . bind ( dir ) ;
@@ -476,13 +470,13 @@ function patcher(roots) {
476470 } ;
477471 return dir ;
478472 }
479- async function handleDirent ( p , v ) {
473+ function handleDirent ( p , v ) {
480474 if ( ! v . isSymbolicLink ( ) ) {
481- return v ;
475+ return Promise . resolve ( v ) ;
482476 }
483477 const f = path . resolve ( p , v . name ) ;
484478 return new Promise ( function handleDirentExecutor ( resolve , reject ) {
485- return guardedReadLink ( f , handleDirentReadLinkCb ) ;
479+ guardedReadLink ( f , handleDirentReadLinkCb ) ;
486480 function handleDirentReadLinkCb ( str ) {
487481 if ( f != str ) {
488482 return resolve ( v ) ;
@@ -524,7 +518,7 @@ function patcher(roots) {
524518 let escapedHop = false ;
525519 readHopLink ( maybe , function readNextHop ( link ) {
526520 if ( link === HOP_NOT_FOUND ) {
527- return cb ( undefined ) ;
521+ return cb ( false ) ;
528522 }
529523 if ( link !== HOP_NON_LINK ) {
530524 if ( nested ) {
@@ -619,7 +613,7 @@ function patcher(roots) {
619613 for ( ; ; ) {
620614 let link = readHopLinkSync ( maybe ) ;
621615 if ( link === HOP_NOT_FOUND ) {
622- return undefined ;
616+ return false ;
623617 }
624618 if ( link !== HOP_NON_LINK ) {
625619 if ( nested ) {
@@ -680,7 +674,7 @@ function patcher(roots) {
680674 nextHop ( loc , function oneHopeNextCb ( next ) {
681675 if ( next == undefined ) {
682676 // file does not exist (broken link)
683- return cb ( enoent ( 'realpath' , start ) ) ;
677+ return cb ( enoent ( 'realpath' , start ) , undefined ) ;
684678 }
685679 else if ( ! next ) {
686680 // we've hit a real file
@@ -691,7 +685,7 @@ function patcher(roots) {
691685 }
692686 oneHop ( start , cb ) ;
693687 }
694- function guardedRealPath ( start , cb , escapedRoot = undefined ) {
688+ function guardedRealPath ( start , cb , escapedRoot ) {
695689 start = stringifyPathLike ( start ) ; // handle the "undefined" case (matches behavior as fs.realpath)
696690 function oneHop ( loc , cb ) {
697691 nextHop ( loc , function guardedRealPathHopCb ( next ) {
@@ -704,7 +698,7 @@ function patcher(roots) {
704698 }
705699 else {
706700 // something funky happened in the filesystem
707- return cb ( enoent ( 'realpath' , start ) ) ;
701+ return cb ( enoent ( 'realpath' , start ) , undefined ) ;
708702 }
709703 } ) ;
710704 }
@@ -733,7 +727,7 @@ function patcher(roots) {
733727 }
734728 }
735729 }
736- function guardedRealPathSync ( start , escapedRoot = undefined ) {
730+ function guardedRealPathSync ( start , escapedRoot ) {
737731 start = stringifyPathLike ( start ) ; // handle the "undefined" case (matches behavior as fs.realpathSync)
738732 for ( let loc = start , next ; ; loc = next ) {
739733 next = nextHopSync ( loc ) ;
0 commit comments