11'use strict'
22
33const promisify = require ( 'promisify-es6' )
4- const bl = require ( 'bl' )
54const Block = require ( 'ipfs-block' )
65const multihash = require ( 'multihashes' )
76const CID = require ( 'cids' )
7+ const streamToValue = require ( '../stream-to-value' )
88
99module . exports = ( send ) => {
1010 return {
@@ -21,25 +21,27 @@ module.exports = (send) => {
2121 opts = { }
2222 }
2323
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 ) => {
3226 if ( Buffer . isBuffer ( res ) ) {
3327 callback ( null , new Block ( res ) )
3428 } else {
35- res . pipe ( bl ( ( err , data ) => {
29+ streamToValue ( res , ( err , data ) => {
3630 if ( err ) {
3731 return callback ( err )
3832 }
3933 callback ( null , new Block ( data ) )
40- } ) )
34+ } )
4135 }
42- } )
36+ }
37+
38+ const request = {
39+ path : 'block/get' ,
40+ args : args ,
41+ qs : opts
42+ }
43+
44+ send . andTransform ( request , transform , callback )
4345 } ) ,
4446 stat : promisify ( ( args , opts , callback ) => {
4547 // TODO this needs to be adjusted with the new go-ipfs http-api
@@ -51,19 +53,22 @@ module.exports = (send) => {
5153 callback = opts
5254 opts = { }
5355 }
54- return send ( {
56+
57+ const request = {
5558 path : 'block/stat' ,
5659 args : args ,
5760 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 ) => {
6265 callback ( null , {
6366 key : stats . Key ,
6467 size : stats . Size
6568 } )
66- } )
69+ }
70+
71+ send . andTransform ( request , transform , callback )
6772 } ) ,
6873 put : promisify ( ( block , cid , callback ) => {
6974 // TODO this needs to be adjusted with the new go-ipfs http-api
@@ -81,15 +86,15 @@ module.exports = (send) => {
8186 block = block . data
8287 }
8388
84- return send ( {
89+ const request = {
8590 path : 'block/put' ,
8691 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 )
9398 } )
9499 }
95100}
0 commit comments