forked from holepunchto/autobase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautobee-simple.js
40 lines (35 loc) · 981 Bytes
/
autobee-simple.js
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
const Hyperbee = require('hyperbee')
module.exports = class SimpleAutobee {
constructor (autobase, opts) {
this.autobase = autobase
this.autobase.start({
unwrap: true,
apply: applyAutobeeBatch,
view: core => new Hyperbee(core.unwrap(), {
...opts,
extension: false
})
})
this.bee = this.autobase.view
}
ready () {
return this.autobase.ready()
}
async put (key, value) {
const op = Buffer.from(JSON.stringify({ type: 'put', key, value }))
return await this.autobase.append(op)
}
async get (key) {
return await this.bee.get(key)
}
}
// A real apply function would need to handle conflicts, beyond last-one-wins.
async function applyAutobeeBatch (bee, batch) {
const b = bee.batch({ update: false })
for (const node of batch) {
const op = JSON.parse(node.value.toString())
// TODO: Handle deletions
if (op.type === 'put') await b.put(op.key, op.value)
}
await b.flush()
}