corestore 6.0.1-alpha.18 → 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
@@ -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
  ? {
@@ -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,
@@ -355,6 +364,10 @@ function deriveSeed (primaryKey, namespace, name) {
355
364
  return out
356
365
  }
357
366
 
367
+ function defaultCache () {
368
+ return new Xache({ maxSize: 65536, maxAge: 0 })
369
+ }
370
+
358
371
  function isStream (s) {
359
372
  return typeof s === 'object' && s && typeof s.pipe === 'function'
360
373
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.0.1-alpha.18",
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' })
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
+ })