Skip to content

Commit 44cd293

Browse files
authored
chore: update to new multiformats (#46)
* chore: update to new multiformats BREAKING CHANGE: uses the CID class from the new multiformats module
1 parent f16ca8f commit 44cd293

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

.aegir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
before: async () => {
99
server = createServer({
1010
host: '127.0.0.1',
11-
port: 57483
11+
port: 57583
1212
}, {
1313
type: 'go',
1414
ipfsHttpModule: require('ipfs-http-client'),

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const DelegatedPeerRouting = require('libp2p-delegated-peer-routing')
2424
const ipfsHttpClient = require('ipfs-http-client')
2525

2626
// default is to use ipfs.io
27-
const routing = new DelegatedPeerRouting(ipfsHttpClient({
27+
const routing = new DelegatedPeerRouting(ipfsHttpClient.create({
2828
// use default api settings
2929
protocol: 'https',
3030
port: 443,

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
"scripts": {
1515
"lint": "aegir lint",
16+
"prepare": "npm run build",
1617
"build": "aegir build",
1718
"test": "aegir test -f test/**/*.spec.js",
1819
"test:node": "aegir test -t node -f test/**/*.spec.js",
@@ -24,17 +25,17 @@
2425
"devDependencies": {
2526
"aegir": "^33.0.0",
2627
"go-ipfs": "^0.8.0",
27-
"ipfs-http-client": "^49.0.4",
28-
"ipfs-utils": "^6.0.6",
29-
"ipfsd-ctl": "^7.0.2",
28+
"ipfs-http-client": "^50.1.0",
29+
"ipfs-utils": "^8.1.2",
30+
"ipfsd-ctl": "^8.0.2",
3031
"it-all": "^1.0.2"
3132
},
3233
"dependencies": {
33-
"cids": "^1.0.0",
3434
"debug": "^4.3.1",
35+
"multiformats": "^9.0.2",
3536
"p-defer": "^3.0.0",
3637
"p-queue": "^6.3.0",
37-
"peer-id": "^0.14.0"
38+
"peer-id": "^0.15.0"
3839
},
3940
"browser": {
4041
"go-ipfs": false

src/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const PeerId = require('peer-id')
4-
const CID = require('cids')
4+
const { base58btc } = require('multiformats/bases/base58')
55
const { default: PQueue } = require('p-queue')
66
const defer = require('p-defer')
77
const debug = require('debug')
@@ -89,8 +89,7 @@ class DelegatedPeerRouting {
8989
* @returns {AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>}
9090
*/
9191
async * getClosestPeers (key, options = {}) {
92-
key = new CID(key)
93-
const keyStr = key.toString()
92+
const keyStr = base58btc.encode(key).substring(1)
9493

9594
log('getClosestPeers starts:', keyStr)
9695
options.timeout = options.timeout || DEFAULT_TIMEOUT
@@ -116,7 +115,7 @@ class DelegatedPeerRouting {
116115
// Track the addresses, so we can yield them when done
117116
result.responses.forEach(response => {
118117
peers.set(response.id, {
119-
id: PeerId.createFromCID(response.id),
118+
id: PeerId.parse(response.id),
120119
multiaddrs: response.addrs
121120
})
122121
})

test/index.spec.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const factory = createFactory({
1515
ipfsBin: isNode ? require('go-ipfs').path() : undefined,
1616
test: true,
1717
disposable: true,
18-
endpoint: 'http://localhost:57483'
18+
endpoint: 'http://localhost:57583'
1919
})
2020

2121
async function spawnNode (bootstrap = []) {
@@ -74,7 +74,7 @@ describe('DelegatedPeerRouting', function () {
7474
})
7575

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

100-
const router = new DelegatedPeerRouting(ipfsHttpClient({
100+
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
101101
protocol: 'http',
102102
port: opts.port,
103103
host: opts.host
@@ -114,13 +114,13 @@ describe('DelegatedPeerRouting', function () {
114114

115115
it('should be able to find peers via the delegate with a peerid', async () => {
116116
const opts = delegatedNode.apiAddr.toOptions()
117-
const router = new DelegatedPeerRouting(ipfsHttpClient({
117+
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
118118
protocol: 'http',
119119
port: opts.port,
120120
host: opts.host
121121
}))
122122

123-
const peer = await router.findPeer(PeerID.createFromB58String(peerIdToFind.id))
123+
const peer = await router.findPeer(PeerID.parse(peerIdToFind.id))
124124
expect(peer).to.be.ok()
125125

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

133133
it('should be able to specify a timeout', async () => {
134134
const opts = delegatedNode.apiAddr.toOptions()
135-
const router = new DelegatedPeerRouting(ipfsHttpClient({
135+
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
136136
protocol: 'http',
137137
port: opts.port,
138138
host: opts.host
139139
}))
140140

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

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

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

170-
const router = new DelegatedPeerRouting(ipfsHttpClient({
170+
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
171171
protocol: 'http',
172172
port: opts.port,
173173
host: opts.host
174174
}))
175175

176176
const nodeId = await delegatedNode.api.id()
177-
const delegatePeerId = PeerID.createFromCID(nodeId.id)
177+
const delegatePeerId = PeerID.parse(nodeId.id)
178178

179-
const key = PeerID.createFromB58String(peerIdToFind.id).id
179+
const key = PeerID.parse(peerIdToFind.id).id
180180
const results = await concat(router.getClosestPeers(key))
181181

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

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

194-
const router = new DelegatedPeerRouting(ipfsHttpClient({
194+
const router = new DelegatedPeerRouting(ipfsHttpClient.create({
195195
protocol: 'http',
196196
port: opts.port,
197197
host: opts.host
198198
}))
199199

200200
const nodeId = await delegatedNode.api.id()
201-
const delegatePeerId = PeerID.createFromCID(nodeId.id)
201+
const delegatePeerId = PeerID.parse(nodeId.id)
202202

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

0 commit comments

Comments
 (0)