Skip to content

Instantly share code, notes, and snippets.

@HDegroote
Created January 19, 2023 11:20
Show Gist options
  • Save HDegroote/78ca9ef4334d6899405a80542861f031 to your computer and use it in GitHub Desktop.
Save HDegroote/78ca9ef4334d6899405a80542861f031 to your computer and use it in GitHub Desktop.
Hypercore behaviour change 10.5.4->10.6.0
import Hyperswarm from 'hyperswarm'
import Corestore from 'corestore'
import ram from 'random-access-memory'
/*
With hypercore 10.5.4, the output is:
origCore lenght: 1
Replication with peer set up
Replication with peer set up
readCore length: 1
With hypercore v10.6.0, the output is:
origCore lenght: 1
Replication with peer set up
Replication with peer set up
readCore length: 0
*/
const store1 = new Corestore(ram)
const store2 = new Corestore(ram)
const origCore = store1.get({ name: 'acore' })
await origCore.append('entry')
console.log('origCore lenght:', origCore.length)
const swarm1 = new Hyperswarm()
setupReplication(swarm1, store1)
await swarm1.join(origCore.discoveryKey, { server: true })
await swarm1.flush()
const swarm2 = new Hyperswarm()
setupReplication(swarm2, store2)
await swarm2.join(origCore.discoveryKey, { client: true })
await swarm2.flush()
const readCore = store2.get({ key: origCore.key })
await readCore.ready()
await readCore.update()
console.log('readCore length:', readCore.length)
await swarm1.destroy()
await swarm2.destroy()
function setupReplication (swarm, corestore) {
swarm.on('connection', (socket) => {
corestore.replicate(socket)
socket.on('error', (err) => {
console.error('socket error:', err.stack)
socket.destroy()
})
console.log('Replication with peer set up')
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment