Skip to content

Commit

Permalink
chore: update to new multiformats (#46)
Browse files Browse the repository at this point in the history
* chore: update to new multiformats

BREAKING CHANGE: uses the CID class from the new multiformats module
  • Loading branch information
achingbrain authored Jul 7, 2021
1 parent f16ca8f commit 44cd293
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
before: async () => {
server = createServer({
host: '127.0.0.1',
port: 57483
port: 57583
}, {
type: 'go',
ipfsHttpModule: require('ipfs-http-client'),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const DelegatedPeerRouting = require('libp2p-delegated-peer-routing')
const ipfsHttpClient = require('ipfs-http-client')

// default is to use ipfs.io
const routing = new DelegatedPeerRouting(ipfsHttpClient({
const routing = new DelegatedPeerRouting(ipfsHttpClient.create({
// use default api settings
protocol: 'https',
port: 443,
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"scripts": {
"lint": "aegir lint",
"prepare": "npm run build",
"build": "aegir build",
"test": "aegir test -f test/**/*.spec.js",
"test:node": "aegir test -t node -f test/**/*.spec.js",
Expand All @@ -24,17 +25,17 @@
"devDependencies": {
"aegir": "^33.0.0",
"go-ipfs": "^0.8.0",
"ipfs-http-client": "^49.0.4",
"ipfs-utils": "^6.0.6",
"ipfsd-ctl": "^7.0.2",
"ipfs-http-client": "^50.1.0",
"ipfs-utils": "^8.1.2",
"ipfsd-ctl": "^8.0.2",
"it-all": "^1.0.2"
},
"dependencies": {
"cids": "^1.0.0",
"debug": "^4.3.1",
"multiformats": "^9.0.2",
"p-defer": "^3.0.0",
"p-queue": "^6.3.0",
"peer-id": "^0.14.0"
"peer-id": "^0.15.0"
},
"browser": {
"go-ipfs": false
Expand Down
7 changes: 3 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const PeerId = require('peer-id')
const CID = require('cids')
const { base58btc } = require('multiformats/bases/base58')
const { default: PQueue } = require('p-queue')
const defer = require('p-defer')
const debug = require('debug')
Expand Down Expand Up @@ -89,8 +89,7 @@ class DelegatedPeerRouting {
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
*/
async * getClosestPeers (key, options = {}) {
key = new CID(key)
const keyStr = key.toString()
const keyStr = base58btc.encode(key).substring(1)

log('getClosestPeers starts:', keyStr)
options.timeout = options.timeout || DEFAULT_TIMEOUT
Expand All @@ -116,7 +115,7 @@ class DelegatedPeerRouting {
// Track the addresses, so we can yield them when done
result.responses.forEach(response => {
peers.set(response.id, {
id: PeerId.createFromCID(response.id),
id: PeerId.parse(response.id),
multiaddrs: response.addrs
})
})
Expand Down
28 changes: 14 additions & 14 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const factory = createFactory({
ipfsBin: isNode ? require('go-ipfs').path() : undefined,
test: true,
disposable: true,
endpoint: 'http://localhost:57483'
endpoint: 'http://localhost:57583'
})

async function spawnNode (bootstrap = []) {
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('DelegatedPeerRouting', function () {
})

it('should accept an http api client instance at construction time', () => {
const client = ipfsHttpClient({
const client = ipfsHttpClient.create({
protocol: 'http',
port: 8000,
host: 'localhost'
Expand All @@ -97,7 +97,7 @@ describe('DelegatedPeerRouting', function () {
it('should be able to find peers via the delegate with a peer id string', async () => {
const opts = delegatedNode.apiAddr.toOptions()

const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
Expand All @@ -114,13 +114,13 @@ describe('DelegatedPeerRouting', function () {

it('should be able to find peers via the delegate with a peerid', async () => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
}))

const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id))
const peer = await router.findPeer(PeerID.parse(peerIdToFind.id))
expect(peer).to.be.ok()

const { id, multiaddrs } = peer
Expand All @@ -132,13 +132,13 @@ describe('DelegatedPeerRouting', function () {

it('should be able to specify a timeout', async () => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
}))

const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id), { timeout: 2000 })
const peer = await router.findPeer(PeerID.parse(peerIdToFind.id), { timeout: 2000 })
expect(peer).to.be.ok()

const { id, multiaddrs } = peer
Expand All @@ -150,7 +150,7 @@ describe('DelegatedPeerRouting', function () {

it('should not be able to find peers not on the network', async () => {
const opts = delegatedNode.apiAddr.toOptions()
const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
Expand All @@ -167,16 +167,16 @@ describe('DelegatedPeerRouting', function () {
it('should be able to query for the closest peers', async () => {
const opts = delegatedNode.apiAddr.toOptions()

const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
}))

const nodeId = await delegatedNode.api.id()
const delegatePeerId = PeerID.createFromCID(nodeId.id)
const delegatePeerId = PeerID.parse(nodeId.id)

const key = PeerID.createFromB58String(peerIdToFind.id).id
const key = PeerID.parse(peerIdToFind.id).id
const results = await concat(router.getClosestPeers(key))

// we should be closest to the 2 other peers
Expand All @@ -188,17 +188,17 @@ describe('DelegatedPeerRouting', function () {
})
})

it('should find closest peers even if the peer doesnt exist', async () => {
it('should find closest peers even if the peer does not exist', async () => {
const opts = delegatedNode.apiAddr.toOptions()

const router = new DelegatedPeerRouting(ipfsHttpClient({
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
protocol: 'http',
port: opts.port,
host: opts.host
}))

const nodeId = await delegatedNode.api.id()
const delegatePeerId = PeerID.createFromCID(nodeId.id)
const delegatePeerId = PeerID.parse(nodeId.id)

const peerId = await PeerID.create({ keyType: 'ed25519' })
const results = await concat(router.getClosestPeers(peerId.id))
Expand Down

0 comments on commit 44cd293

Please sign in to comment.