From a68056402cee1e75135ba0567a490929c6062866 Mon Sep 17 00:00:00 2001 From: "H. Kamran" Date: Tue, 9 Jul 2024 16:02:48 -0700 Subject: [PATCH] Use the base domain for Similarweb (#4) * Use the base domain for Similarweb * Update JSDoc variable --- src/logger.js | 4 ++-- src/passkeys.js | 21 +++++++++------------ src/tests/SimilarWeb.js | 20 ++++++++++++++++++-- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/logger.js b/src/logger.js index 5d17245..1eb75fd 100644 --- a/src/logger.js +++ b/src/logger.js @@ -24,8 +24,8 @@ class Logger { return this.messages; } - clearMessages(){ - this.messages = [] + clearMessages() { + this.messages = []; } } diff --git a/src/passkeys.js b/src/passkeys.js index f4b13d0..e50bd89 100644 --- a/src/passkeys.js +++ b/src/passkeys.js @@ -3,7 +3,7 @@ import Facebook from './tests/Facebook.js'; import Blocklist from './tests/Blocklist.js'; import logger from './logger'; -export default async function(req, env) { +export default async function (req, env) { const { pr, repo } = req.params; const repository = `${env.OWNER}/${repo}`; @@ -18,17 +18,15 @@ export default async function(req, env) { // Validate any additional domains for (const domain of entry['additional-domains'] || []) { - await SimilarWeb(domain, env, entry.file) + await SimilarWeb(domain, env, entry.file); await Blocklist(domain); } // Validate Facebook contact if present if (entry.contact?.facebook) await Facebook(entry.contact.facebook); - } catch (e) { // Return an error response if validation fails - return new Response(`::error file=${entry.file}:: ${e.message}`, - { status: 400 }); + return new Response(`::error file=${entry.file}:: ${e.message}`, { status: 400 }); } } @@ -47,13 +45,12 @@ export default async function(req, env) { * @returns {Promise<*[]>} Returns all modified entry files as an array. */ async function fetchEntries(repo, pr) { - const data = await fetch( - `https://api.github.com/repos/${repo}/pulls/${pr}/files`, { - headers: { - 'Accept': 'application/vnd.github.v3+json', - 'User-Agent': '2factorauth/twofactorauth (+https://2fa.directory/bots)' - } - }); + const data = await fetch(`https://api.github.com/repos/${repo}/pulls/${pr}/files`, { + headers: { + Accept: 'application/vnd.github.v3+json', + 'User-Agent': '2factorauth/twofactorauth (+https://2fa.directory/bots)', + }, + }); if (!data.ok) throw new Error(await data.text()); diff --git a/src/tests/SimilarWeb.js b/src/tests/SimilarWeb.js index 9de98f9..a9abb7c 100644 --- a/src/tests/SimilarWeb.js +++ b/src/tests/SimilarWeb.js @@ -4,12 +4,13 @@ const test = 'SimilarWeb'; /** * Retrieve the Similarweb rank for a given domain - * @param {string} domain The domain to check + * @param {string} entryDomain The domain to check * @param {*} env The environment * @param {string} [file] The filename of the entry, should only be set for non-primary domains * @returns {Promise} Returns `0` if it's a success, `1` otherwise */ -export default async function (domain, env, file) { +export default async function (entryDomain, env, file) { + const domain = getBaseDomain(entryDomain); const res = await fetch(`https://api.similarweb.com/v1/similar-rank/${domain}/rank?api_key=${env.SIMILARWEB_API_KEY}`, { cf: { cacheTtlByStatus: { @@ -56,3 +57,18 @@ export default async function (domain, env, file) { return 0; } + +/** + * Return the base domain of a domain with possible subdomains + * @param {string} domain The domain to parse + * @returns {string} The base domain + */ +function getBaseDomain(hostname) { + let parts = hostname.split('.'); + if (parts.length <= 2) return hostname; + + parts = parts.slice(-3); + if (['co', 'com'].includes(parts[1])) return parts.join('.'); + + return parts.slice(-2).join('.'); +}