corestore 7.0.2 → 7.0.4

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 +32 -4
  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
  }
@@ -179,6 +183,10 @@ class Corestore extends ReadyResource {
179
183
  return this.session({ ...opts, namespace: generateNamespace(this.ns, name) })
180
184
  }
181
185
 
186
+ getAuth (discoveryKey) {
187
+ return this.storage.getAuth(discoveryKey)
188
+ }
189
+
182
190
  _ongc (session) {
183
191
  if (session.sessions.length === 0) this.sessions.gc(session.id)
184
192
  }
@@ -209,14 +217,22 @@ class Corestore extends ReadyResource {
209
217
  }
210
218
 
211
219
  async _close () {
212
- const sessions = []
220
+ const closing = []
213
221
  const hanging = [...this.sessions]
214
- for (const sess of hanging) sessions.push(sess.close())
222
+ for (const sess of hanging) closing.push(sess.close())
215
223
 
216
224
  if (this.watchers !== null) this.cores.unwatch(this)
217
225
 
218
- await Promise.all(sessions)
219
- if (this.root !== null) return
226
+ if (this.root !== null) {
227
+ await Promise.all(closing)
228
+ return
229
+ }
230
+
231
+ for (const store of this.corestores) {
232
+ closing.push(store.close())
233
+ }
234
+
235
+ await Promise.all(closing)
220
236
 
221
237
  const cores = []
222
238
  for (const core of this.cores) cores.push(core.close())
@@ -239,6 +255,8 @@ class Corestore extends ReadyResource {
239
255
  }
240
256
 
241
257
  replicate (isInitiator, opts) {
258
+ this._maybeClosed()
259
+
242
260
  const isExternal = isStream(isInitiator)
243
261
  const stream = Hypercore.createProtocolStream(isInitiator, {
244
262
  ...opts,
@@ -267,7 +285,15 @@ class Corestore extends ReadyResource {
267
285
  return stream
268
286
  }
269
287
 
288
+ _maybeClosed () {
289
+ if (this.closing || (this.root !== null && this.root.closing)) {
290
+ throw new Error('Corestore is closed')
291
+ }
292
+ }
293
+
270
294
  get (opts) {
295
+ this._maybeClosed()
296
+
271
297
  if (b4a.isBuffer(opts) || typeof opts === 'string') opts = { key: opts }
272
298
  if (!opts) opts = {}
273
299
 
@@ -320,6 +346,8 @@ class Corestore extends ReadyResource {
320
346
  if (this.opened === false) await this.ready()
321
347
 
322
348
  const discoveryKey = opts.name ? await this.storage.getAlias({ name: opts.name, namespace: this.ns }) : null
349
+ this._maybeClosed()
350
+
323
351
  const core = this._getCore(discoveryKey, opts)
324
352
 
325
353
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "7.0.2",
3
+ "version": "7.0.4",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "files": [