|
"use strict"; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.default = suffixLookup; |
|
const tldts_core_1 = require("tldts-core"); |
|
const trie_1 = require("./data/trie"); |
|
|
|
|
|
|
|
function lookupInTrie(parts, trie, index, allowedMask) { |
|
let result = null; |
|
let node = trie; |
|
while (node !== undefined) { |
|
|
|
if ((node[0] & allowedMask) !== 0) { |
|
result = { |
|
index: index + 1, |
|
isIcann: node[0] === 1 , |
|
isPrivate: node[0] === 2 , |
|
}; |
|
} |
|
|
|
if (index === -1) { |
|
break; |
|
} |
|
const succ = node[1]; |
|
node = Object.prototype.hasOwnProperty.call(succ, parts[index]) |
|
? succ[parts[index]] |
|
: succ['*']; |
|
index -= 1; |
|
} |
|
return result; |
|
} |
|
|
|
|
|
|
|
function suffixLookup(hostname, options, out) { |
|
var _a; |
|
if ((0, tldts_core_1.fastPathLookup)(hostname, options, out)) { |
|
return; |
|
} |
|
const hostnameParts = hostname.split('.'); |
|
const allowedMask = (options.allowPrivateDomains ? 2 : 0) | |
|
(options.allowIcannDomains ? 1 : 0); |
|
|
|
const exceptionMatch = lookupInTrie(hostnameParts, trie_1.exceptions, hostnameParts.length - 1, allowedMask); |
|
if (exceptionMatch !== null) { |
|
out.isIcann = exceptionMatch.isIcann; |
|
out.isPrivate = exceptionMatch.isPrivate; |
|
out.publicSuffix = hostnameParts.slice(exceptionMatch.index + 1).join('.'); |
|
return; |
|
} |
|
|
|
const rulesMatch = lookupInTrie(hostnameParts, trie_1.rules, hostnameParts.length - 1, allowedMask); |
|
if (rulesMatch !== null) { |
|
out.isIcann = rulesMatch.isIcann; |
|
out.isPrivate = rulesMatch.isPrivate; |
|
out.publicSuffix = hostnameParts.slice(rulesMatch.index).join('.'); |
|
return; |
|
} |
|
|
|
|
|
|
|
out.isIcann = false; |
|
out.isPrivate = false; |
|
out.publicSuffix = (_a = hostnameParts[hostnameParts.length - 1]) !== null && _a !== void 0 ? _a : null; |
|
} |
|
|