corestore 6.1.1 → 6.2.1

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 +13 -0
  2. package/index.js +10 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -66,6 +66,19 @@ const core1 = ns1.get({ name: 'main' }) // These will load different Hypercores
66
66
  const core2 = ns2.get({ name: 'main' })
67
67
  ```
68
68
 
69
+ #### `const storeB = storeA.session(opts)`
70
+ Create a new Corestore that shares resources with the original, like cache, cores, replication streams, and storage, while optionally resetting the namespace, overriding `primaryKey`.
71
+ Useful when an application wants to accept an optional Corestore, but needs to maintain a predictable key derivation.
72
+
73
+ `opts` are the same as the constructor options:
74
+
75
+ ```js
76
+ {
77
+ primaryKey, // Overrides the primaryKey for this session
78
+ namespace, // If set to null it will reset to the DEFAULT_NAMESPACE
79
+ }
80
+ ```
81
+
69
82
  ### License
70
83
  MIT
71
84
 
package/index.js CHANGED
@@ -79,7 +79,7 @@ module.exports = class Corestore extends EventEmitter {
79
79
  async _open () {
80
80
  if (this._root !== this) {
81
81
  await this._root._opening
82
- this.primaryKey = this._root.primaryKey
82
+ if (!this.primaryKey) this.primaryKey = this._root.primaryKey
83
83
  return
84
84
  }
85
85
 
@@ -200,6 +200,7 @@ module.exports = class Corestore extends EventEmitter {
200
200
  if (this._root._closing) throw new Error('The corestore is closed')
201
201
  this.cores.set(id, core)
202
202
  core.ready().then(() => {
203
+ if (core.closing) return // extra safety here as ready is a tick after open
203
204
  for (const { stream } of this._replicationStreams) {
204
205
  core.replicate(stream, { session: true })
205
206
  }
@@ -273,7 +274,7 @@ module.exports = class Corestore extends EventEmitter {
273
274
  })
274
275
 
275
276
  for (const core of this.cores.values()) {
276
- if (!core.opened) continue // If the core is not opened, it will be replicated in preload.
277
+ if (!core.opened || core.closing) continue // If the core is not opened, it will be replicated in preload.
277
278
  core.replicate(stream, { session: true })
278
279
  }
279
280
 
@@ -288,10 +289,15 @@ module.exports = class Corestore extends EventEmitter {
288
289
  }
289
290
 
290
291
  namespace (name) {
292
+ return this.session({ namespace: generateNamespace(this._namespace, name) })
293
+ }
294
+
295
+ session (opts) {
291
296
  return new Corestore(this.storage, {
292
- namespace: generateNamespace(this._namespace, name),
297
+ namespace: this._namespace,
293
298
  cache: this.cache,
294
- _root: this._root
299
+ _root: this._root,
300
+ ...opts
295
301
  })
296
302
  }
297
303
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.1.1",
3
+ "version": "6.2.1",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {