Skip to content

Commit

Permalink
fix(normaliseUrlDomain): allow custom url scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
zaosoula committed Feb 27, 2024
1 parent aed7b09 commit e6c5477
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 64 deletions.
46 changes: 17 additions & 29 deletions lib/build/normalisedURLDomain.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 20 additions & 35 deletions lib/ts/normalisedURLDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,33 @@ export default class NormalisedURLDomain {
function normaliseURLDomainOrThrowError(input: string, ignoreProtocol = false): string {
input = input.trim().toLowerCase();

try {
if (!input.startsWith("http://") && !input.startsWith("https://") && !input.startsWith("supertokens://")) {
throw new Error("converting to proper URL");
// if the input starts with a . (eg: .domain.tld)
if (input.indexOf(".") === 0) {
input = input.substring(1);
}

// if the input dosen't start with a protocol add a default one;
if (input.match(/^[^:]:\/\//)) {
if (input.startsWith("localhost") || isAnIpAddress(input)) {
input = "http://" + input;
} else {
input = "https://" + input;
}
let urlObj = new URL(input);
}

try {
const urlObj = new URL(input);

if (ignoreProtocol) {
if (urlObj.hostname.startsWith("localhost") || isAnIpAddress(urlObj.hostname)) {
input = "http://" + urlObj.host;
return "http://" + urlObj.host;
} else {
input = "https://" + urlObj.host;
return "https://" + urlObj.host;
}
} else {
input = urlObj.protocol + "//" + urlObj.host;
return urlObj.protocol + "//" + urlObj.host;
}

return input;
} catch (err) {}
// not a valid URL

if (input.startsWith("/")) {
} catch {
throw Error("Please provide a valid domain name");
}

if (input.indexOf(".") === 0) {
input = input.substr(1);
}

// If the input contains a . it means they have given a domain name.
// So we try assuming that they have given a domain name
if (
(input.indexOf(".") !== -1 || input.startsWith("localhost")) &&
!input.startsWith("http://") &&
!input.startsWith("https://")
) {
input = "https://" + input;

// at this point, it should be a valid URL. So we test that before doing a recursive call
try {
new URL(input);
return normaliseURLDomainOrThrowError(input, true);
} catch (err) {}
}

throw Error("Please provide a valid domain name");
}

0 comments on commit e6c5477

Please sign in to comment.