diff --git a/package.json b/package.json index a1b596c0c7..e3dbac6be9 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "detect-node": "^2.0.3", "flatmap": "0.0.3", "glob": "^7.0.5", + "ipfs-block": "^0.3.0", "ipfs-merkle-dag": "^0.6.0", "is-ipfs": "^0.2.0", "isstream": "^0.1.2", @@ -47,7 +48,7 @@ "chai": "^3.5.0", "gulp": "^3.9.1", "hapi": "^14.1.0", - "interface-ipfs-core": "^0.13.0", + "interface-ipfs-core": "^0.14.0", "ipfsd-ctl": "^0.14.0", "pre-commit": "^1.1.3", "socket.io": "^1.4.8", @@ -99,4 +100,4 @@ "url": "https://github.com/ipfs/js-ipfs-api/issues" }, "homepage": "https://github.com/ipfs/js-ipfs-api" -} \ No newline at end of file +} diff --git a/src/api/block.js b/src/api/block.js index 563a7bc6a3..20a6968fd1 100644 --- a/src/api/block.js +++ b/src/api/block.js @@ -1,6 +1,8 @@ 'use strict' const promisify = require('promisify-es6') +const bl = require('bl') +const Block = require('ipfs-block') module.exports = (send) => { return { @@ -13,7 +15,17 @@ module.exports = (send) => { path: 'block/get', args: args, qs: opts - }, callback) + }, (err, res) => { + if (err) { + return callback(err) + } + res.pipe(bl((err, data) => { + if (err) { + return callback(err) + } + callback(null, new Block(data)) + })) + }) }), stat: promisify((args, opts, callback) => { if (typeof (opts) === 'function') { @@ -24,18 +36,35 @@ module.exports = (send) => { path: 'block/stat', args: args, qs: opts - }, callback) + }, (err, stats) => { + if (err) { + return callback(err) + } + callback(null, { + key: stats.Key, + size: stats.Size + }) + }) }), - put: promisify((file, callback) => { - if (Array.isArray(file)) { + put: promisify((block, callback) => { + if (Array.isArray(block)) { const err = new Error('block.put() only accepts 1 file') return callback(err) } + if (typeof block === 'object' && block.data) { + block = block.data + } + return send({ path: 'block/put', - files: file - }, callback) + files: block + }, (err, blockInfo) => { + if (err) { + return callback(err) + } + callback(null, new Block(block)) + }) }) } }