Skip to content

Commit 6cd37ac

Browse files
authored
fix: implement .has method from the blockstore interface (#520)
Chains through to the wrapped blockstore.
1 parent eac64fd commit 6cd37ac

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@
208208
"iso-random-stream": "^2.0.0",
209209
"it-all": "^2.0.0",
210210
"it-drain": "^2.0.0",
211-
"libp2p": "next",
211+
"libp2p": "^0.42.0",
212212
"lodash.difference": "^4.5.0",
213213
"lodash.flatten": "^4.4.0",
214214
"lodash.range": "^3.2.0",

src/bitswap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,12 @@ export class Bitswap extends BaseBlockstore {
455455
unwrap () {
456456
return this.blockstore
457457
}
458+
459+
/**
460+
* @param {CID} cid
461+
* @returns {Promise<boolean>}
462+
*/
463+
has (cid) {
464+
return this.blockstore.has(cid)
465+
}
458466
}

test/bitswap.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ describe('start/stop', () => {
5353
})
5454
})
5555

56+
describe('blockstore', () => {
57+
it('should support .has', async () => {
58+
const [block] = await makeBlocks(1)
59+
const libp2p = {
60+
handle: () => {},
61+
unhandle: () => {},
62+
register: () => {},
63+
unregister: () => {},
64+
getConnections: () => []
65+
}
66+
// @ts-ignore not a full libp2p
67+
const bitswap = new Bitswap(libp2p, new MemoryBlockstore())
68+
await bitswap.start()
69+
70+
await expect(bitswap.has(block.cid)).to.eventually.be.false()
71+
72+
await bitswap.put(block.cid, block.data)
73+
74+
await expect(bitswap.has(block.cid)).to.eventually.be.true()
75+
76+
await bitswap.stop()
77+
})
78+
})
79+
5680
describe('bitswap without DHT', function () {
5781
this.timeout(20 * 1000)
5882

0 commit comments

Comments
 (0)