multi-hypercore
multi-writer hypercore
Small module that manages multiple hypercores: feeds you create locally are writeable, others' are readonly. Replicating with another multi-hypercore peers exchanges the content of all of the hypercores.
Usage
var multicore = var hypercore = var ram = var multi = // a multi-hypercore starts off emptyconsole // => 0 // create as many writeable feeds as you want; returns hypercoresmulti { var r = a r }
API
var multicore =
var multi = multicore(hypercore, storage[, opts])
Pass in the a hypercore module (require('hypercore')
), a
random-access-storage
backend, and options. Included opts
are passed into new hypercores created,
and are the same as
hypercore's.
multi.writer(cb)
If no name
is given, a new local writeable feed is created and returned via
cb
.
If name
is given and was created in the past on this local machine, it is
returned. Otherwise it is created. This is useful for managing multiple local
feeds, e.g.
var main = multi // created if doesn't existvar content = multi // created if doesn't exist main === multi // => true
var feeds = multi.feeds()
An array of all hypercores in the multi-hypercore. Check a feed's key
to
find the one you want, or check its writable
/ readable
properties.
Only populated once multi.ready(fn)
is fired.
var feed = multi.feed(key)
Fetch a feed by its key key
(a Buffer
).
var stream = multi.replicate([opts])
Create a duplex stream for replication.
Works just like hypercore, except all local hypercores are exchanged between replication endpoints.
multi.on('feed', function (feed, name) { ... })
Emitted whenever a new feed is added, whether locally or remotely.
Install
With npm installed, run
$ npm install multi-hypercore
Hacks
hypercore-protocol
requires the first feed exchanged to be common between replicating peers. This prevents two strangers from exchanging sets of hypercores. A "fake" hypercore with a hardcoded public key is included in the code to bootstrap the replication process. I discarded the private key, but even if I didn't, it doesn't let me do anything nefarious. You could patch this with your own key of choice.hypercore-protocol
requires all feed keys be known upfront: only discovery keys are exchanged (discoveryKey = hash(key)
), so this module wraps the hypercore replication duplex stream in a secondary duplex stream that exchanges feed public keys upfront before moving on to the hypercore replication mechanism.
License
ISC