summaryrefslogtreecommitdiff
path: root/std/path/win32.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/path/win32.ts')
-rw-r--r--std/path/win32.ts21
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;
}