Skip to content

Full Toolkit for Next-Level Domain Analysis

Extract getBaseDomain function 🚛

Felix Wotschofsky2c017e76da6b
diff --git a/app/lookup/[domain]/whois/page.tsx b/app/lookup/[domain]/whois/page.tsx
index dffcb5c..4a97e7a 100644
--- a/app/lookup/[domain]/whois/page.tsx
+++ b/app/lookup/[domain]/whois/page.tsx
@@ -1,8 +1,8 @@
import type { Metadata } from 'next';
import Link from 'next/link';
import { type FC, Fragment } from 'react';
-import { getDomain } from 'tldts';
+import { getBaseDomain } from '@/lib/utils';
import { lookupWhois } from '@/lib/whois';
type WhoisResultsPageProps = {
@@ -35,8 +35,8 @@ const WhoisResultsPage: FC<WhoisResultsPageProps> = async ({
searchParams: { force },
}) => {
const forceOriginal = force !== undefined;
- const rawDomain = getDomain(domain) || domain;
- const results = await lookupWhois(forceOriginal ? domain : rawDomain);
+ const baseDomain = getBaseDomain(domain);
+ const results = await lookupWhois(forceOriginal ? domain : baseDomain);
if (results.length === 0) {
throw new Error('No results found');
@@ -44,7 +44,7 @@ const WhoisResultsPage: FC<WhoisResultsPageProps> = async ({
return (
<>
- {rawDomain !== domain &&
+ {baseDomain !== domain &&
(force !== undefined ? (
<>
<p className="mt-8 text-muted-foreground">
@@ -56,14 +56,14 @@ const WhoisResultsPage: FC<WhoisResultsPageProps> = async ({
className="underline decoration-dotted underline-offset-4"
href={`/lookup/${domain}/whois`}
>
- {rawDomain} instead
+ {baseDomain} instead
</Link>
</p>
</>
) : (
<>
<p className="mt-8 text-muted-foreground">
- Showing results for {rawDomain}
+ Showing results for {baseDomain}
</p>
<p className="text-muted-foreground">
Force lookup for{' '}
diff --git a/lib/search.ts b/lib/search.ts
index 85fbd00..ca2f794 100644
--- a/lib/search.ts
+++ b/lib/search.ts
@@ -1,6 +1,5 @@
-import { getDomain } from 'tldts';
-
import { bigquery } from '@/lib/bigquery';
+import { getBaseDomain } from '@/lib/utils';
type LookupLogPayload = {
domain: string;
@@ -13,10 +12,7 @@ export const recordLookup = async (payload: LookupLogPayload) => {
return;
}
- // Remove wildcard prefix to avoid base domain not being extracted correctly
- // Remove trailing dot
- const cleanedDomain = payload.domain.replace(/^\*\./, '').replace(/\.$/, '');
- const baseDomain = getDomain(cleanedDomain) || cleanedDomain;
+ const baseDomain = getBaseDomain(payload.domain);
await bigquery
.insertRows({
diff --git a/lib/utils.test.ts b/lib/utils.test.ts
index c047976..5a0d6b2 100644
--- a/lib/utils.test.ts
+++ b/lib/utils.test.ts
@@ -8,7 +8,13 @@ import {
vi,
} from 'vitest';
-import { deduplicate, isAppleDevice, isValidDomain, retry } from './utils';
+import {
+ deduplicate,
+ getBaseDomain,
+ isAppleDevice,
+ isValidDomain,
+ retry,
+} from './utils';
describe('deduplicate', () => {
it('removes duplicate numbers from an array', () => {
@@ -115,6 +121,30 @@ describe('isValidDomain', () => {
});
});
+describe('getBaseDomain', () => {
+ it('extracts the base domain from a given domain', () => {
+ expect(getBaseDomain('example.com')).toBe('example.com');
+ expect(getBaseDomain('subdomain.example.com')).toBe('example.com');
+ expect(getBaseDomain('www.example.co.uk')).toBe('example.co.uk');
+ expect(getBaseDomain('*.example.com')).toBe('example.com');
+ });
+
+ it('removes wildcard prefixes', () => {
+ expect(getBaseDomain('*.example.com')).toBe('example.com');
+ });
+
+ it('removes trailing dots', () => {
+ expect(getBaseDomain('example.com.')).toBe('example.com');
+ expect(getBaseDomain('subdomain.example.com.')).toBe('example.com');
+ });
+
+ it('returns the input when no base domain is found', () => {
+ expect(getBaseDomain('')).toBe('');
+ expect(getBaseDomain('xn--fiqs8s')).toBe('xn--fiqs8s');
+ expect(getBaseDomain('com')).toBe('com');
+ });
+});
+
// User agent strings from https://gist.github.com/pzb/b4b6f57144aea7827ae4
describe('isAppleDevice', () => {
it('detects Apple devices', () => {
diff --git a/lib/utils.ts b/lib/utils.ts
index efee47f..419fc7a 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -1,6 +1,6 @@
import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
-import { parse as tldtsParse } from 'tldts';
+import { getDomain, parse as tldtsParse } from 'tldts';
export const cn = (...inputs: ClassValue[]) => twMerge(clsx(inputs));
@@ -41,6 +41,19 @@ export const isValidDomain = (domain: string) => {
}
};
+/**
+ * Extracts the base domain from a given domain
+ *
+ * Example: www.example.com -> example.com
+ */
+export const getBaseDomain = (domain: string) => {
+ // Remove wildcard prefix to avoid base domain not being extracted correctly
+ // Remove trailing dot
+ const cleanedDomain = domain.replace(/^\*\./, '').replace(/\.$/, '');
+ const baseDomain = getDomain(cleanedDomain) || cleanedDomain;
+ return baseDomain;
+};
+
export const isAppleDevice = () => {
if (typeof window === 'undefined') return false;
const userAgent = window.navigator.userAgent;
diff --git a/lib/whois.ts b/lib/whois.ts
index fbd24bd..a1ee48e 100644
--- a/lib/whois.ts
+++ b/lib/whois.ts
@@ -1,7 +1,6 @@
-import { getDomain } from 'tldts';
import whoiser, { type WhoisSearchResult } from 'whoiser';
-import { isValidDomain } from './utils';
+import { getBaseDomain, isValidDomain } from './utils';
export const lookupWhois = async (domain: string) => {
const result = await whoiser(domain, {
@@ -54,10 +53,10 @@ export const getWhoisSummary = async (
};
}
- const rawDomain = getDomain(domain) || domain;
+ const baseDomain = getBaseDomain(domain);
try {
- const results = await whoiser(rawDomain, {
+ const results = await whoiser(baseDomain, {
timeout: 5000,
raw: true,
});