From 4477c1043b056916f736d7377b70ed531979d070 Mon Sep 17 00:00:00 2001 From: Sean Zellmer Date: Wed, 8 Apr 2026 14:49:21 -0500 Subject: [PATCH 1/2] Fix `core.clear(..., { diff: true })` Previously was left unimplemented after moving to rocks due to limited knowledge about bytes used for a core. But the `blocks` property can be known as it is a hypercore concept. README.md & tests adjusted. Currently doesn't test named or atomic sessions. --- README.md | 10 +++++++++- lib/session-state.js | 5 +++++ test/clear.js | 10 ++++------ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 823355d66..d590c58c9 100644 --- a/README.md +++ b/README.md @@ -308,7 +308,15 @@ The core will also gossip to peers it is connected to, that is no longer has the ```js { - diff: false // Returned `cleared` bytes object is null unless you enable this + diff: false // Returned `cleared` object is null unless you enable this +} +``` + +`cleared` object when `diff` is `true` will look like: + +``` +{ + blocks: Number // The number of blocks cleared } ``` diff --git a/lib/session-state.js b/lib/session-state.js index 38af3d050..aa1aae168 100644 --- a/lib/session-state.js +++ b/lib/session-state.js @@ -547,6 +547,11 @@ class SessionState { } } + if (cleared) { + const blocksToClear = this.core.bitfield.countSet(start, end - start) + cleared.blocks = blocksToClear + } + if (end - start === 1) tx.deleteBlock(start) else tx.deleteBlockRange(start, end) diff --git a/test/clear.js b/test/clear.js index b715219b7..c78ed00e9 100644 --- a/test/clear.js +++ b/test/clear.js @@ -95,13 +95,11 @@ test('clear blocks with diff option', async function (t) { const cleared = await core.clear(1337) t.is(cleared, null) - // todo: reenable bytes use api + const cleared2 = await core.clear(0, { diff: true }) + t.ok(cleared2.blocks > 0) - // const cleared2 = await core.clear(0, { diff: true }) - // t.ok(cleared2.blocks > 0) - - // const cleared3 = await core.clear(0, { diff: true }) - // t.is(cleared3.blocks, 0) + const cleared3 = await core.clear(0, { diff: true }) + t.is(cleared3.blocks, 0) await core.close() }) From 26487d3884376a227c7a7573c7a86254bd3292cf Mon Sep 17 00:00:00 2001 From: Sean Zellmer Date: Wed, 8 Apr 2026 14:51:52 -0500 Subject: [PATCH 2/2] Remove unused intermediary variable --- lib/session-state.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/session-state.js b/lib/session-state.js index aa1aae168..0c6bbe8a7 100644 --- a/lib/session-state.js +++ b/lib/session-state.js @@ -548,8 +548,7 @@ class SessionState { } if (cleared) { - const blocksToClear = this.core.bitfield.countSet(start, end - start) - cleared.blocks = blocksToClear + cleared.blocks = this.core.bitfield.countSet(start, end - start) } if (end - start === 1) tx.deleteBlock(start)