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

Commit 8490a7d

Browse files
committed
feat: bitswap support back again
1 parent 11aead3 commit 8490a7d

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ module.exports = class BlockService {
4646
putStream () {
4747
let ps
4848
if (this.isOnline()) {
49+
// NOTE: This will have to change in order for bitswap
50+
// to understand CID
4951
ps = this._bitswap.putStream()
5052
} else {
5153
ps = this._repo.blockstore.putStream()
@@ -76,7 +78,7 @@ module.exports = class BlockService {
7678

7779
getStream (cid) {
7880
if (this.isOnline()) {
79-
return this._bitswap.getStream(cid)
81+
return this._bitswap.getStream(cid.multihash)
8082
}
8183

8284
return this._repo.blockstore.getStream(cid.multihash)

test/block-service-test.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ module.exports = (repo) => {
192192
})
193193
})
194194

195-
describe.skip('online', () => {
195+
describe('online', () => {
196196
beforeEach(() => {
197197
bs = new BlockService(repo)
198198
})
@@ -203,16 +203,20 @@ module.exports = (repo) => {
203203
})
204204

205205
it('retrieves a block through bitswap', (done) => {
206+
// returns a block with a value equal to its key
206207
const bitswap = {
207208
getStream (key) {
208-
return pull.values([new Block(key)])
209+
return pull.values([
210+
new Block(key)
211+
])
209212
}
210213
}
214+
211215
bs.goOnline(bitswap)
212216

213-
bs.get('secret', (err, res) => {
217+
bs.get('secret', (err, block) => {
214218
expect(err).to.not.exist
215-
expect(res).to.be.eql(new Block('secret'))
219+
expect(block.data).to.be.eql(new Block('secret').data)
216220
done()
217221
})
218222
})
@@ -224,11 +228,18 @@ module.exports = (repo) => {
224228
}
225229
}
226230
bs.goOnline(bitswap)
227-
bs.put(new Block('secret sauce'), done)
231+
232+
const block = new Block('secret sauce')
233+
234+
bs.put({
235+
block: block,
236+
cid: new CID(block.key('sha2-256'))
237+
}, done)
228238
})
229239

230240
it('getStream through bitswap', (done) => {
231241
const b = new Block('secret sauce 1')
242+
const cid = new CID(b.key('sha2-256'))
232243

233244
const bitswap = {
234245
getStream () {
@@ -237,11 +248,13 @@ module.exports = (repo) => {
237248
}
238249

239250
bs.goOnline(bitswap)
251+
240252
pull(
241-
bs.getStream(b.key),
242-
pull.collect((err, res) => {
253+
bs.getStream(cid),
254+
pull.collect((err, blocks) => {
243255
expect(err).to.not.exist
244-
expect(res).to.be.eql([b])
256+
expect(blocks[0].data).to.be.eql(b.data)
257+
expect(blocks[0].key('sha2-256')).to.be.eql(cid.multihash)
245258
done()
246259
})
247260
)

0 commit comments

Comments
 (0)