Skip to content

Commit

Permalink
deps: swap ipfs-http-client for kubo-rpc-client (#140)
Browse files Browse the repository at this point in the history
Replaces deprecated ipfs-http-client module with kubo-rpc-client
  • Loading branch information
achingbrain committed May 31, 2023
1 parent 3d45253 commit 0f3e0a3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createServer } from 'ipfsd-ctl'
import * as ipfsHttpModule from 'ipfs-http-client'
import * as ipfsHttpModule from 'kubo-rpc-client'
import goIpfsModule from 'go-ipfs'

let server
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
node_modules
build
dist
.docs
.coverage
node_modules
package-lock.json
yarn.lock
.vscode
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-delegated-peer-routing/issues"
},
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",
"types": "./dist/src/index.d.ts",
"files": [
Expand Down Expand Up @@ -142,7 +138,7 @@
"@libp2p/interfaces": "^3.3.1",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^2.0.0",
"any-signal": "^3.0.1",
"any-signal": "^4.1.1",
"ipfs-core-types": "^0.14.0",
"multiformats": "^11.0.0",
"p-defer": "^4.0.0",
Expand All @@ -151,11 +147,11 @@
"devDependencies": {
"@libp2p/peer-id-factory": "^2.0.0",
"aegir": "^39.0.9",
"go-ipfs": "^0.15.0",
"ipfs-http-client": "^59.0.0",
"go-ipfs": "^0.20.0",
"ipfsd-ctl": "^13.0.0",
"it-all": "^3.0.1",
"it-drain": "^3.0.2",
"kubo-rpc-client": "^3.0.1",
"uint8arrays": "^4.0.2",
"wherearewe": "^2.0.1"
},
Expand Down
24 changes: 16 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CodeError } from '@libp2p/interfaces/errors'
import { logger } from '@libp2p/logger'
import { peerIdFromBytes } from '@libp2p/peer-id'
import anySignal from 'any-signal'
import { anySignal } from 'any-signal'
import { CID } from 'multiformats/cid'
import defer from 'p-defer'
import PQueue from 'p-queue'
Expand Down Expand Up @@ -169,8 +169,8 @@ class DelegatedPeerRouting implements PeerRouting, Startable {
async findPeer (id: PeerId, options: HTTPClientExtraOptions & AbortOptions = {}): Promise<PeerInfo> {
log('findPeer starts: %p', id)
options.timeout = options.timeout ?? DEFAULT_TIMEOUT
options.signal = anySignal([this.abortController.signal].concat((options.signal != null) ? [options.signal] : []))

const signal = anySignal([this.abortController.signal, options.signal])
const onStart = defer()
const onFinish = defer()

Expand All @@ -182,8 +182,11 @@ class DelegatedPeerRouting implements PeerRouting, Startable {
try {
await onStart.promise

for await (const event of this.client.dht.findPeer(id, options)) {
if (event.name === 'FINAL_PEER') {
for await (const event of this.client.dht.findPeer(id, {
...options,
signal
})) {
if (event.name === 'FINAL_PEER' && event.peer.multiaddrs.length > 0) {
const peerInfo: PeerInfo = {
id: event.peer.id,
multiaddrs: event.peer.multiaddrs,
Expand All @@ -193,16 +196,17 @@ class DelegatedPeerRouting implements PeerRouting, Startable {
return peerInfo
}
}

throw new CodeError('Not found', 'ERR_NOT_FOUND')
} catch (err: any) {
log.error('findPeer errored: %o', err)

throw err
} finally {
signal.clear()
onFinish.resolve()
log('findPeer finished: %p', id)
}

throw new CodeError('Not found', 'ERR_NOT_FOUND')
}

/**
Expand All @@ -220,8 +224,8 @@ class DelegatedPeerRouting implements PeerRouting, Startable {

log('getClosestPeers starts: %s', cidOrPeerId)
options.timeout = options.timeout ?? DEFAULT_TIMEOUT
options.signal = anySignal([this.abortController.signal].concat((options.signal != null) ? [options.signal] : []))

const signal = anySignal([this.abortController.signal, options.signal])
const onStart = defer()
const onFinish = defer()

Expand All @@ -233,7 +237,10 @@ class DelegatedPeerRouting implements PeerRouting, Startable {
try {
await onStart.promise

for await (const event of this.client.dht.query(cidOrPeerId, options)) {
for await (const event of this.client.dht.query(cidOrPeerId, {
...options,
signal
})) {
if (event.name === 'PEER_RESPONSE') {
yield * event.closer.map(closer => ({
id: closer.id,
Expand All @@ -246,6 +253,7 @@ class DelegatedPeerRouting implements PeerRouting, Startable {
log.error('getClosestPeers errored:', err)
throw err
} finally {
signal.clear()
onFinish.resolve()
log('getClosestPeers finished: %b', key)
}
Expand Down
4 changes: 1 addition & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { expect } from 'aegir/chai'
// @ts-expect-error no types
import goIpfs from 'go-ipfs'
import { create, type Options, CID as IPFSCID } from 'ipfs-http-client'
import { type Controller, createFactory } from 'ipfsd-ctl'
import all from 'it-all'
import drain from 'it-drain'
import { create, type Options, CID as IPFSCID } from 'kubo-rpc-client'
import { CID } from 'multiformats/cid'
import pDefer from 'p-defer'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
Expand Down Expand Up @@ -62,11 +62,9 @@ function createIpfsClient (opts: Options): any {
},
dht: {
async * findPeer (peerId: PeerId, options?: AbortOptions) {
// @ts-expect-error ipfs-http-client types are out of date
yield * client.dht.findPeer(peerId, options)
},
async * query (peerId: PeerId | CID, options?: AbortOptions) {
// @ts-expect-error ipfs-http-client types are out of date
yield * client.dht.query(peerId, options)
}
}
Expand Down

0 comments on commit 0f3e0a3

Please sign in to comment.