corestore 7.0.15 → 7.0.17

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.
Files changed (2) hide show
  1. package/index.js +19 -2
  2. package/package.json +2 -1
package/index.js CHANGED
@@ -4,6 +4,7 @@ const ReadyResource = require('ready-resource')
4
4
  const sodium = require('sodium-universal')
5
5
  const crypto = require('hypercore-crypto')
6
6
  const ID = require('hypercore-id-encoding')
7
+ const { STORAGE_EMPTY } = require('hypercore-errors')
7
8
 
8
9
  const auditStore = require('./lib/audit.js')
9
10
 
@@ -119,6 +120,11 @@ class CoreTracker {
119
120
  return core
120
121
  }
121
122
 
123
+ opened (id) {
124
+ const core = this.map.get(id)
125
+ return !!(core && core.opened && !core.closing)
126
+ }
127
+
122
128
  get (id) {
123
129
  // we allow you do call this from the outside, so support normal buffers also
124
130
  if (b4a.isBuffer(id)) id = b4a.toString(id, 'hex')
@@ -352,7 +358,7 @@ class Corestore extends ReadyResource {
352
358
 
353
359
  async _attachMaybe (muxer, discoveryKey) {
354
360
  if (this.opened === false) await this.ready()
355
- if (this.cores.get(toHex(discoveryKey)) === null && !(await this.storage.has(discoveryKey, { ifMigrated: true }))) return
361
+ if (!this.cores.opened(toHex(discoveryKey)) && !(await this.storage.has(discoveryKey, { ifMigrated: true }))) return
356
362
  if (this.closing) return
357
363
 
358
364
  const core = this._openCore(discoveryKey, { createIfMissing: false })
@@ -437,6 +443,11 @@ class Corestore extends ReadyResource {
437
443
  return this._makeSession(conf)
438
444
  }
439
445
 
446
+ if (opts.discoveryKey && !opts.key && !opts.manifest) {
447
+ conf.preload = this._preloadCheckIfExists(opts)
448
+ return this._makeSession(conf)
449
+ }
450
+
440
451
  // if not not we can sync create it, which just is easier for the
441
452
  // upstream user in terms of guarantees (key is there etc etc)
442
453
  const core = this._openCore(null, opts)
@@ -459,6 +470,12 @@ class Corestore extends ReadyResource {
459
470
  return createKeyPair(this.primaryKey, ns, name)
460
471
  }
461
472
 
473
+ async _preloadCheckIfExists (opts) {
474
+ const has = await this.storage.has(opts.discoveryKey)
475
+ if (!has) throw STORAGE_EMPTY('No Hypercore is stored here')
476
+ return this._preload(opts)
477
+ }
478
+
462
479
  async _preload (opts) {
463
480
  if (opts.preload) opts = { ...opts, ...(await opts.preload) }
464
481
  if (this.opened === false) await this.ready()
@@ -518,7 +535,7 @@ class Corestore extends ReadyResource {
518
535
  if (existing && !existing.closing) return existing
519
536
 
520
537
  const core = Hypercore.createCore(this.storage, {
521
- preopen: existing ? existing.closing : null, // always wait for the prev one to close first in any case...
538
+ preopen: (existing && existing.opened) ? existing.closing : null, // always wait for the prev one to close first in any case...
522
539
  eagerUpgrade: true,
523
540
  notDownloadingLinger: opts.notDownloadingLinger,
524
541
  allowFork: opts.allowFork !== false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "7.0.15",
3
+ "version": "7.0.17",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -11,6 +11,7 @@
11
11
  "b4a": "^1.6.7",
12
12
  "hypercore": "^11.0.0",
13
13
  "hypercore-crypto": "^3.4.2",
14
+ "hypercore-errors": "^1.4.0",
14
15
  "hypercore-id-encoding": "^1.3.0",
15
16
  "ready-resource": "^1.1.1",
16
17
  "sodium-universal": "^4.0.1"