hyperjoin 0.0.0 → 1.4.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 ADDED
@@ -0,0 +1,939 @@
1
+ const ReadyResource = require('ready-resource')
2
+ const DHT = require('hyperdht')
3
+ const Dialer = require('./lib/dialer')
4
+ const Connection = require('./lib/connection')
5
+ const Session = require('./lib/session')
6
+ const safetyCatch = require('safety-catch')
7
+
8
+ const b4a = require('b4a')
9
+ const { sortConnections } = require('./lib/heuristics')
10
+ const { toHex } = b4a
11
+
12
+ const AUTO_REFRESH_INTERVAL = 5000
13
+
14
+ // sessions
15
+ const DEFAULT_AUTO_SLOTS = 8
16
+ const DEFAULT_SWARM_GC_IDLE_TIME = 30 * 1000
17
+
18
+ const DEFAULT_DISCOVERY_TIMEOUT = 60 * 1000
19
+ const UPDATED_DISCOVERY_TIMEOUT = 10 * 1000
20
+ const DISCOVERY_STARTUP_DELAY = 5 * 1000 // 5s
21
+ const AUTO_STARTUP_DELAY = 5 * 1000 // 5s
22
+ const MIN_ACTIVE_TIME = 30 * 1000
23
+
24
+ const DHT_FAST_RETRY = 0 // disabled
25
+
26
+ // connections
27
+ const MIN_PEERS = 20
28
+ const MAX_PEERS = 40
29
+
30
+ const ERR_DUPLICATE = 'DuplicateConnection'
31
+ const ERR_IDLE = 'IdleConnection'
32
+ const ERR_DESTROYED = 'ReplicatorDestroyed'
33
+ const ERR_SUSPEND = 'SuspendDrop'
34
+ const ERR_MAXPEERS = 'MaxPeersCap'
35
+
36
+ module.exports = class Replicator extends ReadyResource {
37
+ constructor(rootStore, opts = {}) {
38
+ super()
39
+
40
+ const {
41
+ seed,
42
+ keyPair,
43
+ relayThrough,
44
+ minPeers = MIN_PEERS,
45
+ maxPeers = MAX_PEERS,
46
+ maxParallel,
47
+ firewall = allowAll,
48
+ suspended,
49
+ autoswarm = true,
50
+ manual = false,
51
+ interval = AUTO_REFRESH_INTERVAL,
52
+ gcDisabled = false,
53
+
54
+ // Discovery
55
+ defaultDiscoveryTimeout = DEFAULT_DISCOVERY_TIMEOUT,
56
+ updatedDiscoveryTimeout = UPDATED_DISCOVERY_TIMEOUT,
57
+ discoveryStartupDelay = DISCOVERY_STARTUP_DELAY,
58
+ dhtFastRetry = DHT_FAST_RETRY,
59
+
60
+ // Idle
61
+ gcIdleTime = DEFAULT_SWARM_GC_IDLE_TIME,
62
+
63
+ // autoswarm
64
+ slots = DEFAULT_AUTO_SLOTS,
65
+ autoStartupDelay = AUTO_STARTUP_DELAY,
66
+ minActiveTime = MIN_ACTIVE_TIME
67
+ } = opts
68
+
69
+ if (seed) this.keyPair = DHT.keyPair(seed)
70
+ if (keyPair) this.keyPair = keyPair
71
+
72
+ this.root = rootStore
73
+ this._dhtOwned = !opts.dht
74
+ this.dht =
75
+ opts.dht ||
76
+ new DHT({
77
+ bootstrap: opts.bootstrap,
78
+ nodes: opts.nodes,
79
+ port: opts.port
80
+ })
81
+
82
+ this._firewall = firewall
83
+
84
+ const handlers = {
85
+ ondial: (peer) => this._connect(peer),
86
+ ondialerror: safetyCatch,
87
+ isConnected: (key) => this.hasConnection(key),
88
+ canRelay: () => !!this.relayThrough,
89
+ onban: (peer) => this._handleBan(peer),
90
+ want: () => {
91
+ if (this._isTearingdown) return 0
92
+
93
+ return this._maxPeers - this.connectionCount // throttle
94
+ }
95
+ }
96
+
97
+ this.dialer = new Dialer(handlers, {
98
+ backoffs: opts.backoffs,
99
+ jitter: opts.jitter,
100
+ suspended,
101
+ maxParallel
102
+ })
103
+
104
+ this.relayThrough = relayThrough && toRelaySelector(relayThrough)
105
+
106
+ this.server = this.dht.createServer(
107
+ {
108
+ firewall: this._handleFirewall.bind(this),
109
+ relayThrough: this._selectRelay.bind(this),
110
+ handshakeClearWait: opts.handshakeClearWait
111
+ },
112
+ this._onincoming.bind(this)
113
+ )
114
+
115
+ this.suspended = suspended
116
+ this._presuspended = false
117
+ this._suspending = Promise.resolve()
118
+ this.listening = false
119
+ this._pendingListen = null
120
+
121
+ this.sessions = new Map() // hex(discoveryKey) -> Session
122
+ this.inbound = new Map() // hex(peerId) -> Connection
123
+ this.outbound = new Map() // hex(peerId) -> Connection
124
+
125
+ this._inflight = new Set() // hex(peerId), dedup in-flight dials
126
+ this._inflightCancel = new Set() // cancelCallbacks
127
+ this._minPeers = minPeers
128
+ this._maxPeers = maxPeers
129
+ this._lastConnectionEvict = 0
130
+
131
+ this.slots = slots
132
+ this._autoswarm = autoswarm
133
+ this._autoRefreshInterval = interval
134
+ this._gcIdleTime = gcIdleTime
135
+
136
+ this._autoStartupDelay = autoStartupDelay
137
+ this._minActiveTime = minActiveTime
138
+
139
+ this._defaultDiscoveryTimeout = defaultDiscoveryTimeout
140
+ this._updatedDiscoveryTimeout = updatedDiscoveryTimeout
141
+ this._discoveryStartupDelay = discoveryStartupDelay
142
+ this._dhtFastRetry = dhtFastRetry
143
+
144
+ this._online = this.dht.online
145
+ this._offlineAt = 0
146
+
147
+ this.stats = {
148
+ updates: 0,
149
+ bannedPeers: 0,
150
+ connects: {
151
+ client: {
152
+ attempted: 0,
153
+ opened: 0,
154
+ closed: 0
155
+ },
156
+ server: {
157
+ opened: 0,
158
+ closed: 0
159
+ }
160
+ },
161
+ dht: {
162
+ lookup: 0,
163
+ announce: 0,
164
+ unannounce: 0
165
+ }
166
+ }
167
+
168
+ this._handleNetworkChange = this._handleNetworkChange.bind(this)
169
+ this.dht.on('network-change', this._handleNetworkChange)
170
+
171
+ this._handleNetworkUpdate = this._handleNetworkUpdate.bind(this)
172
+ this.dht.on('network-update', this._handleNetworkUpdate)
173
+
174
+ this._onrootclose = this._onrootclose.bind(this)
175
+ this.root.once('close', this._onrootclose)
176
+
177
+ this.on('update', this._handleUpdate.bind(this))
178
+
179
+ this._gcDisabled = gcDisabled
180
+ this._manual = !!manual
181
+ this._refreshInterval = null
182
+ if (!this._manual) this._startAutoRefresh()
183
+ }
184
+
185
+ get peerId() {
186
+ if (this.keyPair) return this.keyPair.publicKey
187
+
188
+ return null
189
+ }
190
+
191
+ get connectionCount() {
192
+ return this.inbound.size + this.outbound.size
193
+ }
194
+
195
+ get connecting() {
196
+ return this._inflight.size
197
+ }
198
+
199
+ get connections() {
200
+ return this._connections() // combined iterator
201
+ }
202
+
203
+ get peers() {
204
+ return this.dialer.peers
205
+ }
206
+
207
+ get isPresuspended() {
208
+ return this._presuspended
209
+ }
210
+
211
+ get destroyed() {
212
+ return !!(this.closing || this.closed)
213
+ }
214
+
215
+ *_connections() {
216
+ yield* this.inbound.values()
217
+ yield* this.outbound.values()
218
+ }
219
+
220
+ async _open() {
221
+ // TODO: this await is the root cause
222
+ // for `refresh()` being async.
223
+ this.keyPair ||= await this.root.createKeyPair('replicator')
224
+ this._pendingListen = this._safeListen()
225
+ }
226
+
227
+ async _safeListen() {
228
+ try {
229
+ await this.server.listen(this.keyPair)
230
+ this.listening = true
231
+
232
+ if (!this._manual) await this.refresh()
233
+ } catch (err) {
234
+ safetyCatch(err)
235
+ }
236
+ }
237
+
238
+ getConnection(peerId) {
239
+ if (typeof peerId !== 'string') peerId = toHex(peerId)
240
+
241
+ return this.inbound.get(peerId) || this.outbound.get(peerId)
242
+ }
243
+
244
+ hasConnection(peerId) {
245
+ if (typeof peerId !== 'string') peerId = toHex(peerId)
246
+ return !!this.getConnection(peerId) || this._inflight.has(peerId)
247
+ }
248
+
249
+ get activeSessions() {
250
+ const active = []
251
+ for (const session of this.sessions.values()) {
252
+ if (!session.active || session._suspended) continue
253
+ active.push(session)
254
+ }
255
+ return active
256
+ }
257
+
258
+ get announcedSessions() {
259
+ const announced = []
260
+ for (const session of this.sessions.values()) {
261
+ if (!session.isAnnounced) continue
262
+ announced.push(session)
263
+ }
264
+ return announced
265
+ }
266
+
267
+ _selectRelay(force) {
268
+ if (!this.relayThrough) return null
269
+ return this.relayThrough(force, this)
270
+ }
271
+
272
+ suspend({ log = noop } = {}) {
273
+ if (!this.suspended) {
274
+ this.suspended = true
275
+
276
+ this._suspending = this._beginSuspension({ log })
277
+ }
278
+
279
+ return this._suspending
280
+ }
281
+
282
+ async _beginSuspension({ log = noop } = {}) {
283
+ this._cancelInflight()
284
+ await this.flush()
285
+
286
+ const pending = []
287
+ const suspending = [this.server.suspend({ log }), this.dialer.suspend()]
288
+
289
+ for (const session of this.sessions.values()) {
290
+ suspending.push(session.suspend({ log }))
291
+ }
292
+
293
+ for (const connection of this.connections) {
294
+ connection.info.resetAttempts()
295
+ this.dialer.reschedule(connection.info)
296
+ pending.push(this._evictConnection(connection, ERR_SUSPEND))
297
+ }
298
+
299
+ this._inflight.clear()
300
+ this._stopAutoRefresh()
301
+
302
+ await Promise.allSettled(suspending)
303
+
304
+ if (this._dhtOwned) await this.dht.suspend({ log })
305
+
306
+ await Promise.all(pending)
307
+ }
308
+
309
+ // unannounce sessions
310
+ presuspend() {
311
+ if (this._presuspended) return
312
+
313
+ this._presuspended = true
314
+ this.refresh().catch(safetyCatch)
315
+ }
316
+
317
+ async resume({ log = noop, presuspended = false } = {}) {
318
+ if (this._presuspended !== presuspended) {
319
+ this._presuspended = presuspended
320
+ await this.refresh()
321
+ }
322
+
323
+ if (!this.suspended) return
324
+
325
+ if (this._dhtOwned) await this.dht.resume({ log })
326
+
327
+ await this.server.resume({ log })
328
+
329
+ this.suspended = false
330
+
331
+ this.dialer.resume()
332
+
333
+ const pending = []
334
+
335
+ for (const session of this.sessions.values()) {
336
+ pending.push(session.resume({ log }))
337
+ }
338
+
339
+ await Promise.all(pending)
340
+
341
+ if (!this._manual) this._startAutoRefresh()
342
+
343
+ return this.refresh()
344
+ }
345
+
346
+ join(
347
+ store,
348
+ topic,
349
+ {
350
+ priority = 0,
351
+ pin = false,
352
+ discover = false,
353
+ deferSwarming,
354
+ limit,
355
+ server = true,
356
+ client = true
357
+ } = {}
358
+ ) {
359
+ const dk = toHex(topic)
360
+
361
+ const existing = this.sessions.get(dk)
362
+
363
+ let wait = null
364
+ if (existing) {
365
+ if (existing.closing === null) throw new Error('session exists')
366
+
367
+ // Replace closing session but defer announcing until it finishes closing
368
+ this.sessions.delete(dk)
369
+ wait = existing.closing
370
+ }
371
+
372
+ const session = new Session(this, store, topic, {
373
+ priority,
374
+ pin,
375
+ discover,
376
+ limit,
377
+ deferSwarming,
378
+ server,
379
+ client,
380
+ wait
381
+ })
382
+ this.sessions.set(dk, session)
383
+
384
+ session.on('close', () => {
385
+ // don't delete session that replaced this session while closing
386
+ if (this.sessions.get(dk) === session) this.sessions.delete(dk)
387
+ this.emit('remove', session)
388
+ })
389
+
390
+ session.on('state', () => {
391
+ // Don't update dialer if not the current session (aka replaced while closing)
392
+ if (this.sessions.get(dk) !== session) return
393
+
394
+ if (session.active) {
395
+ this.dialer.topicEnable(dk)
396
+ } else {
397
+ this.dialer.topicDisable(dk)
398
+ }
399
+ })
400
+ if (session.active) this.dialer.topicEnable(dk)
401
+
402
+ this.emit('join', session)
403
+
404
+ return session
405
+ }
406
+
407
+ _onincoming(conn) {
408
+ if (this._isTearingdown) {
409
+ conn.on('error', noop)
410
+ return conn.destroy(ERR_DESTROYED)
411
+ }
412
+
413
+ const peerId = conn.remotePublicKey
414
+ const hex = toHex(peerId)
415
+
416
+ const peerInfo = this.dialer.lookup(peerId)
417
+ const initiator = false
418
+
419
+ const attemptDone = peerInfo.markAttempt(initiator)
420
+ const done = (failed = false, cause = null) => {
421
+ attemptDone(failed, cause)
422
+ this.dialer.reschedule(peerInfo)
423
+ }
424
+
425
+ if (this.inbound.has(hex)) {
426
+ const existing = this.inbound.get(hex).stream
427
+ // If both connections are from the same peer,
428
+ // - pick the new one if the existing stream is already established (has sent and received bytes),
429
+ // because the other client must have lost that connection and be reconnecting
430
+ const keepNew = existing.rawBytesRead > 0 && existing.rawBytesWritten > 0
431
+
432
+ if (keepNew === false) {
433
+ done(true, ERR_DUPLICATE)
434
+
435
+ existing.sendKeepAlive()
436
+ conn.on('error', noop)
437
+ conn.destroy(new Error(ERR_DUPLICATE))
438
+ } else {
439
+ done()
440
+
441
+ existing.on('error', noop)
442
+ existing.destroy(new Error(ERR_DUPLICATE))
443
+ this._handleServerConnectionSwap(existing, conn)
444
+ }
445
+
446
+ return
447
+ }
448
+
449
+ // Hard limit max-drop
450
+ if (this.connectionCount >= this._maxPeers + 5) {
451
+ done(true, ERR_MAXPEERS)
452
+ conn.on('error', noop)
453
+ return conn.destroy(ERR_MAXPEERS)
454
+ }
455
+
456
+ done()
457
+
458
+ // connection 'opened' on next tick
459
+ this._initConnection(false, peerId, conn)
460
+ }
461
+
462
+ // `stream` expected to be fully opened
463
+ _initConnection(initiator, peerId, stream) {
464
+ const hex = toHex(peerId)
465
+
466
+ const map = initiator ? this.outbound : this.inbound
467
+
468
+ if (map.has(hex)) throw new Error('unreachable: connection leak') // sanity check
469
+
470
+ const connection = new Connection(this, stream, peerId, initiator)
471
+
472
+ map.set(hex, connection)
473
+
474
+ this.root.replicate(connection.stream)
475
+ connection._watchChannels()
476
+
477
+ if (initiator) this.stats.connects.client.opened++
478
+ else this.stats.connects.server.opened++
479
+
480
+ this.emit('connected', connection)
481
+ this.emit('update')
482
+
483
+ return connection
484
+ }
485
+
486
+ async _evictConnection(connection, err) {
487
+ if (connection.destroying) return connection.destroyed
488
+
489
+ // private - used by gc metrics
490
+ this.emit('_evictConnection', connection.peerId, /* reason */ err)
491
+
492
+ connection.destroy(err)
493
+ await connection.destroyed
494
+ }
495
+
496
+ _handleServerConnectionSwap(existing, conn) {
497
+ let closed = false
498
+
499
+ existing.on('close', () => {
500
+ if (closed) return
501
+
502
+ conn.removeListener('error', noop)
503
+ conn.removeListener('close', onclose)
504
+
505
+ this._onincoming(conn)
506
+ })
507
+
508
+ conn.on('error', noop)
509
+ conn.on('close', onclose)
510
+
511
+ function onclose() {
512
+ closed = true
513
+ }
514
+ }
515
+
516
+ _handleBan(peer) {
517
+ this.stats.bannedPeers++
518
+ this.emit('ban', peer, peer.banReason)
519
+ }
520
+
521
+ async _handleFirewall(remotePublicKey, payload) {
522
+ // firewall self
523
+ if (b4a.equals(remotePublicKey, this.keyPair.publicKey)) return true
524
+
525
+ const peer = this.dialer.lookup(remotePublicKey)
526
+ if (peer.isBanned) return true
527
+
528
+ const firewalled = await this._firewall(remotePublicKey, payload)
529
+
530
+ if (firewalled) {
531
+ peer.setBanned('firewalled')
532
+ this.dialer.reschedule(peer)
533
+ }
534
+
535
+ return firewalled
536
+ }
537
+
538
+ get _isTearingdown() {
539
+ return (
540
+ this.suspended || // replicator
541
+ this.closing ||
542
+ this.closed
543
+ )
544
+ }
545
+
546
+ async _connect(peer, topic) {
547
+ if (this._isTearingdown) return true
548
+
549
+ const hex = toHex(peer.publicKey)
550
+ if (b4a.equals(peer.publicKey, this.peerId)) return true
551
+
552
+ if (this.outbound.has(hex)) return true
553
+ if (this.inbound.has(hex)) return true
554
+ if (this._inflight.has(hex)) return true
555
+
556
+ const peerInfo = this.dialer.lookup(peer.publicKey)
557
+
558
+ this._inflight.add(hex)
559
+
560
+ let firewalled = true
561
+ try {
562
+ firewalled = await this._handleFirewall(peerInfo.publicKey, null)
563
+ } finally {
564
+ // userhandler threw or firewalled - clear ref
565
+ if (firewalled) this._inflight.delete(hex)
566
+ }
567
+
568
+ if (firewalled) return true
569
+
570
+ if (this._isTearingdown) {
571
+ this._inflight.delete(hex)
572
+ return ERR_SUSPEND
573
+ }
574
+
575
+ let stream
576
+ let error = null
577
+ let cancelCallback = null
578
+
579
+ const opened = await new Promise((resolve) => {
580
+ const onerror = (err) => {
581
+ error = err
582
+ }
583
+
584
+ const onclose = () => {
585
+ stream.off('error', onerror)
586
+ resolve(false)
587
+ }
588
+
589
+ const onopen = () => {
590
+ stream.off('error', onerror)
591
+ stream.off('close', onclose)
592
+ resolve(true)
593
+ }
594
+
595
+ stream = this.dht.connect(peer.publicKey, {
596
+ keyPair: this.keyPair,
597
+ relayAddresses: peerInfo.relayAddresses,
598
+ relayThrough: this._selectRelay(peerInfo.forceRelaying)
599
+ })
600
+
601
+ stream.once('error', onerror)
602
+ stream.once('close', onclose)
603
+ stream.once('open', onopen)
604
+
605
+ cancelCallback = () => stream.destroy(ERR_SUSPEND)
606
+ this._inflightCancel.add(cancelCallback)
607
+
608
+ this.stats.connects.client.attempted++
609
+ this.emit('update')
610
+ })
611
+
612
+ this._inflight.delete(hex)
613
+ if (cancelCallback) this._inflightCancel.delete(cancelCallback)
614
+
615
+ if (!opened) {
616
+ this.emit('update')
617
+ return error || true
618
+ }
619
+
620
+ // opened after teardown
621
+ if (this._isTearingdown) {
622
+ stream.once('error', noop)
623
+ const e = new Error(ERR_DESTROYED)
624
+ stream.destroy(e)
625
+ return ERR_DESTROYED
626
+ }
627
+
628
+ // stream opened - hand-off
629
+ this._initConnection(true, peer.publicKey, stream, topic)
630
+
631
+ return false
632
+ }
633
+
634
+ async _gc(now = Date.now()) {
635
+ if (this._gcDisabled) return
636
+
637
+ const evictions = []
638
+ let connectionsEvicted = 0
639
+ let sessionsEvicted = 0
640
+
641
+ let active = 0
642
+ for (const connection of this.connections) {
643
+ if (!connection.destroying) active++
644
+ }
645
+
646
+ // peer-dedupe
647
+
648
+ const [src, dst] =
649
+ this.inbound.size < this.outbound.size
650
+ ? [this.inbound, this.outbound]
651
+ : [this.outbound, this.inbound]
652
+
653
+ for (const [key, connection] of src) {
654
+ if (!dst.has(key)) continue
655
+
656
+ const cmp = b4a.compare(this.peerId, connection.peerId)
657
+ if (cmp === 0) throw new Error('unreachable: self connection')
658
+
659
+ // low-peerId keeps inbound
660
+ const target = (cmp > 0) ^ connection.initiator ? connection : dst.get(key)
661
+ if (target.destroying) continue
662
+
663
+ evictions.push(this._evictConnection(target, ERR_DUPLICATE))
664
+ connectionsEvicted++
665
+ }
666
+
667
+ // GC idle sessions
668
+
669
+ for (const session of this.sessions.values()) {
670
+ if (!session._shouldEvict(now)) continue
671
+ evictions.push(session.destroy())
672
+ sessionsEvicted++
673
+ }
674
+
675
+ // GC Connections
676
+
677
+ if (!this._lastConnectionEvict) this._lastConnectionEvict = now
678
+
679
+ const gcConn = (connection, reason = ERR_IDLE) => {
680
+ evictions.push(this._evictConnection(connection, reason))
681
+ connectionsEvicted++
682
+ this._lastConnectionEvict = now
683
+ }
684
+
685
+ const evict = active - this._maxPeers
686
+
687
+ if (evict > 0) {
688
+ // Evict idle connections
689
+ for (const connection of this.connections) {
690
+ if (connectionsEvicted >= evict) {
691
+ break // target reached
692
+ }
693
+
694
+ if (connection.destroying) continue
695
+ if (!connection.isIdle(now)) continue
696
+
697
+ gcConn(connection, ERR_IDLE)
698
+ }
699
+
700
+ // Last resort if still over max capacity
701
+ const nHeuristicEvict = evict - connectionsEvicted
702
+
703
+ if (nHeuristicEvict > 0) {
704
+ const remaining = Array.from(this.connections).filter((c) => !c.destroying)
705
+ const sorted = sortConnections(remaining, now)
706
+
707
+ for (let i = 0; i < nHeuristicEvict; i++) {
708
+ gcConn(sorted[i], ERR_MAXPEERS)
709
+ }
710
+ }
711
+ }
712
+
713
+ for (const connection of this.connections) {
714
+ if (connection.destroying) continue
715
+ connection._survived++
716
+ }
717
+
718
+ await Promise.all(evictions)
719
+
720
+ if (sessionsEvicted) this.emit('evicted', sessionsEvicted)
721
+
722
+ if (connectionsEvicted) this.emit('dropped', connectionsEvicted)
723
+ }
724
+
725
+ _handleUpdate() {
726
+ this.stats.updates++
727
+ }
728
+
729
+ _onrootclose() {
730
+ if (this.destroyed) return
731
+ this.destroy().catch(safetyCatch)
732
+ }
733
+
734
+ async refresh(now = Date.now()) {
735
+ this._autoselect(now) // updates sessions
736
+
737
+ const done = []
738
+
739
+ for (const sess of this.sessions.values()) {
740
+ if (sess.closing || sess.closed) continue
741
+ done.push(sess.refresh(now)) // applies state
742
+ }
743
+
744
+ for (const conn of this.connections) {
745
+ conn.refresh(now)
746
+ }
747
+
748
+ await Promise.all(done)
749
+
750
+ await this._gc(now)
751
+
752
+ this.dialer.refresh(now)
753
+ }
754
+
755
+ async flush() {
756
+ if (this._pendingListen && !this.suspended) {
757
+ await this._pendingListen
758
+ this._pendingListen = null
759
+ }
760
+
761
+ const pending = []
762
+ for (const sess of this.sessions.values()) {
763
+ if (sess.closing || sess.closed || sess.state === 'destroyed') continue
764
+ pending.push(sess.flush())
765
+ }
766
+ await Promise.all(pending)
767
+
768
+ // pending connects
769
+ await this.dialer.flush()
770
+ }
771
+
772
+ /**
773
+ * Autoswarm policies
774
+ */
775
+ _autoselect(now = Date.now()) {
776
+ const pool = []
777
+ let slots = this.slots
778
+
779
+ for (const session of this.sessions.values()) {
780
+ if (session.isDiscovering) continue
781
+ if (session.isPinned) continue
782
+ if (session.closing || session.closed) continue
783
+
784
+ // don't deactivate pending nor newly-started topics
785
+ if (session.isAuto) {
786
+ const timeActive = now - session._startTime
787
+ const minActive = session._startDelay + this._minActiveTime
788
+
789
+ if (this._autoswarm && timeActive < minActive) {
790
+ slots-- // reserved & immovable
791
+ continue
792
+ }
793
+ }
794
+
795
+ pool.push(session)
796
+ }
797
+
798
+ slots = Math.max(slots, 0) // `slots` reduced during runtime.
799
+
800
+ const order = new Map()
801
+ const priorities = pool.map((i) => i.priority).sort((a, b) => a - b)
802
+ for (let i = 0; i < priorities.length; i++) {
803
+ order.set(priorities[i], i)
804
+ }
805
+
806
+ function score(session) {
807
+ let v = 0
808
+ v += order.get(session.priority) // primary
809
+
810
+ // secondary; rotate over time by last start
811
+ if (session.isAuto) {
812
+ const uptime = now - session._startTime
813
+ v -= (uptime / 60_000) * 0.5
814
+ }
815
+
816
+ return v
817
+ }
818
+
819
+ function heuristicSort(a, b) {
820
+ return score(b) - score(a)
821
+ }
822
+
823
+ // rotate
824
+ pool.sort(heuristicSort)
825
+
826
+ if (!this._autoswarm) slots = 0
827
+
828
+ // autostop
829
+ for (let i = slots; i < pool.length; i++) {
830
+ pool[i]._autoStop(now)
831
+ }
832
+
833
+ // autostart
834
+ const limit = Math.min(pool.length, slots)
835
+
836
+ for (let i = 0; i < limit; i++) {
837
+ pool[i]._autoStart(now)
838
+ }
839
+ }
840
+
841
+ // @deprecated
842
+ async setAutoswarm(enabled) {
843
+ if (this._autoswarm === enabled) return
844
+
845
+ this._autoswarm = enabled
846
+ await this.refresh()
847
+ await this.flush()
848
+ }
849
+
850
+ async _handleNetworkUpdate() {
851
+ // Rising edge for being online
852
+ const networkCameOnline = this.dht.online && !this._online
853
+ if (this._online && !this.dht.online) this._offlineAt = Date.now()
854
+ this._online = this.dht.online
855
+ if (!networkCameOnline) return
856
+
857
+ this.dialer.resetBackoff(this._offlineAt) // failures while offline were ours
858
+ await this._handleNetworkChange()
859
+ }
860
+
861
+ async _handleNetworkChange() {
862
+ if (this.suspended) return
863
+
864
+ for (const connection of this.connections) {
865
+ connection.stream.sendKeepAlive()
866
+ }
867
+
868
+ await this.refresh()
869
+ }
870
+
871
+ _stopAutoRefresh() {
872
+ if (!this._refreshInterval) return
873
+ clearInterval(this._refreshInterval)
874
+ this._refreshInterval = null
875
+ }
876
+
877
+ _startAutoRefresh() {
878
+ if (this._refreshInterval) return
879
+ this._refreshInterval = setInterval(
880
+ () => this.refresh().catch(safetyCatch),
881
+ this._autoRefreshInterval
882
+ )
883
+ this._refreshInterval.unref()
884
+ }
885
+
886
+ // TODO: redesign
887
+ _cancelInflight() {
888
+ for (const cancel of this._inflightCancel) cancel()
889
+ }
890
+
891
+ async _close() {
892
+ this.root.removeListener('close', this._onrootclose)
893
+ this.dht.removeListener('network-change', this._handleNetworkChange)
894
+ this.dht.removeListener('network-update', this._handleNetworkUpdate)
895
+
896
+ this._stopAutoRefresh()
897
+
898
+ const tasks = []
899
+ for (const session of this.sessions.values()) {
900
+ tasks.push(session.destroy())
901
+ }
902
+
903
+ // wait for all un-announces before dropping connections
904
+ await Promise.all(tasks)
905
+
906
+ this.sessions.clear()
907
+
908
+ for (const connection of this.connections) {
909
+ tasks.push(this._evictConnection(connection))
910
+ }
911
+
912
+ this.inbound.clear()
913
+ this.outbound.clear()
914
+
915
+ this._cancelInflight()
916
+ this._inflight.clear()
917
+ this.dialer.destroy()
918
+
919
+ await Promise.all(tasks)
920
+ await this.server.close()
921
+ if (this._dhtOwned && !this.dht.destroyed) await this.dht.destroy()
922
+ }
923
+
924
+ async destroy() {
925
+ await this.close()
926
+ }
927
+ }
928
+
929
+ function noop() {}
930
+
931
+ function allowAll() {
932
+ return false
933
+ }
934
+
935
+ function toRelaySelector(relayThrough) {
936
+ return typeof relayThrough === 'function'
937
+ ? relayThrough
938
+ : (force, replicator) => (force || replicator.dht.randomized ? relayThrough : null)
939
+ }