diff --git a/portal/src/helpers/domain_parsing.test.ts b/portal/src/helpers/domain_parsing.test.ts index eed41945..783c030f 100644 --- a/portal/src/helpers/domain_parsing.test.ts +++ b/portal/src/helpers/domain_parsing.test.ts @@ -34,22 +34,22 @@ const getSubdomainAndPathTestCases: [string, Path][] = [ ['http://name.localhost:8080/', { subdomain: 'name', path: '/index.html' }], ['http://flatland.localhost:8080/', { subdomain: 'flatland', path: '/index.html' }], ['http://subname.suinsname.localhost:8080/', - { subdomain: 'subname.suinsname', path: '/index.html' }], + { subdomain: 'subname.suinsname', path: '/index.html' }], ['https://subsubname.subname.suinsname.portalname.co.uk/', - { subdomain: 'subsubname.subname.suinsname', path: '/index.html' }], + { subdomain: 'subsubname.subname.suinsname', path: '/index.html' }], ['http://docs.localhost/css/print.css', { subdomain: 'docs', path: '/css/print.css' }], ['http://docs.localhost/assets/index-a242f32b.js', - { subdomain: 'docs', path: '/assets/index-a242f32b.js'}] + { subdomain: 'docs', path: '/assets/index-a242f32b.js'}] ] describe('getSubdomainAndPath', () => { - getSubdomainAndPathTestCases.forEach( - ([input, path]) => { - test(`${input} -> - subdomain: ${path.subdomain ?? "null"}, - path: ${path.path ?? "null"}`, - () => { - expect(getSubdomainAndPath(new URL(input))).toEqual(path); - }); - }); + getSubdomainAndPathTestCases.forEach( + ([input, path]) => { + test(`${input} -> + subdomain: ${path.subdomain ?? "null"}, + path: ${path.path ?? "null"}`, + () => { + expect(getSubdomainAndPath(new URL(input))).toEqual(path); + }); + }); }) diff --git a/portal/src/helpers/domain_parsing.ts b/portal/src/helpers/domain_parsing.ts index e75adbeb..c79aac61 100644 --- a/portal/src/helpers/domain_parsing.ts +++ b/portal/src/helpers/domain_parsing.ts @@ -9,18 +9,18 @@ import { Path } from "../types/index"; * @param orig_url The URL to extract the domain from. e.g. "https://example.com" * @returns The domain of the URL. e.g. "example.com" */ - export function getDomain(url: URL): string { - const parsed = parseDomain(url.hostname); - if (parsed.type === ParseResultType.Listed) { - const domain = parsed.domain + "." + parsed.topLevelDomains.join("."); - return domain; - } else if (parsed.type === ParseResultType.Reserved) { - return parsed.labels[parsed.labels.length - 1]; - } else { - console.error("Error while parsing domain name:", parsed); - throw new Error("Error while parsing domain name"); - } - } +export function getDomain(url: URL): string { + const parsed = parseDomain(url.hostname); + if (parsed.type === ParseResultType.Listed) { + const domain = parsed.domain + "." + parsed.topLevelDomains.join("."); + return domain; + } else if (parsed.type === ParseResultType.Reserved) { + return parsed.labels[parsed.labels.length - 1]; + } else { + console.error("Error while parsing domain name:", parsed); + throw new Error("Error while parsing domain name"); + } +} /** * Given a URL, returns the subdomain and path. @@ -32,14 +32,14 @@ export function getSubdomainAndPath(url: URL): Path | null { const parsed = parseDomain(url.hostname); if (parsed.type === ParseResultType.Listed) { return { - subdomain: parsed.subDomains.join("."), - path: url.pathname == "/" ? "/index.html" : removeLastSlash(url.pathname) + subdomain: parsed.subDomains.join("."), + path: url.pathname == "/" ? "/index.html" : removeLastSlash(url.pathname) } as Path; } else if ( parsed.type === ParseResultType.Reserved) { - return { - subdomain: parsed.labels.slice(0, parsed.labels.length-1).join('.'), - path: url.pathname == "/" ? "/index.html" : removeLastSlash(url.pathname) - } as Path; + return { + subdomain: parsed.labels.slice(0, parsed.labels.length-1).join('.'), + path: url.pathname == "/" ? "/index.html" : removeLastSlash(url.pathname) + } as Path; } return null; }