corestore 6.8.0 → 6.8.2
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.
- package/index.js +31 -31
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -185,34 +185,17 @@ module.exports = class Corestore extends ReadyResource {
|
|
|
185
185
|
return rw
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
async _preload (opts) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (!rw) return
|
|
199
|
-
rw.write.unlock()
|
|
200
|
-
if (rw.write.waiting === 0) this._locks.delete(id)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
try {
|
|
204
|
-
while (this.cores.has(id)) {
|
|
205
|
-
const existing = this.cores.get(id)
|
|
206
|
-
if (existing.opened && !existing.closing) return { from: existing, keyPair, auth }
|
|
207
|
-
if (existing.closing) {
|
|
208
|
-
await existing.close()
|
|
209
|
-
} else {
|
|
210
|
-
await existing.ready().catch(safetyCatch)
|
|
211
|
-
}
|
|
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)
|
|
212
198
|
}
|
|
213
|
-
} catch (err) {
|
|
214
|
-
release()
|
|
215
|
-
throw err
|
|
216
199
|
}
|
|
217
200
|
|
|
218
201
|
const userData = {}
|
|
@@ -250,12 +233,10 @@ module.exports = class Corestore extends ReadyResource {
|
|
|
250
233
|
}
|
|
251
234
|
}, () => {
|
|
252
235
|
this.cores.delete(id)
|
|
253
|
-
release()
|
|
254
236
|
})
|
|
255
237
|
core.once('close', () => {
|
|
256
238
|
this._emitCore('core-close', core)
|
|
257
239
|
this.cores.delete(id)
|
|
258
|
-
release()
|
|
259
240
|
})
|
|
260
241
|
core.on('conflict', (len, fork, proof) => {
|
|
261
242
|
this.emit('conflict', core, len, fork, proof)
|
|
@@ -291,12 +272,27 @@ module.exports = class Corestore extends ReadyResource {
|
|
|
291
272
|
if (opts.cache !== false) {
|
|
292
273
|
opts.cache = opts.cache === true || (this.cache && !opts.cache) ? defaultCache() : opts.cache
|
|
293
274
|
}
|
|
275
|
+
if (this._readonly && opts.writable !== false) {
|
|
276
|
+
opts.writable = false
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
let rw = null
|
|
280
|
+
let id = null
|
|
294
281
|
|
|
295
282
|
const core = new Hypercore(null, {
|
|
296
|
-
writable: !this._readonly,
|
|
297
283
|
...opts,
|
|
298
284
|
name: null,
|
|
299
|
-
preload: () =>
|
|
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
|
+
}
|
|
300
296
|
})
|
|
301
297
|
|
|
302
298
|
this._sessions.add(core)
|
|
@@ -309,6 +305,10 @@ module.exports = class Corestore extends ReadyResource {
|
|
|
309
305
|
// but the lifecycle for those are pretty short so prob not worth the complexity
|
|
310
306
|
// as _decFindingPeers clear them all.
|
|
311
307
|
this._sessions.delete(core)
|
|
308
|
+
|
|
309
|
+
if (!rw) return
|
|
310
|
+
rw.write.unlock()
|
|
311
|
+
if (rw.write.waiting === 0) this._locks.delete(id)
|
|
312
312
|
}
|
|
313
313
|
|
|
314
314
|
core.ready().catch(gc)
|