forked from ipfs/public-gateway-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrigin.ts
44 lines (37 loc) · 1.61 KB
/
Origin.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
import { URL } from 'url-ponyfill'
import type { GatewayNode } from './GatewayNode'
import { Tag } from './Tag'
import { Log } from './Log'
import { expectSubdomainRedirect } from './expectSubdomainRedirect'
import { checkViaImgSrc } from './checkViaImgSrc'
import { IMG_HASH } from './constants'
const log = new Log('Origin')
class Origin {
tag: Tag
constructor (public parent: GatewayNode) {
this.tag = new Tag('div', 'Origin')
}
async check (): Promise<void> {
// we are unable to check url after subdomain redirect because some gateways
// may not have proper CORS in place. instead, we manually construct subdomain
// URL and check if it loading known image works
const gwUrl = new URL(this.parent.gateway)
// const imgPathUrl = new URL(`${gwUrl.protocol}//${gwUrl.hostname}/ipfs/${IMG_HASH}?now=${now}&filename=1x1.png#x-ipfs-companion-no-redirect`)
const imgSubdomainUrl = new URL(`${gwUrl.protocol}//${IMG_HASH}.ipfs.${gwUrl.hostname}/?now=${Date.now()}&filename=1x1.png#x-ipfs-companion-no-redirect`)
const imgRedirectedPathUrl = new URL(`${gwUrl.protocol}//${gwUrl.hostname}/ipfs/${IMG_HASH}?now=${Date.now()}&filename=1x1.png#x-ipfs-companion-no-redirect`)
await checkViaImgSrc(imgSubdomainUrl)
.then(async () => await expectSubdomainRedirect(imgRedirectedPathUrl))
.then(() => {
this.tag.win(imgSubdomainUrl.toString())
this.parent.tag.classList.add('origin')
// this.parent.checked()
})
.catch((err) => this.onerror(err))
}
onerror (err: Error): void {
log.error(err)
this.tag.err()
throw err
}
}
export { Origin }