diff --git a/minipfs/src/cid.ts b/minipfs/src/cid.ts index b3a7d969..d20c29e5 100644 --- a/minipfs/src/cid.ts +++ b/minipfs/src/cid.ts @@ -1,5 +1,12 @@ // import { path, url } from '@vikiival/is-ipfs' -import { HTTP_REGEX, IPFS_FS_PREFIX, IPFS_NS_PREFIX, IPFS_PREFIX, IPNS_PREFIX, SMART_IPFS_REGEX } from './constants' +import { + HTTP_REGEX, + IPFS_FS_PREFIX, + IPFS_NS_PREFIX, + IPFS_PREFIX, + IPNS_PREFIX, + SMART_IPFS_REGEX +} from './constants' import { PINATA_GATEWAY } from './gateways' import { IPFS_HASH, IPFS_PATH, IPNS_PATH } from './types' diff --git a/minipfs/src/gateways.ts b/minipfs/src/gateways.ts index a620ca74..8fe88255 100644 --- a/minipfs/src/gateways.ts +++ b/minipfs/src/gateways.ts @@ -3,38 +3,42 @@ export const PINATA_GATEWAY: HTTPS_URI = 'https://gateway.pinata.cloud' // LIST: https://ipfs.github.io/public-gateway-checker/ export type IPFSProviders = + | 'apillon' + | 'aragon' + | 'astyanax' + | 'cf' | 'cloudflare' - | 'ipfs' | 'dweb' - | 'kodadot' - | 'kodadot_beta' - | 'rmrk' + | 'filebase_kodadot' | 'fleek' - | 'nftstorage' - | 'aragon' - | 'cf' - | 'infura' | 'gateway' - | 'filebase_kodadot' + | 'infura' | 'infura_kodadot1' + | 'ipfs' + | 'kodadot' + | 'kodadot_beta' + | 'nftstorage' + | 'rmrk' export type AvailableProviders = IPFSProviders[] export const ipfsProviders: Record = { + apillon: 'https://ipfs.apillon.io', + aragon: 'https://ipfs.eth.aragon.network', + astyanax: 'https://ipfs.astyanax.io', + cf: 'https://cf-ipfs.com', cloudflare: 'https://cloudflare-ipfs.com', - ipfs: 'https://ipfs.io', dweb: 'https://dweb.link', + filebase_kodadot: 'https://kodadot-ultra.myfilebase.com', + fleek: 'https://ipfs.fleek.co', + gateway: 'https://ipfs-gateway.cloud', + infura: 'https://infura-ipfs.io', + infura_kodadot1: 'https://kodadot1.infura-ipfs.io', + ipfs: 'https://gateway.ipfs.io', kodadot: 'https://image.w.kodadot.xyz', kodadot_beta: 'https://image-beta.w.kodadot.xyz', - rmrk: 'https://ipfs2.rmrk.link', - fleek: 'https://ipfs.fleek.co', nftstorage: 'https://nftstorage.link', - aragon: 'https://ipfs.eth.aragon.network', - cf: 'https://cf-ipfs.com', - infura: 'https://infura-ipfs.io', - gateway: 'https://ipfs-gateway.cloud', - filebase_kodadot: 'https://kodadot-ultra.myfilebase.com', - infura_kodadot1: 'https://kodadot1.infura-ipfs.io' + rmrk: 'https://ipfs2.rmrk.link' } const DEFAULT_PROVIDER_LIST: AvailableProviders = [ @@ -51,11 +55,16 @@ export const getProviderList = ( return list.map(provider => ipfsProviders[provider]) } -export const getProperURI = (uri: IPFS_PATH | IPNS_PATH, providers: AvailableProviders = []): HTTPS_URI[] => { +export const getProperURI = ( + uri: IPFS_PATH | IPNS_PATH, + providers: AvailableProviders = [] +): HTTPS_URI[] => { const providerList = getProviderList(providers) return providerList.map(provider => `${provider}${uri}`) } -export const getGatewayURI = (uri: IPFS_PATH | IPNS_PATH) => (provider: IPFSProviders): HTTPS_URI => { - return `${ipfsProviders[provider]}${uri}` -} +export const getGatewayURI = + (uri: IPFS_PATH | IPNS_PATH) => + (provider: IPFSProviders): HTTPS_URI => { + return `${ipfsProviders[provider]}${uri}` + } diff --git a/minipfs/src/magic.ts b/minipfs/src/magic.ts index bf2b6ad8..533f0027 100644 --- a/minipfs/src/magic.ts +++ b/minipfs/src/magic.ts @@ -4,7 +4,11 @@ import { competition } from './race' import { sanitize } from './sanitize' import { HTTPS_URI, IPFS_PATH, IPNS_PATH, SanitizedOutput, URI } from './types' -export function $obtain(uri: URI | string, providers: AvailableProviders = [], fast?: boolean): Promise { +export function $obtain( + uri: URI | string, + providers: AvailableProviders = [], + fast?: boolean +): Promise { const { needProvider, path, formatter }: SanitizedOutput = sanitize(uri) const callback = fast ? obtainFast : obtain @@ -19,7 +23,10 @@ export function $obtain(uri: URI | string, providers: AvailableProviders = [] return callback(path) } -export function $purify(uri: URI | string, providers: AvailableProviders = []): HTTPS_URI[] { +export function $purify( + uri: URI | string, + providers: AvailableProviders = [] +): HTTPS_URI[] { const { needProvider, path }: SanitizedOutput = sanitize(uri) if (needProvider) { diff --git a/minipfs/src/obtain.ts b/minipfs/src/obtain.ts index 44b556ce..f27692e1 100644 --- a/minipfs/src/obtain.ts +++ b/minipfs/src/obtain.ts @@ -2,18 +2,23 @@ import { $fetch, FetchOptions } from 'ofetch' import { URI } from './types' -export function obtain(uri: URI, options?: FetchOptions<'json'>): Promise { +export function obtain( + uri: URI, + options?: FetchOptions<'json'> +): Promise { return $fetch(uri, { retry: 3, mode: 'no-cors', redirect: 'follow', ...options, onRequestError({ error, request }) { - const message = `[KODADOT::MINIPFS] Fail to Obtain ${request}: ${error.message}` + const message = + `[KODADOT::MINIPFS] Fail to Obtain ${request}: ${error.message}` console.warn(message) }, onResponseError({ request, response }) { - const message = `[KODADOT::MINIPFS] Obtaininig ${request} failed with status ${response.status}` + const message = + `[KODADOT::MINIPFS] Obtaininig ${request} failed with status ${response.status}` console.warn(message) } }) diff --git a/minipfs/src/parser.ts b/minipfs/src/parser.ts index 8359123d..d4fada79 100644 --- a/minipfs/src/parser.ts +++ b/minipfs/src/parser.ts @@ -1,4 +1,5 @@ export function isBaseSixtyFour(data: string): boolean { + // eslint-disable-next-line no-useless-escape return /^data:([a-z]+)\/([\.\-\+a-z]+);base64,/.test(data) } @@ -7,7 +8,8 @@ function isBaseSixtyFourJSON(data: string): boolean { } export function canBeJSON(data: string): boolean { - return (data.startsWith('{') && data.endsWith('}')) || (data.startsWith('[') && data.endsWith(']')) + return (data.startsWith('{') && data.endsWith('}')) || + (data.startsWith('[') && data.endsWith(']')) } export function fromBaseSixtyFour(data: string): string { diff --git a/minipfs/src/race.ts b/minipfs/src/race.ts index a653b740..279a69b2 100644 --- a/minipfs/src/race.ts +++ b/minipfs/src/race.ts @@ -1,6 +1,6 @@ // import { AvailableProviders } from './gateways' import { AvailableProviders, getProviderList } from './gateways' -import { HTTPS_URI, IPFS_PATH, IPNS_PATH, FetchCallback } from './types' +import { FetchCallback, HTTPS_URI, IPFS_PATH, IPNS_PATH } from './types' // export const race = async (param: X, callback: UnwrapCallback, providers: AvailableProviders) => { // return Promise.race() diff --git a/minipfs/src/sanitize.ts b/minipfs/src/sanitize.ts index 6a0a904a..a184d2c1 100644 --- a/minipfs/src/sanitize.ts +++ b/minipfs/src/sanitize.ts @@ -1,5 +1,10 @@ import { toArweavePath } from './arweave' -import { canBeIPFS, extractIPFS, isDefaultPinataProvider, toIPFSPath } from './cid' +import { + canBeIPFS, + extractIPFS, + isDefaultPinataProvider, + toIPFSPath +} from './cid' import { ARWEAVE_REGEX, HTTP_REGEX, IPFS_REGEX } from './constants' import { PINATA_GATEWAY } from './gateways' import { baseSixtyFourFormatter, canBeJSON, isBaseSixtyFour } from './parser' @@ -11,11 +16,19 @@ export function sanitize(path: string): SanitizedOutput { } if (canBeJSON(path)) { - return { path: path as HTTPS_URI, needProvider: false, formatter: JSON.parse } + return { + path: path as HTTPS_URI, + needProvider: false, + formatter: JSON.parse + } } if (isBaseSixtyFour(path)) { - return { path: path as HTTPS_URI, needProvider: false, formatter: baseSixtyFourFormatter } + return { + path: path as HTTPS_URI, + needProvider: false, + formatter: baseSixtyFourFormatter + } } if (IPFS_REGEX.test(path)) { @@ -24,7 +37,10 @@ export function sanitize(path: string): SanitizedOutput { if (HTTP_REGEX.test(path)) { if (isDefaultPinataProvider(path)) { - return { path: path.replace(PINATA_GATEWAY, '') as IPFS_PATH | IPNS_PATH, needProvider: true } + return { + path: path.replace(PINATA_GATEWAY, '') as IPFS_PATH | IPNS_PATH, + needProvider: true + } } return { path: path as HTTPS_URI, needProvider: false } } diff --git a/minipfs/src/types.ts b/minipfs/src/types.ts index 332d7c82..4b1a453f 100644 --- a/minipfs/src/types.ts +++ b/minipfs/src/types.ts @@ -14,8 +14,8 @@ export type URI = HTTPS_URI | IPFS | ARWEAVE_URI export type Formatter = (uri: string) => T export type SanitizedOutput = { - path: HTTPS_URI | IPFS, - needProvider: boolean, + path: HTTPS_URI | IPFS + needProvider: boolean formatter?: Formatter }