1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

fix: decode path properly on win32 (#6351)

This commit is contained in:
River 2020-06-18 20:10:07 +10:00 committed by GitHub
parent eea3223ade
commit 2a5af8b36b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View file

@ -82,19 +82,21 @@ export function immutableDefine(
} }
function pathFromURLWin32(url: URL): string { function pathFromURLWin32(url: URL): string {
if (url.hostname !== "") { const hostname = url.hostname;
const pathname = decodeURIComponent(url.pathname.replace(/\//g, "\\"));
if (hostname !== "") {
//TODO(actual-size) Node adds a punycode decoding step, we should consider adding this //TODO(actual-size) Node adds a punycode decoding step, we should consider adding this
return `\\\\${url.hostname}${url.pathname}`; return `\\\\${hostname}${pathname}`;
} }
const validPath = /^\/(?<driveLetter>[A-Za-z]):\//; const validPath = /^\\(?<driveLetter>[A-Za-z]):\\/;
const matches = validPath.exec(url.pathname); const matches = validPath.exec(pathname);
if (!matches?.groups?.driveLetter) { if (!matches?.groups?.driveLetter) {
throw new TypeError("A URL with the file schema must be absolute."); throw new TypeError("A URL with the file schema must be absolute.");
} }
const pathname = url.pathname.replace(/\//g, "\\");
// we don't want a leading slash on an absolute path in Windows // we don't want a leading slash on an absolute path in Windows
return pathname.slice(1); return pathname.slice(1);
} }

View file

@ -7,6 +7,7 @@ unitTest(
{ ignore: Deno.build.os === "windows" }, { ignore: Deno.build.os === "windows" },
function pathFromURLPosix(): void { function pathFromURLPosix(): void {
assertEquals(pathFromURL("file:///test/directory"), "/test/directory"); assertEquals(pathFromURL("file:///test/directory"), "/test/directory");
assertEquals(pathFromURL("file:///space_ .txt"), "/space_ .txt");
assertThrows(() => pathFromURL("file://host/test/directory")); assertThrows(() => pathFromURL("file://host/test/directory"));
assertThrows(() => pathFromURL("https://deno.land/welcome.ts")); assertThrows(() => pathFromURL("https://deno.land/welcome.ts"));
} }
@ -16,6 +17,7 @@ unitTest(
{ ignore: Deno.build.os !== "windows" }, { ignore: Deno.build.os !== "windows" },
function pathFromURLWin32(): void { function pathFromURLWin32(): void {
assertEquals(pathFromURL("file:///c:/windows/test"), "c:\\windows\\test"); assertEquals(pathFromURL("file:///c:/windows/test"), "c:\\windows\\test");
assertEquals(pathFromURL("file:///c:/space_ .txt"), "c:\\space_ .txt");
assertThrows(() => pathFromURL("file:///thing/test")); assertThrows(() => pathFromURL("file:///thing/test"));
assertThrows(() => pathFromURL("https://deno.land/welcome.ts")); assertThrows(() => pathFromURL("https://deno.land/welcome.ts"));
/* TODO(ry) Add tests for these situations /* TODO(ry) Add tests for these situations
@ -24,7 +26,6 @@ unitTest(
* emoji_🙃.txt file:///D:/weird_names/emoji_%F0%9F%99%83.txt * emoji_🙃.txt file:///D:/weird_names/emoji_%F0%9F%99%83.txt
* percent_%.txt file:///D:/weird_names/percent_%25.txt * percent_%.txt file:///D:/weird_names/percent_%25.txt
* pound_#.txt file:///D:/weird_names/pound_%23.txt * pound_#.txt file:///D:/weird_names/pound_%23.txt
* space_ .txt file:///D:/weird_names/space_%20.txt
* swapped_surrogate_pair_<EFBFBD><EFBFBD>.txt file:///D:/weird_names/swapped_surrogate_pair_%EF%BF%BD%EF%BF%BD.txt * swapped_surrogate_pair_<EFBFBD><EFBFBD>.txt file:///D:/weird_names/swapped_surrogate_pair_%EF%BF%BD%EF%BF%BD.txt
*/ */
} }