From 662360509c4c55b3431db8bbd3c82f2f5a927ba2 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Thu, 14 Dec 2023 22:51:24 +0000 Subject: [PATCH] url: return host instead of bool for success --- src/modules/api.js | 5 +++-- src/modules/processing/url.js | 14 ++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/modules/api.js b/src/modules/api.js index 19f657cc..eebbfe27 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -4,13 +4,14 @@ import { apiJSON } from "./sub/utils.js"; import { errorUnsupported } from "./sub/errors.js"; import loc from "../localization/manager.js"; import match from "./processing/match.js"; -import { hasValidHostname, normalizeURL } from "./processing/url.js"; +import { getHostIfValid, normalizeURL } from "./processing/url.js"; export async function getJSON(originalURL, lang, obj) { try { const url = normalizeURL(decodeURIComponent(originalURL)); + const host = getHostIfValid(url); - if (!hasValidHostname(url) || !services[host].enabled) { + if (!host || !services[host].enabled) { return apiJSON(0, { t: errorUnsupported(lang) }); } diff --git a/src/modules/processing/url.js b/src/modules/processing/url.js index a58254a9..75202f5f 100644 --- a/src/modules/processing/url.js +++ b/src/modules/processing/url.js @@ -84,19 +84,17 @@ export function normalizeURL(url) { ); } -export function hasValidHostname(url) { +export function getHostIfValid(url) { const host = psl.parse(url.hostname); - if (host.error) return false; + if (host.error) return; const service = services[host.sld]; - if (!service) return false; - - if ((service.tld ?? 'com') !== host.tld) return false; + if (!service) return; + if ((service.tld ?? 'com') !== host.tld) return; const anySubdomainAllowed = service.subdomains === '*'; const validSubdomain = [null, 'www', ...(service.subdomains ?? [])].includes(host.subdomain); - if (!validSubdomain && !anySubdomainAllowed) - return false; + if (!validSubdomain && !anySubdomainAllowed) return; - return true; + return host.sld; } \ No newline at end of file