forked from contributionls/public-gateway-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (66 loc) · 2.32 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const searchParams = new URLSearchParams(window.location.search);
const cid = searchParams.get('cid') || 'Qmaisz6NMhDB51cCvNWa1GMS7LU1pAxdF4Ld6Ft9kZEP2a'
const corsApi = searchParams.get('cors') || ''
const hashToTest = cid
const hashString = 'Hello from IPFS Gateway Checker'
const $results = document.querySelector('#results')
function returnHtmlLink (gateway) {
let gatewayTitle = gateway.split(hashToTest)[0]
return '<a title="' + gatewayTitle + '" href="' + gateway + '">' + gateway + '</a>'
}
function addNode (gateway, online, status, roundtripInMs) {
const para = document.createElement('div')
let node
if (online) {
node = document.createElement('strong')
node.innerHTML = '✅ - Online - ' + returnHtmlLink(gateway)
node.innerHTML += ' (' + roundtripInMs + 'ms)'
} else {
node = document.createElement('div')
node.innerText = '❌ - Offline - ' + gateway
}
node.setAttribute('title', status)
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)
// opt-out from gateway redirects done by browser extension
const corsHost = corsApi
const testUrl = gatewayAndHash + '#x-ipfs-companion-no-redirect'
const start = performance.now()
fetch(corsHost+testUrl)
.then(res => {
if(res.status === 200){
return true;
}else{
return Promise.reject(false);
}
})
.then(() => {
// console.log('text',text);
// const matched = text.trim() === hashString.trim()
// const status = matched ? 'All good' : 'Output did not match expected output'
const matched = true;
const status = 'All good'
const ms = performance.now() - start
addNode(gatewayAndHash, matched, status, ms);
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))