From cb07362927795686f8606e30e4b0eb2e3f64f668 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 4 Jan 2023 20:08:50 -0300 Subject: [PATCH 1/7] add opts.persistentKey (to avoid writing key to disk) --- index.js | 3 ++- test/all.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index da91dc4..a51acf5 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,7 @@ module.exports = class Corestore extends EventEmitter { this.cores = root ? root.cores : new Map() this.cache = !!opts.cache this.primaryKey = opts.primaryKey || null + this.persistentKey = opts.persistentKey !== false this._keyStorage = null this._namespace = opts.namespace || DEFAULT_NAMESPACE @@ -90,7 +91,7 @@ module.exports = class Corestore extends EventEmitter { if (err && err.code !== 'ENOENT') return reject(err) if (err || st.size < 32 || this._overwrite) { const key = this.primaryKey || crypto.randomBytes(32) - return this._keyStorage.write(0, key, err => { + return this._keyStorage.write(0, this.persistentKey ? key : b4a.alloc(0), err => { if (err) return reject(err) return resolve(key) }) diff --git a/test/all.js b/test/all.js index caf3369..3af3b9c 100644 --- a/test/all.js +++ b/test/all.js @@ -5,6 +5,7 @@ const os = require('os') const path = require('path') const b4a = require('b4a') const sodium = require('sodium-universal') +const fsp = require('fs/promises') const Corestore = require('..') @@ -482,6 +483,47 @@ test('core-open and core-close events', async function (t) { t.is(closed, 2) }) +test('persistent primary key', async function (t) { + const dir = tmpdir() + + const store = new Corestore(dir) + await store.ready() + + const primaryKeyFile = await fsp.readFile(path.join(dir, 'primary-key')) + t.alike(primaryKeyFile, store.primaryKey) + + try { + const store2 = new Corestore(dir, { persistentKey: false }) + await store2.ready() + t.fail('should have failed') + } catch (error) { + t.is(error.code, 'ELOCKED', error.code) + } +}) + +test('non-persistent primary key', async function (t) { + const dir = tmpdir() + + const store = new Corestore(dir, { persistentKey: false }) + await store.ready() + + const primaryKeyFile = await fsp.readFile(path.join(dir, 'primary-key')) + t.alike(primaryKeyFile, b4a.alloc(0)) + + try { + const store2 = new Corestore(dir, { persistentKey: false }) + await store2.ready() + t.fail('should have failed') + } catch (error) { + t.is(error.code, 'ELOCKED', error.code) + } + + const core = store.get({ name: 'core-1' }) + await core.ready() + + await fsp.readFile(path.join(dir, 'primary-key')) +}) + function tmpdir () { return path.join(os.tmpdir(), 'corestore-' + Math.random().toString(16).slice(2)) } From 6d554d6f639df64dda11bc9b12121f28ef4ab771 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 4 Jan 2023 20:22:42 -0300 Subject: [PATCH 2/7] Update all.js --- test/all.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/all.js b/test/all.js index 3af3b9c..267a722 100644 --- a/test/all.js +++ b/test/all.js @@ -499,6 +499,8 @@ test('persistent primary key', async function (t) { } catch (error) { t.is(error.code, 'ELOCKED', error.code) } + + await store.close() }) test('non-persistent primary key', async function (t) { @@ -518,10 +520,22 @@ test('non-persistent primary key', async function (t) { t.is(error.code, 'ELOCKED', error.code) } - const core = store.get({ name: 'core-1' }) - await core.ready() + await store.close() +}) + +test('re open with non-persistent primary key', async function (t) { + const dir = tmpdir() + + const key = randomBytes(32) + + const store = new Corestore(dir, { primaryKey: key, persistentKey: false }) + await store.ready() + t.alike(store.primaryKey, key) + await store.close() - await fsp.readFile(path.join(dir, 'primary-key')) + const store2 = new Corestore(dir, { persistentKey: false }) + await store2.ready() + t.alike(store2.primaryKey, b4a.alloc(0)) }) function tmpdir () { From dcf8f6171fe535a850b5b28d2c8b80e19126a664 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 4 Jan 2023 20:24:11 -0300 Subject: [PATCH 3/7] Update all.js --- test/all.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/all.js b/test/all.js index 267a722..f7bafbf 100644 --- a/test/all.js +++ b/test/all.js @@ -535,7 +535,8 @@ test('re open with non-persistent primary key', async function (t) { const store2 = new Corestore(dir, { persistentKey: false }) await store2.ready() - t.alike(store2.primaryKey, b4a.alloc(0)) + t.unlike(store2.primaryKey, key) + await store2.close() }) function tmpdir () { From 49b4a6fe2a0b7d132cc12142774fd77ed45012db Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 4 Jan 2023 20:30:07 -0300 Subject: [PATCH 4/7] try to fix win --- test/all.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/all.js b/test/all.js index f7bafbf..2c38d7a 100644 --- a/test/all.js +++ b/test/all.js @@ -489,9 +489,6 @@ test('persistent primary key', async function (t) { const store = new Corestore(dir) await store.ready() - const primaryKeyFile = await fsp.readFile(path.join(dir, 'primary-key')) - t.alike(primaryKeyFile, store.primaryKey) - try { const store2 = new Corestore(dir, { persistentKey: false }) await store2.ready() @@ -501,6 +498,9 @@ test('persistent primary key', async function (t) { } await store.close() + + const primaryKeyFile = await fsp.readFile(path.join(dir, 'primary-key')) + t.alike(primaryKeyFile, store.primaryKey) }) test('non-persistent primary key', async function (t) { From c7097b692914b29e921e6e93c1281111eee29aea Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 4 Jan 2023 20:48:47 -0300 Subject: [PATCH 5/7] fixing win --- test/all.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/all.js b/test/all.js index 2c38d7a..ecaa81a 100644 --- a/test/all.js +++ b/test/all.js @@ -509,9 +509,6 @@ test('non-persistent primary key', async function (t) { const store = new Corestore(dir, { persistentKey: false }) await store.ready() - const primaryKeyFile = await fsp.readFile(path.join(dir, 'primary-key')) - t.alike(primaryKeyFile, b4a.alloc(0)) - try { const store2 = new Corestore(dir, { persistentKey: false }) await store2.ready() @@ -521,6 +518,9 @@ test('non-persistent primary key', async function (t) { } await store.close() + + const primaryKeyFile = await fsp.readFile(path.join(dir, 'primary-key')) + t.alike(primaryKeyFile, b4a.alloc(0)) }) test('re open with non-persistent primary key', async function (t) { From 1fd350cef553afc749407926e9a4f021d4eb8b32 Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 4 Jan 2023 20:49:31 -0300 Subject: [PATCH 6/7] Update all.js --- test/all.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/all.js b/test/all.js index ecaa81a..ecd7d32 100644 --- a/test/all.js +++ b/test/all.js @@ -537,6 +537,9 @@ test('re open with non-persistent primary key', async function (t) { await store2.ready() t.unlike(store2.primaryKey, key) await store2.close() + + const primaryKeyFile = await fsp.readFile(path.join(dir, 'primary-key')) + t.alike(primaryKeyFile, b4a.alloc(0)) }) function tmpdir () { From e405776bf7d3aea2f7e2962aa2c747b4f390b20b Mon Sep 17 00:00:00 2001 From: Lucas Barrena Date: Wed, 4 Jan 2023 21:01:09 -0300 Subject: [PATCH 7/7] Update all.js --- test/all.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/all.js b/test/all.js index ecd7d32..4683cb6 100644 --- a/test/all.js +++ b/test/all.js @@ -509,6 +509,9 @@ test('non-persistent primary key', async function (t) { const store = new Corestore(dir, { persistentKey: false }) await store.ready() + t.is(store.primaryKey.length, 32) + t.unlike(store.primaryKey, b4a.alloc(32)) + try { const store2 = new Corestore(dir, { persistentKey: false }) await store2.ready()