blind-peer 2.9.1 → 2.9.3
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 +36 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -61,6 +61,8 @@ class CoreTracker {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
gc() {
|
|
64
|
+
if (!this.record) throw new Error('Record must be set before calling gc')
|
|
65
|
+
|
|
64
66
|
// TODO: support gc-ing till less than last block (required hypercore to support getting byteLength at arbitrary versions)
|
|
65
67
|
const bytesCleared = this.core.byteLength
|
|
66
68
|
const blocksCleared = this.core.length
|
|
@@ -128,11 +130,28 @@ class CoreTracker {
|
|
|
128
130
|
}
|
|
129
131
|
|
|
130
132
|
class WakeupHandler {
|
|
131
|
-
constructor(db, key, discoveryKey) {
|
|
133
|
+
constructor(wakeup, db, key, discoveryKey) {
|
|
134
|
+
this.wakeup = wakeup
|
|
132
135
|
this.db = db
|
|
133
136
|
this.key = key
|
|
134
137
|
this.discoveryKey = discoveryKey
|
|
135
138
|
this.active = false
|
|
139
|
+
this.timeout = setTimeout(this._gc.bind(this), 15_000) // sanity
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
remoteAttached() {
|
|
143
|
+
if (!this.timeout) return
|
|
144
|
+
clearTimeout(this.timeout)
|
|
145
|
+
this.timeout = null
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
onpeeradd(peer, session) {
|
|
149
|
+
this.remoteAttached()
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
onpeerremove(peer, session) {
|
|
153
|
+
this.remoteAttached()
|
|
154
|
+
if (session.peers.length === 0) session.destroy()
|
|
136
155
|
}
|
|
137
156
|
|
|
138
157
|
async onpeeractive(peer, session) {
|
|
@@ -153,9 +172,16 @@ class WakeupHandler {
|
|
|
153
172
|
}
|
|
154
173
|
}
|
|
155
174
|
|
|
156
|
-
|
|
175
|
+
_gc() {
|
|
176
|
+
const sessions = this.wakeup.getSessions(this.key)
|
|
177
|
+
if (!sessions.length) return
|
|
178
|
+
const session = sessions[0]
|
|
157
179
|
if (session.peers.length === 0) session.destroy()
|
|
158
180
|
}
|
|
181
|
+
|
|
182
|
+
ondestroy(session) {
|
|
183
|
+
this.remoteAttached()
|
|
184
|
+
}
|
|
159
185
|
}
|
|
160
186
|
|
|
161
187
|
class BlindPeer extends ReadyResource {
|
|
@@ -253,8 +279,10 @@ class BlindPeer extends ReadyResource {
|
|
|
253
279
|
const sessions = this.wakeup.getSessions(key)
|
|
254
280
|
if (sessions.length) return sessions[0]
|
|
255
281
|
|
|
256
|
-
const handler = new WakeupHandler(this.db, key, discoveryKey)
|
|
257
|
-
|
|
282
|
+
const handler = new WakeupHandler(this.wakeup, this.db, key, discoveryKey)
|
|
283
|
+
const session = this.wakeup.session(key, handler)
|
|
284
|
+
if (session.peers.length) handler.remoteAttached()
|
|
285
|
+
return session
|
|
258
286
|
}
|
|
259
287
|
|
|
260
288
|
async _onwakeup(discoveryKey, muxer) {
|
|
@@ -264,6 +292,8 @@ class BlindPeer extends ReadyResource {
|
|
|
264
292
|
if (!auth) return
|
|
265
293
|
|
|
266
294
|
const stream = muxer.stream
|
|
295
|
+
if (stream.destroying || stream.destroyed) return
|
|
296
|
+
|
|
267
297
|
const session = this._getSession(auth.key, discoveryKey)
|
|
268
298
|
|
|
269
299
|
if (session.hasStream(stream)) return
|
|
@@ -271,7 +301,7 @@ class BlindPeer extends ReadyResource {
|
|
|
271
301
|
|
|
272
302
|
// if new peer, send back the active handler for this peer
|
|
273
303
|
const peer = session.getPeer(stream)
|
|
274
|
-
if (peer && peer.active)
|
|
304
|
+
if (peer && peer.active) session.handlers.onpeeractive(peer, session)
|
|
275
305
|
}
|
|
276
306
|
|
|
277
307
|
async listen() {
|
|
@@ -308,6 +338,7 @@ class BlindPeer extends ReadyResource {
|
|
|
308
338
|
|
|
309
339
|
try {
|
|
310
340
|
const tracker = this.activeReplication.get(id)
|
|
341
|
+
if (!tracker.record) await tracker.refresh()
|
|
311
342
|
const coreBytesCleared = tracker.gc()
|
|
312
343
|
bytesCleared += coreBytesCleared
|
|
313
344
|
} finally {
|