@@ -22,6 +22,7 @@ const assertEncoding = internalFS.assertEncoding;
22
22
const stringToFlags = internalFS . stringToFlags ;
23
23
const SyncWriteStream = internalFS . SyncWriteStream ;
24
24
const getPathFromURL = internalURL . getPathFromURL ;
25
+ const { StorageObject } = require ( 'internal/querystring' ) ;
25
26
26
27
Object . defineProperty ( exports , 'constants' , {
27
28
configurable : false ,
@@ -1574,10 +1575,23 @@ fs.unwatchFile = function(filename, listener) {
1574
1575
} ;
1575
1576
1576
1577
1577
- // Regex to find the device root, including trailing slash. E.g. 'c:\\'.
1578
- const splitRootRe = isWindows ?
1579
- / ^ (?: [ a - z A - Z ] : | [ \\ / ] { 2 } [ ^ \\ / ] + [ \\ / ] [ ^ \\ / ] + ) ? [ \\ / ] * / :
1580
- / ^ [ / ] * / ;
1578
+ var splitRoot ;
1579
+ if ( isWindows ) {
1580
+ // Regex to find the device root on Windows (e.g. 'c:\\'), including trailing
1581
+ // slash.
1582
+ const splitRootRe = / ^ (?: [ a - z A - Z ] : | [ \\ / ] { 2 } [ ^ \\ / ] + [ \\ / ] [ ^ \\ / ] + ) ? [ \\ / ] * / ;
1583
+ splitRoot = function splitRoot ( str ) {
1584
+ return splitRootRe . exec ( str ) [ 0 ] ;
1585
+ } ;
1586
+ } else {
1587
+ splitRoot = function splitRoot ( str ) {
1588
+ for ( var i = 0 ; i < str . length ; ++ i ) {
1589
+ if ( str . charCodeAt ( i ) !== 47 /*'/'*/ )
1590
+ return str . slice ( 0 , i ) ;
1591
+ }
1592
+ return str ;
1593
+ } ;
1594
+ }
1581
1595
1582
1596
function encodeRealpathResult ( result , options ) {
1583
1597
if ( ! options || ! options . encoding || options . encoding === 'utf8' )
@@ -1605,11 +1619,17 @@ if (isWindows) {
1605
1619
nextPart = function nextPart ( p , i ) { return p . indexOf ( '/' , i ) ; } ;
1606
1620
}
1607
1621
1622
+ const emptyObj = new StorageObject ( ) ;
1608
1623
fs . realpathSync = function realpathSync ( p , options ) {
1609
- options = getOptions ( options , { } ) ;
1610
- handleError ( ( p = getPathFromURL ( p ) ) ) ;
1611
- if ( typeof p !== 'string' )
1612
- p += '' ;
1624
+ if ( ! options )
1625
+ options = emptyObj ;
1626
+ else
1627
+ options = getOptions ( options , emptyObj ) ;
1628
+ if ( typeof p !== 'string' ) {
1629
+ handleError ( ( p = getPathFromURL ( p ) ) ) ;
1630
+ if ( typeof p !== 'string' )
1631
+ p += '' ;
1632
+ }
1613
1633
nullCheck ( p ) ;
1614
1634
1615
1635
p = pathModule . resolve ( p ) ;
@@ -1621,8 +1641,8 @@ fs.realpathSync = function realpathSync(p, options) {
1621
1641
return maybeCachedResult ;
1622
1642
}
1623
1643
1624
- const seenLinks = { } ;
1625
- const knownHard = { } ;
1644
+ const seenLinks = new StorageObject ( ) ;
1645
+ const knownHard = new StorageObject ( ) ;
1626
1646
const original = p ;
1627
1647
1628
1648
// current character position in p
@@ -1635,10 +1655,8 @@ fs.realpathSync = function realpathSync(p, options) {
1635
1655
var previous ;
1636
1656
1637
1657
// Skip over roots
1638
- var m = splitRootRe . exec ( p ) ;
1639
- pos = m [ 0 ] . length ;
1640
- current = m [ 0 ] ;
1641
- base = m [ 0 ] ;
1658
+ current = base = splitRoot ( p ) ;
1659
+ pos = current . length ;
1642
1660
1643
1661
// On windows, check that the root exists. On unix there is no need.
1644
1662
if ( isWindows && ! knownHard [ base ] ) {
@@ -1677,7 +1695,8 @@ fs.realpathSync = function realpathSync(p, options) {
1677
1695
// Use stats array directly to avoid creating an fs.Stats instance just
1678
1696
// for our internal use.
1679
1697
1680
- binding . lstat ( pathModule . _makeLong ( base ) ) ;
1698
+ var baseLong = pathModule . _makeLong ( base ) ;
1699
+ binding . lstat ( baseLong ) ;
1681
1700
1682
1701
if ( ( statValues [ 1 /*mode*/ ] & S_IFMT ) !== S_IFLNK ) {
1683
1702
knownHard [ base ] = true ;
@@ -1693,13 +1712,13 @@ fs.realpathSync = function realpathSync(p, options) {
1693
1712
var dev = statValues [ 0 /*dev*/ ] . toString ( 32 ) ;
1694
1713
var ino = statValues [ 7 /*ino*/ ] . toString ( 32 ) ;
1695
1714
id = `${ dev } :${ ino } ` ;
1696
- if ( seenLinks . hasOwnProperty ( id ) ) {
1715
+ if ( seenLinks [ id ] ) {
1697
1716
linkTarget = seenLinks [ id ] ;
1698
1717
}
1699
1718
}
1700
1719
if ( linkTarget === null ) {
1701
- binding . stat ( pathModule . _makeLong ( base ) ) ;
1702
- linkTarget = binding . readlink ( pathModule . _makeLong ( base ) ) ;
1720
+ binding . stat ( baseLong ) ;
1721
+ linkTarget = binding . readlink ( baseLong ) ;
1703
1722
}
1704
1723
resolvedLink = pathModule . resolve ( previous , linkTarget ) ;
1705
1724
@@ -1711,10 +1730,8 @@ fs.realpathSync = function realpathSync(p, options) {
1711
1730
p = pathModule . resolve ( resolvedLink , p . slice ( pos ) ) ;
1712
1731
1713
1732
// Skip over roots
1714
- m = splitRootRe . exec ( p ) ;
1715
- pos = m [ 0 ] . length ;
1716
- current = m [ 0 ] ;
1717
- base = m [ 0 ] ;
1733
+ current = base = splitRoot ( p ) ;
1734
+ pos = current . length ;
1718
1735
1719
1736
// On windows, check that the root exists. On unix there is no need.
1720
1737
if ( isWindows && ! knownHard [ base ] ) {
@@ -1730,18 +1747,23 @@ fs.realpathSync = function realpathSync(p, options) {
1730
1747
1731
1748
fs . realpath = function realpath ( p , options , callback ) {
1732
1749
callback = maybeCallback ( typeof options === 'function' ? options : callback ) ;
1733
- options = getOptions ( options , { } ) ;
1734
- if ( handleError ( ( p = getPathFromURL ( p ) ) , callback ) )
1735
- return ;
1736
- if ( typeof p !== 'string' )
1737
- p += '' ;
1750
+ if ( ! options )
1751
+ options = emptyObj ;
1752
+ else
1753
+ options = getOptions ( options , emptyObj ) ;
1754
+ if ( typeof p !== 'string' ) {
1755
+ if ( handleError ( ( p = getPathFromURL ( p ) ) , callback ) )
1756
+ return ;
1757
+ if ( typeof p !== 'string' )
1758
+ p += '' ;
1759
+ }
1738
1760
if ( ! nullCheck ( p , callback ) )
1739
1761
return ;
1740
1762
1741
1763
p = pathModule . resolve ( p ) ;
1742
1764
1743
- const seenLinks = { } ;
1744
- const knownHard = { } ;
1765
+ const seenLinks = new StorageObject ( ) ;
1766
+ const knownHard = new StorageObject ( ) ;
1745
1767
1746
1768
// current character position in p
1747
1769
var pos ;
@@ -1752,11 +1774,8 @@ fs.realpath = function realpath(p, options, callback) {
1752
1774
// the partial path scanned in the previous round, with slash
1753
1775
var previous ;
1754
1776
1755
- var m = splitRootRe . exec ( p ) ;
1756
- pos = m [ 0 ] . length ;
1757
- current = m [ 0 ] ;
1758
- base = m [ 0 ] ;
1759
- previous = '' ;
1777
+ current = base = splitRoot ( p ) ;
1778
+ pos = current . length ;
1760
1779
1761
1780
// On windows, check that the root exists. On unix there is no need.
1762
1781
if ( isWindows && ! knownHard [ base ] ) {
@@ -1819,7 +1838,7 @@ fs.realpath = function realpath(p, options, callback) {
1819
1838
var dev = statValues [ 0 /*ino*/ ] . toString ( 32 ) ;
1820
1839
var ino = statValues [ 7 /*ino*/ ] . toString ( 32 ) ;
1821
1840
id = `${ dev } :${ ino } ` ;
1822
- if ( seenLinks . hasOwnProperty ( id ) ) {
1841
+ if ( seenLinks [ id ] ) {
1823
1842
return gotTarget ( null , seenLinks [ id ] , base ) ;
1824
1843
}
1825
1844
}
@@ -1843,11 +1862,8 @@ fs.realpath = function realpath(p, options, callback) {
1843
1862
function gotResolvedLink ( resolvedLink ) {
1844
1863
// resolve the link, then start over
1845
1864
p = pathModule . resolve ( resolvedLink , p . slice ( pos ) ) ;
1846
- var m = splitRootRe . exec ( p ) ;
1847
- pos = m [ 0 ] . length ;
1848
- current = m [ 0 ] ;
1849
- base = m [ 0 ] ;
1850
- previous = '' ;
1865
+ current = base = splitRoot ( p ) ;
1866
+ pos = current . length ;
1851
1867
1852
1868
// On windows, check that the root exists. On unix there is no need.
1853
1869
if ( isWindows && ! knownHard [ base ] ) {
0 commit comments