corestore 6.0.1-alpha.16 → 6.0.1-alpha.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -3,6 +3,7 @@ const safetyCatch = require('safety-catch')
3
3
  const crypto = require('hypercore-crypto')
4
4
  const sodium = require('sodium-universal')
5
5
  const Hypercore = require('hypercore')
6
+ const Xache = require('xache')
6
7
  const b4a = require('b4a')
7
8
 
8
9
  const [NS] = crypto.namespace('corestore', 1)
@@ -20,6 +21,7 @@ module.exports = class Corestore extends EventEmitter {
20
21
  this.storage = Hypercore.defaultStorage(storage, { lock: PRIMARY_KEY_FILE_NAME })
21
22
  this.cores = opts._cores || new Map()
22
23
  this.primaryKey = null
24
+ this.cache = !!opts.cache
23
25
 
24
26
  this._keyStorage = null
25
27
  this._primaryKey = opts.primaryKey
@@ -153,10 +155,10 @@ module.exports = class Corestore extends EventEmitter {
153
155
  while (this.cores.has(id)) {
154
156
  const existing = this.cores.get(id)
155
157
  if (existing.opened && !existing.closing) return { from: existing, keyPair, auth }
156
- if (!existing.opened) {
157
- await existing.ready().catch(safetyCatch)
158
- } else if (existing.closing) {
158
+ if (existing.closing) {
159
159
  await existing.close()
160
+ } else {
161
+ await existing.ready().catch(safetyCatch)
160
162
  }
161
163
  }
162
164
 
@@ -175,6 +177,7 @@ module.exports = class Corestore extends EventEmitter {
175
177
  encryptionKey: opts.encryptionKey || null,
176
178
  userData,
177
179
  auth,
180
+ cache: opts.cache,
178
181
  createIfMissing: !opts._discoveryKey,
179
182
  keyPair: keyPair && keyPair.publicKey
180
183
  ? {
@@ -211,7 +214,7 @@ module.exports = class Corestore extends EventEmitter {
211
214
  auth: {
212
215
  sign: (msg) => sign(keyPair, msg),
213
216
  verify: (signable, signature) => {
214
- return sodium.crypto_sign_detached(signature, signable, keyPair.publicKey)
217
+ return crypto.verify(signable, signature, keyPair.publicKey)
215
218
  }
216
219
  }
217
220
  }
@@ -224,6 +227,11 @@ module.exports = class Corestore extends EventEmitter {
224
227
 
225
228
  get (opts = {}) {
226
229
  opts = validateGetOptions(opts)
230
+
231
+ if (opts.cache !== false) {
232
+ opts.cache = opts.cache === true || (this.cache && !opts.cache) ? defaultCache() : opts.cache
233
+ }
234
+
227
235
  const core = new Hypercore(null, {
228
236
  ...opts,
229
237
  name: null,
@@ -279,6 +287,7 @@ module.exports = class Corestore extends EventEmitter {
279
287
  return new Corestore(this.storage, {
280
288
  primaryKey: this._opening.then(() => this.primaryKey),
281
289
  namespace: generateNamespace(this._namespace, name),
290
+ cache: this.cache,
282
291
  _opening: this._opening,
283
292
  _cores: this.cores,
284
293
  _streams: this._replicationStreams,
@@ -321,9 +330,7 @@ module.exports = class Corestore extends EventEmitter {
321
330
 
322
331
  function sign (keyPair, message) {
323
332
  if (!keyPair.secretKey) throw new Error('Invalid key pair')
324
- const signature = b4a.allocUnsafe(sodium.crypto_sign_BYTES)
325
- sodium.crypto_sign_detached(signature, message, keyPair.secretKey)
326
- return signature
333
+ return crypto.sign(message, keyPair.secretKey)
327
334
  }
328
335
 
329
336
  function validateGetOptions (opts) {
@@ -357,6 +364,10 @@ function deriveSeed (primaryKey, namespace, name) {
357
364
  return out
358
365
  }
359
366
 
367
+ function defaultCache () {
368
+ return new Xache({ maxSize: 65536, maxAge: 0 })
369
+ }
370
+
360
371
  function isStream (s) {
361
372
  return typeof s === 'object' && s && typeof s.pipe === 'function'
362
373
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.0.1-alpha.16",
3
+ "version": "6.0.1-alpha.19",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -30,6 +30,7 @@
30
30
  "hypercore": "next",
31
31
  "hypercore-crypto": "^3.2.1",
32
32
  "safety-catch": "^1.0.1",
33
- "sodium-universal": "^3.0.4"
33
+ "sodium-universal": "^3.0.4",
34
+ "xache": "^1.1.0"
34
35
  }
35
36
  }
package/test/all.js CHANGED
@@ -1,4 +1,4 @@
1
- const { test, configure } = require('brittle')
1
+ const test = require('brittle')
2
2
  const crypto = require('hypercore-crypto')
3
3
  const ram = require('random-access-memory')
4
4
  const os = require('os')
@@ -8,8 +8,6 @@ const sodium = require('sodium-universal')
8
8
 
9
9
  const Corestore = require('..')
10
10
 
11
- configure({ serial: true })
12
-
13
11
  test('basic get with caching', async function (t) {
14
12
  const store = new Corestore(ram)
15
13
  const core1a = store.get({ name: 'core-1' })
@@ -287,6 +285,44 @@ test('different primary keys yield different keypairs', async function (t) {
287
285
  t.unlike(kp1.publicKey, kp2.publicKey)
288
286
  })
289
287
 
288
+ test('keypair auth sign', async function (t) {
289
+ const store = new Corestore(ram)
290
+ const keyPair = await store.createKeyPair('foo')
291
+ const message = b4a.from('hello world')
292
+
293
+ const sig = keyPair.auth.sign(message)
294
+
295
+ t.is(sig.length, 64)
296
+ t.ok(crypto.verify(message, sig, keyPair.publicKey))
297
+ t.absent(crypto.verify(message, b4a.alloc(64), keyPair.publicKey))
298
+ })
299
+
300
+ test('keypair auth verify', async function (t) {
301
+ const store = new Corestore(ram)
302
+ const keyPair = await store.createKeyPair('foo')
303
+ const message = b4a.from('hello world')
304
+
305
+ const sig = crypto.sign(message, keyPair.secretKey)
306
+
307
+ t.is(sig.length, 64)
308
+ t.ok(keyPair.auth.verify(message, sig))
309
+ t.absent(keyPair.auth.verify(message, b4a.alloc(64)))
310
+ })
311
+
312
+ test('core caching after reopen regression', async function (t) {
313
+ const store = new Corestore(ram)
314
+ const core = store.get({ name: 'test-core' })
315
+ await core.ready()
316
+
317
+ core.close()
318
+ await core.opening
319
+
320
+ const core2 = store.get({ name: 'test-core' })
321
+ await core2.ready()
322
+
323
+ t.pass('did not infinite loop')
324
+ })
325
+
290
326
  function tmpdir () {
291
327
  return path.join(os.tmpdir(), 'corestore-' + Math.random().toString(16).slice(2))
292
328
  }
package/test/cache.js ADDED
@@ -0,0 +1,46 @@
1
+ const test = require('brittle')
2
+ const RAM = require('random-access-memory')
3
+
4
+ const Corestore = require('..')
5
+
6
+ test('core cache', async function (t) {
7
+ const store = new Corestore(RAM, { cache: true })
8
+
9
+ const core = store.get({ name: 'core' })
10
+ await core.append(['a', 'b', 'c'])
11
+
12
+ const p = core.get(0)
13
+ const q = core.get(0)
14
+
15
+ t.is(await p, await q)
16
+ })
17
+
18
+ test('clear cache on truncate', async function (t) {
19
+ const store = new Corestore(RAM, { cache: true })
20
+
21
+ const core = store.get({ name: 'core' })
22
+ await core.append(['a', 'b', 'c'])
23
+
24
+ const p = core.get(0)
25
+
26
+ await core.truncate(0)
27
+ await core.append('d')
28
+
29
+ const q = core.get(0)
30
+
31
+ t.alike(await p, Buffer.from('a'))
32
+ t.alike(await q, Buffer.from('d'))
33
+ })
34
+
35
+ test('core cache on namespace', async function (t) {
36
+ const store = new Corestore(RAM, { cache: true })
37
+ const ns1 = store.namespace('test-namespace-1')
38
+
39
+ const c1 = store.get({ name: 'test-core' })
40
+ const c2 = ns1.get({ name: 'test-core' })
41
+
42
+ await Promise.all([c1.ready(), c2.ready()])
43
+
44
+ t.ok(c1.cache)
45
+ t.ok(c2.cache)
46
+ })