Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #356 from ipfs/feat/block-api-fix
Browse files Browse the repository at this point in the history
feat(block): make the block api follow the interface definition
  • Loading branch information
daviddias committed Aug 24, 2016
2 parents b24dab8 + a4e43e8 commit f58cf1e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -99,4 +100,4 @@
"url": "https://github.com/ipfs/js-ipfs-api/issues"
},
"homepage": "https://github.com/ipfs/js-ipfs-api"
}
}
41 changes: 35 additions & 6 deletions src/api/block.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const promisify = require('promisify-es6')
const bl = require('bl')
const Block = require('ipfs-block')

module.exports = (send) => {
return {
Expand All @@ -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') {
Expand All @@ -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))
})
})
}
}

0 comments on commit f58cf1e

Please sign in to comment.