-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathipfs-tester.js
68 lines (68 loc) · 1.67 KB
/
ipfs-tester.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
/*
copyright 2018 [email protected]
*/
function removeEtxSlash(str){
//remove extra slashes in the path
try{//chrome
var reg=new RegExp('(?<!\:)\/{2,}','g');
return str.replace(reg,'/');
}catch(e){//for others
console.log('fallback')
var rand;
while(str.indexOf((rand=Math.random().toString()))>=0);
return str.replace(/\:\//g,rand).replace(/\/{2,}/g,'/').replace(new RegExp(rand,'g'),':/');
}
}
function toIPFSPath(addr){
addr=removeEtxSlash(addr.trim());
if(addr.startsWith('/ipns/'))return addr;
var r;
if(r=addr.match(/Qm[A-z\d]{44}.*$/)){
return removeEtxSlash(`/ipfs/${r[0]}`);
}
throw(new Error('Cannot parse IPFS path'));
}
class fetch_controllable{
constructor(url,opt=null){
this.controller = new AbortController();
this.fetch=fetch(url,Object.assign({signal:this.controller.signal},opt));
}
abort(){
this.controller.abort();
}
then(...args){
this.fetch=this.fetch.then(...args);
return this;
}
catch(...args){
this.fetch=this.fetch.catch(...args);
return this;
}
finally(...args){
this.fetch=this.fetch.finally(...args);
return this;
}
}
function fetch_c(url,opt){
return new fetch_controllable(url,opt);
}
class GatewayTester{
constructor(){
this.testingFetch_c=new Set();
}
stop(){
if(this.testingFetch_c.size){
for(let f of this.testingFetch_c){f.abort();}
}
}
test(gateway,path='QmVnS9etu7B3wN9S7DwRCtM37pwx6XJ3b48ag9A8H1akAc'){
let url=removeEtxSlash(`${gateway}${toIPFSPath(path)}`);
let f=fetch_c(url,{
method:'HEAD',
}).then(res=>{
return res;
}).finally(()=>this.testingFetch_c.delete(f));
this.testingFetch_c.add(f);
return f;
}
}