From ccc96d469761707e1b7a5e1f34f4178209425f1d Mon Sep 17 00:00:00 2001 From: rafapaezbas <15270736+rafapaezbas@users.noreply.github.com> Date: Tue, 27 Aug 2024 17:47:01 +0200 Subject: [PATCH] add get db key as a static method (#373) --- index.js | 10 +++++++++- test.js | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1d6600d..47a031a 100644 --- a/index.js +++ b/index.js @@ -44,6 +44,14 @@ module.exports = class Hyperdrive extends ReadyResource { return this.entries()[Symbol.asyncIterator]() } + static async getDriveKey (corestore) { + const core = makeBee(undefined, corestore) + await core.ready() + const key = core.key + await core.close() + return key + } + static getContentKey (m, key) { if (m instanceof Hypercore) { if (m.core.compat) return null @@ -634,7 +642,7 @@ function shallowReadStream (files, folder, keys) { }) } -function makeBee (key, corestore, opts) { +function makeBee (key, corestore, opts = {}) { const name = key ? undefined : 'db' const core = corestore.get({ key, name, exclusive: true, onwait: opts.onwait, encryptionKey: opts.encryptionKey, compat: opts.compat, active: opts.active }) diff --git a/test.js b/test.js index cb8fe3c..769136c 100644 --- a/test.js +++ b/test.js @@ -1485,6 +1485,22 @@ test('truncate throws when truncating future version)', async t => { ) }) +test('get drive key without using the constructor', async (t) => { + t.plan(1) + const corestore = new Corestore(RAM.reusable()) + const key = await Hyperdrive.getDriveKey(corestore.session()) + const drive = new Hyperdrive(corestore.session()) + + t.teardown(() => { + corestore.close() + drive.close() + }) + + await drive.ready() + + t.is(key.toString('hex'), drive.key.toString('hex')) +}) + async function testenv (teardown) { const corestore = new Corestore(RAM) await corestore.ready()