1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import { fastPathLookup, } from 'tldts-core';
import { exceptions, rules } from './data/trie';
/**
* Lookup parts of domain in Trie
*/
function lookupInTrie(parts, trie, index, allowedMask) {
var result = null;
var node = trie;
while (node !== undefined) {
// We have a match!
if ((node.$ & allowedMask) !== 0) {
result = {
index: index + 1,
isIcann: node.$ === 1 /* ICANN */,
isPrivate: node.$ === 2 /* PRIVATE */
};
}
// No more `parts` to look for
if (index === -1) {
break;
}
var succ = node.succ;
node = succ && (succ[parts[index]] || succ['*']);
index -= 1;
}
return result;
}
/**
* Check if `hostname` has a valid public suffix in `trie`.
*/
export default function suffixLookup(hostname, options, out) {
if (fastPathLookup(hostname, options, out) === true) {
return;
}
var hostnameParts = hostname.split('.');
var allowedMask = (options.allowPrivateDomains === true ? 2 /* PRIVATE */ : 0) |
(options.allowIcannDomains === true ? 1 /* ICANN */ : 0);
// Look for exceptions
var exceptionMatch = lookupInTrie(hostnameParts, 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;
}
// Look for a match in rules
var rulesMatch = lookupInTrie(hostnameParts, rules, hostnameParts.length - 1, allowedMask);
if (rulesMatch !== null) {
out.isIcann = rulesMatch.isIcann;
out.isPrivate = rulesMatch.isPrivate;
out.publicSuffix = hostnameParts.slice(rulesMatch.index).join('.');
return;
}
// No match found...
// Prevailing rule is '*' so we consider the top-level domain to be the
// public suffix of `hostname` (e.g.: 'example.org' => 'org').
out.isIcann = false;
out.isPrivate = false;
out.publicSuffix = hostnameParts[hostnameParts.length - 1];
}
//# sourceMappingURL=suffix-trie.js.map
|