Skip to content

Instantly share code, notes, and snippets.

@jaredatron
Last active April 16, 2021 20:35
Show Gist options
  • Save jaredatron/d526c545a6a55a8ca52fb26656d2c4f3 to your computer and use it in GitHub Desktop.
Save jaredatron/d526c545a6a55a8ca52fb26656d2c4f3 to your computer and use it in GitHub Desktop.
tail a hypercore given its public key
hyperspace-simulator tail_hypercore.js -- 0ce9e824c768daf0564f6f1ec24e04430111b8845476464ffbf60e8e3ef06347
const stats = require('process-top')() // instantiate a stats collector
const { Client } = require('hyperspace')
const publicKey = process.argv[2]
main()
async function main() {
if (!publicKey) throw new Error(`publicKey?`)
console.log(`tailing hypercore "${publicKey}"`)
const client = new Client()
await client.ready()
const store = client.corestore()
const core = store.get({
key: publicKey,
valueEncoding: 'json',
})
console.log(core)
console.log('waiting for core to be ready…')
await core.ready()
console.log('replicating core…')
await client.replicate(core)
async function logLatestBlock(){
try{
if (core.length <= 0) {
console.warn('core.length === 0')
}else{
const block = await core.get(core.length - 1)
console.log(block)
}
}catch(error){
console.error(`error getting latest block ${block.length -1}: ` + error)
}
}
logLatestBlock()
core.on('append', logLatestBlock)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment