hypercore 11.40.5 → 11.41.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.
- package/index.js +6 -1
- package/lib/core.js +13 -0
- package/lib/replicator.js +9 -22
- 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
|
@@ -67,7 +67,7 @@ class Attachable {
|
|
|
67
67
|
constructor(replicator) {
|
|
68
68
|
this.replicator = replicator
|
|
69
69
|
this.resolved = false
|
|
70
|
-
this.processing =
|
|
70
|
+
this.processing = 0
|
|
71
71
|
this.refs = []
|
|
72
72
|
this.wants = null
|
|
73
73
|
}
|
|
@@ -129,7 +129,7 @@ class Attachable {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
gc() {
|
|
132
|
-
if (this.refs.length > 0 || this.processing) return
|
|
132
|
+
if (this.refs.length > 0 || this.processing > 0) return
|
|
133
133
|
this._unref()
|
|
134
134
|
|
|
135
135
|
if (this.wants === null) return
|
|
@@ -163,8 +163,7 @@ class Attachable {
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
processed() {
|
|
166
|
-
this.processing
|
|
167
|
-
this.gc()
|
|
166
|
+
if (--this.processing === 0) this.gc()
|
|
168
167
|
}
|
|
169
168
|
|
|
170
169
|
_cancel(r, err) {
|
|
@@ -1257,7 +1256,7 @@ class Peer {
|
|
|
1257
1256
|
}
|
|
1258
1257
|
|
|
1259
1258
|
this.dataProcessing++
|
|
1260
|
-
|
|
1259
|
+
const processing = isBlockRequest(req) ? this.replicator._markProcessing(req.block.index) : null
|
|
1261
1260
|
|
|
1262
1261
|
try {
|
|
1263
1262
|
if (!this.replicator._matchingRequest(req, data) || !(await this.core.verify(data, this))) {
|
|
@@ -1292,7 +1291,7 @@ class Peer {
|
|
|
1292
1291
|
this.replicator._oninvaliddata(err, req, data, this)
|
|
1293
1292
|
return
|
|
1294
1293
|
} finally {
|
|
1295
|
-
if (
|
|
1294
|
+
if (processing !== null) processing.processed()
|
|
1296
1295
|
this.dataProcessing--
|
|
1297
1296
|
}
|
|
1298
1297
|
|
|
@@ -2829,22 +2828,10 @@ module.exports = class Replicator {
|
|
|
2829
2828
|
}
|
|
2830
2829
|
|
|
2831
2830
|
_markProcessing(index) {
|
|
2832
|
-
const b = this._blocks.get(index)
|
|
2833
|
-
if (b)
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
}
|
|
2837
|
-
|
|
2838
|
-
const h = this._hashes.get(index)
|
|
2839
|
-
if (h) h.processing = true
|
|
2840
|
-
}
|
|
2841
|
-
|
|
2842
|
-
_markProcessed(index) {
|
|
2843
|
-
const b = this._blocks.get(index)
|
|
2844
|
-
if (b) return b.processed()
|
|
2845
|
-
|
|
2846
|
-
const h = this._hashes.get(index)
|
|
2847
|
-
if (h) h.processed()
|
|
2831
|
+
const b = this._blocks.get(index) || this._hashes.get(index)
|
|
2832
|
+
if (!b) return null
|
|
2833
|
+
b.processing++
|
|
2834
|
+
return b
|
|
2848
2835
|
}
|
|
2849
2836
|
|
|
2850
2837
|
_markInflight(index) {
|
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) {
|