-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
executable file
·183 lines (133 loc) · 4.11 KB
/
update.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env node
'use strict';
// Source: https://github.com/handshake-org/hs-names/blob/7270561b4b1a5b45e77252e2e4740ffee34ba876/update.js
// TLD Resources:
// https://www.icann.org/resources/pages/tlds-2012-02-25-en
// https://data.iana.org/TLD/tlds-alpha-by-domain.txt
// https://www.internic.net/domain/root.zone
// SLD Resources:
// https://www.google.com/search?q=alexa+top+100000
// https://www.quora.com/What-are-the-top-100-000-most-visited-websites
// https://s3.amazonaws.com/alexa-static/top-1m.csv.zip
const assert = require('assert');
const fs = require('bfile');
const path = require('path');
const {fromZone} = require('bns/lib/wire');
const {countLabels, trimFQDN} = require('bns/lib/util');
const util = require('./lib/util');
const {TLD, BLACKLIST, TRADEMARKS} = require('./lib/common');
const DATA_PATH = util.DATA_PATH;
const TLD_PATH = path.resolve(DATA_PATH, 'tlds-alpha-by-domain.txt');
const ALEXA_PATH = path.resolve(DATA_PATH, 'top-1m.csv');
const ROOT_PATH = path.resolve(DATA_PATH, 'root.zone');
const {CUSTOM} = require(path.resolve(DATA_PATH, 'custom.js'));
// New words come from updated /usr/share/dict/words
const WORDS = require(path.resolve(DATA_PATH, 'words.json'));
const CCTLD = (() => {
const data = fs.readFileSync(TLD_PATH, 'utf8');
const lines = data.split('\n');
const result = [];
for (const line of lines) {
const name = line.trim().toLowerCase();
if (name.length === 0)
continue;
if (name[0] === '#')
continue;
assert(name.length <= 63);
// ccTLDs only!
if (util.isCCTLD(name))
result.push(name);
}
return result;
})();
const GTLD = (() => {
const data = fs.readFileSync(TLD_PATH, 'utf8');
const lines = data.split('\n');
const result = [];
for (const line of lines) {
const name = line.trim().toLowerCase();
if (name.length === 0)
continue;
if (name[0] === '#')
continue;
assert(name.length <= 63);
// gTLDs only!
if (util.isGTLD(name))
result.push(name);
}
return result;
})();
const RTLD = (() => {
const text = fs.readFileSync(ROOT_PATH, 'utf8');
const records = fromZone(text);
const set = new Set();
const result = [];
for (const rr of records) {
if (countLabels(rr.name) !== 1)
continue;
const name = rr.name.toLowerCase();
if (set.has(name))
continue;
set.add(name);
result.push(trimFQDN(name));
}
return result;
})();
const ALEXA = (() => {
const data = fs.readFileSync(ALEXA_PATH, 'utf8');
const lines = data.split('\n');
const result = [];
let cur = 1;
for (const line of lines) {
const ln = line.trim().toLowerCase();
if (ln.length === 0)
continue;
const items = ln.split(/\s*,\s*/);
assert(items.length === 2);
const [num, domain] = items;
const rank = parseInt(num, 10);
assert((rank >>> 0) === rank);
// No idea why alexa does this.
if (rank !== cur) {
assert(rank > cur);
console.error('Warning: rank inconsistency!');
console.error('Rank %d is missing.', cur);
cur = rank;
}
result.push(domain);
cur += 1;
}
return result;
})();
const dir = util.NAMES_PATH;
if (!fs.existsSync(path.dirname(dir)))
fs.mkdirSync(path.dirname(dir));
if (!fs.existsSync(dir))
fs.mkdirSync(dir);
fs.writeFileSync(
path.resolve(dir, 'blacklist.json'),
JSON.stringify(BLACKLIST, null, 2) + '\n');
fs.writeFileSync(
path.resolve(dir, 'custom.json'),
JSON.stringify(CUSTOM, null, 2) + '\n');
fs.writeFileSync(
path.resolve(dir, 'tld.json'),
JSON.stringify(TLD, null, 2) + '\n');
fs.writeFileSync(
path.resolve(dir, 'cctld.json'),
JSON.stringify(CCTLD, null, 2) + '\n');
fs.writeFileSync(
path.resolve(dir, 'gtld.json'),
JSON.stringify(GTLD, null, 2) + '\n');
fs.writeFileSync(
path.resolve(dir, 'rtld.json'),
JSON.stringify(RTLD, null, 2) + '\n');
fs.writeFileSync(
path.resolve(dir, 'alexa.json'),
JSON.stringify(ALEXA, null, 2) + '\n');
fs.writeFileSync(
path.resolve(dir, 'words.json'),
JSON.stringify(WORDS, null, 2) + '\n');
fs.writeFileSync(
path.resolve(dir, 'trademarks.json'),
JSON.stringify(TRADEMARKS, null, 2) + '\n');