Skip to content

Commit

Permalink
fix(parseURL): Add protocol check when removing leading slash in wind…
Browse files Browse the repository at this point in the history
…ows file paths
  • Loading branch information
kalekdev committed Jul 12, 2024
1 parent 387d01a commit 70d64cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ export function parseURL(input = "", defaultProto?: string): ParsedURL {
input
.replace(/\\/g, "/")
.match(/^[\s\0]*([\w+.-]{2,}:)?\/\/([^/@]+@)?(.*)/) || [];
const [, host = "", path = ""] = hostAndPath.match(/([^#/?]*)(.*)?/) || [];
const { pathname, search, hash } = parsePath(
path.replace(/\/(?=[A-Za-z]:)/, ""),
);

// eslint-disable-next-line prefer-const
let [, host = "", path = ""] = hostAndPath.match(/([^#/?]*)(.*)?/) || [];

if (protocol === "file:") {
path = path.replace(/\/(?=[A-Za-z]:)/, "");
}

const { pathname, search, hash } = parsePath(path);

return {
protocol: protocol.toLowerCase(),
Expand Down
11 changes: 11 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ describe("parseURL", () => {
search: "",
},
},
{
input: "https://test.com/t:est",
out: {
auth: "",
hash: "",
host: "test.com",
pathname: "/t:est",
protocol: "https:",
search: "",
},
},
{
input: "https://host.name\\@foo.bar/meme3.php?url=http://0.0.0.0/2.svg",
out: {
Expand Down

0 comments on commit 70d64cb

Please sign in to comment.