corestore 7.0.1 → 7.0.3

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 +34 -8
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -136,6 +136,7 @@ class Corestore extends ReadyResource {
136
136
  this.streamTracker = this.root ? this.root.streamTracker : new StreamTracker()
137
137
  this.cores = this.root ? this.root.cores : new CoreTracker()
138
138
  this.sessions = new SessionTracker()
139
+ this.corestores = this.root ? this.root.corestores : new Set()
139
140
  this.globalCache = this.root ? this.root.globalCache : (opts.globalCache || null)
140
141
  this.primaryKey = this.root ? this.root.primaryKey : (opts.primaryKey || null)
141
142
  this.ns = opts.namespace || DEFAULT_NAMESPACE
@@ -147,6 +148,8 @@ class Corestore extends ReadyResource {
147
148
 
148
149
  this._ongcBound = this._ongc.bind(this)
149
150
 
151
+ if (this.root) this.corestores.add(this)
152
+
150
153
  this.ready().catch(noop)
151
154
  }
152
155
 
@@ -171,6 +174,7 @@ class Corestore extends ReadyResource {
171
174
  }
172
175
 
173
176
  session (opts) {
177
+ this._maybeClosed()
174
178
  const root = this.root || this
175
179
  return new Corestore(null, { ...opts, root })
176
180
  }
@@ -209,14 +213,22 @@ class Corestore extends ReadyResource {
209
213
  }
210
214
 
211
215
  async _close () {
212
- const sessions = []
216
+ const closing = []
213
217
  const hanging = [...this.sessions]
214
- for (const sess of hanging) sessions.push(sess.close())
218
+ for (const sess of hanging) closing.push(sess.close())
215
219
 
216
220
  if (this.watchers !== null) this.cores.unwatch(this)
217
221
 
218
- await Promise.all(sessions)
219
- if (this.root !== null) return
222
+ if (this.root !== null) {
223
+ await Promise.all(closing)
224
+ return
225
+ }
226
+
227
+ for (const store of this.corestores) {
228
+ closing.push(store.close())
229
+ }
230
+
231
+ await Promise.all(closing)
220
232
 
221
233
  const cores = []
222
234
  for (const core of this.cores) cores.push(core.close())
@@ -239,6 +251,8 @@ class Corestore extends ReadyResource {
239
251
  }
240
252
 
241
253
  replicate (isInitiator, opts) {
254
+ this._maybeClosed()
255
+
242
256
  const isExternal = isStream(isInitiator)
243
257
  const stream = Hypercore.createProtocolStream(isInitiator, {
244
258
  ...opts,
@@ -267,7 +281,15 @@ class Corestore extends ReadyResource {
267
281
  return stream
268
282
  }
269
283
 
284
+ _maybeClosed () {
285
+ if (this.closing || (this.root !== null && this.root.closing)) {
286
+ throw new Error('Corestore is closed')
287
+ }
288
+ }
289
+
270
290
  get (opts) {
291
+ this._maybeClosed()
292
+
271
293
  if (b4a.isBuffer(opts) || typeof opts === 'string') opts = { key: opts }
272
294
  if (!opts) opts = {}
273
295
 
@@ -278,8 +300,9 @@ class Corestore extends ReadyResource {
278
300
  ongc: null,
279
301
  core: null,
280
302
  active: opts.active !== false,
281
- encryptionKey: opts.encryptionKey || null,
282
- isBlockKey: !!opts.isBlockKey,
303
+ encryption: opts.encryption || null,
304
+ encryptionKey: opts.encryptionKey || null, // back compat, should remove
305
+ isBlockKey: !!opts.isBlockKey, // back compat, should remove
283
306
  valueEncoding: opts.valueEncoding || null,
284
307
  exclusive: !!opts.exclusive,
285
308
  manifest: opts.manifest || null,
@@ -319,6 +342,8 @@ class Corestore extends ReadyResource {
319
342
  if (this.opened === false) await this.ready()
320
343
 
321
344
  const discoveryKey = opts.name ? await this.storage.getAlias({ name: opts.name, namespace: this.ns }) : null
345
+ this._maybeClosed()
346
+
322
347
  const core = this._getCore(discoveryKey, opts)
323
348
 
324
349
  return {
@@ -326,8 +351,9 @@ class Corestore extends ReadyResource {
326
351
  core,
327
352
  sessions: this.sessions.get(core.id),
328
353
  ongc: this._ongcBound,
329
- encryptionKey: opts.encryptionKey || null,
330
- isBlockKey: !!opts.isBlockKey
354
+ encryption: opts.encryption || null,
355
+ encryptionKey: opts.encryptionKey || null, // back compat, should remove
356
+ isBlockKey: !!opts.isBlockKey // back compat, should remove
331
357
  }
332
358
  }
333
359
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "7.0.1",
3
+ "version": "7.0.3",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [