Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 58f1191

Browse files
committed
Updates getBlocks API.
1 parent 6779e25 commit 58f1191

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,18 @@ If the block could not be found, expect `err.code` to be `'ENOENT'`.
157157

158158
Asynchronously returns the blocks whose content multihashes match the array
159159
`multihashes`.
160-
Returns an error (`err.code === 'ENOENT'`) if *any* the blocks do not exist.
160+
161+
`blocks` is an object that maps each `multihash` to an object of the form
162+
163+
```js
164+
{
165+
err: Error
166+
block: Block
167+
}
168+
```
169+
170+
Expect `blocks[multihash].err.code === 'ENOENT'` and `blocks[multihash].block
171+
=== null` if a block did not exist.
161172

162173
*Does not guarantee atomicity.*
163174

src/block-service.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ function BlockService (ipfsRepo, exchange) {
5252
return callback(new Error('Invalid batch of multihashes'))
5353
}
5454

55-
const blocks = []
55+
var results = {}
5656

5757
async.each(multihashes, (multihash, next) => {
5858
this.getBlock(multihash, extension, (err, block) => {
59-
if (err) { return next(err) }
60-
if (block) {
61-
blocks.push(block)
59+
results[multihash] = {
60+
err: err,
61+
block: block
6262
}
6363
next()
6464
})
6565
}, (err) => {
66-
callback(err, blocks)
66+
callback(err, results)
6767
})
6868
}
6969

test/block-service-test.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,16 @@ module.exports = (repo) => {
9393

9494
bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
9595
expect(err).to.not.exist
96-
expect(blocks).to.have.lengthOf(3)
96+
expect(Object.keys(blocks)).to.have.lengthOf(3)
97+
expect(blocks[b1.key]).to.exist
98+
expect(blocks[b1.key].err).to.not.exist
99+
expect(blocks[b1.key].block.data).to.deep.equal(b1.data)
100+
expect(blocks[b2.key]).to.exist
101+
expect(blocks[b2.key].err).to.not.exist
102+
expect(blocks[b2.key].block.data).to.deep.equal(b2.data)
103+
expect(blocks[b3.key]).to.exist
104+
expect(blocks[b3.key].err).to.not.exist
105+
expect(blocks[b3.key].block.data).to.deep.equal(b3.data)
97106
done()
98107
})
99108
})
@@ -108,8 +117,17 @@ module.exports = (repo) => {
108117
expect(err).to.not.exist
109118

110119
bs.getBlocks([b1.key, b2.key, b3.key], (err, blocks) => {
111-
expect(err).to.exist
112-
expect(blocks).to.have.length.below(3)
120+
expect(err).to.not.exist
121+
expect(Object.keys(blocks)).to.have.lengthOf(3)
122+
expect(blocks[b1.key]).to.exist
123+
expect(blocks[b1.key].err).to.not.exist
124+
expect(blocks[b1.key].block.data).to.deep.equal(b1.data)
125+
expect(blocks[b2.key]).to.exist
126+
expect(blocks[b2.key].err).to.exist
127+
expect(blocks[b2.key].block).to.not.exist
128+
expect(blocks[b3.key]).to.exist
129+
expect(blocks[b3.key].err).to.not.exist
130+
expect(blocks[b3.key].block.data).to.deep.equal(b3.data)
113131
done()
114132
})
115133
})

0 commit comments

Comments
 (0)