tldts-icann 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ import {
2
+ fastPathLookup,
3
+ IPublicSuffix,
4
+ ISuffixLookupOptions,
5
+ } from 'tldts-core';
6
+ import { exceptions, ITrie, rules } from './data/trie';
7
+
8
+ interface IMatch {
9
+ index: number;
10
+ }
11
+
12
+ /**
13
+ * Lookup parts of domain in Trie
14
+ */
15
+ function lookupInTrie(
16
+ parts: string[],
17
+ trie: ITrie,
18
+ index: number,
19
+ ): IMatch | null {
20
+ let result: IMatch | null = null;
21
+ let node: ITrie | undefined = trie;
22
+ while (node !== undefined) {
23
+ // We have a match!
24
+ if (node[0] === 1) {
25
+ result = {
26
+ index: index + 1,
27
+ };
28
+ }
29
+
30
+ // No more `parts` to look for
31
+ if (index === -1) {
32
+ break;
33
+ }
34
+
35
+ const succ: { [label: string]: ITrie } = node[1];
36
+ node = Object.prototype.hasOwnProperty.call(succ, parts[index]!)
37
+ ? succ[parts[index]!]
38
+ : succ['*'];
39
+ index -= 1;
40
+ }
41
+
42
+ return result;
43
+ }
44
+
45
+ /**
46
+ * Check if `hostname` has a valid public suffix in `trie`.
47
+ */
48
+ export default function suffixLookup(
49
+ hostname: string,
50
+ options: ISuffixLookupOptions,
51
+ out: IPublicSuffix,
52
+ ): void {
53
+ if (fastPathLookup(hostname, options, out)) {
54
+ return;
55
+ }
56
+
57
+ const hostnameParts = hostname.split('.');
58
+
59
+ // Look for exceptions
60
+ const exceptionMatch = lookupInTrie(
61
+ hostnameParts,
62
+ exceptions,
63
+ hostnameParts.length - 1,
64
+ );
65
+
66
+ if (exceptionMatch !== null) {
67
+ out.publicSuffix = hostnameParts.slice(exceptionMatch.index + 1).join('.');
68
+ return;
69
+ }
70
+
71
+ // Look for a match in rules
72
+ const rulesMatch = lookupInTrie(
73
+ hostnameParts,
74
+ rules,
75
+ hostnameParts.length - 1,
76
+ );
77
+
78
+ if (rulesMatch !== null) {
79
+ out.publicSuffix = hostnameParts.slice(rulesMatch.index).join('.');
80
+ return;
81
+ }
82
+
83
+ // No match found...
84
+ // Prevailing rule is '*' so we consider the top-level domain to be the
85
+ // public suffix of `hostname` (e.g.: 'example.org' => 'org').
86
+ out.publicSuffix = hostnameParts[hostnameParts.length - 1] ?? null;
87
+ }