1 | "use strict";
|
2 |
|
3 | var icannTrie = require("../lists/icann.complete");
|
4 |
|
5 | var privateTrie = require("../lists/private.complete");
|
6 |
|
7 | var normalize = require("./normalize.js");
|
8 |
|
9 | var lookUp = require("./tries/lookUp");
|
10 |
|
11 |
|
12 | var urlParts = /^(:?\/\/|https?:\/\/)?([^/]*@)?(.+?)(:\d{2,5})?([/?].*)?$/;
|
13 |
|
14 | var dot = /\./g;
|
15 | var emptyArr = [];
|
16 |
|
17 | function matchTld(domain, options) {
|
18 |
|
19 | if (options.customTlds) {
|
20 |
|
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 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function 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);
|
91 |
|
92 | if (urlSplit === null) {
|
93 | return null;
|
94 | }
|
95 |
|
96 | domain = urlSplit[3];
|
97 |
|
98 | tld = matchTld(domain, normalizedOptions);
|
99 |
|
100 | if (tld === null) {
|
101 | return null;
|
102 | }
|
103 |
|
104 |
|
105 | urlSplit = domain.slice(0, -tld.length).split(dot);
|
106 |
|
107 | if (tld.charAt(0) === ".") {
|
108 |
|
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 |
|
121 | module.exports = parseDomain; |
\ | No newline at end of file |