Skip to content

Commit

Permalink
Use the base domain for Similarweb (#4)
Browse files Browse the repository at this point in the history
* Use the base domain for Similarweb

* Update JSDoc variable
  • Loading branch information
hkamran80 authored Jul 9, 2024
1 parent 902ffa9 commit a680564
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Logger {
return this.messages;
}

clearMessages(){
this.messages = []
clearMessages() {
this.messages = [];
}
}

Expand Down
21 changes: 9 additions & 12 deletions src/passkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand All @@ -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 });
}
}

Expand All @@ -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());

Expand Down
20 changes: 18 additions & 2 deletions src/tests/SimilarWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>} 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: {
Expand Down Expand Up @@ -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('.');
}

0 comments on commit a680564

Please sign in to comment.