-
Notifications
You must be signed in to change notification settings - Fork 2
/
impls.js
129 lines (108 loc) · 4.52 KB
/
impls.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
import {readFileSync} from 'node:fs';
import {print_table} from './ens-normalize.js/derive/utils.js';
export const ENS_NORMALIZE_DEV = new URL('../ens-normalize.js/', import.meta.url); // local install, sibling of root
export const ENS_NORMALIZE_GIT = new URL('./ens-normalize.js/', import.meta.url); // installed via submodule
function read_package_version(path) {
if (typeof path === 'string') {
path = new URL(`./node_modules/${path}/package.json`, import.meta.url);
}
return JSON.parse(readFileSync(path)).version;
}
export async function import_ens_normalize(version) {
if (!version || version === 'latest' || version === 'git') {
return import(new URL('./src/lib.js', ENS_NORMALIZE_GIT));
} else if (version === 'dev') { // NOTE: this only works for raffy
return import(new URL('./src/lib.js', ENS_NORMALIZE_DEV));
} else {
return import(`./old-versions/${version}.js`);
}
}
export async function impl_for_version(version) {
let lib = await import_ens_normalize(version);
return new Impl('ens_normalize', lib.ens_normalize, version);
}
class Impl {
constructor(name, fn, version) {
this.name = name;
this.fn = fn;
this.version = version;
}
get slug() {
return this.name.replace(/\s+/, '').toLowerCase() + '_' + this.version;
}
toString() {
return `${this.name} (${this.version})`;
}
}
export const IMPLS = [];
export function require_impl(name) {
let impl = IMPLS.find(x => x.name === name);
if (!impl) throw new Error(`expected implementation: ${name}`);
return impl;
}
// ********************************************************************************
import {ens_normalize as ethers5} from '@ethersproject/hash/lib/ens-normalize/lib.js';
IMPLS.push(new Impl('ethers5', ethers5, read_package_version('ethers5')));
import {ensNormalize as ethers6} from 'ethers';
IMPLS.push(new Impl('ethers', ethers6, read_package_version('ethers')));
import A from '@ensdomains/eth-ens-namehash';
IMPLS.push(new Impl('eth-ens-namehash', A.normalize.bind(A), read_package_version('@ensdomains/eth-ens-namehash')));
import B from '@ensdomains/ens-validation';
IMPLS.push(new Impl('ens-validation', name => {
let norm = name.split('.').map(B.toUnicode).join('.');
if (!B.validate(norm)) throw new Error('invalid');
return norm;
}, read_package_version('@ensdomains/ens-validation')));
/*
// frozen ENSIP-1 ESM build
const {normalize: normalize_2_0_15} = await import(new URL('./test/[email protected]', ENS_NORMALIZE_GIT));
IMPLS.push(new Impl('eth-ens-namehash', normalize_2_0_15, '2.0.15'));
*/
// ********************************************************************************
// git branch instead
const {ens_normalize: ens_normalize_git} = await import_ens_normalize('git');
IMPLS.push(new Impl('ens_normalize.git', ens_normalize_git, read_package_version(new URL('./package.json', ENS_NORMALIZE_GIT))));
// raffy's local branch
const {ens_normalize: ens_normalize_dev} = await import_ens_normalize('dev');
IMPLS.push(new Impl('ens_normalize.dev', ens_normalize_dev, read_package_version(new URL('./package.json', ENS_NORMALIZE_DEV))));
// ********************************************************************************
import {create_uts46} from '@adraffy/ens-norm-uts46';
const UTS46_VERSION = read_package_version(`@adraffy/ens-norm-uts46`);
export const uts46 = create_uts46({
version: 2003,
use_STD3: true,
valid_deviations: true, // 20231228: always true with 15.1
check_hyphens: true,
check_bidi: true,
contextJ: true,
check_leading_cm: true,
punycode: true,
});
IMPLS.push(new Impl('UTS46', uts46, UTS46_VERSION));
export const ens0 = create_uts46({
version: 2003,
use_STD3: true, // 20231228: why was this missing?
valid_deviations: true,
check_hyphens: false, // 20220918: i had these as true
punycode: false, // probably should be false
});
IMPLS.push(new Impl('ENS0', ens0, UTS46_VERSION));
export const strict2008 = create_uts46({
version: 2008,
use_STD3: true, // 20231228: why was this missing?
check_hyphens: true,
check_bidi: true,
contextJ: true,
contextO: true,
check_leading_cm: true,
punycode: true,
});
IMPLS.push(new Impl('Strict 2008', strict2008, UTS46_VERSION));
// ********************************************************************************
// dump out if run directly
if (process.argv[1] === new URL(import.meta.url).pathname) {
print_table(
['#', 'Name', 'Slug', 'Version'],
IMPLS.map((impl, i) => [i, impl.name, impl.slug, impl.version])
);
}