@@ -16,7 +16,6 @@ const {
1616 ObjectSetPrototypeOf,
1717 ReflectGetOwnPropertyDescriptor,
1818 ReflectOwnKeys,
19- RegExpPrototypeSymbolReplace,
2019 SafeMap,
2120 SafeSet,
2221 StringPrototypeCharAt,
@@ -779,6 +778,8 @@ function isURL(self) {
779778 * for invalid URL inputs.
780779 */
781780const kParseURLSymbol = Symbol ( 'kParseURL' ) ;
781+ const kCreateURLFromPosixPathSymbol = Symbol ( 'kCreateURLFromPosixPath' ) ;
782+ const kCreateURLFromWindowsPathSymbol = Symbol ( 'kCreateURLFromWindowsPath' ) ;
782783
783784class URL {
784785 #context = new URLContext ( ) ;
@@ -813,7 +814,11 @@ class URL {
813814 }
814815
815816 const raiseException = parseSymbol !== kParseURLSymbol ;
816- const href = bindingUrl . parse ( input , base , raiseException ) ;
817+ const interpretAsWindowsPath = parseSymbol === kCreateURLFromWindowsPathSymbol ;
818+ const pathToFileURL = interpretAsWindowsPath || ( parseSymbol === kCreateURLFromPosixPathSymbol ) ;
819+ const href = pathToFileURL ?
820+ bindingUrl . pathToFileURL ( input , interpretAsWindowsPath , base ) :
821+ bindingUrl . parse ( input , base , raiseException ) ;
817822 if ( href ) {
818823 this . #updateContext( href ) ;
819824 }
@@ -1500,76 +1505,9 @@ function fileURLToPath(path, options = kEmptyObject) {
15001505 return ( windows ?? isWindows ) ? getPathFromURLWin32 ( path ) : getPathFromURLPosix ( path ) ;
15011506}
15021507
1503- // RFC1738 defines the following chars as "unsafe" for URLs
1504- // @see https://www.ietf.org/rfc/rfc1738.txt 2.2. URL Character Encoding Issues
1505- const percentRegEx = / % / g;
1506- const newlineRegEx = / \n / g;
1507- const carriageReturnRegEx = / \r / g;
1508- const tabRegEx = / \t / g;
1509- const quoteRegEx = / " / g;
1510- const hashRegex = / # / g;
1511- const spaceRegEx = / / g;
1512- const questionMarkRegex = / \? / g;
1513- const openSquareBracketRegEx = / \[ / g;
1514- const backslashRegEx = / \\ / g;
1515- const closeSquareBracketRegEx = / ] / g;
1516- const caretRegEx = / \^ / g;
1517- const verticalBarRegEx = / \| / g;
1518- const tildeRegEx = / ~ / g;
1519-
1520- function encodePathChars ( filepath , options = kEmptyObject ) {
1521- if ( StringPrototypeIncludes ( filepath , '%' ) ) {
1522- filepath = RegExpPrototypeSymbolReplace ( percentRegEx , filepath , '%25' ) ;
1523- }
1524-
1525- if ( StringPrototypeIncludes ( filepath , '\t' ) ) {
1526- filepath = RegExpPrototypeSymbolReplace ( tabRegEx , filepath , '%09' ) ;
1527- }
1528- if ( StringPrototypeIncludes ( filepath , '\n' ) ) {
1529- filepath = RegExpPrototypeSymbolReplace ( newlineRegEx , filepath , '%0A' ) ;
1530- }
1531- if ( StringPrototypeIncludes ( filepath , '\r' ) ) {
1532- filepath = RegExpPrototypeSymbolReplace ( carriageReturnRegEx , filepath , '%0D' ) ;
1533- }
1534- if ( StringPrototypeIncludes ( filepath , ' ' ) ) {
1535- filepath = RegExpPrototypeSymbolReplace ( spaceRegEx , filepath , '%20' ) ;
1536- }
1537- if ( StringPrototypeIncludes ( filepath , '"' ) ) {
1538- filepath = RegExpPrototypeSymbolReplace ( quoteRegEx , filepath , '%22' ) ;
1539- }
1540- if ( StringPrototypeIncludes ( filepath , '#' ) ) {
1541- filepath = RegExpPrototypeSymbolReplace ( hashRegex , filepath , '%23' ) ;
1542- }
1543- if ( StringPrototypeIncludes ( filepath , '?' ) ) {
1544- filepath = RegExpPrototypeSymbolReplace ( questionMarkRegex , filepath , '%3F' ) ;
1545- }
1546- if ( StringPrototypeIncludes ( filepath , '[' ) ) {
1547- filepath = RegExpPrototypeSymbolReplace ( openSquareBracketRegEx , filepath , '%5B' ) ;
1548- }
1549- // Back-slashes must be special-cased on Windows, where they are treated as path separator.
1550- if ( ! options . windows && StringPrototypeIncludes ( filepath , '\\' ) ) {
1551- filepath = RegExpPrototypeSymbolReplace ( backslashRegEx , filepath , '%5C' ) ;
1552- }
1553- if ( StringPrototypeIncludes ( filepath , ']' ) ) {
1554- filepath = RegExpPrototypeSymbolReplace ( closeSquareBracketRegEx , filepath , '%5D' ) ;
1555- }
1556- if ( StringPrototypeIncludes ( filepath , '^' ) ) {
1557- filepath = RegExpPrototypeSymbolReplace ( caretRegEx , filepath , '%5E' ) ;
1558- }
1559- if ( StringPrototypeIncludes ( filepath , '|' ) ) {
1560- filepath = RegExpPrototypeSymbolReplace ( verticalBarRegEx , filepath , '%7C' ) ;
1561- }
1562- if ( StringPrototypeIncludes ( filepath , '~' ) ) {
1563- filepath = RegExpPrototypeSymbolReplace ( tildeRegEx , filepath , '%7E' ) ;
1564- }
1565-
1566- return filepath ;
1567- }
1568-
15691508function pathToFileURL ( filepath , options = kEmptyObject ) {
15701509 const windows = options ?. windows ?? isWindows ;
15711510 if ( windows && StringPrototypeStartsWith ( filepath , '\\\\' ) ) {
1572- const outURL = new URL ( 'file://' ) ;
15731511 // UNC path format: \\server\share\resource
15741512 // Handle extended UNC path and standard UNC path
15751513 // "\\?\UNC\" path prefix should be ignored.
@@ -1592,12 +1530,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
15921530 ) ;
15931531 }
15941532 const hostname = StringPrototypeSlice ( filepath , prefixLength , hostnameEndIndex ) ;
1595- outURL . hostname = domainToASCII ( hostname ) ;
1596- outURL . pathname = encodePathChars (
1597- RegExpPrototypeSymbolReplace ( backslashRegEx , StringPrototypeSlice ( filepath , hostnameEndIndex ) , '/' ) ,
1598- { windows } ,
1599- ) ;
1600- return outURL ;
1533+ return new URL ( StringPrototypeSlice ( filepath , hostnameEndIndex ) , hostname , kCreateURLFromWindowsPathSymbol ) ;
16011534 }
16021535 let resolved = windows ? path . win32 . resolve ( filepath ) : path . posix . resolve ( filepath ) ;
16031536 // path.resolve strips trailing slashes so we must add them back
@@ -1608,7 +1541,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
16081541 resolved [ resolved . length - 1 ] !== path . sep )
16091542 resolved += '/' ;
16101543
1611- return new URL ( `file:// ${ encodePathChars ( resolved , { windows } ) } ` ) ;
1544+ return new URL ( resolved , undefined , windows ? kCreateURLFromWindowsPathSymbol : kCreateURLFromPosixPathSymbol ) ;
16121545}
16131546
16141547function toPathIfFileURL ( fileURLOrPath ) {
0 commit comments