diff --git a/src/GatewayNode.ts b/src/GatewayNode.ts index f6f45328..8de0e592 100644 --- a/src/GatewayNode.ts +++ b/src/GatewayNode.ts @@ -11,6 +11,7 @@ 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') @@ -18,6 +19,7 @@ class GatewayNode extends UiComponent /* implements Checkable */ { // tag: Tag status: Status cors: Cors + ipns: IPNSCheck origin: Origin trustless: Trustless link: HTMLDivElement & { url?: URL } @@ -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) @@ -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)) diff --git a/src/Ipns.ts b/src/Ipns.ts new file mode 100644 index 00000000..dff92557 --- /dev/null +++ b/src/Ipns.ts @@ -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 { + 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 } diff --git a/src/Tag.ts b/src/Tag.ts index a0470b02..50fa4d67 100644 --- a/src/Tag.ts +++ b/src/Tag.ts @@ -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 diff --git a/src/constants.ts b/src/constants.ts index 6231e90b..2892bf67 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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' @@ -12,5 +13,6 @@ export { HASH_TO_TEST, // IFRAME_HASH, IMG_HASH, + IPNS_PATH_TO_TEST, TRUSTLESS_RESPONSE_TYPES } diff --git a/src/index.html b/src/index.html index 222cf34b..75ae49b0 100644 --- a/src/index.html +++ b/src/index.html @@ -51,6 +51,7 @@

Public Gateways

Online
+
Country
diff --git a/src/styles.css b/src/styles.css index 72565b82..a9f1b852 100644 --- a/src/styles.css +++ b/src/styles.css @@ -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;