Migrate from psl to tldts for public suffix detection

This commit is contained in:
2025-10-10 10:29:29 -04:00
parent a030817d27
commit bfcecc5eff
6 changed files with 86 additions and 121 deletions
+4 -4
View File
@@ -1,9 +1,9 @@
import { expect, test } from "vitest";
import { extractTld, isLikelyDomain } from "./domain";
import { getDomainParts, isLikelyDomain } from "./domain";
test("extractTld basic", () => {
expect(extractTld("example.com")).toBe("com");
expect(extractTld("sub.example.co.uk")).toBe("uk");
test("getDomainParts.tld basic", () => {
expect(getDomainParts("example.com").tld).toBe("com");
expect(getDomainParts("sub.example.co.uk").tld).toBe("uk");
});
test("isLikelyDomain", () => {
+4 -28
View File
@@ -1,37 +1,13 @@
import psl from "psl";
export function extractTld(domain: string): string {
const lower = domain.trim().toLowerCase();
try {
const parsed = psl.parse?.(lower) as { tld?: string };
const suffix = parsed?.tld;
if (suffix) {
const labels = String(suffix).split(".").filter(Boolean);
if (labels.length) return labels[labels.length - 1];
}
} catch {
// ignore and fall back
}
const parts = lower.split(".").filter(Boolean);
return parts[parts.length - 1] ?? lower;
}
import { getPublicSuffix } from "tldts";
export function getDomainParts(domain: string): {
publicSuffix: string;
tld: string;
} {
const lower = domain.toLowerCase().trim();
let publicSuffix: string | undefined;
try {
const parsed = psl.parse?.(lower) as { tld?: string };
publicSuffix = parsed?.tld;
} catch {
// ignore
}
if (!publicSuffix) {
const parts = lower.split(".").filter(Boolean);
publicSuffix = parts.length ? parts[parts.length - 1] : lower;
}
const suffix = getPublicSuffix(lower) || "";
const publicSuffix =
suffix || lower.split(".").filter(Boolean).pop() || lower;
const labels = publicSuffix.split(".").filter(Boolean);
const tld = labels.length ? labels[labels.length - 1] : publicSuffix;
return { publicSuffix, tld };