diff --git a/README.md b/README.md index 4d8bc5a..3317b99 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,18 @@ If the block could not be found, expect `err.code` to be `'ENOENT'`. Asynchronously returns the blocks whose content multihashes match the array `multihashes`. -Returns an error (`err.code === 'ENOENT'`) if *any* the blocks do not exist. + +`blocks` is an object that maps each `multihash` to an object of the form + +```js +{ + err: Error + block: Block +} +``` + +Expect `blocks[multihash].err.code === 'ENOENT'` and `blocks[multihash].block +=== null` if a block did not exist. *Does not guarantee atomicity.* diff --git a/src/block-service.js b/src/block-service.js index e4b6365..1f89101 100644 --- a/src/block-service.js +++ b/src/block-service.js @@ -52,18 +52,18 @@ function BlockService (ipfsRepo, exchange) { return callback(new Error('Invalid batch of multihashes')) } - const blocks = [] + var results = {} async.each(multihashes, (multihash, next) => { this.getBlock(multihash, extension, (err, block) => { - if (err) { return next(err) } - if (block) { - blocks.push(block) + results[multihash] = { + err: err, + block: block } next() }) }, (err) => { - callback(err, blocks) + callback(err, results) }) } diff --git a/test/block-service-test.js b/test/block-service-test.js index e0c5ba9..880816a 100644 --- a/test/block-service-test.js +++ b/test/block-service-test.js @@ -93,7 +93,16 @@ module.exports = (repo) => { bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => { expect(err).to.not.exist - expect(blocks).to.have.lengthOf(3) + expect(Object.keys(blocks)).to.have.lengthOf(3) + expect(blocks[b1.key]).to.exist + expect(blocks[b1.key].err).to.not.exist + expect(blocks[b1.key].block.data).to.deep.equal(b1.data) + expect(blocks[b2.key]).to.exist + expect(blocks[b2.key].err).to.not.exist + expect(blocks[b2.key].block.data).to.deep.equal(b2.data) + expect(blocks[b3.key]).to.exist + expect(blocks[b3.key].err).to.not.exist + expect(blocks[b3.key].block.data).to.deep.equal(b3.data) done() }) }) @@ -108,8 +117,17 @@ module.exports = (repo) => { expect(err).to.not.exist bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => { - expect(err).to.exist - expect(blocks).to.have.length.below(3) + expect(err).to.not.exist + expect(Object.keys(blocks)).to.have.lengthOf(3) + expect(blocks[b1.key]).to.exist + expect(blocks[b1.key].err).to.not.exist + expect(blocks[b1.key].block.data).to.deep.equal(b1.data) + expect(blocks[b2.key]).to.exist + expect(blocks[b2.key].err).to.exist + expect(blocks[b2.key].block).to.not.exist + expect(blocks[b3.key]).to.exist + expect(blocks[b3.key].err).to.not.exist + expect(blocks[b3.key].block.data).to.deep.equal(b3.data) done() }) })