From f3d5f35d9aa456806a785a6b388c61f0339cc4d1 Mon Sep 17 00:00:00 2001 From: Jan Keith Darunday Date: Wed, 18 Mar 2026 02:14:05 +0800 Subject: [PATCH 1/2] Add test case for write after close corruption --- test.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test.js b/test.js index 97fd7dc6..b362d80a 100644 --- a/test.js +++ b/test.js @@ -1845,6 +1845,57 @@ test('dedup mode', async (t) => { t.is(drive.blobs.core.length, len + 1) }) +test('write after close should not corrupt drive', async (t) => { + const platformCorestore = new Corestore(await t.tmp() , { + manifestVersion: 1, + compat: false, + wait: true + }) + await platformCorestore.ready() + + { + const corestore = platformCorestore.session({ writable: true }) + await corestore.ready() + t.teardown(() => corestore.close()) + + const drive = new Hyperdrive(corestore) + await drive.ready() + t.teardown(() => drive.close()) + + await drive.db.put('manifest', 'hello world') + + const batch = drive.batch() + try { + for (let i = 0; i < 14; i++) { + const stream = batch.createWriteStream('/file' + i + '.txt') + const close = new Promise(resolve => stream.on('close', resolve)) + stream.end('hello world' + i) + await close + + if (i === 2) await drive.close() + } + await batch.flush() + t.fail('batch should have errored when drive is closed') + } catch (err) { + t.pass('batch should error when drive is closed') + } + await corestore.close() + } + + { + const corestore = platformCorestore.session({ writable: false }) + await corestore.ready() + t.teardown(() => corestore.close()) + + const drive = new Hyperdrive(corestore) + await drive.ready() + t.teardown(() => drive.close()) + + const manifest = await drive.db.get('manifest') + t.is(manifest.value, 'hello world', 'should correctly read manifest') + } +}) + async function testenv(t) { const { teardown } = t From c25f3095d62ea69fb291b7fc71aeb0665fda8580 Mon Sep 17 00:00:00 2001 From: Jan Keith Darunday Date: Wed, 18 Mar 2026 02:20:00 +0800 Subject: [PATCH 2/2] Apply npm run format --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index b362d80a..0b3bc1d2 100644 --- a/test.js +++ b/test.js @@ -1846,7 +1846,7 @@ test('dedup mode', async (t) => { }) test('write after close should not corrupt drive', async (t) => { - const platformCorestore = new Corestore(await t.tmp() , { + const platformCorestore = new Corestore(await t.tmp(), { manifestVersion: 1, compat: false, wait: true @@ -1868,7 +1868,7 @@ test('write after close should not corrupt drive', async (t) => { try { for (let i = 0; i < 14; i++) { const stream = batch.createWriteStream('/file' + i + '.txt') - const close = new Promise(resolve => stream.on('close', resolve)) + const close = new Promise((resolve) => stream.on('close', resolve)) stream.end('hello world' + i) await close