UNPKG

4.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseDomain = exports.ParseResultType = exports.RESERVED_TOP_LEVEL_DOMAINS = void 0;
4const serialized_tries_1 = require("./serialized-tries");
5const look_up_1 = require("./trie/look-up");
6const sanitize_1 = require("./sanitize");
7const parse_trie_1 = require("./trie/parse-trie");
8exports.RESERVED_TOP_LEVEL_DOMAINS = [
9 "localhost",
10 "local",
11 "example",
12 "invalid",
13 "test",
14];
15var ParseResultType;
16(function (ParseResultType) {
17 /**
18 * This parse result is returned in case the given hostname does not adhere to [RFC 1034](https://tools.ietf.org/html/rfc1034).
19 */
20 ParseResultType["Invalid"] = "INVALID";
21 /**
22 * This parse result is returned if the given hostname was an IPv4 or IPv6.
23 */
24 ParseResultType["Ip"] = "IP";
25 /**
26 * This parse result is returned when the given hostname
27 * - is the root domain (the empty string `""`)
28 * - belongs to the top-level domain `localhost`, `local`, `example`, `invalid` or `test`
29 */
30 ParseResultType["Reserved"] = "RESERVED";
31 /**
32 * This parse result is returned when the given hostname is valid and does not belong to a reserved top-level domain, but is not listed in the public suffix list.
33 */
34 ParseResultType["NotListed"] = "NOT_LISTED";
35 /**
36 * This parse result is returned when the given hostname belongs to a top-level domain that is listed in the public suffix list.
37 */
38 ParseResultType["Listed"] = "LISTED";
39})(ParseResultType = exports.ParseResultType || (exports.ParseResultType = {}));
40const getAtIndex = (array, index) => {
41 return index >= 0 && index < array.length ? array[index] : undefined;
42};
43const 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};
50let parsedIcannTrie;
51let parsedPrivateTrie;
52/**
53 * Splits the given hostname in topLevelDomains, a domain and subDomains.
54 */
55const 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 // Parse the serialized trie lazily
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};
96exports.parseDomain = parseDomain;
97//# sourceMappingURL=parse-domain.js.map
\No newline at end of file