tldts 5.7.84 → 5.7.85

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,110 @@
1
+ import {
2
+ fastPathLookup,
3
+ IPublicSuffix,
4
+ ISuffixLookupOptions,
5
+ } from 'tldts-core';
6
+ import { exceptions, ITrie, rules } from './data/trie';
7
+
8
+ // Flags used to know if a rule is ICANN or Private
9
+ const enum RULE_TYPE {
10
+ ICANN = 1,
11
+ PRIVATE = 2,
12
+ }
13
+
14
+ interface IMatch {
15
+ index: number;
16
+ isIcann: boolean;
17
+ isPrivate: boolean;
18
+ }
19
+
20
+ /**
21
+ * Lookup parts of domain in Trie
22
+ */
23
+ function lookupInTrie(
24
+ parts: string[],
25
+ trie: ITrie,
26
+ index: number,
27
+ allowedMask: number,
28
+ ): IMatch | null {
29
+ let result: IMatch | null = null;
30
+ let node: ITrie | undefined = trie;
31
+ while (node !== undefined) {
32
+ // We have a match!
33
+ if ((node.$ & allowedMask) !== 0) {
34
+ result = {
35
+ index: index + 1,
36
+ isIcann: node.$ === RULE_TYPE.ICANN,
37
+ isPrivate: node.$ === RULE_TYPE.PRIVATE,
38
+ };
39
+ }
40
+
41
+ // No more `parts` to look for
42
+ if (index === -1) {
43
+ break;
44
+ }
45
+
46
+ const succ: {
47
+ [label: string]: ITrie;
48
+ } = node.succ;
49
+ node = succ && (succ[parts[index]] || succ['*']);
50
+ index -= 1;
51
+ }
52
+
53
+ return result;
54
+ }
55
+
56
+ /**
57
+ * Check if `hostname` has a valid public suffix in `trie`.
58
+ */
59
+ export default function suffixLookup(
60
+ hostname: string,
61
+ options: ISuffixLookupOptions,
62
+ out: IPublicSuffix,
63
+ ): void {
64
+ if (fastPathLookup(hostname, options, out) === true) {
65
+ return;
66
+ }
67
+
68
+ const hostnameParts = hostname.split('.');
69
+
70
+ const allowedMask =
71
+ (options.allowPrivateDomains === true ? RULE_TYPE.PRIVATE : 0) |
72
+ (options.allowIcannDomains === true ? RULE_TYPE.ICANN : 0);
73
+
74
+ // Look for exceptions
75
+ const exceptionMatch = lookupInTrie(
76
+ hostnameParts,
77
+ exceptions,
78
+ hostnameParts.length - 1,
79
+ allowedMask,
80
+ );
81
+
82
+ if (exceptionMatch !== null) {
83
+ out.isIcann = exceptionMatch.isIcann;
84
+ out.isPrivate = exceptionMatch.isPrivate;
85
+ out.publicSuffix = hostnameParts.slice(exceptionMatch.index + 1).join('.');
86
+ return;
87
+ }
88
+
89
+ // Look for a match in rules
90
+ const rulesMatch = lookupInTrie(
91
+ hostnameParts,
92
+ rules,
93
+ hostnameParts.length - 1,
94
+ allowedMask,
95
+ );
96
+
97
+ if (rulesMatch !== null) {
98
+ out.isIcann = rulesMatch.isIcann;
99
+ out.isPrivate = rulesMatch.isPrivate;
100
+ out.publicSuffix = hostnameParts.slice(rulesMatch.index).join('.');
101
+ return;
102
+ }
103
+
104
+ // No match found...
105
+ // Prevailing rule is '*' so we consider the top-level domain to be the
106
+ // public suffix of `hostname` (e.g.: 'example.org' => 'org').
107
+ out.isIcann = false;
108
+ out.isPrivate = false;
109
+ out.publicSuffix = hostnameParts[hostnameParts.length - 1];
110
+ }