This repository was archived by the owner on Feb 12, 2024. It is now read-only.
File tree 6 files changed +20
-39
lines changed
packages/ipfs-http-client/src/dht
6 files changed +20
-39
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ const CID = require('cids')
5
5
const multiaddr = require ( 'multiaddr' )
6
6
const configure = require ( '../lib/configure' )
7
7
const toUrlSearchParams = require ( '../lib/to-url-search-params' )
8
+ const { FinalPeer } = require ( './response-types' )
8
9
9
10
module . exports = configure ( api => {
10
11
return async function findPeer ( peerId , options = { } ) {
@@ -18,11 +19,7 @@ module.exports = configure(api => {
18
19
} )
19
20
20
21
for await ( const data of res . ndjson ( ) ) {
21
- if ( data . Type === 3 ) {
22
- throw new Error ( data . Extra )
23
- }
24
-
25
- if ( data . Type === 2 && data . Responses ) {
22
+ if ( data . Type === FinalPeer && data . Responses ) {
26
23
const { ID , Addrs } = data . Responses [ 0 ]
27
24
return {
28
25
id : ID ,
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ const CID = require('cids')
4
4
const multiaddr = require ( 'multiaddr' )
5
5
const configure = require ( '../lib/configure' )
6
6
const toUrlSearchParams = require ( '../lib/to-url-search-params' )
7
+ const { Provider } = require ( './response-types' )
7
8
8
9
module . exports = configure ( api => {
9
10
return async function * findProvs ( cid , options = { } ) {
@@ -17,16 +18,7 @@ module.exports = configure(api => {
17
18
} )
18
19
19
20
for await ( const message of res . ndjson ( ) ) {
20
- // 3 = QueryError
21
- // https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
22
- // https://github.com/libp2p/go-libp2p-kad-dht/blob/master/routing.go#L525-L526
23
- if ( message . Type === 3 ) {
24
- throw new Error ( message . Extra )
25
- }
26
-
27
- // 4 = Provider
28
- // https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L20
29
- if ( message . Type === 4 && message . Responses ) {
21
+ if ( message . Type === Provider && message . Responses ) {
30
22
for ( const { ID , Addrs } of message . Responses ) {
31
23
yield {
32
24
id : ID ,
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ const { Buffer } = require('buffer')
4
4
const encodeBufferURIComponent = require ( '../lib/encode-buffer-uri-component' )
5
5
const configure = require ( '../lib/configure' )
6
6
const toUrlSearchParams = require ( '../lib/to-url-search-params' )
7
+ const { Value } = require ( './response-types' )
7
8
8
9
module . exports = configure ( api => {
9
10
return async function get ( key , options = { } ) {
@@ -21,16 +22,7 @@ module.exports = configure(api => {
21
22
} )
22
23
23
24
for await ( const message of res . ndjson ( ) ) {
24
- // 3 = QueryError
25
- // https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
26
- // https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L472-L473
27
- if ( message . Type === 3 ) {
28
- throw new Error ( message . Extra )
29
- }
30
-
31
- // 5 = Value
32
- // https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L21
33
- if ( message . Type === 5 ) {
25
+ if ( message . Type === Value ) {
34
26
return message . Extra
35
27
}
36
28
}
Original file line number Diff line number Diff line change @@ -20,13 +20,6 @@ module.exports = configure(api => {
20
20
} )
21
21
22
22
for await ( let message of res . ndjson ( ) ) {
23
- // 3 = QueryError
24
- // https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
25
- // https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L283-L284
26
- if ( message . Type === 3 ) {
27
- throw new Error ( message . Extra )
28
- }
29
-
30
23
message = toCamel ( message )
31
24
message . id = new CID ( message . id )
32
25
if ( message . responses ) {
Original file line number Diff line number Diff line change @@ -21,13 +21,6 @@ module.exports = configure(api => {
21
21
} )
22
22
23
23
for await ( let message of res . ndjson ( ) ) {
24
- // 3 = QueryError
25
- // https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L18
26
- // https://github.com/ipfs/go-ipfs/blob/eb11f569b064b960d1aba4b5b8ca155a3bd2cb21/core/commands/dht.go#L472-L473
27
- if ( message . Type === 3 ) {
28
- throw new Error ( message . Extra )
29
- }
30
-
31
24
message = toCamel ( message )
32
25
message . id = new CID ( message . id )
33
26
if ( message . responses ) {
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ // Response types are defined here:
4
+ // https://github.com/libp2p/go-libp2p-core/blob/6e566d10f4a5447317a66d64c7459954b969bdab/routing/query.go#L15-L24
5
+ module . exports = {
6
+ SendingQuery : 0 ,
7
+ PeerResponse : 1 ,
8
+ FinalPeer : 2 ,
9
+ QueryError : 3 ,
10
+ Provider : 4 ,
11
+ Value : 5 ,
12
+ AddingPeer : 6 ,
13
+ DialingPeer : 7
14
+ }
You can’t perform that action at this time.
0 commit comments