@ -1514,6 +1514,10 @@ function encodeRealpathResult(result, options, err) {
}
}
}
}
// This is removed from the fs exports in lib/module.js in order to make
// sure that this stays internal.
const realpathCacheKey = fs . realpathCacheKey = Symbol ( 'realpathCacheKey' ) ;
fs . realpathSync = function realpathSync ( p , options ) {
fs . realpathSync = function realpathSync ( p , options ) {
if ( ! options )
if ( ! options )
options = { } ;
options = { } ;
@ -1528,6 +1532,13 @@ fs.realpathSync = function realpathSync(p, options) {
const seenLinks = { } ;
const seenLinks = { } ;
const knownHard = { } ;
const knownHard = { } ;
const cache = options [ realpathCacheKey ] ;
const original = p ;
const maybeCachedResult = cache && cache . get ( p ) ;
if ( maybeCachedResult ) {
return maybeCachedResult ;
}
// current character position in p
// current character position in p
var pos ;
var pos ;
@ -1568,39 +1579,47 @@ fs.realpathSync = function realpathSync(p, options) {
pos = nextPartRe . lastIndex ;
pos = nextPartRe . lastIndex ;
// continue if not a symlink
// continue if not a symlink
if ( knownHard [ base ] ) {
if ( knownHard [ base ] || ( cache && cache . get ( base ) === base ) ) {
continue ;
continue ;
}
}
var resolvedLink ;
var resolvedLink ;
var stat = fs . lstatSync ( base ) ;
const maybeCachedResolved = cache && cache . get ( base ) ;
if ( ! stat . isSymbolicLink ( ) ) {
if ( maybeCachedResolved ) {
knownHard [ base ] = true ;
resolvedLink = maybeCachedResolved ;
continue ;
} else {
}
var stat = fs . lstatSync ( base ) ;
if ( ! stat . isSymbolicLink ( ) ) {
knownHard [ base ] = true ;
continue ;
}
// read the link if it wasn't read before
// read the link if it wasn't read before
// dev/ino always return 0 on windows, so skip the check.
// dev/ino always return 0 on windows, so skip the check.
var linkTarget = null ;
let linkTarget = null ;
if ( ! isWindows ) {
let id ;
var id = stat . dev . toString ( 32 ) + ':' + stat . ino . toString ( 32 ) ;
if ( ! isWindows ) {
if ( seenLinks . hasOwnProperty ( id ) ) {
id = ` ${ stat . dev . toString ( 32 ) } : ${ stat . ino . toString ( 32 ) } ` ;
linkTarget = seenLinks [ id ] ;
if ( seenLinks . hasOwnProperty ( id ) ) {
linkTarget = seenLinks [ id ] ;
}
}
}
}
if ( linkTarget === null ) {
if ( linkTarget === null ) {
fs . statSync ( base ) ;
fs . statSync ( base ) ;
linkTarget = fs . readlinkSync ( base ) ;
linkTarget = fs . readlinkSync ( base ) ;
}
}
resolvedLink = pathModule . resolve ( previous , linkTarget ) ;
resolvedLink = pathModule . resolve ( previous , linkTarget ) ;
if ( ! isWindows ) seenLinks [ id ] = linkTarget ;
if ( cache ) cache . set ( base , resolvedLink ) ;
if ( ! isWindows ) seenLinks [ id ] = linkTarget ;
}
// resolve the link, then start over
// resolve the link, then start over
p = pathModule . resolve ( resolvedLink , p . slice ( pos ) ) ;
p = pathModule . resolve ( resolvedLink , p . slice ( pos ) ) ;
start ( ) ;
start ( ) ;
}
}
if ( cache ) cache . set ( original , p ) ;
return encodeRealpathResult ( p , options ) ;
return encodeRealpathResult ( p , options ) ;
} ;
} ;
@ -1696,8 +1715,9 @@ fs.realpath = function realpath(p, options, callback) {
// stat & read the link if not read before
// stat & read the link if not read before
// call gotTarget as soon as the link target is known
// call gotTarget as soon as the link target is known
// dev/ino always return 0 on windows, so skip the check.
// dev/ino always return 0 on windows, so skip the check.
let id ;
if ( ! isWindows ) {
if ( ! isWindows ) {
var id = stat . dev . toString ( 32 ) + ':' + stat . ino . toString ( 32 ) ;
id = ` ${ stat . dev . toString ( 32 ) } : ${ stat . ino . toString ( 32 ) } ` ;
if ( seenLinks . hasOwnProperty ( id ) ) {
if ( seenLinks . hasOwnProperty ( id ) ) {
return gotTarget ( null , seenLinks [ id ] , base ) ;
return gotTarget ( null , seenLinks [ id ] , base ) ;
}
}