From 2a9d7657a96fad9af48dfce56f73be2697a3db6b Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 31 Jan 2020 12:46:37 +0000 Subject: [PATCH] refactor: return peer ids as strings (#1226) BREAKING CHANGE: Where `PeerID`s were previously [CID]s, now they are Strings - `ipfs.bitswap.stat().peers[n]` is now a String (was a CID) - `ipfs.dht.findPeer().id` is now a String (was a CID) - `ipfs.dht.findProvs()[n].id` is now a String (was a CID) - `ipfs.dht.provide()[n].id` is now a String (was a CID) - `ipfs.dht.put()[n].id` is now a String (was a CID) - `ipfs.dht.query()[n].id` is now a String (was a CID) - `ipfs.id().id` is now a String (was a CID) - `ipfs.id().addresses[n]` are now [Multiaddr]s (were Strings) --- package.json | 4 ++-- src/bitswap/stat.js | 2 +- src/dht/find-peer.js | 2 +- src/dht/find-provs.js | 2 +- src/dht/provide.js | 2 +- src/dht/put.js | 2 +- src/dht/query.js | 2 +- src/id.js | 9 ++++++++- src/swarm/addrs.js | 3 +-- src/swarm/peers.js | 3 +-- 10 files changed, 18 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 5fe20ecc4..844f26807 100644 --- a/package.json +++ b/package.json @@ -68,8 +68,8 @@ "async": "^3.1.0", "browser-process-platform": "~0.1.1", "go-ipfs-dep": "^0.4.22", - "interface-ipfs-core": "^0.129.0", - "ipfsd-ctl": "^1.0.2", + "interface-ipfs-core": "^0.131.0", + "ipfsd-ctl": "^2.1.0", "it-all": "^1.0.1", "it-concat": "^1.0.0", "it-pipe": "^1.1.0", diff --git a/src/bitswap/stat.js b/src/bitswap/stat.js index e041f5d9d..36101a1f4 100644 --- a/src/bitswap/stat.js +++ b/src/bitswap/stat.js @@ -23,7 +23,7 @@ function toCoreInterface (res) { return { provideBufLen: res.ProvideBufLen, wantlist: (res.Wantlist || []).map(k => new CID(k['/'])), - peers: (res.Peers || []).map(p => new CID(p)), + peers: (res.Peers || []), blocksReceived: new Big(res.BlocksReceived), dataReceived: new Big(res.DataReceived), blocksSent: new Big(res.BlocksSent), diff --git a/src/dht/find-peer.js b/src/dht/find-peer.js index 9d8f517e5..ef5c6c92a 100644 --- a/src/dht/find-peer.js +++ b/src/dht/find-peer.js @@ -37,7 +37,7 @@ module.exports = configure(({ ky }) => { // https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L395-L396 for (const { ID, Addrs } of message.Responses) { return { - id: new CID(ID), + id: ID, addrs: (Addrs || []).map(a => multiaddr(a)) } } diff --git a/src/dht/find-provs.js b/src/dht/find-provs.js index 7adbaf38c..e70935dec 100644 --- a/src/dht/find-provs.js +++ b/src/dht/find-provs.js @@ -35,7 +35,7 @@ module.exports = configure(({ ky }) => { if (message.Type === 4 && message.Responses) { for (const { ID, Addrs } of message.Responses) { yield { - id: new CID(ID), + id: ID, addrs: (Addrs || []).map(a => multiaddr(a)) } } diff --git a/src/dht/provide.js b/src/dht/provide.js index f9013bfed..cdcae2f84 100644 --- a/src/dht/provide.js +++ b/src/dht/provide.js @@ -36,7 +36,7 @@ module.exports = configure(({ ky }) => { message.id = new CID(message.id) if (message.responses) { message.responses = message.responses.map(({ ID, Addrs }) => ({ - id: new CID(ID), + id: ID, addrs: (Addrs || []).map(a => multiaddr(a)) })) } else { diff --git a/src/dht/put.js b/src/dht/put.js index 6d0ecf6fc..8dc924f7a 100644 --- a/src/dht/put.js +++ b/src/dht/put.js @@ -38,7 +38,7 @@ module.exports = configure(({ ky }) => { message.id = new CID(message.id) if (message.responses) { message.responses = message.responses.map(({ ID, Addrs }) => ({ - id: new CID(ID), + id: ID, addrs: (Addrs || []).map(a => multiaddr(a)) })) } diff --git a/src/dht/query.js b/src/dht/query.js index 1628c0cc8..b2b878572 100644 --- a/src/dht/query.js +++ b/src/dht/query.js @@ -26,7 +26,7 @@ module.exports = configure(({ ky }) => { message = toCamel(message) message.id = new CID(message.id) message.responses = (message.responses || []).map(({ ID, Addrs }) => ({ - id: new CID(ID), + id: ID, addrs: (Addrs || []).map(a => multiaddr(a)) })) yield message diff --git a/src/id.js b/src/id.js index d6080ea67..cfec69d3d 100644 --- a/src/id.js +++ b/src/id.js @@ -2,6 +2,7 @@ const configure = require('./lib/configure') const toCamel = require('./lib/object-to-camel') +const multiaddr = require('multiaddr') module.exports = configure(({ ky }) => { return async options => { @@ -14,6 +15,12 @@ module.exports = configure(({ ky }) => { searchParams: options.searchParams }).json() - return toCamel(res) + const output = toCamel(res) + + if (output.addresses) { + output.addresses = output.addresses.map(ma => multiaddr(ma)) + } + + return output } }) diff --git a/src/swarm/addrs.js b/src/swarm/addrs.js index 25bd98d02..c4b6576f6 100644 --- a/src/swarm/addrs.js +++ b/src/swarm/addrs.js @@ -1,6 +1,5 @@ 'use strict' -const CID = require('cids') const multiaddr = require('multiaddr') const configure = require('../lib/configure') @@ -16,7 +15,7 @@ module.exports = configure(({ ky }) => { }).json() return Object.keys(res.Addrs).map(id => ({ - id: new CID(id), + id, addrs: (res.Addrs[id] || []).map(a => multiaddr(a)) })) } diff --git a/src/swarm/peers.js b/src/swarm/peers.js index f10035e06..8388af49f 100644 --- a/src/swarm/peers.js +++ b/src/swarm/peers.js @@ -1,7 +1,6 @@ 'use strict' const multiaddr = require('multiaddr') -const CID = require('cids') const configure = require('../lib/configure') module.exports = configure(({ ky }) => { @@ -25,7 +24,7 @@ module.exports = configure(({ ky }) => { const info = {} try { info.addr = multiaddr(peer.Addr) - info.peer = new CID(peer.Peer) + info.peer = peer.Peer } catch (error) { info.error = error info.rawPeerInfo = peer