autobase icon indicating copy to clipboard operation
autobase copied to clipboard

Cannot read property 'value' of undefined when using Autobase with Hyperbee's createHistorySream

Open hexadecible opened this issue 4 years ago • 2 comments

I'm using a rebased index with Hyperbee, and when I attempt to bee.createHistoryStream({ live: true }) I get the following error:

node:events:346
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read property 'value' of undefined
    at RebasedHypercore.get (/Users/gh/Documents/tests/autobase/node_modules/autobase/lib/rebase.js:216:19)
    at processTicksAndRejections (node:internal/process/task_queues:94:5)
    at async HyperBee.getBlock (/Users/gh/Documents/tests/autobase/node_modules/hyperbee/index.js:357:21)
    at async Batch.getBlock (/Users/gh/Documents/tests/autobase/node_modules/hyperbee/index.js:527:9)
    at async HistoryIterator.next (/Users/gh/Documents/tests/autobase/node_modules/hyperbee/iterators/history.js:32:18)
Emitted 'error' event on Readable instance at:
    at ReadableState.afterDestroy (/Users/gh/Documents/tests/autobase/node_modules/streamx/index.js:442:19)
    at Readable._destroy (/Users/gh/Documents/tests/autobase/node_modules/streamx/index.js:535:5)
    at ReadableState.updateNonPrimary (/Users/gh/Documents/tests/autobase/node_modules/streamx/index.js:349:16)
    at ReadableState.update (/Users/gh/Documents/tests/autobase/node_modules/streamx/index.js:333:69)
    at ReadableState.afterRead (/Users/gh/Documents/tests/autobase/node_modules/streamx/index.js:472:58)

Example code used:

const Hyperbee = require("hyperbee")
const Autobase = require("autobase")
const Corestore = require("corestore")
const ram = require("random-access-memory")

const store = new Corestore(ram)
const writer = store.get({ name: 'writer' })
const base = new Autobase([writer], { input: writer })

const index = base.createRebasedIndex({
  unwrap: true,
  apply: _apply.bind(this)
})

const hb = new Hyperbee(index, {
  keyEncoding: 'utf-8',
  valueEncoding: 'json',
  extension: false
})

const stream = hb.createHistoryStream({ live: true })

stream.on('data', data => {
  // do something
})

await put('hi', { hello: 'world' })

async function put(key, value, opts) {
  const op = Buffer.from(JSON.stringify({ type: 'put', key, value }))
  return await base.append(op, opts)
}

async function _apply(batch) {
  const b = hb.batch({ update: false })
  for (const node of batch) {
    const op = JSON.parse(node.value.toString())
    if (op.type === 'put') await b.put(op.key, op.value)
  }
  await b.flush()
}

I showed this to @mafintosh, who said it's because the history stream is reading out of bounds on the Hypercore.

hexadecible avatar Oct 30 '21 20:10 hexadecible

👍 and the solution for now it to make it throw a proper error :)

mafintosh avatar Oct 31 '21 08:10 mafintosh

Better error in alpha 1 (added by you!). Let's leave this open until we decide if this is a feature or a bug

mafintosh avatar Nov 01 '21 16:11 mafintosh