forked from 131/node-tld
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.js
More file actions
50 lines (34 loc) · 1.12 KB
/
update.js
File metadata and controls
50 lines (34 loc) · 1.12 KB
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
"use strict";
var fs = require('fs');
var https = require('https');
var REMOTE_TLD_URL = "https://publicsuffix.org/list/effective_tld_names.dat";
var TLD_CACHE = 'effective_tld_names.dat';
var TLD_CACHE_JSON = 'effective_tld_names.json';
var update = function(chain) {
var dest = fs.createWriteStream(TLD_CACHE);
https.get(REMOTE_TLD_URL, function(res) {
res.pipe(dest);
res.on("end", function() {
parse(chain);
});
});
};
var parse = function(chain) {
var contents = fs.readFileSync(TLD_CACHE, 'utf-8');
contents = contents.replace(new RegExp('//.*?$', 'mg'), "").trim();
contents = contents.split(new RegExp('[ \t\r\n]+'));
var tlds = {}, splitter = new RegExp("(\\!|\\*\\.)?(.*)");
contents.forEach(function(line) {
if(!splitter.test(line)) return;
line = splitter.exec(line);
var tld = line[2],
level = tld.split(".").length,
modifier = line[1];
if(modifier == "*.") level++;
if(modifier == "!") level--;
tlds[tld] = level;
});
fs.writeFileSync(TLD_CACHE_JSON, JSON.stringify(tlds, null, 2));
chain();
};
update(console.log.bind(null, "All done"));