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

feat(block): make the block api follow the interface definition #356

Merged
merged 1 commit into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})
})
}
}