autobee 1.0.4 → 1.0.6
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 +28 -11
- package/lib/boot.js +7 -6
- package/lib/encoding.js +10 -0
- package/lib/writers.js +19 -7
- package/package.json +1 -1
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.
|
|
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.
|
|
265
|
-
|
|
266
|
-
|
|
266
|
+
if (this.wakeupCapability) {
|
|
267
|
+
if (this.bootstrap !== this.local) {
|
|
268
|
+
await this.bootstrap.setGroup(this.wakeupCapability.discoveryKey)
|
|
269
|
+
}
|
|
267
270
|
|
|
268
|
-
|
|
269
|
-
|
|
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
|
-
|
|
408
|
-
const
|
|
409
|
-
|
|
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 (
|
|
688
|
-
|
|
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
|
-
|
|
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
|
@@ -83,11 +83,7 @@ class ActiveWriters {
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
for (const writer of this.active.values()) {
|
|
86
|
-
await writer.
|
|
87
|
-
|
|
88
|
-
if (writer !== this.localWriter && !writer.isIndexer) {
|
|
89
|
-
await writer.detachAndClose()
|
|
90
|
-
}
|
|
86
|
+
await writer.reset()
|
|
91
87
|
}
|
|
92
88
|
}
|
|
93
89
|
|
|
@@ -126,7 +122,7 @@ class ActiveWriters {
|
|
|
126
122
|
const core = this.auto.store.get({
|
|
127
123
|
key,
|
|
128
124
|
encryption,
|
|
129
|
-
group: local ? null : this.auto.wakeupCapability.discoveryKey
|
|
125
|
+
group: local || !this.auto.wakeupCapability ? null : this.auto.wakeupCapability.discoveryKey
|
|
130
126
|
})
|
|
131
127
|
|
|
132
128
|
await core.ready()
|
|
@@ -264,12 +260,28 @@ class Writer {
|
|
|
264
260
|
detach() {
|
|
265
261
|
if (!this.isAttached) return
|
|
266
262
|
this.isAttached = false
|
|
267
|
-
this.download.destroy()
|
|
263
|
+
if (this.download) this.download.destroy()
|
|
268
264
|
this.download = null
|
|
269
265
|
this.writers.active.delete(this.id)
|
|
270
266
|
this.removePending()
|
|
271
267
|
}
|
|
272
268
|
|
|
269
|
+
async reset() {
|
|
270
|
+
this.removePending()
|
|
271
|
+
|
|
272
|
+
if (this.waiting) {
|
|
273
|
+
this.writers.triggers.remove(this.waiting)
|
|
274
|
+
this.waiting = null
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const length = await this.update()
|
|
278
|
+
if (length < this.core.length) {
|
|
279
|
+
this.addPending()
|
|
280
|
+
} else if (this !== this.localWriter && this.weight < 2) {
|
|
281
|
+
await this.detachAndClose()
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
273
285
|
close() {
|
|
274
286
|
this.isClosed = true
|
|
275
287
|
|