This repository was archived by the owner on Mar 10, 2020. It is now read-only.
File tree 5 files changed +103
-7
lines changed
5 files changed +103
-7
lines changed Original file line number Diff line number Diff line change 36
36
"debug" : " ^4.1.0" ,
37
37
"detect-node" : " ^2.0.4" ,
38
38
"end-of-stream" : " ^1.4.1" ,
39
+ "err-code" : " ^1.1.2" ,
39
40
"flatmap" : " 0.0.3" ,
40
41
"glob" : " ^7.1.3" ,
41
42
"ipfs-block" : " ~0.8.0" ,
85
86
"eslint-plugin-react" : " ^7.11.1" ,
86
87
"go-ipfs-dep" : " ~0.4.18" ,
87
88
"gulp" : " ^3.9.1" ,
88
- "interface-ipfs-core" : " ~0.84.3 " ,
89
+ "interface-ipfs-core" : " ipfs/interface-ipfs-core#fix/update-dht-responses " ,
89
90
"ipfsd-ctl" : " ~0.40.0" ,
90
91
"pull-stream" : " ^3.6.9" ,
91
92
"stream-equal" : " ^1.1.1"
Original file line number Diff line number Diff line change 2
2
3
3
const promisify = require ( 'promisify-es6' )
4
4
const streamToValue = require ( '../utils/stream-to-value' )
5
+ const streamToValueWithTransformer = require ( '../utils/stream-to-value-with-transformer' )
6
+
7
+ const errcode = require ( 'err-code' )
5
8
6
9
module . exports = ( send ) => {
7
10
return promisify ( ( peerId , opts , callback ) => {
@@ -17,10 +20,51 @@ module.exports = (send) => {
17
20
opts = { }
18
21
}
19
22
20
- send . andTransform ( {
23
+ const handleResult = ( res , callback ) => {
24
+ // Inconsistent return values in the browser
25
+ if ( Array . isArray ( res ) ) {
26
+ res = res [ 0 ]
27
+ }
28
+
29
+ // Type 2 keys
30
+ if ( res . Type !== 2 ) {
31
+ const errMsg = `key was not found (type 2)`
32
+
33
+ log . error ( errMsg )
34
+ return callback ( errcode ( new Error ( errMsg ) , 'ERR_KEY_TYPE_2_NOT_FOUND' ) )
35
+ }
36
+
37
+ const id = res . Responses [ 0 ] . ID
38
+ const addresses = res . Responses [ 0 ] . Addrs . map ( ( addr ) => {
39
+ // inconsistencies js / go - go does not add `/ipfs/{id}` to the address
40
+ if ( addr . split ( '/ipfs/' ) > - 1 ) {
41
+ return addr
42
+ } else {
43
+ return `${ addr } /ipfs/${ id } `
44
+ }
45
+ } )
46
+
47
+ const response = {
48
+ ...res ,
49
+ Responses : [ {
50
+ ID : id ,
51
+ Addrs : addresses
52
+ } ]
53
+ }
54
+
55
+ callback ( null , response )
56
+ }
57
+
58
+ send ( {
21
59
path : 'dht/findpeer' ,
22
60
args : peerId ,
23
61
qs : opts
24
- } , streamToValue , callback )
62
+ } , ( err , result ) => {
63
+ if ( err ) {
64
+ return callback ( err )
65
+ }
66
+
67
+ streamToValueWithTransformer ( result , handleResult , callback )
68
+ } )
25
69
} )
26
70
}
Original file line number Diff line number Diff line change @@ -17,10 +17,33 @@ module.exports = (send) => {
17
17
opts = { }
18
18
}
19
19
20
- send . andTransform ( {
20
+ const handleResult = ( res , callback ) => {
21
+ // Inconsistent return values in the browser vs node
22
+ if ( Array . isArray ( res ) ) {
23
+ res = res [ 0 ]
24
+ }
25
+
26
+ // Type 4 keys
27
+ if ( res . Type !== 4 ) {
28
+ const errMsg = `key was not found (type 4)`
29
+
30
+ log . error ( errMsg )
31
+ return callback ( errcode ( new Error ( errMsg ) , 'ERR_KEY_TYPE_4_NOT_FOUND' ) )
32
+ }
33
+
34
+ callback ( null , res )
35
+ }
36
+
37
+ send ( {
21
38
path : 'dht/findprovs' ,
22
39
args : cid ,
23
40
qs : opts
24
- } , streamToValue , callback )
41
+ } , ( err , result ) => {
42
+ if ( err ) {
43
+ return callback ( err )
44
+ }
45
+
46
+ streamToValueWithTransformer ( result , handleResult , callback )
47
+ } )
25
48
} )
26
49
}
Original file line number Diff line number Diff line change @@ -17,10 +17,20 @@ module.exports = (send) => {
17
17
opts = { }
18
18
}
19
19
20
- send . andTransform ( {
20
+ send ( {
21
21
path : 'dht/query' ,
22
22
args : peerId ,
23
23
qs : opts
24
- } , streamToValue , callback )
24
+ } , ( err , result ) => {
25
+ if ( err ) {
26
+ return callback ( err )
27
+ }
28
+
29
+ if ( typeof result . pipe === 'function' ) {
30
+ streamToValue ( result , callback )
31
+ } else {
32
+ callback ( null , result )
33
+ }
34
+ } )
25
35
} )
26
36
}
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const streamToValue = require ( './stream-to-value' )
4
+
5
+ function streamToValueWithTransformer ( response , transformer , callback ) {
6
+ if ( typeof response . pipe === 'function' ) {
7
+ streamToValue ( response , ( err , res ) => {
8
+ if ( err ) {
9
+ return callback ( err )
10
+ }
11
+ transformer ( res , callback )
12
+ } )
13
+ } else {
14
+ transformer ( response , callback )
15
+ }
16
+ }
17
+
18
+ module . exports = streamToValueWithTransformer
You can’t perform that action at this time.
0 commit comments