-
Notifications
You must be signed in to change notification settings - Fork 56
/
index.js
125 lines (106 loc) · 3.71 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
'use strict';
// Load rules
var Trie = require('./lib/suffix-trie.js');
var allRules = Trie.fromJson(require('./rules.json'));
// Internals
var extractHostname = require('./lib/clean-host.js');
var getDomain = require('./lib/domain.js');
var getPublicSuffix = require('./lib/public-suffix.js');
var getSubdomain = require('./lib/subdomain.js');
var isValidHostname = require('./lib/is-valid.js');
var isIp = require('./lib/is-ip.js');
var tldExists = require('./lib/tld-exists.js');
// Flags representing steps in the `parse` function. They are used to implement
// a early stop mechanism (simulating some form of laziness) to avoid doing more
// work than necessary to perform a given action (e.g.: we don't need to extract
// the domain and subdomain if we are only interested in public suffix).
var TLD_EXISTS = 1;
var PUBLIC_SUFFIX = 2;
var DOMAIN = 3;
var SUB_DOMAIN = 4;
var ALL = 5;
/**
* Creates a new instance of tldjs
* @param {Object.<rules,validHosts>} options [description]
* @return {tldjs|Object} [description]
*/
function factory(options) {
var rules = options.rules || allRules || {};
var validHosts = options.validHosts || [];
var _extractHostname = options.extractHostname || extractHostname;
/**
* Process a given url and extract all information. This is a higher level API
* around private functions of `tld.js`. It allows to remove duplication (only
* extract hostname from url once for all operations) and implement some early
* termination mechanism to not pay the price of what we don't need (this
* simulates laziness at a lower cost).
*
* @param {string} url
* @param {number|undefined} _step - where should we stop processing
* @return {object}
*/
function parse(url, _step) {
var step = _step || ALL;
var result = {
hostname: _extractHostname(url),
isValid: null,
isIp: null,
tldExists: false,
publicSuffix: null,
domain: null,
subdomain: null,
};
if (result.hostname === null) {
result.isIp = false;
result.isValid = false;
return result;
}
// Check if `hostname` is a valid ip address
result.isIp = isIp(result.hostname);
if (result.isIp) {
result.isValid = true;
return result;
}
// Check if `hostname` is valid
result.isValid = isValidHostname(result.hostname);
if (result.isValid === false) { return result; }
// Check if tld exists
if (step === ALL || step === TLD_EXISTS) {
result.tldExists = tldExists(rules, result.hostname);
}
if (step === TLD_EXISTS) { return result; }
// Extract public suffix
result.publicSuffix = getPublicSuffix(rules, result.hostname);
if (step === PUBLIC_SUFFIX) { return result; }
// Extract domain
result.domain = getDomain(validHosts, result.publicSuffix, result.hostname);
if (step === DOMAIN) { return result; }
// Extract subdomain
result.subdomain = getSubdomain(result.hostname, result.domain);
return result;
}
return {
extractHostname: _extractHostname,
isValidHostname: isValidHostname,
isValid: function (hostname) {
// eslint-disable-next-line
console.error('DeprecationWarning: "isValid" is deprecated, please use "isValidHostname" instead.');
return isValidHostname(hostname);
},
parse: parse,
tldExists: function (url) {
return parse(url, TLD_EXISTS).tldExists;
},
getPublicSuffix: function (url) {
return parse(url, PUBLIC_SUFFIX).publicSuffix;
},
getDomain: function (url) {
return parse(url, DOMAIN).domain;
},
getSubdomain: function (url) {
return parse(url, SUB_DOMAIN).subdomain;
},
fromUserSettings: factory
};
}
module.exports = factory({});