From ed0b1d462718166143b67056c36c7db15cc736d7 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Fri, 26 Jun 2020 13:34:17 +0100 Subject: fix(cli/js/web/url): Support UNC paths on Windows (#6418) --- cli/js/web/url.ts | 11 +++++++---- cli/tests/unit/url_test.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'cli') diff --git a/cli/js/web/url.ts b/cli/js/web/url.ts index 76aa21919..60690d46a 100644 --- a/cli/js/web/url.ts +++ b/cli/js/web/url.ts @@ -62,12 +62,15 @@ function parse(url: string, isBase = true): URLParts | undefined { parts.password = ""; [parts.hostname, restUrl] = takePattern(restUrl, /^[/\\]{2}([^/\\?#]*)/); parts.port = ""; + if (build.os == "windows" && parts.hostname == "") { + // UNC paths. e.g. "\\\\localhost\\foo\\bar" on Windows should be + // representable as `new URL("file:////localhost/foo/bar")` which is + // equivalent to: `new URL("file://localhost/foo/bar")`. + [parts.hostname, restUrl] = takePattern(restUrl, /^[/\\]{2,}([^/\\?#]*)/); + } } else if (specialSchemes.includes(parts.protocol)) { let restAuthority; - [restAuthority, restUrl] = takePattern( - restUrl, - /^[/\\]{2}[/\\]*([^/\\?#]+)/ - ); + [restAuthority, restUrl] = takePattern(restUrl, /^[/\\]{2,}([^/\\?#]+)/); if (isBase && restAuthority == "") { return undefined; } diff --git a/cli/tests/unit/url_test.ts b/cli/tests/unit/url_test.ts index b4c31ea6d..d22787f1c 100644 --- a/cli/tests/unit/url_test.ts +++ b/cli/tests/unit/url_test.ts @@ -191,6 +191,21 @@ unitTest(function urlDriveLetter() { assertEquals(new URL("http://example.com/C:").href, "http://example.com/C:"); }); +unitTest(function urlUncHostname() { + assertEquals( + new URL("file:////").href, + Deno.build.os == "windows" ? "file:///" : "file:////" + ); + assertEquals( + new URL("file:////server").href, + Deno.build.os == "windows" ? "file://server/" : "file:////server" + ); + assertEquals( + new URL("file:////server/file").href, + Deno.build.os == "windows" ? "file://server/file" : "file:////server/file" + ); +}); + unitTest(function urlHostnameUpperCase() { assertEquals(new URL("https://EXAMPLE.COM").href, "https://example.com/"); }); -- cgit v1.2.3