Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

validateGetOptions should prioritize key over name #20

Open
wants to merge 1 commit into
base: master
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
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ function validateGetOptions (opts) {
opts.publicKey = opts.keyPair.publicKey
opts.secretKey = opts.keyPair.secretKey
}
if (opts.publicKey && opts.name) {
// The key takes precedence over the name
opts.name = null
}
if (opts.name && typeof opts.name !== 'string') throw new Error('name option must be a String')
if (opts.name && opts.secretKey) throw new Error('Cannot provide both a name and a secret key')
if (opts.publicKey && !b4a.isBuffer(opts.publicKey)) throw new Error('publicKey option must be a Buffer or Uint8Array')
Expand Down
11 changes: 11 additions & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ test('core caching after reopen regression', async function (t) {
t.pass('did not infinite loop')
})

test('when passed both key and name options, the key takes precedence', async function (t) {
const store = new Corestore(ram)
const core1 = store.get({ name: 'test-core' })
await core1.ready()

const core2 = store.get({ key: core1.key, name: 'test-core-2' })
await core2.ready()

t.alike(core1.key, core2.key)
})

function tmpdir () {
return path.join(os.tmpdir(), 'corestore-' + Math.random().toString(16).slice(2))
}
Expand Down
9 changes: 9 additions & 0 deletions test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ test('core cache on namespace', async function (t) {
t.ok(c1.cache)
t.ok(c2.cache)
})

test('false cache option does not enable caching', async function (t) {
const store = new Corestore(RAM)

const core = store.get({ name: 'core' })
await core.ready()

t.absent(core.cache)
})