Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(helia): ⚡️ Remove dependence on preload node #44

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@helia/unixfs": "1.x",
"blockstore-level": "^1.1.4",
"datastore-level": "^10.1.4",
"dns-over-http-resolver": "2.1.3",
"fastify": "4.24.3",
"fastify-metrics": "10.3.2",
"file-type": "18.x",
Expand Down
17 changes: 8 additions & 9 deletions src/heliaFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { unixfs, type UnixFS } from '@helia/unixfs'
import { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'
import debug from 'debug'
import DOHResolver from 'dns-over-http-resolver'
import { createHelia, type Helia } from 'helia'
import { LRUCache } from 'lru-cache'
import { CID } from 'multiformats/cid'
Expand All @@ -13,8 +14,6 @@ const ROOT_FILE_PATTERNS = [
'index.shtml'
]

const DELEGATED_ROUTING_API = 'https://node3.delegate.ipfs.io/api/v0/name/resolve/'

interface HeliaPathParts {
namespace: string
address: string
Expand All @@ -25,8 +24,8 @@ interface HeliaPathParts {
* Fetches files from IPFS or IPNS
*/
export class HeliaFetch {
private readonly dohResolver = new DOHResolver()
private fs!: UnixFS
private readonly delegatedRoutingApi: string
private readonly log: debug.Debugger
private readonly PARSE_PATH_REGEX = /^\/(?<namespace>ip[fn]s)\/(?<address>[^/$]+)(?<relativePath>[^$^?]*)/
private readonly rootFilePatterns: string[]
Expand All @@ -40,12 +39,10 @@ export class HeliaFetch {
constructor ({
node,
rootFilePatterns = ROOT_FILE_PATTERNS,
delegatedRoutingApi = DELEGATED_ROUTING_API,
logger
}: {
node?: Helia
rootFilePatterns?: string[]
delegatedRoutingApi?: string
logger?: debug.Debugger
} = {}) {
// setup a logger
Expand All @@ -59,7 +56,6 @@ export class HeliaFetch {
this.node = node
}
this.rootFilePatterns = rootFilePatterns
this.delegatedRoutingApi = delegatedRoutingApi
this.ready = this.init()
this.log('Initialized')
}
Expand Down Expand Up @@ -160,9 +156,12 @@ export class HeliaFetch {
*/
private async fetchIpns (address: string, options?: Parameters<UnixFS['cat']>[1]): Promise<AsyncIterable<Uint8Array>> {
if (!this.ipnsResolutionCache.has(address)) {
this.log('Fetching from Delegate Routing:', address)
const { Path } = await (await fetch(this.delegatedRoutingApi + address)).json()
this.ipnsResolutionCache.set(address, Path ?? 'not-found')
this.log('Fetching from DNS over HTTP:', address)
const txtRecords = await this.dohResolver.resolveTxt(`_dnslink.${address}`)
const pathEntry = txtRecords.find(([record]) => record.startsWith('dnslink='))
const path = pathEntry?.[0].replace('dnslink=', '')
this.log('Got Path from DNS over HTTP:', path)
this.ipnsResolutionCache.set(address, path ?? 'not-found')
}
if (this.ipnsResolutionCache.get(address) === 'not-found') {
this.log('No Path found:', address)
Expand Down
Loading