const {Client: HyperspaceClient } = require('hyperspace')
const Hyperbee = require('hyperbee')
const { SSL_OP_EPHEMERAL_RSA } = require('constants')
const FEEDURL = 'hyper://3e09925a14b933bbc5d35757f0f1b06b169036dc64b3c9a4fd18f8a62308b059/'
const client = new HyperspaceClient()
const corestore = client.corestore()
main()
async function main() {
const bee = await getBee(urlToKey(FEEDURL))
//console.log('bee', bee)
const stream = await bee.createReadStream({reverse: true, limit: 20})
stream.on('data', (chunk) => {
console.log(chunk)
})
stream.on('error', (e) => {
e.StackTraceLimit = 100
console.error('error', e)
})
stream.on('end', async () => {
console.log('stream ended')
await client.close()
})
stream.on('end', async () => {
console.log('stream closed')
await client.close()
})
}
async function getBee(key) {
let feed = corestore.get({key: key}) //, sparse: true})
let db = new Hyperbee(feed, {
keyEncoding: 'utf8',
valueEncoding: 'json'
})
await db.ready()
return db
}
function urlToKey (url) {
return Buffer.from(/([0-9a-f]{64})/i.exec(url)[1], 'hex')
}