corestore 6.8.1 → 6.8.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 +32 -33
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -185,35 +185,17 @@ module.exports = class Corestore extends ReadyResource {
185
185
  return rw
186
186
  }
187
187
 
188
- async _preload (opts) {
189
- if (!this.primaryKey) await this.ready()
190
-
191
- const { discoveryKey, keyPair, auth } = await this._generateKeys(opts)
192
- const id = b4a.toString(discoveryKey, 'hex')
193
-
194
- const readonly = opts.writable === false || this._readonly
195
- const rw = (opts && opts.exclusive && !readonly && keyPair) ? this._getLock(id) : null
196
-
197
- if (rw) await rw.write.lock()
198
- const release = () => {
199
- if (!rw) return
200
- rw.write.unlock()
201
- if (rw.write.waiting === 0) this._locks.delete(id)
202
- }
203
-
204
- try {
205
- while (this.cores.has(id)) {
206
- const existing = this.cores.get(id)
207
- if (existing.opened && !existing.closing) return { from: existing, keyPair, auth }
208
- if (existing.closing) {
209
- await existing.close()
210
- } else {
211
- await existing.ready().catch(safetyCatch)
212
- }
188
+ async _preload (id, keys, opts) {
189
+ const { keyPair, auth } = keys
190
+
191
+ while (this.cores.has(id)) {
192
+ const existing = this.cores.get(id)
193
+ if (existing.opened && !existing.closing) return { from: existing, keyPair, auth }
194
+ if (existing.closing) {
195
+ await existing.close()
196
+ } else {
197
+ await existing.ready().catch(safetyCatch)
213
198
  }
214
- } catch (err) {
215
- release()
216
- throw err
217
199
  }
218
200
 
219
201
  const userData = {}
@@ -251,12 +233,10 @@ module.exports = class Corestore extends ReadyResource {
251
233
  }
252
234
  }, () => {
253
235
  this.cores.delete(id)
254
- release()
255
236
  })
256
237
  core.once('close', () => {
257
238
  this._emitCore('core-close', core)
258
239
  this.cores.delete(id)
259
- release()
260
240
  })
261
241
  core.on('conflict', (len, fork, proof) => {
262
242
  this.emit('conflict', core, len, fork, proof)
@@ -286,18 +266,33 @@ module.exports = class Corestore extends ReadyResource {
286
266
  }
287
267
 
288
268
  get (opts = {}) {
289
- if (this._root.closing) throw new Error('The corestore is closed')
269
+ if (this.closing || this._root.closing) throw new Error('The corestore is closed')
290
270
  opts = validateGetOptions(opts)
291
271
 
292
272
  if (opts.cache !== false) {
293
273
  opts.cache = opts.cache === true || (this.cache && !opts.cache) ? defaultCache() : opts.cache
294
274
  }
275
+ if (this._readonly && opts.writable !== false) {
276
+ opts.writable = false
277
+ }
278
+
279
+ let rw = null
280
+ let id = null
295
281
 
296
282
  const core = new Hypercore(null, {
297
- writable: !this._readonly,
298
283
  ...opts,
299
284
  name: null,
300
- preload: () => this._preload(opts)
285
+ preload: async () => {
286
+ if (!this.primaryKey) await this.ready()
287
+
288
+ const keys = await this._generateKeys(opts)
289
+
290
+ id = b4a.toString(keys.discoveryKey, 'hex')
291
+ rw = (opts.exclusive && opts.writable !== false && keys.keyPair) ? this._getLock(id) : null
292
+
293
+ if (rw) await rw.write.lock()
294
+ return await this._preload(id, keys, opts)
295
+ }
301
296
  })
302
297
 
303
298
  this._sessions.add(core)
@@ -310,6 +305,10 @@ module.exports = class Corestore extends ReadyResource {
310
305
  // but the lifecycle for those are pretty short so prob not worth the complexity
311
306
  // as _decFindingPeers clear them all.
312
307
  this._sessions.delete(core)
308
+
309
+ if (!rw) return
310
+ rw.write.unlock()
311
+ if (rw.write.waiting === 0) this._locks.delete(id)
313
312
  }
314
313
 
315
314
  core.ready().catch(gc)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.8.1",
3
+ "version": "6.8.3",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {