Skip to content

Commit

Permalink
feat: /ipns/ check (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
whizzzkid authored Oct 21, 2022
1 parent f5fc723 commit 10a5c13
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/GatewayNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { UiComponent } from './UiComponent'
import { Log } from './Log'
import { gatewayHostname } from './gatewayHostname'
import { HASH_TO_TEST } from './constants'
import { IPNSCheck } from './Ipns'

const log = new Log('GatewayNode')

class GatewayNode extends UiComponent /* implements Checkable */ {
// tag: Tag
status: Status
cors: Cors
ipns: IPNSCheck
origin: Origin
trustless: Trustless
link: HTMLDivElement & { url?: URL }
Expand All @@ -42,6 +44,9 @@ class GatewayNode extends UiComponent /* implements Checkable */ {
this.cors = new Cors(this)
this.tag.append(this.cors.tag)

this.ipns = new IPNSCheck(this)
this.tag.append(this.ipns.tag)

this.origin = new Origin(this)
this.tag.append(this.origin.tag)

Expand Down Expand Up @@ -77,6 +82,7 @@ class GatewayNode extends UiComponent /* implements Checkable */ {
// this.flag.check().then(() => log.debug(this.gateway, 'Flag success')),
this.status.check().then(() => log.debug(this.gateway, 'Status success')).then(this.onSuccessfulCheck.bind(this)),
this.cors.check().then(() => log.debug(this.gateway, 'CORS success')).then(this.onSuccessfulCheck.bind(this)),
this.ipns.check().then(() => log.debug(this.gateway, 'IPNS success')).then(this.onSuccessfulCheck.bind(this)),
this.origin.check().then(() => log.debug(this.gateway, 'Origin success')).then(this.onSuccessfulCheck.bind(this)),
this.trustless.check().then(
() => log.debug(this.gateway, 'Trustless success')).then(this.onSuccessfulCheck.bind(this))
Expand Down
50 changes: 50 additions & 0 deletions src/Ipns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import fetchPonyfill from 'fetch-ponyfill'

import { CheckBase } from './CheckBase'
import { IPNS_PATH_TO_TEST } from './constants'
import type { GatewayNode } from './GatewayNode'

import { Log } from './Log'

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}`
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 }
2 changes: 1 addition & 1 deletion src/Tag.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TagStatus } from './TagStatus'

type TagClasses = 'Status' | 'Node' | 'Cors' | 'Origin' | 'Flag' | 'Trustless'
type TagClasses = 'Cors' | 'Flag' | 'Ipns' | 'Node' | 'Origin' | 'Status' | 'Trustless'

type TagContent = TagStatus

Expand Down
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

const HASH_STRING = 'Hello from IPFS Gateway Checker'
const HASH_TO_TEST = 'bafybeifx7yeb55armcsxwwitkymga5xf53dxiarykms3ygqic223w5sk3m'
const IMG_HASH = 'bafybeibwzifw52ttrkqlikfzext5akxu7lz4xiwjgwzmqcpdzmp3n5vnbe' // 1x1.png
// const IFRAME_HASH = 'bafkreifx3g6bkkwl7b4v43lvcqfo5vshbiehuvmpky2zayhfpg5qj7y3ca'
const HASH_STRING = 'Hello from IPFS Gateway Checker'
const IPNS_PATH_TO_TEST = '/ipns/en.wikipedia-on-ipfs.org/favicon.ico'
const TRUSTLESS_RESPONSE_TYPES = ['raw', 'car']
const DEFAULT_IPFS_GATEWAY = 'https://ipfs.io'

Expand All @@ -12,5 +13,6 @@ export {
HASH_TO_TEST,
// IFRAME_HASH,
IMG_HASH,
IPNS_PATH_TO_TEST,
TRUSTLESS_RESPONSE_TYPES
}
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ <h1 class='f3 fw2 montserrat aqua ttu ma0'>Public Gateways</h1>
<div class="Node Header">
<div class="Status" title="Online status: is it possible to read data?" style="cursor: help">Online</div>
<div class="Cors" title="Allows Cross-Origin Resource Sharing (CORS fetch)"><a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#The_HTTP_response_headers" target="_blank">CORS</a></div>
<div class="Ipns" title="Allows IPNS Resolution to IPFS Content Addresses"><a href="https://docs.ipfs.tech/concepts/ipns" target="_blank">IPNS</a></div>
<div class="Origin" title="Provides Origin Isolation (Subdomain Gateway)"><a href="https://docs.ipfs.io/how-to/address-ipfs-on-web/#subdomain-gateway" target="_blank">Origin</a></div>
<div class="Trustless" title="Supports Trustless Gateway Specification"><a href="https://docs.ipfs.tech/reference/http/gateway/#trustless-verifiable-retrieval" target="_blank">Block/CAR</a></div>
<div class="Flag">Country</div>
Expand Down
3 changes: 2 additions & 1 deletion src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ div.Node div.Status,
div.Node div.Cors,
div.Node div.Origin,
div.Node div.Trustless,
div.Node div.Ipns,
div.Node div.Flag {
width: 6em;
width: 7.2em;
text-align: center;
margin: 0 0.5em;
user-select: none;
Expand Down

0 comments on commit 10a5c13

Please sign in to comment.