diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-07-30 23:37:26 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-30 18:37:26 -0400 |
commit | 6e7208bec2911ac0d1729f334fc90bc50b8f9203 (patch) | |
tree | 1361d123ca43e66bb0725295b59a1b5075108517 /std/path/win32.ts | |
parent | 0da4779b178a0c97925c1ef5589d0f24a37f4501 (diff) |
fix(cli/rt): Fix file URL to path conversion on Windows (#6920)
Diffstat (limited to 'std/path/win32.ts')
-rw-r--r-- | std/path/win32.ts | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/std/path/win32.ts b/std/path/win32.ts index 0283f4b9c..eed1cbdb8 100644 --- a/std/path/win32.ts +++ b/std/path/win32.ts @@ -907,16 +907,25 @@ export function parse(path: string): ParsedPath { /** Converts a file URL to a path string. * - * fromFileUrl("file:///C:/Users/foo"); // "C:\\Users\\foo" * fromFileUrl("file:///home/foo"); // "\\home\\foo" - * - * Note that non-file URLs are treated as file URLs and irrelevant components - * are ignored. + * fromFileUrl("file:///C:/Users/foo"); // "C:\\Users\\foo" + * fromFileUrl("file://localhost/home/foo"); // "\\\\localhost\\home\\foo" */ export function fromFileUrl(url: string | URL): string { - return decodeURIComponent( - (url instanceof URL ? url : new URL(url)).pathname + url = url instanceof URL ? url : new URL(url); + if (url.protocol != "file:") { + throw new TypeError("Must be a file URL."); + } + let path = decodeURIComponent( + url.pathname .replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/") .replace(/\//g, "\\"), ); + if (url.hostname != "") { + // Note: The `URL` implementation guarantees that the drive letter and + // hostname are mutually exclusive. Otherwise it would not have been valid + // to append the hostname and path like this. + path = `\\\\${url.hostname}${path}`; + } + return path; } |