import rules from "./psl.js"; // Registrable domain (eTLD+1) per the Public Suffix List algorithm. IPs/single labels returned as-is. export function getDomain(hostname) { const host = hostname.toLowerCase().replace(/\.$/, ""); if (!host.includes(".") || host.includes(":") || /^[\d.]+$/.test(host)) return host; const labels = host.split("."); let suffix = 1; // implicit "*" rule for (let i = 0; i < labels.length; i++) { // longest suffix first const s = labels.slice(i).join("."); if (rules.has("!" + s)) { suffix = labels.length - i - 1; break; } if (rules.has(s) || rules.has(["*", ...labels.slice(i + 1)].join("."))) { suffix = labels.length - i; break; } } return suffix >= labels.length ? host : labels.slice(-suffix - 1).join("."); }