Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 5252f50

Browse files
author
Alan Shaw
authored
fix: response for findpeer and findprovs (#1039)
* fix: response for findpeer and findprovs Pick out the correct item from the response, do not assume the first is the one we want. License: MIT Signed-off-by: Alan Shaw <[email protected]> * fix: allow CID instance to be passed License: MIT Signed-off-by: Alan Shaw <[email protected]> * docs: add comments for magic Type numbers License: MIT Signed-off-by: Alan Shaw <[email protected]> * fix: reinstate not found error License: MIT Signed-off-by: Alan Shaw <[email protected]> * fix: module name * fix: add skip for dht.findprovs timeout test Go IPFS does not implement this option and the error that was being checked for was a false positive - it was an error to report no providers were found, which is not an error just a fact and this PR fixes this by removing that error and thus causing this test to now fail. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 1a8bcdd commit 5252f50

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

src/dht/findpeer.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ module.exports = (send) => {
2525
const handleResult = (res, callback) => {
2626
// Inconsistent return values in the browser
2727
if (Array.isArray(res)) {
28-
res = res[0]
28+
res = res.find(r => r.Type === 2)
2929
}
3030

3131
// Type 2 keys
32-
if (res.Type !== 2) {
33-
const errMsg = `key was not found (type 2)`
34-
35-
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_2_NOT_FOUND'))
32+
// 2 = FinalPeer
33+
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
34+
if (!res || res.Type !== 2) {
35+
const errMsg = `key was not found (type 4)`
36+
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
3637
}
3738

3839
const responseReceived = res.Responses[0]
@@ -49,7 +50,7 @@ module.exports = (send) => {
4950

5051
send({
5152
path: 'dht/findpeer',
52-
args: peerId,
53+
args: peerId.toString(),
5354
qs: opts
5455
}, (err, result) => {
5556
if (err) {

src/dht/findprovs.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const streamToValueWithTransformer = require('../utils/stream-to-value-with-tran
66
const multiaddr = require('multiaddr')
77
const PeerId = require('peer-id')
88
const PeerInfo = require('peer-info')
9-
const errcode = require('err-code')
109

1110
module.exports = (send) => {
1211
return promisify((cid, opts, callback) => {
@@ -25,20 +24,14 @@ module.exports = (send) => {
2524
const handleResult = (res, callback) => {
2625
// Inconsistent return values in the browser vs node
2726
if (Array.isArray(res)) {
28-
res = res[0]
27+
res = res.find(r => r.Type === 4)
2928
}
3029

3130
// callback with an empty array if no providers are found
32-
if (!res) {
33-
const responses = []
34-
return callback(null, responses)
35-
}
36-
37-
// Type 4 keys
38-
if (res.Type !== 4) {
39-
const errMsg = `key was not found (type 4)`
40-
41-
return callback(errcode(new Error(errMsg), 'ERR_KEY_TYPE_4_NOT_FOUND'))
31+
// 4 = Provider
32+
// https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L20
33+
if (!res || res.Type !== 4) {
34+
return callback(null, [])
4235
}
4336

4437
const responses = res.Responses.map((r) => {
@@ -60,7 +53,7 @@ module.exports = (send) => {
6053

6154
send({
6255
path: 'dht/findprovs',
63-
args: cid,
56+
args: cid.toString(),
6457
qs: opts
6558
}, (err, result) => {
6659
if (err) {

test/interface.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ describe('interface-ipfs-core tests', () => {
9292
name: 'should provide from one node and find it through another node',
9393
reason: 'FIXME go-ipfs endpoint doesn\'t conform with the others https://github.com/ipfs/go-ipfs/issues/5047'
9494
},
95+
{
96+
name: 'should take options to override timeout config',
97+
reason: 'FIXME go-ipfs does not support a timeout option'
98+
},
9599
// dht.get
96100
{
97101
name: 'should get a value after it was put on another node',

0 commit comments

Comments
 (0)