Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid calling getDomainFromOriginOrURL twice per getEntity #243

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions lib/create-entity-finder-api.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
const DOMAIN_IN_URL_REGEX = /:\/\/(\S*?)(:\d+)?(\/|$)/
const DOMAIN_CHARACTERS = /([a-z0-9.-]+\.[a-z0-9]+|localhost)/i
const DOMAIN_CHARACTERS = /(?:[a-z0-9.-]+\.[a-z0-9]+|localhost)/i
const IP_REGEX = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
const ROOT_DOMAIN_REGEX = /[^.]+\.([^.]+|(gov|com|co|ne)\.\w{2})$/i

function getDomainFromOriginOrURL(originOrURL) {
if (typeof originOrURL !== 'string') return null
if (originOrURL.length > 10000 || originOrURL.startsWith('data:')) return null
/**
* @param {string} originOrURL
* @return {[string|null, string|null]} - The first item is the root domain, the second item is the domain.
*/
function parseDomains(originOrURL) {
if (typeof originOrURL !== 'string') return [null, null]
if (originOrURL.length > 10000 || originOrURL.startsWith('data:')) return [null, null]
let m = originOrURL.match(DOMAIN_IN_URL_REGEX)
if (m) return m[1]
let domain;
if (m) {
domain = m[1]
}
m = originOrURL.match(DOMAIN_CHARACTERS)
if (m) return m[0]
return null
if (m) {
domain = m[0]
}

if (!domain) return [null, null]
if (IP_REGEX.test(domain)) return [domain, domain]
m = domain.match(ROOT_DOMAIN_REGEX)
const rootDomain = m && m[0] || domain;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a little nervous that this passed the linter 😆


return [rootDomain, domain]
}

function getRootDomain(originOrURL) {
const domain = getDomainFromOriginOrURL(originOrURL)
if (!domain) return null
if (IP_REGEX.test(domain)) return domain
const match = domain.match(ROOT_DOMAIN_REGEX)
return (match && match[0]) || domain
function getRootDomain(originOrURL,) {
return parseDomains(originOrURL)[0];
}

function sliceSubdomainFromDomain(domain, rootDomain) {
Expand All @@ -30,8 +41,7 @@ function sliceSubdomainFromDomain(domain, rootDomain) {
}

function getEntityInDataset(entityByDomain, entityBySubDomain, entityByRootDomain, originOrURL) {
const domain = getDomainFromOriginOrURL(originOrURL)
const rootDomain = getRootDomain(domain)
const [rootDomain, domain] = parseDomains(originOrURL);
if (!domain || !rootDomain) return undefined
if (entityByDomain.has(domain)) return entityByDomain.get(domain)

Expand Down