Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opts.persistentKey (to avoid writing key to disk) #43

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use a better name i think.


this._keyStorage = null
this._namespace = opts.namespace || DEFAULT_NAMESPACE
Expand Down Expand Up @@ -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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont write anything if you are not persisting. just return before instead and resolve

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the issue is that we still need to lock from the storage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I don't remember if without writing then the file exists anyway, maybe it does, will check later!

if (err) return reject(err)
return resolve(key)
})
Expand Down
63 changes: 63 additions & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('..')

Expand Down Expand Up @@ -482,6 +483,68 @@ 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()

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)
}

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) {
const dir = tmpdir()

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()
t.fail('should have failed')
} catch (error) {
t.is(error.code, 'ELOCKED', error.code)
}

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) {
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()

const store2 = new Corestore(dir, { persistentKey: false })
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 () {
return path.join(os.tmpdir(), 'corestore-' + Math.random().toString(16).slice(2))
}
Expand Down