autobee 2.1.2 → 2.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 (2) hide show
  1. package/index.js +81 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -265,6 +265,62 @@ module.exports = class Autobee extends ReadyResource {
265
265
  return w.views()
266
266
  }
267
267
 
268
+ // the cores a mirror should pin: our writer core, our views when we are
269
+ // trusted, and (with wait) the most recent trusted head so a mirror that
270
+ // forgets to add one still pins a valid entrypoint
271
+ async cores({ wait = true, local = true, all = false } = {}) {
272
+ if (!this._bootGuard.opened) await this._bootGuard.ready()
273
+
274
+ const result = { key: this.key, views: null, writers: [] }
275
+ const views = new Map()
276
+
277
+ if (local) {
278
+ result.writers.push(this.local.key)
279
+
280
+ if (await this.trusted.isTrusted(this.local.key, this.view)) {
281
+ const localViews = await this.writers.localWriter.views()
282
+ for (const { key } of localViews) {
283
+ views.set(b4a.toString(key, 'hex'), key)
284
+ }
285
+ }
286
+ }
287
+
288
+ if (wait) {
289
+ const head = await this.trusted.mostRecentTrusted(this.view, null)
290
+
291
+ if (head && (!local || !b4a.equals(head.key, this.local.key))) {
292
+ const oplog = await this._resolveOplogHint(head.key, head.length)
293
+ if (oplog && oplog.op.views) {
294
+ const skey = oplog.op.views.system.key
295
+ const vkey = oplog.op.views.view.key
296
+
297
+ views.set(b4a.toString(skey, 'hex'), skey)
298
+ views.set(b4a.toString(vkey, 'hex'), vkey)
299
+ }
300
+
301
+ result.writers.push(head.key)
302
+ }
303
+
304
+ if (all) {
305
+ const [viewCores, systemCores] = await Promise.all([
306
+ this.bee.cores({ local: false }),
307
+ this.system.bee.cores({ local: false })
308
+ ])
309
+
310
+ for (const key of viewCores) {
311
+ views.set(b4a.toString(key, 'hex'), key)
312
+ }
313
+ for (const key of systemCores) {
314
+ views.set(b4a.toString(key, 'hex'), key)
315
+ }
316
+ }
317
+ }
318
+
319
+ result.views = [...views.values()]
320
+
321
+ return result
322
+ }
323
+
268
324
  static getViewEncryption(bootstrap, encryptionKey, name) {
269
325
  return AutobeeEncryption.getViewEncryption(bootstrap, encryptionKey, name)
270
326
  }
@@ -288,6 +344,29 @@ module.exports = class Autobee extends ReadyResource {
288
344
  async _close() {
289
345
  this._interrupting = true
290
346
 
347
+ // the local core holds the exclusive lock but the local writer closes it
348
+ // early in the teardown, so hand the lock to a detached session that
349
+ // outlives the teardown and only release it once everything is down
350
+ let lock = null
351
+
352
+ if (this.local.exclusive) {
353
+ lock = this.store.session()
354
+ const local = lock.get({ key: this.local.key, active: false })
355
+ await local.ready()
356
+
357
+ // transfer exclusiveness, the detached session now unlocks on close
358
+ local.exclusive = true
359
+ this.local.exclusive = false
360
+ }
361
+
362
+ try {
363
+ await this._teardown()
364
+ } finally {
365
+ if (lock) await lock.close()
366
+ }
367
+ }
368
+
369
+ async _teardown() {
291
370
  try {
292
371
  await ApplyView.close(this.view, this)
293
372
  } catch (err) {
@@ -295,7 +374,6 @@ module.exports = class Autobee extends ReadyResource {
295
374
  }
296
375
 
297
376
  if (this.writers) await this.writers.close()
298
- await this.local.close()
299
377
  await this.system.close()
300
378
  await this._wakeup.close()
301
379
  if (this.bootstrap) await this.bootstrap.close()
@@ -310,6 +388,8 @@ module.exports = class Autobee extends ReadyResource {
310
388
  // don't rely on the store teardown to stop the notify watcher
311
389
  if (this._notifyHandler) this._notifyHandler.destroy()
312
390
 
391
+ await this.local.close()
392
+
313
393
  // rugpull the rest
314
394
  await this.store.close()
315
395
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "2.1.2",
3
+ "version": "2.2.1",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",