-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdomains.js
57 lines (49 loc) · 1.28 KB
/
domains.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
import * as https from 'https';
import * as fs from 'fs';
import * as readline from 'readline';
// const options = {
// host: 'raw.githubusercontent.com',
// port: 443,
// path: 'publicsuffix/list/master/public_suffix_list.dat',
// method: 'GET'
// };
const options = {
host: 'publicsuffix.org',
path: '/list/public_suffix_list.dat',
method: 'GET'
};
const base = (domain) => {
let arr = domain.split('.')
if (arr.length === 1) {
return arr[0]
} else {
arr.shift()
return arr.join('.')
}
}
let domains = []
let req = https.request(options, (res) => {
res.setEncoding('utf8');
let lineReader = readline.createInterface({input: res});
lineReader.on('line', (line) => {
if (!line.startsWith("//") && (line.trim().length != 0)) {
domains.push(line)
}
});
lineReader.on('close', () => {
const compare = (a, b) => {
let comp = base(a).localeCompare(base(b))
if (comp === 0) {
return b.length - a.length
} else {
return comp || a.localeCompare(b)
}
}
let sorted = domains.sort(compare)
fs.writeFileSync('./docs/javascripts/domains.js', "export const domains = " + JSON.stringify(sorted, null, 2));
})
});
req.on('error', (e) => {
console.log('problem with request: ' + e.message);
});
req.end();