Skip to content

Commit

Permalink
fixup! feat(cli/js/web/url): Support drive letters in file URLs on Wi…
Browse files Browse the repository at this point in the history
…ndows
  • Loading branch information
nayeemrmn committed May 4, 2020
1 parent 07067b3 commit 11168d4
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions cli/js/web/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,22 @@ function takePattern(string: string, pattern: RegExp): [string, string] {

function parse(url: string, isBase = true): URLParts | undefined {
const parts: Partial<URLParts> = {};
let restUrl;
[parts.protocol, restUrl] = takePattern(url, /^([a-z]+):/);
let restUrl = url.replace(/\\/g, "/");
[parts.protocol, restUrl] = takePattern(restUrl, /^([a-z]+):/);
if (isBase && parts.protocol == "") {
return undefined;
}
if (parts.protocol == "file") {
parts.username = "";
parts.password = "";
[parts.hostname, restUrl] = takePattern(restUrl, /^[/\\]{2}([^/\\?#]*)/);
[parts.hostname, restUrl] = takePattern(restUrl, /^\/{2}([^/?#]*)/);
if (parts.hostname.includes(":")) {
return undefined;
}
parts.port = "";
} else if (specialSchemes.includes(parts.protocol)) {
let restAuthority;
[restAuthority, restUrl] = takePattern(
restUrl,
/^[/\\]{2}[/\\]*([^/\\?#]+)/
);
[restAuthority, restUrl] = takePattern(restUrl, /^\/{2}\/*([^/?#]+)/);
if (isBase && restAuthority == "") {
return undefined;
}
Expand All @@ -93,7 +90,9 @@ function parse(url: string, isBase = true): URLParts | undefined {
parts.port = "";
}
[parts.path, restUrl] = takePattern(restUrl, /^([^?#]*)/);
parts.path = parts.path.replace(/\\/g, "/");
if (build.os == "windows" && parts.protocol == "file") {
parts.path = parts.path.replace(/^\/*([A-Za-z]:)(\/|$)/, "/$1/");
}
[parts.query, restUrl] = takePattern(restUrl, /^(\?[^#]*)/);
[parts.hash] = takePattern(restUrl, /^(#.*)/);
return parts as URLParts;
Expand All @@ -117,12 +116,7 @@ function isAbsolutePath(path: string): boolean {

// Resolves `.`s and `..`s where possible.
// Preserves repeating and trailing `/`s by design.
// On Windows, drive letter paths will be given a leading slash, and also a
// trailing slash if there are no other components e.g. "C:" -> "/C:/".
function normalizePath(path: string): string {
if (build.os == "windows") {
path = path.replace(/^\/*([A-Za-z]:)(\/|$)/, "/$1/");
}
const isAbsolute = isAbsolutePath(path);
path = path.replace(/^\//, "");
const pathSegments = path.split("/");
Expand Down

0 comments on commit 11168d4

Please sign in to comment.