Skip to content

Commit

Permalink
feat(helia): helia-ipns
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>
  • Loading branch information
whizzzkid committed Oct 11, 2023
1 parent d3d5e13 commit 118f91f
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 8 deletions.
208 changes: 208 additions & 0 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 @@ -45,6 +45,7 @@
"typescript": "5.x"
},
"dependencies": {
"@helia/ipns": "2.0.1",
"@helia/unixfs": "1.x",
"express": "4.x",
"express-prom-bundle": "6.x",
Expand Down
26 changes: 18 additions & 8 deletions src/heliaFetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ipns } from '@helia/ipns'
import { dht, pubsub } from '@helia/ipns/routing'
import { unixfs, type UnixFS } from '@helia/unixfs'
import { MemoryBlockstore } from 'blockstore-core'
import { MemoryDatastore } from 'datastore-core'
Expand All @@ -13,18 +15,16 @@ const ROOT_FILE_PATTERNS = [
'index.shtml'
]

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

/**
* Fetches files from IPFS or IPNS
*/
export class HeliaFetch {
private fs!: UnixFS
private readonly delegatedRoutingApi: string
private readonly log: debug.Debugger
private readonly rootFilePatterns: string[]
public node!: Helia
public ready: Promise<void>
private ipns!: ReturnType<typeof ipns>
private readonly ipnsResolutionCache = new LRUCache<string, string>({
max: 10000,
ttl: 1000 * 60 * 60 * 24
Expand All @@ -33,12 +33,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 @@ -52,7 +50,6 @@ export class HeliaFetch {
this.node = node
}
this.rootFilePatterns = rootFilePatterns
this.delegatedRoutingApi = delegatedRoutingApi
this.ready = this.init()
this.log('Initialized')
}
Expand All @@ -65,6 +62,19 @@ export class HeliaFetch {
blockstore: new MemoryBlockstore(),
datastore: new MemoryDatastore()
})
this.ipns = ipns(this.node, [
dht(this.node),
pubsub({
datastore: this.node.datastore,
libp2p: {
peerId: this.node.libp2p.peerId,
services: {
// mismatch types
pubsub: this.node.libp2p.services.pubsub
}
}
})
])
this.fs = unixfs(this.node)
this.log('Helia Setup Complete!')
}
Expand Down Expand Up @@ -134,8 +144,8 @@ 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')
const cid = await this.ipns.resolveDns(address)
this.ipnsResolutionCache.set(address, cid.toString() ?? 'not-found')
}
if (this.ipnsResolutionCache.get(address) === 'not-found') {
this.log('No Path found:', address)
Expand Down

0 comments on commit 118f91f

Please sign in to comment.