Skip to content

Instantly share code, notes, and snippets.

@tomasciccola
Created December 6, 2022 15:15
Show Gist options
  • Save tomasciccola/888c4deeca5cac8db106e1e1b84358d7 to your computer and use it in GitHub Desktop.
Save tomasciccola/888c4deeca5cac8db106e1e1b84358d7 to your computer and use it in GitHub Desktop.
testing corestore and namespaces
// this example should answer the question: do corestore namespaces sync separately?
// so:
// from peer B:
// 1. create corestore
// 2. create two namespaces for that core (i.e. authstore, blobs)
// 3. publish the store on the swarm
// from peer B:
// Try:
// 1. Replicate one core under one namespace
// 2. Does it work?
import Corestore from 'corestore'
import Hyperswarm from 'hyperswarm'
const store = new Corestore('./store1')
await store.ready()
console.log('store key', store.primaryKey.toString('hex'))
// authstore
const authstore = store.namespace('authstore')
const authstoreCore = authstore.get({name: 'main'})
await authstoreCore.ready()
console.log('authstore key', authstoreCore.key.toString('hex'))
authstoreCore.append('my super secret', { encoding: 'utf-8'})
let data = await authstoreCore.get(0)
console.log('data to authstore =>', data.toString())
// blobstore
const blobstore = store.namespace('blobstore')
const blobstoreCore = blobstore.get({name: 'main'})
await blobstoreCore.ready()
console.log('blobstore key', blobstoreCore.key.toString('hex'))
blobstoreCore.append('my super blob', { encoding: 'utf-8'})
data = await blobstoreCore.get(0)
console.log('data to blobstore =>', data.toString())
// SWARM
const topic = Buffer.alloc(32).fill('szgy')
const swarm = new Hyperswarm()
swarm.on('connection' , (conn,info) => {
console.log('replicating data')
store.replicate(conn)
})
const discovery = swarm.join(topic)
await discovery.flushed()
import Corestore from 'corestore'
import Hyperswarm from 'hyperswarm'
const key = Buffer.alloc(32).fill(process.argv[2], 'hex')
const store = new Corestore('./store2')
await store.ready()
// SWARM
const topic = Buffer.alloc(32).fill('szgy')
const swarm = new Hyperswarm()
swarm.on('connection' , async (conn,info) => {
console.log('replicating data')
store.replicate(conn)
const otherCore = store.get(key)
console.log('getting core with key', key.toString('hex'))
await otherCore.ready()
const data = await otherCore.get(0)
console.log('data!', data.toString())
})
const discovery = swarm.join(topic)
await discovery.flushed()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment