-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
93 lines (88 loc) · 2.04 KB
/
main.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
const IP_ADDRESS_REGEX = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/
const ONE_MINUTE = 60_000
const MEASUREMENT_DELAY = ONE_MINUTE
const UPDATE_NODES_DELAY = 10 * ONE_MINUTE
const getNodes = async () => {
// TODO: Find a publicly documented API
const res = await fetch(
'https://api.viewblock.io/arweave/nodes?page=1&network=mainnet',
{
headers: {
Origin: 'https://viewblock.io'
}
}
)
const body = await res.json()
const nodes = [
{
host: 'arweave.net',
port: 443,
protocol: 'https'
},
...body.docs
.map(d => d.id)
.sort()
.map(addr => {
const [host, port] = addr.split(':')
const protocol = IP_ADDRESS_REGEX.test(host) ? 'http' : 'https'
return {
host,
port: port
? Number(port)
: protocol === 'http' ? 80 : 443,
protocol
}
})
]
console.log(`Found ${nodes.length} nodes`)
return nodes
}
const measure = async node => {
const measurement = {
node,
alive: false,
timeout: false,
ttfbMs: null
}
const start = new Date()
try {
const res = await fetch(
`${node.protocol}://${node.host}:${node.port}/`,
{
signal: AbortSignal.timeout(10_000)
}
)
if (!res.ok) {
throw new Error()
}
} catch (err) {
if (err.name === 'TimeoutError') {
measurement.timeout = true
}
return measurement
}
measurement.alive = true
measurement.ttfbMs = new Date().getTime() - start.getTime()
return measurement
}
let nodes = await getNodes()
;(async () => {
while (true) {
await new Promise(resolve => setTimeout(resolve, UPDATE_NODES_DELAY))
try {
nodes = await getNodes()
} catch (err) {
console.error('Error updating nodes')
console.error(err)
}
}
})()
while (true) {
console.log(
await measure(
nodes[Math.floor(Math.random() * nodes.length)]
)
)
console.log('Waiting 60 seconds...')
await new Promise(resolve => setTimeout(resolve, MEASUREMENT_DELAY))
}