1
1
'use strict'
2
2
3
3
const promisify = require ( 'promisify-es6' )
4
- const bl = require ( 'bl' )
5
4
const Block = require ( 'ipfs-block' )
6
5
const multihash = require ( 'multihashes' )
7
6
const CID = require ( 'cids' )
7
+ const streamToValue = require ( '../stream-to-value' )
8
8
9
9
module . exports = ( send ) => {
10
10
return {
@@ -21,25 +21,27 @@ module.exports = (send) => {
21
21
opts = { }
22
22
}
23
23
24
- return send ( {
25
- path : 'block/get' ,
26
- args : args ,
27
- qs : opts
28
- } , ( err , res ) => {
29
- if ( err ) {
30
- return callback ( err )
31
- }
24
+ // Transform the response from Buffer or a Stream to a Block
25
+ const transform = ( res , callback ) => {
32
26
if ( Buffer . isBuffer ( res ) ) {
33
27
callback ( null , new Block ( res ) )
34
28
} else {
35
- res . pipe ( bl ( ( err , data ) => {
29
+ streamToValue ( res , ( err , data ) => {
36
30
if ( err ) {
37
31
return callback ( err )
38
32
}
39
33
callback ( null , new Block ( data ) )
40
- } ) )
34
+ } )
41
35
}
42
- } )
36
+ }
37
+
38
+ const request = {
39
+ path : 'block/get' ,
40
+ args : args ,
41
+ qs : opts
42
+ }
43
+
44
+ send . andTransform ( request , transform , callback )
43
45
} ) ,
44
46
stat : promisify ( ( args , opts , callback ) => {
45
47
// TODO this needs to be adjusted with the new go-ipfs http-api
@@ -51,19 +53,22 @@ module.exports = (send) => {
51
53
callback = opts
52
54
opts = { }
53
55
}
54
- return send ( {
56
+
57
+ const request = {
55
58
path : 'block/stat' ,
56
59
args : args ,
57
60
qs : opts
58
- } , ( err , stats ) => {
59
- if ( err ) {
60
- return callback ( err )
61
- }
61
+ }
62
+
63
+ // Transform the response from { Key, Size } objects to { key, size } objects
64
+ const transform = ( stats , callback ) => {
62
65
callback ( null , {
63
66
key : stats . Key ,
64
67
size : stats . Size
65
68
} )
66
- } )
69
+ }
70
+
71
+ send . andTransform ( request , transform , callback )
67
72
} ) ,
68
73
put : promisify ( ( block , cid , callback ) => {
69
74
// TODO this needs to be adjusted with the new go-ipfs http-api
@@ -81,15 +86,15 @@ module.exports = (send) => {
81
86
block = block . data
82
87
}
83
88
84
- return send ( {
89
+ const request = {
85
90
path : 'block/put' ,
86
91
files : block
87
- } , ( err , blockInfo ) => {
88
- if ( err ) {
89
- return callback ( err )
90
- }
91
- callback ( null , new Block ( block ) )
92
- } )
92
+ }
93
+
94
+ // Transform the response to a Block
95
+ const transform = ( blockInfo , callback ) => callback ( null , new Block ( block ) )
96
+
97
+ send . andTransform ( request , transform , callback )
93
98
} )
94
99
}
95
100
}
0 commit comments