-
Notifications
You must be signed in to change notification settings - Fork 348
/
Ipns.ts
49 lines (42 loc) · 1.3 KB
/
Ipns.ts
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
import fetchPonyfill from 'fetch-ponyfill'
import { CheckBase } from './CheckBase'
import { Log } from './Log'
import { IPNS_PATH_TO_TEST } from './constants'
import type { GatewayNode } from './GatewayNode'
import type { Checkable } from './types'
const { fetch } = fetchPonyfill()
const log = new Log('Ipns')
class IPNSCheck extends CheckBase implements Checkable {
_className = 'Ipns'
_tagName = 'div'
constructor (protected parent: GatewayNode) {
super(parent, 'div', 'Ipns')
}
async check (): Promise<void> {
const now = Date.now()
// Since gateway URLs are hard coded with /ipfs/, we need to parse URLs and override the path to /ipns/.
const gatewayUrl = new URL(this.parent.gateway)
gatewayUrl.pathname = IPNS_PATH_TO_TEST
const testUrl = `${gatewayUrl.href}?now=${now}#x-ipfs-companion-no-redirect`
try {
const response = await fetch(testUrl)
if (response.status === 200) {
this.tag.win()
} else {
log.debug(`${this.parent.gateway} does not support IPNS`)
throw new Error(`URL '${testUrl} is not reachable`)
}
} catch (err) {
log.error(err)
this.onerror()
throw err
}
}
checked (): void {
log.warn('Not implemented yet')
}
onerror (): void {
this.tag.err()
}
}
export { IPNSCheck }