/
build-tries.js
97 lines (84 loc) · 3.39 KB
/
build-tries.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
"use strict";
// Disabling promise ESLint rules because we can't use async/await here
// before all supported Node versions support it.
/* eslint-disable promise/always-return, promise/prefer-await-to-callbacks */
const os = require("os");
const fs = require("fs");
const path = require("path");
const childProcess = require("child_process");
const got = require("got");
const mkdirp = require("mkdirp");
try {
require.resolve("../lib/tries/parsePubSuffixList");
} catch (error) {
// This conditions occurs when the CI system or a developer checks out the repo for the first time.
// It happens because the postinstall hook kicks in *before* the lib is built.
// In that case, it's safe to just skip the step.
console.error("Lib does not exist yet, skipping build-tries step.");
process.exit(0); // eslint-disable-line no-process-exit
}
const parsePubSuffixList = require("../lib/tries/parsePubSuffixList");
const serializeTrie = require("../lib/tries/serializeTrie");
const PUBLIC_SUFFIX_URL = "https://publicsuffix.org/list/public_suffix_list.dat";
const rootPath = path.resolve(__dirname, "..");
const triesPath = path.resolve(rootPath, "build", "tries");
const tries = [
{
listName: "icann",
type: "complete",
filename: "icann.complete.json",
}, {
listName: "icann",
type: "light",
filename: "icann.light.json",
}, {
listName: "private",
type: "complete",
filename: "private.complete.json",
},
];
process.stderr.write(`Downloading public suffix list from ${PUBLIC_SUFFIX_URL}... `);
got(PUBLIC_SUFFIX_URL, { timeout: 60*1000 })
.then(res => {
process.stderr.write("ok" + os.EOL);
return res.body;
})
.then(parsePubSuffixList)
.then(parsed =>
tries
.map(trie => {
const parsedList = parsed[trie.listName];
return {
path: path.resolve(triesPath, trie.filename),
content: JSON.stringify({
updatedAt: new Date().toISOString(),
trie: serializeTrie(parsedList, trie.type),
}),
};
})
.forEach(file => {
process.stderr.write(`Writing ${file.path}... `);
mkdirp.sync(path.dirname(file.path));
fs.writeFileSync(file.path, file.content, "utf8");
process.stderr.write("ok" + os.EOL);
})
)
.then(() => {
process.stderr.write("Running sanity check... ");
childProcess.execFileSync(process.execPath, [require.resolve("mocha/bin/_mocha"), "-R", "dot"], {
cwd: rootPath,
encoding: "utf8",
});
process.stderr.write("ok" + os.EOL);
})
.catch(err => {
console.error("");
console.error(`Could not update list of known top-level domains for parse-domain because of "${err.message}"`);
const prebuiltList = JSON.parse(fs.readFileSync(path.resolve(triesPath, tries[0].filename), "utf8"));
console.error("Using possibly outdated prebuilt list from " + new Date(prebuiltList.updatedAt).toDateString());
// We can recover using the (possibly outdated) prebuilt list, hence exit code 0
process.exit(0); // eslint-disable-line no-process-exit
})
.catch(() => {
process.exit(1); // eslint-disable-line no-process-exit
});