From a8a430abecd643d17bc58cd2f49019dfc7977cad Mon Sep 17 00:00:00 2001 From: Dominic Cassidy Date: Tue, 6 Oct 2026 09:45:10 +0300 Subject: [PATCH 1/4] Reimplement purge on top of rocksdb storage purge() was left behind by the move to rocksdb storage: it called _closeAllSessions and core.purge, neither of which exist anymore, so it always threw, and its test was disabled. Core.purge() now closes every session, closes the core and deletes it with the storage's deleteCore. Storage that the core would close on its own (autoClose) is kept open until the delete is done, then closed. The tests are rewritten for rocksdb storage and enabled again. Removing all of a core's data also needs hypercore-storage's deleteCore to clear the default session's data, which it currently leaves behind. Co-Authored-By: Claude Opus 5.5 --- index.js | 2 +- lib/core.js | 16 +++++++++++ test/all.js | 2 +- test/purge.js | 74 +++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 72 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 4f04de3df..6df118abf 100644 --- a/index.js +++ b/index.js @@ -937,7 +937,7 @@ class Hypercore extends EventEmitter { } async purge() { - await this._closeAllSessions(null) + if (this.opened === false) await this.opening await this.core.purge() } diff --git a/lib/core.js b/lib/core.js index 9f02ba05d..6c7984fce 100644 --- a/lib/core.js +++ b/lib/core.js @@ -939,6 +939,22 @@ module.exports = class Core { await Promise.allSettled(all) } + // closes every session and deletes the core from storage + async purge() { + const db = this.db + const ptr = this.storage.core + const autoClose = this.autoClose + + // closing the last session would close the storage along with the core + this.autoClose = false + + await this.closeAllSessions(null) + await this.close() + await db.deleteCore(ptr) + + if (autoClose) await db.close() + } + async destroy() { if (this.destroyed === true) return this.destroyed = true diff --git a/test/all.js b/test/all.js index 6b770b2ff..45f9d4bd2 100644 --- a/test/all.js +++ b/test/all.js @@ -28,7 +28,7 @@ async function runTests() { await import('./move-to.js') await import('./mutex.js') await import('./preload.js') - // await import('./purge.js') // todo: implement purge + await import('./purge.js') await import('./push.js') await import('./remote-bitfield.js') await import('./remote-length.js') diff --git a/test/purge.js b/test/purge.js index e15547c67..a99fa2846 100644 --- a/test/purge.js +++ b/test/purge.js @@ -1,38 +1,32 @@ const test = require('brittle') -const fs = require('fs') -const Path = require('path') +const crypto = require('hypercore-crypto') const Hypercore = require('..') +const { createStorage } = require('./helpers') test('basic purge', async function (t) { const dir = await t.tmp() - const core = new Hypercore(dir) - await core.append(['a', 'b', 'c']) - - const oplogLoc = Path.join(dir, 'oplog') - const treeLoc = Path.join(dir, 'tree') - const bitfieldLoc = Path.join(dir, 'bitfield') - const dataLoc = Path.join(dir, 'data') - t.is(fs.existsSync(oplogLoc), true) - t.is(fs.existsSync(treeLoc), true) - t.is(fs.existsSync(bitfieldLoc), true) - t.is(fs.existsSync(dataLoc), true) - t.is(fs.readdirSync(dir).length, 4) // Sanity check + const keyPair = crypto.keyPair() + const core = new Hypercore(await createStorage(t, dir), { keyPair }) + await core.append(['a', 'b', 'c']) await core.purge() t.is(core.closed, true) - t.is(fs.existsSync(oplogLoc), false) - t.is(fs.existsSync(treeLoc), false) - t.is(fs.existsSync(bitfieldLoc), false) - t.is(fs.existsSync(dataLoc), false) - t.is(fs.readdirSync(dir).length, 0) // Nothing remains + + const reopened = new Hypercore(await createStorage(t, dir), { key: keyPair.publicKey }) + await reopened.ready() + + t.is(reopened.length, 0, 'nothing remains') + t.is(reopened.writable, false, 'auth is gone with it') + + await reopened.close() }) test('purge closes all sessions', async function (t) { const dir = await t.tmp() - const core = new Hypercore(dir) + const core = new Hypercore(await createStorage(t, dir)) await core.append(['a', 'b', 'c']) const otherSession = core.session() await otherSession.ready() @@ -42,3 +36,43 @@ test('purge closes all sessions', async function (t) { t.is(core.closed, true) t.is(otherSession.closed, true) }) + +test('purge from another session', async function (t) { + const dir = await t.tmp() + const core = new Hypercore(await createStorage(t, dir)) + await core.append(['a', 'b', 'c']) + const otherSession = core.session() + + await otherSession.purge() + + t.is(core.closed, true) + t.is(otherSession.closed, true) +}) + +test('purge leaves other cores in the storage alone', async function (t) { + const dir = await t.tmp() + + const keyPair = crypto.keyPair() + const manifest = { signers: [{ publicKey: keyPair.publicKey }] } + const key = Hypercore.key(manifest) + + const kept = new Hypercore(await createStorage(t, dir)) + await kept.append(['x', 'y']) + const keptKey = kept.key + await kept.close() + + const purged = new Hypercore(await createStorage(t, dir), { key, manifest, keyPair }) + await purged.append(['a', 'b', 'c']) + await purged.purge() + + const a = new Hypercore(await createStorage(t, dir), { key: keptKey }) + await a.ready() + t.is(a.length, 2, 'other core intact') + t.alike(await a.get(1), Buffer.from('y')) + await a.close() + + const b = new Hypercore(await createStorage(t, dir), { key }) + await b.ready() + t.is(b.length, 0, 'purged core gone') + await b.close() +}) From abb5e30f6de8600cc73c701efb66006c061928c8 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 7 Oct 2026 11:41:56 +0100 Subject: [PATCH 2/4] handle failed core close --- lib/core.js | 13 +++++++++++-- test/purge.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/core.js b/lib/core.js index 6c7984fce..a1716e6d1 100644 --- a/lib/core.js +++ b/lib/core.js @@ -941,6 +941,8 @@ module.exports = class Core { // closes every session and deletes the core from storage async purge() { + if (this.closing !== null) throw new Error('Cannot purge a closed core') + const db = this.db const ptr = this.storage.core const autoClose = this.autoClose @@ -948,8 +950,15 @@ module.exports = class Core { // closing the last session would close the storage along with the core this.autoClose = false - await this.closeAllSessions(null) - await this.close() + try { + await this.closeAllSessions(null) + if (this.hasSession() === true) throw new Error('Cannot purge while sessions are open') + await this.close() + } catch (err) { + this.autoClose = autoClose + throw err + } + await db.deleteCore(ptr) if (autoClose) await db.close() diff --git a/test/purge.js b/test/purge.js index a99fa2846..67bdfbcd8 100644 --- a/test/purge.js +++ b/test/purge.js @@ -1,4 +1,5 @@ const test = require('brittle') +const b4a = require('b4a') const crypto = require('hypercore-crypto') const Hypercore = require('..') @@ -76,3 +77,38 @@ test('purge leaves other cores in the storage alone', async function (t) { t.is(b.length, 0, 'purged core gone') await b.close() }) + +test('purge on a closed core fails clearly', async function (t) { + const dir = await t.tmp() + const core = new Hypercore(await createStorage(t, dir)) + await core.append(['a']) + await core.close() + + await t.exception(core.purge(), /closed/) +}) + +test('a session that will not close aborts the purge and leaves the core intact', async function (t) { + const dir = await t.tmp() + const core = new Hypercore(await createStorage(t, dir)) + await core.append(['a', 'b']) + const key = core.key + + const stuck = core.session() + await stuck.ready() + stuck.close = () => Promise.reject(new Error('busy')) + + await t.exception(core.purge(), /sessions are open/) + + t.is(core.core.closed, false, 'the core stays open for the session that refused') + t.is(core.core.autoClose, true, 'autoClose restored') + t.alike(await stuck.get(1), b4a.from('b'), 'still readable') + + delete stuck.close + await stuck.close() + t.is(core.core.closed, true, 'closing the last session closes the core again') + + const reopened = new Hypercore(await createStorage(t, dir), { key }) + await reopened.ready() + t.is(reopened.length, 2, 'nothing was deleted') + await reopened.close() +}) From f7bc41e19aab0ecba86062b7c65a987511338fa8 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 7 Oct 2026 11:42:12 +0100 Subject: [PATCH 3/4] add test to ensure data is actually removed --- test/purge.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/purge.js b/test/purge.js index 67bdfbcd8..803e62115 100644 --- a/test/purge.js +++ b/test/purge.js @@ -1,6 +1,7 @@ const test = require('brittle') const b4a = require('b4a') const crypto = require('hypercore-crypto') +const { core: keys } = require('hypercore-storage/lib/keys.js') const Hypercore = require('..') const { createStorage } = require('./helpers') @@ -78,6 +79,52 @@ test('purge leaves other cores in the storage alone', async function (t) { await b.close() }) +test('purge deletes the rows, not just the record', async function (t) { + const dir = await t.tmp() + + // a keyless core opens the storage's default core, so key both explicitly + const keptPair = crypto.keyPair() + const kept = new Hypercore(await createStorage(t, dir), { + key: keptPair.publicKey, + keyPair: keptPair + }) + await kept.append(['x', 'y']) + const keptPtr = kept.core.storage.core + await kept.close() + + const keyPair = crypto.keyPair() + const core = new Hypercore(await createStorage(t, dir), { key: keyPair.publicKey, keyPair }) + await core.append(['a', 'b', 'c']) + await core.setUserData('hello', b4a.from('world')) + const ptr = core.core.storage.core + t.not(ptr.corePointer, keptPtr.corePointer) + + await core.purge() + + const storage = await createStorage(t, dir) + t.is( + await count(storage, keys.core(ptr.corePointer), keys.core(ptr.corePointer + 1)), + 0, + 'core rows' + ) + t.is( + await count(storage, keys.data(ptr.dataPointer), keys.data(ptr.dataPointer + 1)), + 0, + 'blocks, tree, bitfield and user data' + ) + t.not( + await count(storage, keys.core(keptPtr.corePointer), keys.core(keptPtr.corePointer + 1)), + 0, + 'other core rows untouched' + ) + t.not( + await count(storage, keys.data(keptPtr.dataPointer), keys.data(keptPtr.dataPointer + 1)), + 0, + 'other core data untouched' + ) + await storage.close() +}) + test('purge on a closed core fails clearly', async function (t) { const dir = await t.tmp() const core = new Hypercore(await createStorage(t, dir)) @@ -112,3 +159,9 @@ test('a session that will not close aborts the purge and leaves the core intact' t.is(reopened.length, 2, 'nothing was deleted') await reopened.close() }) + +async function count(storage, gte, lt) { + let n = 0 + for await (const _ of storage.db.iterator({ gte, lt })) n++ + return n +} From 64d42ad28cb23fd39d3bd7be708b0f3ade6d7a01 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 14 Oct 2026 12:36:27 +0100 Subject: [PATCH 4/4] bump dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 19798dd95..7adc43fbc 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "hypercore-crypto": "^3.2.1", "hypercore-errors": "^1.5.0", "hypercore-id-encoding": "^1.2.0", - "hypercore-storage": "^3.2.0", + "hypercore-storage": "^3.3.2", "is-options": "^1.0.1", "nanoassert": "^2.0.0", "protomux": "^3.5.0",