Skip to content

Commit

Permalink
Merge pull request #177 from kodadot/minipfs/🦋
Browse files Browse the repository at this point in the history
minipfs/🦋
  • Loading branch information
vikiival authored Nov 6, 2023
2 parents 90e7ecf + a137e3a commit f85cab4
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 36 deletions.
9 changes: 8 additions & 1 deletion minipfs/src/cid.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
53 changes: 31 additions & 22 deletions minipfs/src/gateways.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPFSProviders, HTTPS_URI> = {
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 = [
Expand All @@ -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<HTTPS_URI>(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}`
}
11 changes: 9 additions & 2 deletions minipfs/src/magic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(uri: URI | string, providers: AvailableProviders = [], fast?: boolean): Promise<T> {
export function $obtain<T>(
uri: URI | string,
providers: AvailableProviders = [],
fast?: boolean
): Promise<T> {
const { needProvider, path, formatter }: SanitizedOutput = sanitize(uri)
const callback = fast ? obtainFast : obtain

Expand All @@ -19,7 +23,10 @@ export function $obtain<T>(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) {
Expand Down
11 changes: 8 additions & 3 deletions minipfs/src/obtain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
import { $fetch, FetchOptions } from 'ofetch'
import { URI } from './types'

export function obtain<T>(uri: URI, options?: FetchOptions<'json'>): Promise<T> {
export function obtain<T>(
uri: URI,
options?: FetchOptions<'json'>
): Promise<T> {
return $fetch<T>(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)
}
})
Expand Down
4 changes: 3 additions & 1 deletion minipfs/src/parser.ts
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion minipfs/src/race.ts
Original file line number Diff line number Diff line change
@@ -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 <T, X, R = any>(param: X, callback: UnwrapCallback, providers: AvailableProviders) => {
// return Promise.race()
Expand Down
24 changes: 20 additions & 4 deletions minipfs/src/sanitize.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)) {
Expand All @@ -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 }
}
Expand Down
4 changes: 2 additions & 2 deletions minipfs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export type URI = HTTPS_URI | IPFS | ARWEAVE_URI
export type Formatter = <T>(uri: string) => T

export type SanitizedOutput = {
path: HTTPS_URI | IPFS,
needProvider: boolean,
path: HTTPS_URI | IPFS
needProvider: boolean
formatter?: Formatter
}

Expand Down

0 comments on commit f85cab4

Please sign in to comment.