mirror of
https://github.com/jakejarvis/rdapper.git
synced 2026-09-23 01:25:31 -04:00
Migrate from psl to tldts for public suffix detection
This commit is contained in:
@@ -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
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user