diff --git a/src/utils.ts b/src/utils.ts index ae60d2be..7ea72b9f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -456,7 +456,10 @@ export function withoutProtocol(input: string): string { * @group utils */ export function withProtocol(input: string, protocol: string): string { - const match = input.match(PROTOCOL_REGEX); + let match = input.match(PROTOCOL_REGEX); + if (!match) { + match = input.match(/^\/{2,}/); + } if (!match) { return protocol + input; } diff --git a/test/utilities.test.ts b/test/utilities.test.ts index c7fd0571..16c3f12d 100644 --- a/test/utilities.test.ts +++ b/test/utilities.test.ts @@ -178,6 +178,21 @@ describe("withHttps", () => { describe("withProtocol", () => { const tests = [ + { + input: "example.com", + protocol: "https://", + out: "https://example.com", + }, + { + input: "//example.com", + protocol: "https://", + out: "https://example.com", + }, + { + input: "://example.com", // Malformed URL is considered without protocol + protocol: "https://", + out: "https://://example.com", + }, { input: "http://example.com", protocol: "https://",