@@ -16,7 +16,6 @@ const {
16
16
ObjectSetPrototypeOf,
17
17
ReflectGetOwnPropertyDescriptor,
18
18
ReflectOwnKeys,
19
- RegExpPrototypeSymbolReplace,
20
19
SafeMap,
21
20
SafeSet,
22
21
StringPrototypeCharAt,
@@ -779,6 +778,8 @@ function isURL(self) {
779
778
* for invalid URL inputs.
780
779
*/
781
780
const kParseURLSymbol = Symbol ( 'kParseURL' ) ;
781
+ const kCreateURLFromPosixPathSymbol = Symbol ( 'kCreateURLFromPosixPath' ) ;
782
+ const kCreateURLFromWindowsPathSymbol = Symbol ( 'kCreateURLFromWindowsPath' ) ;
782
783
783
784
class URL {
784
785
#context = new URLContext ( ) ;
@@ -812,8 +813,17 @@ class URL {
812
813
base = `${ base } ` ;
813
814
}
814
815
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
+ }
817
827
if ( href ) {
818
828
this . #updateContext( href ) ;
819
829
}
@@ -1500,76 +1510,9 @@ function fileURLToPath(path, options = kEmptyObject) {
1500
1510
return ( windows ?? isWindows ) ? getPathFromURLWin32 ( path ) : getPathFromURLPosix ( path ) ;
1501
1511
}
1502
1512
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
-
1569
1513
function pathToFileURL ( filepath , options = kEmptyObject ) {
1570
1514
const windows = options ?. windows ?? isWindows ;
1571
1515
if ( windows && StringPrototypeStartsWith ( filepath , '\\\\' ) ) {
1572
- const outURL = new URL ( 'file://' ) ;
1573
1516
// UNC path format: \\server\share\resource
1574
1517
// Handle extended UNC path and standard UNC path
1575
1518
// "\\?\UNC\" path prefix should be ignored.
@@ -1592,12 +1535,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
1592
1535
) ;
1593
1536
}
1594
1537
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 ) ;
1601
1539
}
1602
1540
let resolved = windows ? path . win32 . resolve ( filepath ) : path . posix . resolve ( filepath ) ;
1603
1541
// path.resolve strips trailing slashes so we must add them back
@@ -1608,7 +1546,7 @@ function pathToFileURL(filepath, options = kEmptyObject) {
1608
1546
resolved [ resolved . length - 1 ] !== path . sep )
1609
1547
resolved += '/' ;
1610
1548
1611
- return new URL ( `file:// ${ encodePathChars ( resolved , { windows } ) } ` ) ;
1549
+ return new URL ( resolved , undefined , windows ? kCreateURLFromWindowsPathSymbol : kCreateURLFromPosixPathSymbol ) ;
1612
1550
}
1613
1551
1614
1552
function toPathIfFileURL ( fileURLOrPath ) {
0 commit comments