Initial import of BugPass
This commit is contained in:
commit
df3554542c
20 files changed
+1588
No files matched your search
@@ -0,0 +1,15 @@
|
||||
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(".");
|
||||
}
|
||||
Reference in new issue
Block a user