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.
- package/LICENSE +13 -0
- package/README.md +290 -0
- package/dist/cjs/index.js +656 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/src/data/trie.js +14 -0
- package/dist/cjs/src/data/trie.js.map +1 -0
- package/dist/cjs/src/suffix-trie.js +57 -0
- package/dist/cjs/src/suffix-trie.js.map +1 -0
- package/dist/cjs/tsconfig.tsbuildinfo +1 -0
- package/dist/es6/index.js +33 -0
- package/dist/es6/index.js.map +1 -0
- package/dist/es6/src/data/trie.js +11 -0
- package/dist/es6/src/data/trie.js.map +1 -0
- package/dist/es6/src/suffix-trie.js +54 -0
- package/dist/es6/src/suffix-trie.js.map +1 -0
- package/dist/es6/tsconfig.bundle.tsbuildinfo +1 -0
- package/dist/index.cjs.min.js +2 -0
- package/dist/index.cjs.min.js.map +1 -0
- package/dist/index.esm.min.js +2 -0
- package/dist/index.esm.min.js.map +1 -0
- package/dist/index.umd.min.js +2 -0
- package/dist/index.umd.min.js.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/src/data/trie.d.ts +5 -0
- package/dist/types/src/suffix-trie.d.ts +5 -0
- package/index.ts +62 -0
- package/package.json +88 -0
- package/src/data/trie.ts +14 -0
- package/src/suffix-trie.ts +87 -0
|
@@ -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
|
+
}
|