Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: async .key
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 2, 2016
1 parent 9358c42 commit 2d2185b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
7 changes: 4 additions & 3 deletions src/cli/commands/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ function addBlock (buf) {

waterfall([
(cb) => Block.create(buf, cb),
(block, cb) => ipfs.block.put(block, cb)
], (err, block) => {
(block, cb) => ipfs.block.put(block, cb),
(block, cb) => block.key(cb)
], (err, key) => {
if (err) {
throw err
}

console.log(mh.toB58String(block.key()))
console.log(mh.toB58String(key))
})
})
}
Expand Down
26 changes: 18 additions & 8 deletions src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Block = require('ipfs-block')
const multihash = require('multihashes')
const CID = require('cids')
const waterfall = require('async/waterfall')

module.exports = function block (self) {
return {
Expand All @@ -17,21 +18,30 @@ module.exports = function block (self) {

if (Buffer.isBuffer(block)) {
block = new Block(block)
return
}

if (typeof cid === 'function') {
// legacy (without CID)
callback = cid
cid = new CID(block.key('sha2-256'))
cid === undefined
}

self._blockService.put({
block: block,
cid: cid
}, (err) => {
callback(err, block)
})
waterfall([
(cb) => {
if (cid) {
return cb(null, cid)
}

block.key('sha2-256', (err, key) => {
if (err) {
return cb(err)
}

cb(null, new CID(key))
})
},
(cid, cb) => self._blockService.put({block: block, cid: cid})
], callback)
},
rm: (cid, callback) => {
cid = cleanCid(cid)
Expand Down
14 changes: 10 additions & 4 deletions src/http-api/resources/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ exports.put = {

waterfall([
(cb) => Block.create(data, cb),
(block, cb) => ipfs.block.put(block, cb)
], (err, block) => {
(block, cb) => ipfs.block.put(block, cb),
(block, cb) => block.key('sha2-256', (err, key) => {
if (err) {
return cb(err)
}
cb(null, [key, block])
})
], (err, res) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -96,8 +102,8 @@ exports.put = {
}

return reply({
Key: mh.toB58String(block.key('sha2-256')),
Size: block.data.length
Key: mh.toB58String(res[0]),
Size: res[1].data.length
})
})
}
Expand Down
11 changes: 8 additions & 3 deletions test/core/both/test-bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const _ = require('lodash')
const series = require('async/series')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const map = require('async/map')
const leftPad = require('left-pad')
const Block = require('ipfs-block')
const mh = require('multihashes')
Expand Down Expand Up @@ -126,7 +127,8 @@ describe.skip('bitswap', () => {
cb()
},
(cb) => remoteNode.block.put(block, cb),
(cb) => inProcNode.block.get(block.key('sha2-256'), cb),
(cb) => block.key('sha2-256', cb),
(key, cb) => inProcNode.block.get(key, cb),
(b, cb) => {
expect(b.data).to.be.eql(block.data)
cb()
Expand All @@ -145,8 +147,11 @@ describe.skip('bitswap', () => {
(cb) => parallel(_.range(6).map((i) => makeBlock), (err, _blocks) => {
expect(err).to.not.exist
blocks = _blocks
keys = blocks.map((b) => b.key('sha2-256'))
cb()
map(blocks, (b, cb) => b.key('sha2-256', cb), (err, res) => {
expect(err).to.not.exist
keys = res
cb()
})
}),
(cb) => addNode(8, (err, _ipfs) => {
remoteNodes.push(_ipfs)
Expand Down
13 changes: 8 additions & 5 deletions test/http-api/ipfs-api/test-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const expect = require('chai').expect
const multihash = require('multihashes')
const waterfall = require('async/waterfall')

module.exports = (ctl) => {
describe('.block', () => {
Expand All @@ -14,11 +15,13 @@ module.exports = (ctl) => {
size: 12
}

ctl.block.put(data, (err, block) => {
expect(err).not.to.exist
expect(block.key()).to.deep.equal(multihash.fromB58String(expectedResult.key))
done()
})
waterfall([
(cb) => ctl.block.put(data, cb),
(block, cb) => block.key(cb),
(key, cb) => {
expect(key).to.deep.equal(multihash.fromB58String(expectedResult.key))
}
], done)
})
})

Expand Down

0 comments on commit 2d2185b

Please sign in to comment.