forked from 131/node-tld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (29 loc) · 927 Bytes
/
index.js
File metadata and controls
42 lines (29 loc) · 927 Bytes
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
"use strict";
var url = require('url');
var parse_url = function(remote_url) {
if(typeof remote_url == "string")
remote_url = url.parse(remote_url);
return parse_host(remote_url.hostname);
};
var tlds = null;
var parse_host = function(host) {
if(!tlds)
tlds = require('./effective_tld_names.json');
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;