corestore 6.10.1 → 6.12.0

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 (3) hide show
  1. package/README.md +1 -0
  2. package/index.js +21 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -81,6 +81,7 @@ Useful when an application wants to accept an optional Corestore, but needs to m
81
81
  {
82
82
  primaryKey, // Overrides the primaryKey for this session
83
83
  namespace, // If set to null it will reset to the DEFAULT_NAMESPACE
84
+ detach: true // By disabling this, closing the session will also close the store that created the session
84
85
  }
85
86
  ```
86
87
 
package/index.js CHANGED
@@ -27,6 +27,7 @@ module.exports = class Corestore extends ReadyResource {
27
27
  this.cores = root ? root.cores : new Map()
28
28
  this.cache = !!opts.cache
29
29
  this.primaryKey = opts.primaryKey || null
30
+ this.passive = !!opts.passive
30
31
 
31
32
  this._keyStorage = null
32
33
  this._bootstrap = opts._bootstrap || null
@@ -36,6 +37,7 @@ module.exports = class Corestore extends ReadyResource {
36
37
  this._replicationStreams = root ? root._replicationStreams : []
37
38
  this._overwrite = opts.overwrite === true
38
39
  this._readonly = opts.writable === false
40
+ this._attached = opts._attached || null
39
41
 
40
42
  this._sessions = new Set() // sessions for THIS namespace
41
43
  this._rootStoreSessions = new Set()
@@ -362,15 +364,24 @@ module.exports = class Corestore extends ReadyResource {
362
364
  const isExternal = isStream(isInitiator) || !!(opts && opts.stream)
363
365
  const stream = Hypercore.createProtocolStream(isInitiator, {
364
366
  ...opts,
365
- ondiscoverykey: discoveryKey => {
367
+ ondiscoverykey: async discoveryKey => {
366
368
  const core = this.get({ _discoveryKey: discoveryKey })
367
- return core.ready().catch(safetyCatch)
369
+
370
+ try {
371
+ await core.ready()
372
+ } catch {
373
+ return
374
+ }
375
+
376
+ if (this.passive && !core.closing) core.replicate(stream, { session: true })
368
377
  }
369
378
  })
370
379
 
371
- for (const core of this.cores.values()) {
372
- if (!core.opened || core.closing) continue // If the core is not opened, it will be replicated in preload.
373
- core.replicate(stream, { session: true })
380
+ if (!this.passive) {
381
+ for (const core of this.cores.values()) {
382
+ if (!core.opened || core.closing) continue // If the core is not opened, it will be replicated in preload.
383
+ core.replicate(stream, { session: true })
384
+ }
374
385
  }
375
386
 
376
387
  const streamRecord = { stream, isExternal }
@@ -395,6 +406,7 @@ module.exports = class Corestore extends ReadyResource {
395
406
  namespace: this._namespace,
396
407
  cache: this.cache,
397
408
  writable: !this._readonly,
409
+ _attached: opts && opts.detach === false ? this : null,
398
410
  _root: this._root,
399
411
  ...opts
400
412
  })
@@ -431,9 +443,13 @@ module.exports = class Corestore extends ReadyResource {
431
443
 
432
444
  async _close () {
433
445
  this._root._rootStoreSessions.delete(this)
446
+
434
447
  await this._closeNamespace()
448
+
435
449
  if (this._root === this) {
436
450
  await this._closePrimaryNamespace()
451
+ } else if (this._attached) {
452
+ await this._attached.close()
437
453
  }
438
454
  }
439
455
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.10.1",
3
+ "version": "6.12.0",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {