autobee 1.0.4 → 1.0.5

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 CHANGED
@@ -101,7 +101,7 @@ module.exports = class Autobee extends ReadyResource {
101
101
 
102
102
  this.wakeupCapability = null
103
103
  this._wakeup = new AutobeeWakeup(this, handlers)
104
- this._previousDrain = 0
104
+ this.previousDrain = 0
105
105
 
106
106
  this.ready().catch(noop)
107
107
  }
@@ -141,6 +141,7 @@ module.exports = class Autobee extends ReadyResource {
141
141
  }
142
142
 
143
143
  _registerWakeup() {
144
+ if (!this.wakeupCapability) return
144
145
  this._wakeup.recouple()
145
146
  this._wakeup.setCapability(this.wakeupCapability.key, this.wakeupCapability.discoveryKey)
146
147
  }
@@ -242,6 +243,7 @@ module.exports = class Autobee extends ReadyResource {
242
243
  this.discoveryKey = result.bootstrap.core.discoveryKey
243
244
  this.id = result.bootstrap.core.id
244
245
  this.encryptionKey = result.encryptionKey
246
+ this.previousDrain = result.previousDrain
245
247
 
246
248
  if (this.encrypted) {
247
249
  asserts.assert(this.encryptionKey !== null, 'Encryption key is expected')
@@ -261,12 +263,14 @@ module.exports = class Autobee extends ReadyResource {
261
263
 
262
264
  this._registerWakeup()
263
265
 
264
- if (this.bootstrap !== this.local) {
265
- await this.bootstrap.setGroup(this.wakeupCapability.discoveryKey)
266
- }
266
+ if (this.wakeupCapability) {
267
+ if (this.bootstrap !== this.local) {
268
+ await this.bootstrap.setGroup(this.wakeupCapability.discoveryKey)
269
+ }
267
270
 
268
- this._notifyHandler = this.store.notifyGroup(this.wakeupCapability.discoveryKey)
269
- this._notifyHandler.on('update', this._bumpSoonBound)
271
+ this._notifyHandler = this.store.notifyGroup(this.wakeupCapability.discoveryKey)
272
+ this._notifyHandler.on('update', this._bumpSoonBound)
273
+ }
270
274
 
271
275
  const system = result.system || EMPTY_HEAD
272
276
 
@@ -404,9 +408,11 @@ module.exports = class Autobee extends ReadyResource {
404
408
  async _flushWakeup() {
405
409
  const hints = this._wakeup.flush()
406
410
 
407
- for await (const key of this._notifyHandler.updates({ since: this.previousDrain })) {
408
- const hex = b4a.toString(key, 'hex')
409
- hints.set(hex, -1)
411
+ if (this._notifyHandler) {
412
+ for await (const key of this._notifyHandler.updates({ since: this.previousDrain })) {
413
+ const hex = b4a.toString(key, 'hex')
414
+ hints.set(hex, -1)
415
+ }
410
416
  }
411
417
 
412
418
  this.previousDrain = Date.now()
@@ -683,9 +689,20 @@ module.exports = class Autobee extends ReadyResource {
683
689
  }
684
690
 
685
691
  _storeBoot() {
692
+ const proms = []
693
+ proms.push(
694
+ this.local.setUserData(
695
+ 'autobee/previous-drain',
696
+ encoding.encodePreviousDrain(this.previousDrain)
697
+ )
698
+ )
699
+
686
700
  const boot = this.system.bootRecord()
687
- if (!boot) return
688
- return this.local.setUserData('autobee/head', encoding.encodeBootRecord(boot))
701
+ if (boot) {
702
+ proms.push(this.local.setUserData('autobee/head', encoding.encodeBootRecord(boot)))
703
+ }
704
+
705
+ return Promise.all(proms)
689
706
  }
690
707
 
691
708
  static decodeValue(buf, opts) {
package/lib/boot.js CHANGED
@@ -11,7 +11,8 @@ module.exports = async function boot(
11
11
  bootstrap: null,
12
12
  encryptionKey: null,
13
13
  system: null,
14
- flushes: null
14
+ flushes: null,
15
+ previousDrain: 0
15
16
  }
16
17
 
17
18
  const manifest = keyPair
@@ -91,15 +92,15 @@ module.exports = async function boot(
91
92
  await result.local.setUserData('referrer', result.key)
92
93
  }
93
94
 
94
- const [systemHead, encryptionKeyBuffer] = await Promise.all([
95
+ const [systemHead, encryptionKeyBuffer, prevDrainBuffer] = await Promise.all([
95
96
  result.local.getUserData('autobee/head'),
96
- result.local.getUserData('autobee/encryption')
97
+ result.local.getUserData('autobee/encryption'),
98
+ result.local.getUserData('autobee/previous-drain')
97
99
  ])
98
100
 
99
101
  if (systemHead) result.system = encoding.decodeBootRecord(systemHead)
100
- if (encryptionKeyBuffer) {
101
- result.encryptionKey = encryptionKeyBuffer
102
- }
102
+ if (encryptionKeyBuffer) result.encryptionKey = encryptionKeyBuffer
103
+ if (prevDrainBuffer) result.previousDrain = encoding.decodePreviousDrain(prevDrainBuffer)
103
104
 
104
105
  if (!result.encryptionKey && (encryptionKey || encrypt)) {
105
106
  if (!encryptionKey) {
package/lib/encoding.js CHANGED
@@ -18,6 +18,8 @@ exports.Oplog = Oplog
18
18
  exports.ManifestData = ManifestData
19
19
  exports.encodeBootRecord = encodeBootRecord
20
20
  exports.decodeBootRecord = decodeBootRecord
21
+ exports.encodePreviousDrain = encodePreviousDrain
22
+ exports.decodePreviousDrain = decodePreviousDrain
21
23
  exports.encodeSystemWriter = encodeSystemWriter
22
24
  exports.decodeSystemWriter = decodeSystemWriter
23
25
 
@@ -29,6 +31,14 @@ function decodeBootRecord(value) {
29
31
  return c.decode(Link, value)
30
32
  }
31
33
 
34
+ function encodePreviousDrain(d) {
35
+ return c.encode(c.uint, d)
36
+ }
37
+
38
+ function decodePreviousDrain(value) {
39
+ return c.decode(c.uint, value)
40
+ }
41
+
32
42
  function encodeSystemWriter(m) {
33
43
  return c.encode(SystemWriter, m)
34
44
  }
package/lib/writers.js CHANGED
@@ -126,7 +126,7 @@ class ActiveWriters {
126
126
  const core = this.auto.store.get({
127
127
  key,
128
128
  encryption,
129
- group: local ? null : this.auto.wakeupCapability.discoveryKey
129
+ group: local || !this.auto.wakeupCapability ? null : this.auto.wakeupCapability.discoveryKey
130
130
  })
131
131
 
132
132
  await core.ready()
@@ -264,7 +264,7 @@ class Writer {
264
264
  detach() {
265
265
  if (!this.isAttached) return
266
266
  this.isAttached = false
267
- this.download.destroy()
267
+ if (this.download) this.download.destroy()
268
268
  this.download = null
269
269
  this.writers.active.delete(this.id)
270
270
  this.removePending()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autobee",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Bee based autobase",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Holepunch Inc.",