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

Commit bbec147

Browse files
committed
feat: CID support on put and putStream
1 parent be318f5 commit bbec147

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,41 @@ module.exports = class BlockService {
2424
return this._bitswap != null
2525
}
2626

27-
put (block, callback) {
27+
// Note: we have to pass the CID, so that bitswap can then use it for
28+
// the smart selectors. For now, passing the CID is used so that we know
29+
// the right multihash, this means that now we have multiple hashes official
30+
// support \o/
31+
put (blockAndCID, callback) {
2832
callback = callback || (() => {})
29-
if (!block) {
30-
return callback(new Error('Missing block'))
33+
if (!blockAndCID) {
34+
return callback(new Error('Missing block and CID'))
3135
}
3236

3337
pull(
3438
pull.values([
35-
block
39+
blockAndCID
3640
]),
3741
this.putStream(),
3842
pull.onEnd(callback)
3943
)
4044
}
4145

4246
putStream () {
47+
let ps
4348
if (this.isOnline()) {
44-
return this._bitswap.putStream()
49+
ps = this._bitswap.putStream()
50+
} else {
51+
ps = this._repo.blockstore.putStream()
4552
}
4653

4754
return pull(
48-
pull.map((block) => {
49-
return { data: block.data, key: block.key() }
55+
pull.map((blockAndCID) => {
56+
return {
57+
data: blockAndCID.block.data,
58+
key: blockAndCID.cid.multihash
59+
}
5060
}),
51-
this._repo.blockstore.putStream()
61+
ps
5262
)
5363
}
5464

test/block-service-test.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = (repo) => {
2424
const cid = new CID(b.key())
2525

2626
series([
27-
(cb) => bs.put(b, cb),
27+
(cb) => bs.put({ block: b, cid: cid }, cb),
2828
(cb) => bs.get(cid, (err, block) => {
2929
if (err) {
3030
return cb(err)
@@ -52,9 +52,9 @@ module.exports = (repo) => {
5252

5353
pull(
5454
pull.values([
55-
b1,
56-
b2,
57-
b3
55+
{ block: b1, cid: new CID(b1.key()) },
56+
{ block: b2, cid: new CID(b2.key()) },
57+
{ block: b3, cid: new CID(b3.key()) }
5858
]),
5959
bs.putStream(),
6060
pull.collect((err, meta) => {
@@ -72,9 +72,9 @@ module.exports = (repo) => {
7272

7373
pull(
7474
pull.values([
75-
b1,
76-
b2,
77-
b3
75+
{ block: b1, cid: new CID(b1.key()) },
76+
{ block: b2, cid: new CID(b2.key()) },
77+
{ block: b3, cid: new CID(b3.key()) }
7878
]),
7979
bs.putStream(),
8080
pull.onEnd((err) => {
@@ -110,7 +110,7 @@ module.exports = (repo) => {
110110

111111
it('delete a block', (done) => {
112112
const b = new Block('Will not live that much')
113-
bs.put(b, (err) => {
113+
bs.put({ block: b, cid: new CID(b.key()) }, (err) => {
114114
expect(err).to.not.exist
115115
const cid = new CID(b.key())
116116
bs.delete(cid, (err) => {
@@ -156,6 +156,9 @@ module.exports = (repo) => {
156156

157157
pull(
158158
pull.values(blocks),
159+
pull.map((block) => {
160+
return { block: block, cid: new CID(block.key()) }
161+
}),
159162
bs.putStream(),
160163
pull.onEnd((err) => {
161164
expect(err).to.not.exist

0 commit comments

Comments
 (0)