Skip to content

Instantly share code, notes, and snippets.

@HDegroote
Created July 25, 2022 18:24
Show Gist options
  • Save HDegroote/d1b892153aebde299a254f095dc60aa0 to your computer and use it in GitHub Desktop.
Save HDegroote/d1b892153aebde299a254f095dc60aa0 to your computer and use it in GitHub Desktop.
Replicate corestores example
// Tested on Hyperswarm V4, corestore V6, Hypercore V10
// Server
const Hyperswarm = require('hyperswarm')
const Corestore = require("corestore")
const ram = require("random-access-memory")
async function runServer(){
const corestore = new Corestore(ram)
await corestore.ready()
const core = corestore.get({name: "my core"})
const core2 = corestore.get({name: "my core2"})
await core.ready()
while (core.length < 25) {
await core.append(`core1--block #${core.length}`)
await core2.append(`core2--block #${core2.length}`)
}
// Replicate the corestore
const swarm = new Hyperswarm()
swarm.on('connection', socket => corestore.replicate(socket))
// The corestore can replicate all the cores it contains
// when connected with a peer who knows their key.
// However, the peers first need to be connected.
// In this case we do this by joining the swarm on the
// keys we host, so the client can find us
swarm.join(core.discoveryKey, { server: true, client: false })
// Note that for this demo, it actually suffices to join
// the swarm only on the first key, as once the client
// connected on the first core, the server can also share
// the second core with the client.
// But for generality's sake, we're joining on both
swarm.join(core2.discoveryKey, { server: true, client: false })
await swarm.flush()
console.log('Serving core1: ', core.key.toString('hex'))
console.log('Serving core2: ', core2.key.toString('hex'))
}
runServer()
// Client
const Hyperswarm = require('hyperswarm')
const Corestore = require("corestore")
const ram = require("random-access-memory")
const { once } = require("events");
const KEY1 = // Key for core 1 (as str)
const KEY2 = // Key for core 2 (as str)
async function runClient(){
const corestore = new Corestore(ram)
const core1 = corestore.get({
key: Buffer.from(KEY1, "hex"),
valueEncoding: "utf-8"
})
const core2 = corestore.get(
{key: Buffer.from(KEY2, "hex"),
valueEncoding: "utf-8"
})
const swarm = new Hyperswarm()
swarm.on('connection', socket => corestore.replicate(socket))
await core1.ready()
await core2.ready()
swarm.join(core1.discoveryKey, { server: false, client: true })
swarm.join(core2.discoveryKey, { server: false, client: true })
await swarm.flush()
await ensureIsReadable(core1)
console.log(await core1.get(0))
console.log(await core1.get(1))
await ensureIsReadable(core2)
console.log(await core2.get(0))
console.log(await core2.get(1))
}
async function ensureIsReadable(core){
const key = core.key.toString("hex")
if (core.writable || core.peers.length) {
console.log(`Core ${key} is writable or at least one peer was found`);
} else {
console.log(`Waiting for peers to connect for core ${key}`);
const [peer] = await once(core, "peer-add");
const peerKey = peer.remotePublicKey.toString("hex")
console.log(`Connected to ${peerKey} for core ${key}`)
}
}
runClient()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment