Skip to content

Commit a1b95fc

Browse files
authored
fix: identity supports async get and getAll (#365)
Mark get and getAll as async to allow delegating to async children.
1 parent da789f7 commit a1b95fc

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

packages/blockstore-core/src/identity.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NotFoundError } from 'interface-store'
22
import { BaseBlockstore } from './base.js'
33
import type { Blockstore, Pair } from 'interface-blockstore'
4-
import type { AbortOptions, Await, AwaitGenerator, AwaitIterable } from 'interface-store'
4+
import type { AbortOptions, Await, AwaitIterable } from 'interface-store'
55
import type { CID } from 'multiformats/cid'
66

77
// https://github.com/multiformats/multicodec/blob/d06fc6194710e8909bac64273c43f16b56ca4c34/table.csv#L2
@@ -45,7 +45,7 @@ export class IdentityBlockstore extends BaseBlockstore {
4545
return this.child.put(key, block, options)
4646
}
4747

48-
* get (key: CID, options?: AbortOptions): AwaitGenerator<Uint8Array> {
48+
async * get (key: CID, options?: AbortOptions): AsyncGenerator<Uint8Array> {
4949
if (key.multihash.code === IDENTITY_CODEC) {
5050
if (this.maxDigestLength != null && key.multihash.digest.byteLength > this.maxDigestLength) {
5151
throw new IdentityHashDigestTooLongError(`Identity digest too long - ${key.multihash.digest.byteLength} > this.maxDigestLength`)
@@ -97,7 +97,7 @@ export class IdentityBlockstore extends BaseBlockstore {
9797
}
9898
}
9999

100-
* getAll (options?: AbortOptions): AwaitGenerator<Pair> {
100+
async * getAll (options?: AbortOptions): AsyncGenerator<Pair> {
101101
if (this.child != null) {
102102
yield * this.child.getAll(options)
103103
}

packages/blockstore-core/test/identity.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { sha256 } from 'multiformats/hashes/sha2'
1111
import { IdentityBlockstore } from '../src/identity.js'
1212
import { MemoryBlockstore } from '../src/memory.js'
1313
import type { Blockstore } from 'interface-blockstore'
14+
import type { AbortOptions } from 'interface-store'
1415

1516
describe('identity', () => {
1617
let blockstore: Blockstore
@@ -86,6 +87,20 @@ describe('identity', () => {
8687
expect(toBuffer(await all(blockstore.get(cid)))).to.equalBytes(block)
8788
})
8889

90+
it('gets CIDs from child (async)', async () => {
91+
const block = Uint8Array.from([0, 1, 2, 3, 4])
92+
const multihash = await sha256.digest(block)
93+
const cid = CID.createV1(raw.code, multihash)
94+
95+
await child.put(cid, block)
96+
97+
const { get } = child
98+
child.get = async function * (key: CID, options: AbortOptions) { yield * get.bind(child)(key, options) }
99+
blockstore = new IdentityBlockstore(child)
100+
expect(blockstore.has(cid)).to.be.true()
101+
expect(toBuffer(await all(blockstore.get(cid)))).to.equalBytes(block)
102+
})
103+
89104
it('has CIDs from child', async () => {
90105
const block = Uint8Array.from([0, 1, 2, 3, 4])
91106
const multihash = await sha256.digest(block)
@@ -128,6 +143,24 @@ describe('identity', () => {
128143
expect(result[0].cid.toString()).to.equal(cid.toString())
129144
})
130145

146+
it('gets all pairs from child (async)', async () => {
147+
const block = Uint8Array.from([0, 1, 2, 3, 4])
148+
const multihash = await sha256.digest(block)
149+
const cid = CID.createV1(raw.code, multihash)
150+
151+
await child.put(cid, block)
152+
153+
const { getAll } = child
154+
child.getAll = async function * (options?: AbortOptions) { yield * getAll.bind(child)(options) }
155+
blockstore = new IdentityBlockstore(child)
156+
expect(blockstore.has(cid)).to.be.true()
157+
158+
const result = await all(blockstore.getAll())
159+
160+
expect(result).to.have.lengthOf(1)
161+
expect(result[0].cid.toString()).to.equal(cid.toString())
162+
})
163+
131164
it('should enforce a maximum digest size', async () => {
132165
blockstore = new IdentityBlockstore(child, {
133166
maxDigestLength: 5
@@ -141,7 +174,7 @@ describe('identity', () => {
141174
expect(blockstore.put(CID.createV1(raw.code, identity.digest(ok)), buf)).to.be.ok()
142175
expect(blockstore.has(CID.createV1(raw.code, identity.digest(ok)))).to.be.ok()
143176

144-
expect(() => all(blockstore.get(CID.createV1(raw.code, identity.digest(tooLong))))).to.throw()
177+
await expect(all(blockstore.get(CID.createV1(raw.code, identity.digest(tooLong))))).to.eventually.be.rejected()
145178
.with.property('name', 'IdentityHashDigestTooLongError')
146179

147180
expect(() => blockstore.put(CID.createV1(raw.code, identity.digest(tooLong)), buf)).to.throw()

0 commit comments

Comments
 (0)