This repository was archived by the owner on Mar 10, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +104
-9
lines changed Expand file tree Collapse file tree 5 files changed +104
-9
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 1
1
'use strict'
2
2
3
3
const promisify = require ( 'promisify-es6' )
4
- const streamToValue = require ( '../utils/stream-to-value' )
4
+ const streamToValueWithTransformer = require ( '../utils/stream-to-value-with-transformer' )
5
+
6
+ const errcode = require ( 'err-code' )
5
7
6
8
module . exports = ( send ) => {
7
9
return promisify ( ( peerId , opts , callback ) => {
@@ -17,10 +19,50 @@ module.exports = (send) => {
17
19
opts = { }
18
20
}
19
21
20
- send . andTransform ( {
22
+ const handleResult = ( res , callback ) => {
23
+ // Inconsistent return values in the browser
24
+ if ( Array . isArray ( res ) ) {
25
+ res = res [ 0 ]
26
+ }
27
+
28
+ // Type 2 keys
29
+ if ( res . Type !== 2 ) {
30
+ const errMsg = `key was not found (type 2)`
31
+
32
+ return callback ( errcode ( new Error ( errMsg ) , 'ERR_KEY_TYPE_2_NOT_FOUND' ) )
33
+ }
34
+
35
+ const id = res . Responses [ 0 ] . ID
36
+ const addresses = res . Responses [ 0 ] . Addrs . map ( ( addr ) => {
37
+ // inconsistencies js / go - go does not add `/ipfs/{id}` to the address
38
+ if ( addr . split ( '/ipfs/' ) > - 1 ) {
39
+ return addr
40
+ } else {
41
+ return `${ addr } /ipfs/${ id } `
42
+ }
43
+ } )
44
+
45
+ const response = {
46
+ ...res ,
47
+ Responses : [ {
48
+ ID : id ,
49
+ Addrs : addresses
50
+ } ]
51
+ }
52
+
53
+ callback ( null , response )
54
+ }
55
+
56
+ send ( {
21
57
path : 'dht/findpeer' ,
22
58
args : peerId ,
23
59
qs : opts
24
- } , streamToValue , callback )
60
+ } , ( err , result ) => {
61
+ if ( err ) {
62
+ return callback ( err )
63
+ }
64
+
65
+ streamToValueWithTransformer ( result , handleResult , callback )
66
+ } )
25
67
} )
26
68
}
Original file line number Diff line number Diff line change 1
1
'use strict'
2
2
3
3
const promisify = require ( 'promisify-es6' )
4
- const streamToValue = require ( '../utils/stream-to-value' )
4
+ const streamToValueWithTransformer = require ( '../utils/stream-to-value-with-transformer' )
5
+
6
+ const errcode = require ( 'err-code' )
5
7
6
8
module . exports = ( send ) => {
7
9
return promisify ( ( cid , opts , callback ) => {
@@ -17,10 +19,32 @@ module.exports = (send) => {
17
19
opts = { }
18
20
}
19
21
20
- send . andTransform ( {
22
+ const handleResult = ( res , callback ) => {
23
+ // Inconsistent return values in the browser vs node
24
+ if ( Array . isArray ( res ) ) {
25
+ res = res [ 0 ]
26
+ }
27
+
28
+ // Type 4 keys
29
+ if ( res . Type !== 4 ) {
30
+ const errMsg = `key was not found (type 4)`
31
+
32
+ return callback ( errcode ( new Error ( errMsg ) , 'ERR_KEY_TYPE_4_NOT_FOUND' ) )
33
+ }
34
+
35
+ callback ( null , res )
36
+ }
37
+
38
+ send ( {
21
39
path : 'dht/findprovs' ,
22
40
args : cid ,
23
41
qs : opts
24
- } , streamToValue , callback )
42
+ } , ( err , result ) => {
43
+ if ( err ) {
44
+ return callback ( err )
45
+ }
46
+
47
+ streamToValueWithTransformer ( result , handleResult , callback )
48
+ } )
25
49
} )
26
50
}
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