hypercore 11.40.4 → 11.41.0
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 +6 -1
- package/lib/core.js +13 -0
- package/lib/replicator.js +18 -14
- package/lib/session-state.js +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -88,6 +88,7 @@ class Hypercore extends EventEmitter {
|
|
|
88
88
|
this.preload = null
|
|
89
89
|
this.closing = null
|
|
90
90
|
this.opening = null
|
|
91
|
+
this.orphaned = false
|
|
91
92
|
|
|
92
93
|
this._readonly = opts.writable === false
|
|
93
94
|
this._preappend = preappend.bind(this)
|
|
@@ -244,6 +245,10 @@ class Hypercore extends EventEmitter {
|
|
|
244
245
|
if (opts.checkout !== undefined && !opts.name && !opts.atom) {
|
|
245
246
|
throw ASSERTION('Checkouts are only supported on atoms or named sessions', this.discoveryKey)
|
|
246
247
|
}
|
|
248
|
+
if (this.weak === true && opts.weak !== undefined && !opts.weak) {
|
|
249
|
+
// a weak session does not keep the core open, so it cannot hand out one that does
|
|
250
|
+
throw ASSERTION('Cannot make a strong session on a weak session', this.discoveryKey)
|
|
251
|
+
}
|
|
247
252
|
|
|
248
253
|
const wait = opts.wait === false ? false : this.wait
|
|
249
254
|
const writable = opts.writable === undefined ? !this._readonly : opts.writable === true
|
|
@@ -735,7 +740,7 @@ class Hypercore extends EventEmitter {
|
|
|
735
740
|
|
|
736
741
|
if (this.weak === false) {
|
|
737
742
|
this.core.activeSessions--
|
|
738
|
-
core.activeSessions++
|
|
743
|
+
if (core.activeSessions++ === 0) core.orphaned = false
|
|
739
744
|
}
|
|
740
745
|
|
|
741
746
|
if (this._monitorIndex >= 0) {
|
package/lib/core.js
CHANGED
|
@@ -28,6 +28,7 @@ module.exports = class Core {
|
|
|
28
28
|
this.sessionStates = []
|
|
29
29
|
this.monitors = []
|
|
30
30
|
this.activeSessions = 0
|
|
31
|
+
this.orphaned = false
|
|
31
32
|
this.gc = 0 // corestore uses this to main a gc strike pool
|
|
32
33
|
|
|
33
34
|
this.id = opts.key ? z32.encode(opts.key) : null
|
|
@@ -121,6 +122,16 @@ module.exports = class Core {
|
|
|
121
122
|
return this.activeSessions !== 0
|
|
122
123
|
}
|
|
123
124
|
|
|
125
|
+
emitOrphaned() {
|
|
126
|
+
if (this.orphaned === true) return
|
|
127
|
+
this.orphaned = true
|
|
128
|
+
|
|
129
|
+
for (const s of this.allSessions()) {
|
|
130
|
+
if (s._stateIndex === -1 || s.closing !== null) continue
|
|
131
|
+
s.emit('orphaned')
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
124
135
|
compact() {
|
|
125
136
|
const compacting = []
|
|
126
137
|
for (const s of this.sessionStates) {
|
|
@@ -131,6 +142,8 @@ module.exports = class Core {
|
|
|
131
142
|
|
|
132
143
|
checkIfIdle() {
|
|
133
144
|
if (!this.opened || this.destroyed === true || this.hasSession() === true) return
|
|
145
|
+
this.emitOrphaned()
|
|
146
|
+
if (this.hasSession() === true) return // a listener may have re-opened the core
|
|
134
147
|
if (this.replicator.idle() === false) return
|
|
135
148
|
if (this.state === null || this.state.mutex.idle() === false) return
|
|
136
149
|
this.onidle()
|
package/lib/replicator.js
CHANGED
|
@@ -611,7 +611,7 @@ class Peer {
|
|
|
611
611
|
}
|
|
612
612
|
|
|
613
613
|
signalUpgrade() {
|
|
614
|
-
if (this._shouldUpdateCanUpgrade() === true) this._updateCanUpgradeAndSync()
|
|
614
|
+
if (this._shouldUpdateCanUpgrade() === true) this._updateCanUpgradeAndSync(false)
|
|
615
615
|
else this.sendSync()
|
|
616
616
|
}
|
|
617
617
|
|
|
@@ -856,31 +856,35 @@ class Peer {
|
|
|
856
856
|
)
|
|
857
857
|
}
|
|
858
858
|
|
|
859
|
-
async _updateCanUpgradeAndSync() {
|
|
859
|
+
async _updateCanUpgradeAndSync(onlyIfChanged) {
|
|
860
860
|
const { length, fork } = this.core.state
|
|
861
861
|
|
|
862
862
|
const remoteLength = this.remoteLength
|
|
863
863
|
const remoteFork = this.remoteFork
|
|
864
864
|
const canUpgrade = await this._canUpgrade(this.remoteLength, this.remoteFork)
|
|
865
865
|
|
|
866
|
-
if (
|
|
867
|
-
this.syncsProcessing > 0 ||
|
|
868
|
-
length !== this.core.state.length ||
|
|
869
|
-
fork !== this.core.state.fork
|
|
870
|
-
) {
|
|
866
|
+
if (length !== this.core.state.length || fork !== this.core.state.fork) {
|
|
871
867
|
return
|
|
872
868
|
}
|
|
873
|
-
if (
|
|
869
|
+
if (this.syncsProcessing > 0) {
|
|
870
|
+
this.needsSync = true // the in-flight onsync sends when it finishes
|
|
874
871
|
return
|
|
875
872
|
}
|
|
876
|
-
if (
|
|
873
|
+
if (remoteLength !== this.remoteLength || remoteFork !== this.remoteFork) {
|
|
874
|
+
// if we are still longer, the onsync that changed it already signalled
|
|
875
|
+
if (fork === this.remoteFork && length > this.remoteLength) return
|
|
876
|
+
this.signalUpgrade() // remote changed while we checked, re-evaluate
|
|
877
877
|
return
|
|
878
878
|
}
|
|
879
879
|
|
|
880
|
-
this.canUpgrade
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
880
|
+
if (canUpgrade !== this.canUpgrade) {
|
|
881
|
+
this.canUpgrade = canUpgrade
|
|
882
|
+
if (canUpgrade) {
|
|
883
|
+
this.lastUpgradableLength = this.remoteLength
|
|
884
|
+
this.lastUpgradableFork = this.remoteFork
|
|
885
|
+
}
|
|
886
|
+
} else if (onlyIfChanged === true) {
|
|
887
|
+
return
|
|
884
888
|
}
|
|
885
889
|
|
|
886
890
|
this.sendSync()
|
|
@@ -1295,7 +1299,7 @@ class Peer {
|
|
|
1295
1299
|
this.replicator._ondata(this, req, data)
|
|
1296
1300
|
|
|
1297
1301
|
if (this._shouldUpdateCanUpgrade() === true) {
|
|
1298
|
-
this._updateCanUpgradeAndSync()
|
|
1302
|
+
this._updateCanUpgradeAndSync(true)
|
|
1299
1303
|
}
|
|
1300
1304
|
}
|
|
1301
1305
|
|
package/lib/session-state.js
CHANGED
|
@@ -104,7 +104,7 @@ class SessionState {
|
|
|
104
104
|
addSession(s) {
|
|
105
105
|
if (s._stateIndex !== -1) return
|
|
106
106
|
s._stateIndex = this.sessions.push(s) - 1
|
|
107
|
-
if (s.weak === false
|
|
107
|
+
if (s.weak === false && this.core.activeSessions++ === 0) this.core.orphaned = false
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
removeSession(s) {
|