Skip to content

Commit 2441415

Browse files
anonrigtargos
authored andcommitted
url: reduce pathToFileURL cpp calls
PR-URL: #48709 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 74d8f96 commit 2441415

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

‎lib/internal/url.js‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,25 +1496,27 @@ const backslashRegEx = /\\/g;
14961496
constnewlineRegEx=/\n/g;
14971497
constcarriageReturnRegEx=/\r/g;
14981498
consttabRegEx=/\t/g;
1499+
constquestionRegex=/\?/g;
1500+
consthashRegex=/#/g;
14991501

15001502
functionencodePathChars(filepath){
1501-
if(StringPrototypeIncludes(filepath,'%'))
1503+
if(StringPrototypeIndexOf(filepath,'%')!==-1)
15021504
filepath=RegExpPrototypeSymbolReplace(percentRegEx,filepath,'%25');
15031505
// In posix, backslash is a valid character in paths:
1504-
if(!isWindows&&StringPrototypeIncludes(filepath,'\\'))
1506+
if(!isWindows&&StringPrototypeIndexOf(filepath,'\\')!==-1)
15051507
filepath=RegExpPrototypeSymbolReplace(backslashRegEx,filepath,'%5C');
1506-
if(StringPrototypeIncludes(filepath,'\n'))
1508+
if(StringPrototypeIndexOf(filepath,'\n')!==-1)
15071509
filepath=RegExpPrototypeSymbolReplace(newlineRegEx,filepath,'%0A');
1508-
if(StringPrototypeIncludes(filepath,'\r'))
1510+
if(StringPrototypeIndexOf(filepath,'\r')!==-1)
15091511
filepath=RegExpPrototypeSymbolReplace(carriageReturnRegEx,filepath,'%0D');
1510-
if(StringPrototypeIncludes(filepath,'\t'))
1512+
if(StringPrototypeIndexOf(filepath,'\t')!==-1)
15111513
filepath=RegExpPrototypeSymbolReplace(tabRegEx,filepath,'%09');
15121514
returnfilepath;
15131515
}
15141516

15151517
functionpathToFileURL(filepath){
1516-
constoutURL=newURL('file://');
15171518
if(isWindows&&StringPrototypeStartsWith(filepath,'\\\\')){
1519+
constoutURL=newURL('file://');
15181520
// UNC path format: \\server\share\resource
15191521
consthostnameEndIndex=StringPrototypeIndexOf(filepath,'\\',2);
15201522
if(hostnameEndIndex===-1){
@@ -1535,18 +1537,29 @@ function pathToFileURL(filepath){
15351537
outURL.hostname=domainToASCII(hostname);
15361538
outURL.pathname=encodePathChars(
15371539
RegExpPrototypeSymbolReplace(backslashRegEx,StringPrototypeSlice(filepath,hostnameEndIndex),'/'));
1538-
}else{
1539-
letresolved=path.resolve(filepath);
1540-
// path.resolve strips trailing slashes so we must add them back
1541-
constfilePathLast=StringPrototypeCharCodeAt(filepath,
1542-
filepath.length-1);
1543-
if((filePathLast===CHAR_FORWARD_SLASH||
1544-
(isWindows&&filePathLast===CHAR_BACKWARD_SLASH))&&
1545-
resolved[resolved.length-1]!==path.sep)
1546-
resolved+='/';
1547-
outURL.pathname=encodePathChars(resolved);
1540+
returnoutURL;
15481541
}
1549-
returnoutURL;
1542+
letresolved=path.resolve(filepath);
1543+
// path.resolve strips trailing slashes so we must add them back
1544+
constfilePathLast=StringPrototypeCharCodeAt(filepath,
1545+
filepath.length-1);
1546+
if((filePathLast===CHAR_FORWARD_SLASH||
1547+
(isWindows&&filePathLast===CHAR_BACKWARD_SLASH))&&
1548+
resolved[resolved.length-1]!==path.sep)
1549+
resolved+='/';
1550+
1551+
// Call encodePathChars first to avoid encoding % again for ? and #.
1552+
resolved=encodePathChars(resolved);
1553+
1554+
// Question and hash character should be included in pathname.
1555+
// Therefore, encoding is required to eliminate parsing them in different states.
1556+
// This is done as an optimization to not creating a URL instance and
1557+
// later triggering pathname setter, which impacts performance
1558+
if(StringPrototypeIndexOf(resolved,'?')!==-1)
1559+
resolved=RegExpPrototypeSymbolReplace(questionRegex,resolved,'%3F');
1560+
if(StringPrototypeIndexOf(resolved,'#')!==-1)
1561+
resolved=RegExpPrototypeSymbolReplace(hashRegex,resolved,'%23');
1562+
returnnewURL(`file://${resolved}`);
15501563
}
15511564

15521565
functiontoPathIfFileURL(fileURLOrPath){

0 commit comments

Comments
(0)