forked from storacha/ipfs-car
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.ts
43 lines (33 loc) · 924 Bytes
/
memory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { CID } from 'multiformats'
import { BaseBlockstore } from 'blockstore-core'
import { Blockstore } from './index'
export class MemoryBlockStore extends BaseBlockstore implements Blockstore {
store: Map<string, Uint8Array>
constructor () {
super()
this.store = new Map()
}
async * blocks () {
for (const [cidStr, bytes] of this.store.entries()) {
yield { cid: CID.parse(cidStr), bytes }
}
}
put (cid: CID, bytes: Uint8Array) {
this.store.set(cid.toString(), bytes)
return Promise.resolve()
}
get (cid: CID) : Promise<Uint8Array> {
const bytes = this.store.get(cid.toString())
if (!bytes) {
throw new Error(`block with cid ${cid.toString()} no found`)
}
return Promise.resolve(bytes)
}
has (cid: CID) {
return Promise.resolve(this.store.has(cid.toString()))
}
close () {
this.store.clear()
return Promise.resolve()
}
}