forked from ipfs/public-gateway-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
48 lines (43 loc) · 1.41 KB
/
app.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
const hashToTest = 'Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a'
const hashString = 'Hello from IPFS Gateway Checker'
const $results = document.querySelector('#results')
function addNode (gateway, online, title) {
const para = document.createElement('div')
let node
if (online) {
node = document.createElement('strong')
node.innerText = '✅ - Online - ' + gateway
} else {
node = document.createElement('div')
node.innerText = '❌ - Offline - ' + gateway
}
node.setAttribute('title', title)
para.appendChild(node)
$results.appendChild(para)
}
function updateStats (total, checked) {
document.getElementById('stats').innerText = checked + '/' + total + ' gateways checked'
}
function checkGateways (gateways) {
const total = gateways.length
let checked = 0
gateways.forEach((gateway) => {
const gatewayAndHash = gateway.replace(':hash', hashToTest)
fetch(gatewayAndHash)
.then(res => res.text())
.then((text) => {
const matched = text.trim() === hashString.trim()
addNode(gatewayAndHash, matched, matched ? 'All good' : 'Output did not match expected output')
checked++
updateStats(total, checked)
}).catch((err) => {
window.err = err
addNode(gatewayAndHash, false, err)
checked++
updateStats(total, checked)
})
})
}
fetch('./gateways.json')
.then(res => res.json())
.then(gateways => checkGateways(gateways))