@@ -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 ( ) ;
@@ -812,8 +813,17 @@ class URL {
812813 base = `${ base } ` ;
813814 }
814815
815- const raiseException = parseSymbol !== kParseURLSymbol ;
816- const href = bindingUrl . parse ( input , base , raiseException ) ;
816+ let href ;
817+ if ( arguments . length < 3 ) {
818+ href = bindingUrl . parse ( input , base , true ) ;
819+ } else {
820+ const raiseException = parseSymbol !== kParseURLSymbol ;
821+ const interpretAsWindowsPath = parseSymbol === kCreateURLFromWindowsPathSymbol ;
822+ const pathToFileURL = interpretAsWindowsPath || ( parseSymbol === kCreateURLFromPosixPathSymbol ) ;
823+ href = pathToFileURL ?
824+ bindingUrl . pathToFileURL ( input , interpretAsWindowsPath , base ) :
825+ bindingUrl . parse ( input , base , raiseException ) ;
826+ }
817827 if ( href ) {
818828 this . #updateContext( href ) ;
819829 }
@@ -1500,76 +1510,9 @@ function fileURLToPath(path, options = kEmptyObject) {
15001510 return ( windows ?? isWindows ) ? getPathFromURLWin32 ( path ) : getPathFromURLPosix ( path ) ;
15011511}
15021512
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-
15691513function pathToFileURL ( filepath , options = kEmptyObject ) {
15701514 const windows = options ?. windows ?? isWindows ;
15711515 if ( windows && StringPrototypeStartsWith ( filepath , '\\\\' ) ) {
1572- const outURL = new URL ( 'file://' ) ;
15731516 // UNC path format: \\server\share\resource
15741517 // Handle extended UNC path and standard UNC path
15751518 // "\\?\UNC\" path prefix should be ignored.
@@ -1592,12 +1535,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
15921535 ) ;
15931536 }
15941537 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 ;
1538+ return new URL ( StringPrototypeSlice ( filepath , hostnameEndIndex ) , hostname , kCreateURLFromWindowsPathSymbol ) ;
16011539 }
16021540 let resolved = windows ? path . win32 . resolve ( filepath ) : path . posix . resolve ( filepath ) ;
16031541 // path.resolve strips trailing slashes so we must add them back
@@ -1608,7 +1546,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
16081546 resolved [ resolved . length - 1 ] !== path . sep )
16091547 resolved += '/' ;
16101548
1611- return new URL ( `file:// ${ encodePathChars ( resolved , { windows } ) } ` ) ;
1549+ return new URL ( resolved , undefined , windows ? kCreateURLFromWindowsPathSymbol : kCreateURLFromPosixPathSymbol ) ;
16121550}
16131551
16141552function toPathIfFileURL ( fileURLOrPath ) {
0 commit comments