-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
173 lines (143 loc) · 4.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
module.exports = PublicSuffixList;
var __slice = [].slice;
var is = require('./lib/is');
var bind = require('./lib/bind');
var waterfall = require('./lib/waterfall');
var RuleLoader = require('./lib/rule-loader');
var TYPE_EXCEPTION = 1;
var TYPE_WILDCARD = 2;
var TYPE_RULE = 3;
var TYPES = [TYPE_RULE, TYPE_WILDCARD, TYPE_EXCEPTION];
var ruleLoader = null;
function PublicSuffixList (options) {
options = is.Object(options) ? options : {};
// load Mozilla's effective_tld_names.dat by default
if (!(is.String(options.filename) || is.Buffer(options.buffer) || is.Array(options.lines))) {
options.filename = __dirname + '/effective_tld_names.dat';
}
ruleLoader = new RuleLoader(options);
var psl = {
options : options,
initialize : bind(_invoke, null, 'initialize'),
initializeSync : bind(_invoke, null, 'initializeSync'),
lookup : _lookup,
domain : _domain,
validate : _validate,
validateTLD : _validateTLD,
ruleLoader : ruleLoader,
tld : _tld
};
return psl;
}
/**
* @function
* @param {string} fnId function to call
* @returns {*}
*/
function _invoke (fnId) {
var args = __slice.apply(arguments).slice(1);
return ruleLoader[fnId].apply(ruleLoader, args);
}
/**
* @function
* @param {string} domainString
* @returns {string} fqdn
*/
function _domain (domainString) {
var result = this.lookup(domainString);
if (!result) return null;
if (result.exception) return null;
var fqdn = result.domain + '.' + result.tld;
return fqdn;
}
/**
* @function
* @param {string} tldString
* @returns {string} tld
*/
function _tld (tldString) {
var result = this.lookup(tldString, true);
if (!result) return null;
if (result.type === TYPE_EXCEPTION) return null;
var rgx = null;
if (result.type === TYPE_WILDCARD) rgx = new RegExp('(.*)(\.)?('+result.rule+')','i');
else if (result.type === TYPE_RULE) rgx = new RegExp('^(\.)?('+result.rule+')','i');
return rgx.test(tldString) ? result.rule : null;
}
/**
* @function
* @param {string} domainString
* @returns {boolean}
*/
function _validate (domainString) {
var result = this.lookup(domainString);
return !!result && result.exception !== true;
}
/**
* @function
* @param {string} domainString
* @returns {boolean}
*/
function _validateTLD (tldString) {
var result = this.lookup(tldString, true, true);
if (!result || result.type === TYPE_EXCEPTION) return false;
var rgx = null;
if (result.type === TYPE_WILDCARD) rgx = new RegExp('(.*)(\.)?('+result.rule+')','i');
else if (result.type === TYPE_RULE) rgx = new RegExp('^(\.)?('+result.rule+')','i');
return rgx.test(tldString);
}
/**
* @function
* @param {string} domainString
* @param {boolean} ruleOnly
* @param {boolean} ignoreLeadingDot
* @returns {object} result
*/
function _lookup (domainString, ruleOnly, ignoreLeadingDot) {
if (!is.String(domainString)) return null;
if (!ignoreLeadingDot && domainString.charAt(0) === '.') return null;
domainString = domainString.toLowerCase();
var matchingRules = _invoke('findRules', domainString);
var results = [];
TYPES.forEach(function (type) {
var rules = matchingRules[type] || [];
rules = rules
.map(function (rule) {
return {
type : type,
rule : rule,
length : rule.length
};
});
results = results.concat(rules);
});
if (!results.length) return null;
results.sort(function (a, b) { return b.length - a.length; });
var matchingRule = results[0];
if (ruleOnly) return matchingRule;
var tldIndex = -1;
if (matchingRule.type === TYPE_RULE || matchingRule.type === TYPE_WILDCARD) {
tldIndex = domainString.lastIndexOf('.' + matchingRule.rule);
} else if (matchingRule.type === TYPE_EXCEPTION) {
tldIndex = domainString.lastIndexOf('.');
}
var domainPart = domainString.substring(0, tldIndex);
if (typeof domainPart !== 'string') return null;
if (matchingRule.type === TYPE_WILDCARD) {
tldIndex = domainPart.lastIndexOf('.');
domainPart = domainString.substring(0, tldIndex);
}
var remainingParts = domainPart.split(/\./).filter(function(p) { return !!p.length; });
if (!remainingParts.length) return null;
var result = {};
if (matchingRule.type === TYPE_EXCEPTION) {
result.rule = matchingRule.rule;
result.exception = true;
} else if (matchingRule.type === TYPE_WILDCARD) {
result.rule = matchingRule.rule;
}
result.domain = remainingParts[remainingParts.length-1];
result.tld = domainString.substring(tldIndex + 1);
if (remainingParts.length > 1) result.subdomain = domainPart.substring(0, domainPart.length - result.domain.length - 1);
return result;
}