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

Commit

Permalink
feat(block): make the block api follow the interface definition
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Aug 24, 2016
1 parent b24dab8 commit a4e43e8
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 a4e43e8

Please sign in to comment.