forked from 131/node-tld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (48 loc) · 1.2 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
"use strict";
var url = require('url');
var parse_url = function (remote_url) {
if (typeof remote_url == "string")
remote_url = url.parse(remote_url);
// support ips and localhost
if (remote_url.hostname === 'localhost' || /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/.test(remote_url.hostname)) {
return {
tld: '',
domain: remote_url.hostname,
sub: '',
};
} else {
return parse_host(remote_url.hostname);
}
};
var tlds = {
"com": 1,
"au": 1,
"com.au": 2,
"me": 1,
"br": 1,
"com.br": 2,
"il": 1,
"co.il": 2,
"myshopify.com": 2,
"uk": 1,
"co.uk": 2,
}
var parse_host = function (host) {
var parts = host.split(".");
var stack = "", tld_level = 1; //unknown tld are 1st level
for (var i = parts.length - 1, part; i >= 0; i--) {
part = parts[i];
stack = stack ? part + "." + stack : part;
if (!tlds[stack]) break;
tld_level = tlds[stack];
}
if (parts.length <= tld_level)
throw new Error("Invalid TLD");
return {
tld: parts.slice(-tld_level).join('.'),
domain: parts.slice(-tld_level - 1).join('.'),
sub: parts.slice(0, (-tld_level - 1)).join('.'),
};
};
module.exports = parse_url;
module.exports.parse_host = parse_host;