From 6624fbfdb909d6845ffed7ff68e7e241f8a5cecd Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 30 Sep 2026 17:32:57 +0100 Subject: [PATCH 1/4] delete removes alias as well --- index.js | 6 ++++++ test/all.js | 1 + 2 files changed, 7 insertions(+) diff --git a/index.js b/index.js index 2e28113..495f176 100644 --- a/index.js +++ b/index.js @@ -589,9 +589,15 @@ class CorestoreStorage { // no core stored here if (!auth) return + const corestoreRx = new CorestoreRX(this.db, EMPTY) + const recordPromise = corestoreRx.getCore(auth.discoveryKey) + corestoreRx.tryFlush() + const record = await recordPromise + const tx = this.db.write({ autoDestroy: true }) tx.tryDelete(store.core(auth.discoveryKey)) + if (record && record.alias) tx.tryDelete(store.coreByAlias(record.alias)) // clear core const start = core.core(ptr.corePointer) diff --git a/test/all.js b/test/all.js index 7d4b916..24fc0d9 100644 --- a/test/all.js +++ b/test/all.js @@ -17,6 +17,7 @@ async function runTests() { // Fixtures await import('./migration.js') + await import('./delete.js') test.resume() } From c42a1c5520dd16e500b39dda5cfbfb829dbb893d Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Thu, 1 Oct 2026 14:01:11 +0100 Subject: [PATCH 2/4] add test --- test/delete.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 test/delete.js diff --git a/test/delete.js b/test/delete.js new file mode 100644 index 0000000..2718f08 --- /dev/null +++ b/test/delete.js @@ -0,0 +1,80 @@ +const test = require('brittle') +const b4a = require('b4a') +const crypto = require('hypercore-crypto') + +const { create, writeBlocks, readBlocks, toArray } = require('./helpers') + +test('deleteCore - removes the core, its blocks and its alias', async function (t) { + const s = await create(t) + t.teardown(() => s.close()) + + const namespace = b4a.alloc(32, 1) + const alias = { name: 'bee', namespace } + const discoveryKey = crypto.randomBytes(32) + + const core = await s.createCore({ key: crypto.randomBytes(32), discoveryKey, alias }) + await writeBlocks(core, 5) + + t.alike(await s.getAlias(alias), discoveryKey, 'alias resolves before delete') + + await s.deleteCore(core.core) + await core.close() + + t.is(await s.hasCore(discoveryKey), false, 'core record is gone') + t.is(await s.getAlias(alias), null, 'alias is gone') + t.alike(await toArray(s.createDiscoveryKeyStream(namespace)), [], 'namespace listing is empty') + t.alike(await toArray(s.createDiscoveryKeyStream()), [], 'store listing is empty') +}) + +test('deleteCore - a deleted alias can be minted again', async function (t) { + const s = await create(t) + t.teardown(() => s.close()) + + const namespace = b4a.alloc(32, 2) + const alias = { name: 'bee', namespace } + + const first = await s.createCore({ + key: crypto.randomBytes(32), + discoveryKey: crypto.randomBytes(32), + alias + }) + await writeBlocks(first, 3) + await s.deleteCore(first.core) + await first.close() + + const discoveryKey = crypto.randomBytes(32) + const second = await s.createCore({ key: crypto.randomBytes(32), discoveryKey, alias }) + await writeBlocks(second, 2) + + t.alike(await s.getAlias(alias), discoveryKey, 'alias points at the new core') + t.alike(await toArray(s.createDiscoveryKeyStream(namespace)), [discoveryKey]) + t.is((await readBlocks(second, 2)).length, 2) + + await second.close() +}) + +test('deleteCore - other aliases in the namespace survive', async function (t) { + const s = await create(t) + t.teardown(() => s.close()) + + const namespace = b4a.alloc(32, 3) + const cores = [] + + for (let i = 0; i < 3; i++) { + const discoveryKey = crypto.randomBytes(32) + const core = await s.createCore({ + key: crypto.randomBytes(32), + discoveryKey, + alias: { name: 'core-' + i, namespace } + }) + cores.push({ core, discoveryKey }) + } + + await s.deleteCore(cores[1].core.core) + for (const { core } of cores) await core.close() + + t.is(await s.getAlias({ name: 'core-1', namespace }), null) + t.alike(await s.getAlias({ name: 'core-0', namespace }), cores[0].discoveryKey) + t.alike(await s.getAlias({ name: 'core-2', namespace }), cores[2].discoveryKey) + t.is((await toArray(s.createDiscoveryKeyStream(namespace))).length, 2) +}) From 3d0523f5a2f3d9994a9d3f32c000171325df3dca Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 7 Oct 2026 10:34:48 +0100 Subject: [PATCH 3/4] expose deleteAlias method --- index.js | 28 +++++++++++++++++++++++++++- test/delete.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 495f176..22b0e8c 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const RocksDB = require('rocksdb-native') +const b4a = require('b4a') const rrp = require('resolve-reject-promise') const ScopeLock = require('scope-lock') const DeviceFile = require('device-file') @@ -594,10 +595,18 @@ class CorestoreStorage { corestoreRx.tryFlush() const record = await recordPromise + // only drop the alias if it still points at this core + const aliasRx = new CorestoreRX(this.db, EMPTY) + const aliasPromise = record && record.alias ? aliasRx.getCoreByAlias(record.alias) : null + aliasRx.tryFlush() + const aliased = await aliasPromise + const tx = this.db.write({ autoDestroy: true }) tx.tryDelete(store.core(auth.discoveryKey)) - if (record && record.alias) tx.tryDelete(store.coreByAlias(record.alias)) + if (aliased && b4a.equals(aliased, auth.discoveryKey)) { + tx.tryDelete(store.coreByAlias(record.alias)) + } // clear core const start = core.core(ptr.corePointer) @@ -862,6 +871,23 @@ class CorestoreStorage { return discoveryKeyPromise } + // only deletes the alias if it still points at discoveryKey + async deleteAlias(alias, discoveryKey) { + if (this.version === 0) await this._migrateStore() + + const rx = new CorestoreRX(this.db, EMPTY) + const currentPromise = rx.getCoreByAlias(alias) + rx.tryFlush() + const current = await currentPromise + + if (current === null || !b4a.equals(current, discoveryKey)) return false + + const tx = this.db.write({ autoDestroy: true }) + tx.tryDelete(store.coreByAlias(alias)) + await tx.flush() + return true + } + async getSeed() { if (this.version === 0) await this._migrateStore() diff --git a/test/delete.js b/test/delete.js index 2718f08..af6bef7 100644 --- a/test/delete.js +++ b/test/delete.js @@ -78,3 +78,31 @@ test('deleteCore - other aliases in the namespace survive', async function (t) { t.alike(await s.getAlias({ name: 'core-2', namespace }), cores[2].discoveryKey) t.is((await toArray(s.createDiscoveryKeyStream(namespace))).length, 2) }) + +test('deleteAlias - drops the alias and leaves the core alone', async function (t) { + const s = await create(t) + const namespace = b4a.alloc(32, 7) + const alias = { name: 'bee', namespace } + const discoveryKey = b4a.alloc(32, 1) + + const core = await s.createCore({ key: crypto.randomBytes(32), discoveryKey, alias }) + await writeBlocks(core, 2) + await core.close() + + t.is( + await s.deleteAlias(alias, b4a.alloc(32, 9)), + false, + 'a mismatching discovery key is refused' + ) + t.alike(await s.getAlias(alias), discoveryKey, 'alias untouched') + + t.is(await s.deleteAlias(alias, discoveryKey), true) + + t.is(await s.getAlias(alias), null, 'alias is gone') + t.is(await s.hasCore(discoveryKey), true, 'core record survives') + t.alike(await toArray(s.createDiscoveryKeyStream()), [discoveryKey], 'core still listed') + + t.is(await s.deleteAlias(alias, discoveryKey), false, 'deleting a missing alias is a noop') + + await s.close() +}) From 65313f79d7a1ef26a56851ce8b5d69e236091921 Mon Sep 17 00:00:00 2001 From: Christophe Diederichs Date: Wed, 14 Oct 2026 11:24:03 +0100 Subject: [PATCH 4/4] review by @lejeunerenard --- index.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 22b0e8c..2218b87 100644 --- a/index.js +++ b/index.js @@ -596,10 +596,7 @@ class CorestoreStorage { const record = await recordPromise // only drop the alias if it still points at this core - const aliasRx = new CorestoreRX(this.db, EMPTY) - const aliasPromise = record && record.alias ? aliasRx.getCoreByAlias(record.alias) : null - aliasRx.tryFlush() - const aliased = await aliasPromise + const aliased = record && record.alias ? await this.getAlias(record.alias) : null const tx = this.db.write({ autoDestroy: true }) @@ -875,10 +872,7 @@ class CorestoreStorage { async deleteAlias(alias, discoveryKey) { if (this.version === 0) await this._migrateStore() - const rx = new CorestoreRX(this.db, EMPTY) - const currentPromise = rx.getCoreByAlias(alias) - rx.tryFlush() - const current = await currentPromise + const current = await this.getAlias(alias) if (current === null || !b4a.equals(current, discoveryKey)) return false