1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseDomain = exports.ParseResultType = exports.RESERVED_TOP_LEVEL_DOMAINS = void 0;
|
4 | const serialized_tries_1 = require("./serialized-tries");
|
5 | const look_up_1 = require("./trie/look-up");
|
6 | const sanitize_1 = require("./sanitize");
|
7 | const parse_trie_1 = require("./trie/parse-trie");
|
8 | exports.RESERVED_TOP_LEVEL_DOMAINS = [
|
9 | "localhost",
|
10 | "local",
|
11 | "example",
|
12 | "invalid",
|
13 | "test",
|
14 | ];
|
15 | var ParseResultType;
|
16 | (function (ParseResultType) {
|
17 | |
18 |
|
19 |
|
20 | ParseResultType["Invalid"] = "INVALID";
|
21 | |
22 |
|
23 |
|
24 | ParseResultType["Ip"] = "IP";
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 | ParseResultType["Reserved"] = "RESERVED";
|
31 | |
32 |
|
33 |
|
34 | ParseResultType["NotListed"] = "NOT_LISTED";
|
35 | |
36 |
|
37 |
|
38 | ParseResultType["Listed"] = "LISTED";
|
39 | })(ParseResultType = exports.ParseResultType || (exports.ParseResultType = {}));
|
40 | const getAtIndex = (array, index) => {
|
41 | return index >= 0 && index < array.length ? array[index] : undefined;
|
42 | };
|
43 | const splitLabelsIntoDomains = (labels, index) => {
|
44 | return {
|
45 | subDomains: labels.slice(0, Math.max(0, index)),
|
46 | domain: getAtIndex(labels, index),
|
47 | topLevelDomains: labels.slice(index + 1),
|
48 | };
|
49 | };
|
50 | let parsedIcannTrie;
|
51 | let parsedPrivateTrie;
|
52 |
|
53 |
|
54 |
|
55 | const parseDomain = (hostname) => {
|
56 | const sanitizationResult = (0, sanitize_1.sanitize)(hostname);
|
57 | if (sanitizationResult.type === sanitize_1.SanitizationResultType.Error) {
|
58 | return {
|
59 | type: ParseResultType.Invalid,
|
60 | hostname,
|
61 | errors: sanitizationResult.errors,
|
62 | };
|
63 | }
|
64 | if (sanitizationResult.type === sanitize_1.SanitizationResultType.ValidIp) {
|
65 | return {
|
66 | type: ParseResultType.Ip,
|
67 | hostname: sanitizationResult.ip,
|
68 | ipVersion: sanitizationResult.ipVersion,
|
69 | };
|
70 | }
|
71 | const { labels, domain } = sanitizationResult;
|
72 | if (hostname === "" ||
|
73 | exports.RESERVED_TOP_LEVEL_DOMAINS.includes(labels[labels.length - 1])) {
|
74 | return {
|
75 | type: ParseResultType.Reserved,
|
76 | hostname: domain,
|
77 | labels,
|
78 | };
|
79 | }
|
80 |
|
81 | parsedIcannTrie = parsedIcannTrie !== null && parsedIcannTrie !== void 0 ? parsedIcannTrie : (0, parse_trie_1.parseTrie)(serialized_tries_1.icannTrie);
|
82 | parsedPrivateTrie = parsedPrivateTrie !== null && parsedPrivateTrie !== void 0 ? parsedPrivateTrie : (0, parse_trie_1.parseTrie)(serialized_tries_1.privateTrie);
|
83 | const icannTlds = (0, look_up_1.lookUpTldsInTrie)(labels, parsedIcannTrie);
|
84 | const privateTlds = (0, look_up_1.lookUpTldsInTrie)(labels, parsedPrivateTrie);
|
85 | if (icannTlds.length === 0 && privateTlds.length === 0) {
|
86 | return {
|
87 | type: ParseResultType.NotListed,
|
88 | hostname: domain,
|
89 | labels,
|
90 | };
|
91 | }
|
92 | const indexOfPublicSuffixDomain = labels.length - Math.max(privateTlds.length, icannTlds.length) - 1;
|
93 | const indexOfIcannDomain = labels.length - icannTlds.length - 1;
|
94 | return Object.assign({ type: ParseResultType.Listed, hostname: domain, labels, icann: splitLabelsIntoDomains(labels, indexOfIcannDomain) }, splitLabelsIntoDomains(labels, indexOfPublicSuffixDomain));
|
95 | };
|
96 | exports.parseDomain = parseDomain;
|
97 |
|
\ | No newline at end of file |