Unstoppable, scalable multiwriter Hyperbee.
Still experimental and under heavy development. Expect breaking changes.
npm install autobeeMultiple peers each write to their own local Hypercore. An apply function you provide merges those writes into a shared Hyperbee view deterministically. The view is consistent across all peers once they replicate.
const Autobee = require('autobee')
const Corestore = require('corestore')
const store = new Corestore('./my-db')
const db = new Autobee(store, null, { apply })
await db.ready()
// append some data
await db.append(Buffer.from(JSON.stringify({ hello: 'world' })))
// read it back from the view
const node = await db.view.get(Buffer.from('latest'))
console.log(JSON.parse(node.value))
async function apply(nodes, view, host) {
for (const node of nodes) {
const op = JSON.parse(node.value)
if (op.addWriter) host.addWriter(op.addWriter)
if (op.removeWriter) host.removeWriter(op.removeWriter)
const w = view.write()
w.tryPut(Buffer.from('latest'), node.value)
await w.flush()
}
}To add a second writer and replicate:
const db1 = new Autobee(store1, null, { apply })
await db1.ready()
// share db1.key with others so they can join
const db2 = new Autobee(store2, db1.key, { apply })
await db2.ready()
// db1 adds db2 as a writer
await db1.append(Buffer.from(JSON.stringify({ addWriter: db2.local.id })))
// replicate using any stream
const s1 = db1.replicate(true)
const s2 = db2.replicate(false)
s1.pipe(s2).pipe(s1)Create a new Autobee. store is a Corestore. key is the public key of an existing Autobee to join — omit or pass null to create a new one.
Options:
{
apply (nodes, view, host) {}, // called with batches of new nodes to apply to the view
open (bee, db) {}, // called to create a custom view, return it
close (view) {}, // called when the db closes
update (view, changes) {}, // called after apply when the view has been updated
encryptionKey: Buffer, // 32-byte key to encrypt all data at rest
encrypted: false, // set true if using encryptionKey
keyPair: { publicKey, secretKey }, // custom signing key pair for the local writer
optimistic: true // allow optimistic writes from unknown writers
}The public key of this Autobee. Share this with peers so they can join.
The discovery key. Use this to find peers on the network.
The public key encoded as a hex string.
The local writer Hypercore. Use db.local.key or db.local.id to identify this writer to others.
A read-only snapshot of the Hyperbee view. Updated after each apply cycle. Use the standard Hyperbee API to read from it.
Alias for db.view.
true if this instance has been added as a writer.
true if this writer is an indexer.
Append one or more values to the local writer. Triggers an apply cycle.
await db.append(Buffer.from('hello'))
await db.append([buf1, buf2, buf3])Optionally pass { optimistic: true } to write without waiting to be a confirmed writer.
await db.append(buf, { optimistic: true })Trigger a new apply cycle. Useful after replication to process new data.
Wait until the current apply cycle has finished.
Wait until all known writers have been fully indexed.
Create a replication stream. Pass true for the initiating side, false for the other.
const s1 = db1.replicate(true)
const s2 = db2.replicate(false)
s1.pipe(s2).pipe(s1)Hint that a new writer core is available at key with at least length entries. Used to wake up replication when you learn about a peer out of band.
Rotate the local writer to a different key. The new writer takes over as the active oplog.
Returns the current system and view core positions. Used for replication coordination.
Returns true if val is an Autobee instance.
The apply function is called with a batch of nodes from writers, a writable view (Hyperbee batch), and a host object.
async function apply(nodes, view, host) {
for (const node of nodes) {
// node.key — writer public key (Buffer)
// node.value — the value appended (Buffer)
// node.length — position in the writer's core
const op = JSON.parse(node.value)
// manage writers
if (op.addWriter) host.addWriter(op.addWriter)
if (op.removeWriter) host.removeWriter(op.removeWriter)
// write to the view
const w = view.write()
w.tryPut(Buffer.from('key'), node.value)
await w.flush()
}
}Add a writer by public key (Buffer or hex string). Options:
{
isIndexer: true // default
}Remove a writer by public key (Buffer or hex string).
Acknowledge a writer without changing their permissions.
Interrupt the current apply cycle. The db emits 'interrupt' with the reason. Useful for pausing apply while waiting on external data.
Create an anchor node. Returns { key, length }. Anchors are used to create a verifiable checkpoint in the log that can be used by future writers to prove causal ordering.
true if the system has not yet processed any nodes. Use this to bootstrap the first writer.
Pass an encryptionKey to encrypt all writer cores and the view at rest.
const db = new Autobee(store, null, {
apply,
encrypted: true,
encryptionKey: crypto.randomBytes(32)
})All peers must use the same encryption key.
Encode a value into an Autobee block with optional metadata.
Decode an Autobee block back to its value.
{ length: 0, key: null }. The empty head used to represent the genesis state.
Apache-2.0