-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-ens.js
85 lines (73 loc) · 2.91 KB
/
update-ens.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
import { ens_normalize, ens_tokenize, ens_beautify, WHOLE_MAP } from '@adraffy/ens-normalize';
import { readFile, writeFile } from 'fs/promises';
const package_json = JSON.parse(await readFile("package.json"));
const COMMIT_HASH = package_json["dependencies"]["@adraffy/ens-normalize"].replace("github:adraffy/ens-normalize.js#", "");
const SPEC_URL = "https://raw.githubusercontent.com/adraffy/ens-normalize.js/" + COMMIT_HASH + "/derive/output/spec.json"
const SPEC_PATH = "spec.json";
const TESTS_URL = "https://raw.githubusercontent.com/adraffy/ens-normalize.js/" + COMMIT_HASH + "/validate/tests.json"
const TESTS_PATH = "../../tests/ens-normalize-tests.json";
function parse_whole_map(whole_map) {
const new_whole_map = {};
for (const [cp, value] of whole_map) {
const key1 = cp.toString();
if (typeof value === "number") {
new_whole_map[key1] = value;
continue;
}
const { V, M } = value;
new_whole_map[key1] = {};
new_whole_map[key1]["V"] = V;
new_whole_map[key1]["M"] = {};
for (const [cp, set] of M) {
const key2 = cp.toString();
new_whole_map[key1]["M"][key2] = [];
for (let {N} of set) {
if (N.startsWith("Restricted")) {
N = N.slice(11, -1);
}
new_whole_map[key1]["M"][key2].push(N);
}
}
}
return new_whole_map;
}
async function download_files() {
console.log("Downloading spec...");
const spec = await (await fetch(SPEC_URL)).json();
spec["whole_map"] = parse_whole_map(WHOLE_MAP)
await writeFile(SPEC_PATH, JSON.stringify(spec, null, 2));
console.log("Downloading tests...");
const tests = await (await fetch(TESTS_URL)).json();
await writeFile(TESTS_PATH, JSON.stringify(tests, null, 2));
}
async function generate_tests() {
console.log("Generating tests...");
const raw_tests = JSON.parse(await readFile(TESTS_PATH));
const new_tests = [];
for (const test of raw_tests) {
if (test.error === undefined) {
let expected_norm = test.norm === undefined ? test.name : test.norm
if (ens_normalize(test.name) !== expected_norm) {
throw new Error(`Test ${test.name} should be normalized to ${test.norm}`);
}
new_tests.push({
name: test.name,
norm: ens_normalize(test.name),
beautified: ens_beautify(test.name),
tokenized: ens_tokenize(test.name),
});
}
else {
new_tests.push({
name: test.name,
tokenized: ens_tokenize(test.name),
error: test.error,
comment: test.comment,
});
}
}
await writeFile(TESTS_PATH, JSON.stringify(new_tests, null, 2));
}
await download_files();
await generate_tests();
console.log("Done!");