-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implementing Trustless Server Checks (#310)
- Loading branch information
Showing
6 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import fetchPonyfill from 'fetch-ponyfill' | ||
|
||
import { CheckBase } from './CheckBase' | ||
import { HASH_TO_TEST, TRUSTLESS_RESPONSE_TYPES } from './constants' | ||
import type { GatewayNode } from './GatewayNode' | ||
|
||
import { Log } from './Log' | ||
|
||
const { fetch } = fetchPonyfill() | ||
|
||
const log = new Log('Trustless') | ||
|
||
class Trustless extends CheckBase implements Checkable { | ||
_className = 'Trustless' | ||
_tagName = 'div' | ||
constructor (protected parent: GatewayNode) { | ||
super(parent, 'div', 'Trustless') | ||
} | ||
|
||
async check (): Promise<void> { | ||
const now = Date.now() | ||
const gatewayAndHash = this.parent.gateway.replace(':hash', HASH_TO_TEST) | ||
this.parent.tag.classList.add('trustless') | ||
try { | ||
const trustlessResponseTypesTests = await Promise.all(TRUSTLESS_RESPONSE_TYPES.map( | ||
async (trustlessTypes): Promise<boolean> => { | ||
const testUrl = `${gatewayAndHash}?format=${trustlessTypes}&now=${now}#x-ipfs-companion-no-redirect` | ||
const response = await fetch(testUrl) | ||
return Boolean(response.headers.get('Content-Type')?.includes(`application/vnd.ipld.${trustlessTypes}`)) | ||
} | ||
)) | ||
|
||
if (!trustlessResponseTypesTests.includes(false)) { | ||
this.tag.win() | ||
} else { | ||
log.debug('The response type did not match the expected type') | ||
this.onerror() | ||
throw new Error(`URL '${gatewayAndHash} does not support Trustless`) | ||
} | ||
} catch (err) { | ||
log.error(err) | ||
this.onerror() | ||
throw err | ||
} | ||
} | ||
|
||
checked (): void { | ||
log.warn('Not implemented yet') | ||
} | ||
|
||
onerror (): void { | ||
this.tag.err() | ||
} | ||
} | ||
|
||
export { Trustless } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters