Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export function parsePath(input = ""): ParsedPath {
* @returns An object with two properties: username and password.
*/
export function parseAuth(input = ""): ParsedAuth {
const [username, password] = input.split(":");
const separator = input.indexOf(":");
const username = separator === -1 ? input : input.slice(0, separator);
const password = separator === -1 ? "" : input.slice(separator + 1);
return {
username: decode(username),
password: decode(password),
Expand Down
17 changes: 16 additions & 1 deletion test/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "vitest";
import { parseURL, parseHost, parseFilename } from "../src";
import { parseURL, parseHost, parseFilename, parseAuth } from "../src";

describe("parseURL", () => {
const tests = [
Expand Down Expand Up @@ -294,3 +294,18 @@ describe("parseFilename", () => {
});
}
});

describe("parseAuth", () => {
test.each([
["user:pass:word", { username: "user", password: "pass:word" }],
["user::pass:", { username: "user", password: ":pass:" }],
["user:pass%3Aword:tail", { username: "user", password: "pass:word:tail" }],
["user:pass", { username: "user", password: "pass" }],
["user", { username: "user", password: "" }],
])(
"preserves the password after the first colon in %s",
(input, expected) => {
expect(parseAuth(input)).toEqual(expected);
},
);
});