Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 022c650

Browse files
committed
fix: code review
1 parent 9a3283f commit 022c650

File tree

12 files changed

+93
-133
lines changed

12 files changed

+93
-133
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ You can check the development status at the [Kanban Board](https://waffle.io/ipf
3434

3535
[![Throughput Graph](https://graphs.waffle.io/ipfs/js-ipfs/throughput.svg)](https://waffle.io/ipfs/js-ipfs/metrics/throughput)
3636

37-
**Please read this:** Circuit Relay (pierce through NATs and dial between any node in the network) is a fundamental piece that is not finalized yet. There are multiple applications that can be built without this service but nevertheless it is fundamental to get that magic IPFS experience. If you want to track progress or contribute, please follow:
38-
39-
- ✅ Relay: https://github.com/ipfs/js-ipfs/pull/1063
40-
4137
[**`Weekly Core Dev Calls`**](https://github.com/ipfs/pm/issues/650)
4238

4339
## Tech Lead

src/cli/commands/dht.js

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

3-
/*
4-
Issue commands directly through the DHT.
5-
*/
63
module.exports = {
74
command: 'dht <command>',
85

src/cli/commands/dht/find-peer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ module.exports = {
1010
builder: {},
1111

1212
handler (argv) {
13-
argv.ipfs.dht.findpeer(argv.peerID, (err, result) => {
13+
argv.ipfs.dht.findPeer(argv.peerID, (err, result) => {
1414
if (err) {
1515
throw err
1616
}
1717

18-
result.responses[0].addrs.forEach((element) => {
19-
print(element)
20-
})
18+
const addresses = result.multiaddrs.toArray().map((ma) => ma.toString())
19+
20+
print(addresses)
2121
})
2222
}
2323
}

src/cli/commands/dht/find-providers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ module.exports = {
1717

1818
handler (argv) {
1919
const opts = {
20-
'num-providers': argv['num-providers']
20+
numProviders: argv['num-providers']
2121
}
2222

23-
argv.ipfs.dht.findprovs(argv.key, opts, (err, result) => {
23+
argv.ipfs.dht.findProvs(argv.key, opts, (err, result) => {
2424
if (err) {
2525
throw err
2626
}
2727

28-
result.responses.forEach((element) => {
29-
print(element.id)
28+
result.forEach((element) => {
29+
print(element.id.toB58String())
3030
})
3131
})
3232
}

src/cli/commands/dht/query.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ module.exports = {
1515
throw err
1616
}
1717

18-
result.forEach((element) => {
19-
print(element.ID)
18+
result.forEach((peerID) => {
19+
print(peerID.id.toB58String())
2020
})
2121
})
2222
}

src/core/components/dht.js

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const promisify = require('promisify-es6')
44
const every = require('async/every')
55
const PeerId = require('peer-id')
6+
const PeerInfo = require('peer-info')
67
const CID = require('cids')
7-
const multihash = require('multihashes')
88
const each = require('async/each')
99
const setImmediate = require('async/setImmediate')
1010
const errCode = require('err-code')
@@ -15,6 +15,8 @@ module.exports = (self) => {
1515
* Given a key, query the DHT for its best value.
1616
*
1717
* @param {Buffer} key
18+
* @param {Object} options - get options
19+
* @param {number} options.maxTimeout - optional timeout
1820
* @param {function(Error)} [callback]
1921
* @returns {Promise|void}
2022
*/
@@ -28,9 +30,9 @@ module.exports = (self) => {
2830

2931
if (!Buffer.isBuffer(key)) {
3032
try {
31-
key = multihash.fromB58String(key)
33+
key = (new CID(key)).buffer()
3234
} catch (err) {
33-
return callback(err)
35+
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
3436
}
3537
}
3638

@@ -52,9 +54,9 @@ module.exports = (self) => {
5254
put: promisify((key, value, callback) => {
5355
if (!Buffer.isBuffer(key)) {
5456
try {
55-
key = multihash.fromB58String(key)
57+
key = (new CID(key)).buffer()
5658
} catch (err) {
57-
return callback(err)
59+
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
5860
}
5961
}
6062

@@ -65,17 +67,21 @@ module.exports = (self) => {
6567
* Find peers in the DHT that can provide a specific value, given a key.
6668
*
6769
* @param {CID} key - They key to find providers for.
70+
* @param {Object} options - findProviders options
71+
* @param {number} options.maxTimeout - how long the query should maximally run, in milliseconds (default: 60000)
72+
* @param {number} options.maxNumProviders - maximum number of providers to find
6873
* @param {function(Error, Array<PeerInfo>)} [callback]
6974
* @returns {Promise<PeerInfo>|void}
7075
*/
71-
findprovs: promisify((key, opts, callback) => {
72-
if (typeof opts === 'function') {
73-
callback = opts
74-
opts = {}
76+
findProvs: promisify((key, options, callback) => {
77+
if (typeof options === 'function') {
78+
callback = options
79+
options = {}
7580
}
7681

77-
opts = opts || {}
78-
opts.maxNumProviders = opts['num-providers']
82+
options = options || {}
83+
options.timeout = options.maxTimeout // TODO create PR kad-dht
84+
options.maxNumProviders = options.numProviders
7985

8086
if (typeof key === 'string') {
8187
try {
@@ -85,61 +91,29 @@ module.exports = (self) => {
8591
}
8692
}
8793

88-
self.libp2p.contentRouting.findProviders(key, opts, (err, res) => {
89-
if (err) {
90-
return callback(err)
91-
}
92-
93-
// convert to go-ipfs return value, we need to revisit
94-
// this. For now will just conform.
95-
const goResult = {
96-
responses: res.map((peerInfo) => ({
97-
id: peerInfo.id.toB58String(),
98-
addrs: peerInfo.multiaddrs.toArray().map((a) => a.toString())
99-
})),
100-
type: 4
101-
}
102-
103-
callback(null, goResult)
104-
})
94+
self.libp2p.contentRouting.findProviders(key, options, callback)
10595
}),
10696

10797
/**
10898
* Query the DHT for all multiaddresses associated with a `PeerId`.
10999
*
110100
* @param {PeerId} peer - The id of the peer to search for.
111-
* @param {function(Error, Array<Multiaddr>)} [callback]
112-
* @returns {Promise<Array<Multiaddr>>|void}
101+
* @param {function(Error, Array<PeerInfo>)} [callback]
102+
* @returns {Promise<Array<PeerInfo>>|void}
113103
*/
114-
findpeer: promisify((peer, callback) => {
104+
findPeer: promisify((peer, callback) => {
115105
if (typeof peer === 'string') {
116106
peer = PeerId.createFromB58String(peer)
117107
}
118108

119-
self.libp2p.peerRouting.findPeer(peer, (err, info) => {
120-
if (err) {
121-
return callback(err)
122-
}
123-
124-
// convert to go-ipfs return value, we need to revisit
125-
// this. For now will just conform.
126-
const goResult = {
127-
responses: [{
128-
id: info.id.toB58String(),
129-
addrs: info.multiaddrs.toArray().map((a) => a.toString())
130-
}],
131-
type: 2
132-
}
133-
134-
callback(null, goResult)
135-
})
109+
self.libp2p.peerRouting.findPeer(peer, callback)
136110
}),
137111

138112
/**
139113
* Announce to the network that we are providing given values.
140114
*
141115
* @param {CID|Array<CID>} keys - The keys that should be announced.
142-
* @param {Object} [options={}]
116+
* @param {Object} options - provide options
143117
* @param {bool} [options.recursive=false] - Provide not only the given object but also all objects linked from it.
144118
* @param {function(Error)} [callback]
145119
* @returns {Promise|void}
@@ -181,15 +155,15 @@ module.exports = (self) => {
181155
* Find the closest peers to a given `PeerId`, by querying the DHT.
182156
*
183157
* @param {PeerId} peer - The `PeerId` to run the query agains.
184-
* @param {function(Error, Array<PeerId>)} [callback]
185-
* @returns {Promise<Array<PeerId>>|void}
158+
* @param {function(Error, Array<PeerInfo>)} [callback]
159+
* @returns {Promise<Array<PeerInfo>>|void}
186160
*/
187161
query: promisify((peerId, callback) => {
188162
if (typeof peerId === 'string') {
189163
try {
190164
peerId = PeerId.createFromB58String(peerId)
191165
} catch (err) {
192-
callback(err)
166+
return callback(err)
193167
}
194168
}
195169

@@ -198,9 +172,9 @@ module.exports = (self) => {
198172
if (err) {
199173
return callback(err)
200174
}
201-
callback(null, peerIds.map((id) => {
202-
return { ID: id.toB58String() }
203-
}))
175+
176+
// callback(null, peerIds)
177+
callback(null, peerIds.map((id) => new PeerInfo(id)))
204178
})
205179
})
206180
}

0 commit comments

Comments
 (0)