fix(cli/rt): Fix file URL to path conversion on Windows (#6920)

This commit is contained in:
Nayeem Rahman 2020-07-30 23:37:26 +01:00 committed by GitHub
parent 0da4779b17
commit 6e7208bec2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 40 deletions

View file

@ -64,23 +64,18 @@
}
function pathFromURLWin32(url) {
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
return `\\\\${hostname}${pathname}`;
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}`;
}
const validPath = /^\\(?<driveLetter>[A-Za-z]):\\/;
const matches = validPath.exec(pathname);
if (!matches?.groups?.driveLetter) {
throw new TypeError("A URL with the file schema must be absolute.");
}
// we don't want a leading slash on an absolute path in Windows
return pathname.slice(1);
return path;
}
function pathFromURLPosix(url) {