UNPKG

3.17 kBJavaScriptView Raw
1"use strict";
2
3var icannTrie = require("../lists/icann.complete");
4
5var privateTrie = require("../lists/private.complete");
6
7var normalize = require("./normalize.js");
8
9var lookUp = require("./tries/lookUp"); // eslint-disable-next-line
10
11
12var urlParts = /^(:?\/\/|https?:\/\/)?([^/]*@)?(.+?)(:\d{2,5})?([/?].*)?$/; // 1 = protocol, 2 = auth, 3 = domain, 4 = port, 5 = path
13
14var dot = /\./g;
15var emptyArr = [];
16
17function matchTld(domain, options) {
18 // for potentially unrecognized tlds, try matching against custom tlds
19 if (options.customTlds) {
20 // try matching against a built regexp of custom tlds
21 var tld = domain.match(options.customTlds);
22
23 if (tld !== null) {
24 return tld[0];
25 }
26 }
27
28 var tries = (options.privateTlds ? [privateTrie] : emptyArr).concat(icannTrie);
29 var _iteratorNormalCompletion = true;
30 var _didIteratorError = false;
31 var _iteratorError = undefined;
32
33 try {
34 for (var _iterator = tries[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
35 var trie = _step.value;
36
37 var _tld = lookUp(trie, domain);
38
39 if (_tld !== null) {
40 return "." + _tld;
41 }
42 }
43 } catch (err) {
44 _didIteratorError = true;
45 _iteratorError = err;
46 } finally {
47 try {
48 if (!_iteratorNormalCompletion && _iterator["return"] != null) {
49 _iterator["return"]();
50 }
51 } finally {
52 if (_didIteratorError) {
53 throw _iteratorError;
54 }
55 }
56 }
57
58 return null;
59}
60/* eslint-disable jsdoc/no-undefined-types */
61
62/**
63 * Removes all unnecessary parts of the domain (e.g. protocol, auth, port, path, query)
64 * and parses the remaining domain. The returned object contains the properties 'subdomain', 'domain' and 'tld'.
65 *
66 * Since the top-level domain is handled differently by every country, this function only
67 * supports all tlds listed in src/build/tld.txt.
68 *
69 * If the given url is not valid or isn't supported by the tld.txt, this function returns null.
70 *
71 * @param {string} url
72 * @param {Object} [options]
73 * @param {Array<string>|RegExp} [options.customTlds]
74 * @param {boolean} [options.privateTlds]
75 * @returns {Object|null}
76 */
77
78
79function parseDomain(url, options) {
80 var normalizedUrl = normalize.url(url);
81 var tld = null;
82 var urlSplit;
83 var domain;
84
85 if (!normalizedUrl) {
86 return null;
87 }
88
89 var normalizedOptions = normalize.options(options);
90 urlSplit = normalizedUrl.match(urlParts); // urlSplit is null if the url contains certain characters like '\n', '\r'.
91
92 if (urlSplit === null) {
93 return null;
94 }
95
96 domain = urlSplit[3]; // domain will now be something like sub.domain.example.com
97
98 tld = matchTld(domain, normalizedOptions);
99
100 if (tld === null) {
101 return null;
102 } // remove tld and split by dot
103
104
105 urlSplit = domain.slice(0, -tld.length).split(dot);
106
107 if (tld.charAt(0) === ".") {
108 // removes the remaining dot, if present (added to handle localhost)
109 tld = tld.slice(1);
110 }
111
112 domain = urlSplit.pop();
113 var subdomain = urlSplit.join(".");
114 return {
115 tld: tld,
116 domain: domain,
117 subdomain: subdomain
118 };
119}
120
121module.exports = parseDomain;
\No newline at end of file