From 387d8deab18c58a74cdad5f63af0071a12f14ebd Mon Sep 17 00:00:00 2001 From: HDegroote <75906619+HDegroote@users.noreply.github.com> Date: Mon, 24 Feb 2025 10:34:52 +0100 Subject: [PATCH] Throw if snapshots not opened --- index.js | 4 ++++ test/basic.js | 30 ++++++++++++++++++++++++++++++ test/complex.js | 6 +++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 13af5bd..fd7c1dd 100644 --- a/index.js +++ b/index.js @@ -89,6 +89,10 @@ class BeeDiffStream extends Union { throw new Error('Incompatible Hypercore version--must have signedLength property') } + if (!leftSnapshot.opened || !rightSnapshot.opened) { + throw new Error('Snapshot must be opened') + } + // We know that everything indexed in both snapshots is shared const sharedIndexedL = Math.min( leftSnapshot.core.signedLength, rightSnapshot.core.signedLength diff --git a/test/basic.js b/test/basic.js index 9e76508..3b2ecc5 100644 --- a/test/basic.js +++ b/test/basic.js @@ -2,6 +2,8 @@ const test = require('brittle') const Hyperbee = require('hyperbee') const b4a = require('b4a') const Hypercore = require('hypercore') +const tmpDir = require('test-tmp') +const Corestore = require('corestore') const BeeDiffStream = require('../index') const { streamToArray, setup, encodedOpen, jsonKeyedOpen, confirm, replicateAndSync } = require('./helpers') @@ -852,6 +854,34 @@ test('supports diffing values skipped by hyperbee encoding', async t => { t.alike(diffs.map(({ right }) => right?.key), [undefined, undefined]) // deletions }) +test('throws if a snapshot is not opened', async t => { + const store = new Corestore(await tmpDir()) + const core = store.get({ name: 'bee' }) + const bee = new Hyperbee(core) + t.exception( + () => new BeeDiffStream(bee, bee), + /Snapshot must be opened/, + 'none open' + ) + + await bee.ready() + const snap = bee.snapshot() + + t.exception( + () => new BeeDiffStream(bee, snap), + /Snapshot must be opened/, + 'right not open' + ) + t.exception( + () => new BeeDiffStream(snap, bee), + /Snapshot must be opened/, + 'left not open' + ) + + await snap.ready() + t.execution(() => new BeeDiffStream(snap, bee), 'sanity check that happy path does not throw') +}) + function sameKeysAndValues (t, actual, expected) { const extractKeysAndValues = ({ left, right }) => { const res = {} diff --git a/test/complex.js b/test/complex.js index 2c13d8a..b4bb304 100644 --- a/test/complex.js +++ b/test/complex.js @@ -23,6 +23,7 @@ test('complex scenario with many diff cases', async t => { // Corresponds to the state just before going offline const baselineBee = base2.view.bee.snapshot() + await baselineBee.ready() t.is(base2.view.bee.core.signedLength, 13) // Sanity check @@ -53,8 +54,10 @@ test('complex scenario with many diff cases', async t => { // State just before reconnecting with base1 const refBee = base2.view.bee.snapshot() + const refBeeSnap = refBee.snapshot() + await refBeeSnap.ready() const [refNewState, refOldState] = diffsToValues( - await streamToArray(new BeeDiffStream(baselineBee, refBee.snapshot())) + await streamToArray(new BeeDiffStream(baselineBee, refBeeSnap)) ) t.alike(refNewState, [ '2-1 added by 2', @@ -106,6 +109,7 @@ test('complex scenario with many diff cases', async t => { // The peers sync and the autobases are linearised await confirm([base1, base2]) const newBee = base2.view.bee.snapshot() + await newBee.ready() const [newState, refState] = diffsToValues( await streamToArray(new BeeDiffStream(refBee, newBee)) )