@cero-base/core 2.2.1 → 2.3.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/README.md +32 -212
- package/package.json +18 -9
- package/src/blobs/index.js +9 -0
- package/src/blobs/server.js +2 -0
- package/src/database/changes.js +1 -1
- package/src/database/dispatch.js +341 -209
- package/src/database/encryption.js +83 -33
- package/src/database/index.js +188 -84
- package/src/database/rotation.js +43 -30
- package/src/identity/index.js +20 -78
- package/src/index.js +3 -10
- package/src/lib/constants.js +2 -0
- package/src/lib/errors.js +3 -11
- package/src/lib/spec/index.js +136 -132
- package/src/lib/spec/schema.json +84 -99
- package/src/lib/utils.js +57 -3
- package/src/mailbox/inbox.js +103 -0
- package/src/mailbox/index.js +146 -0
- package/src/mailbox/post.js +69 -0
- package/src/network/discovery.js +26 -1
- package/src/network/index.js +105 -64
- package/src/network/presence.js +6 -2
- package/src/pairing/index.js +330 -254
- package/src/pairing/invite.js +83 -144
- package/src/pairing/request.js +54 -56
- package/src/rpc/index.js +18 -93
- package/src/rpc/peer.js +4 -1
- package/src/storage/index.js +78 -39
- package/types/blobs/index.d.ts +22 -9
- package/types/blobs/server.d.ts +4 -2
- package/types/database/changes.d.ts +2 -2
- package/types/database/dispatch.d.ts +19 -13
- package/types/database/encryption.d.ts +67 -29
- package/types/database/index.d.ts +144 -119
- package/types/database/rotation.d.ts +32 -13
- package/types/identity/index.d.ts +6 -47
- package/types/index.d.ts +3 -16
- package/types/lib/constants.d.ts +1 -0
- package/types/lib/errors.d.ts +3 -9
- package/types/lib/spec/index.d.ts +52 -16
- package/types/lib/utils.d.ts +40 -8
- package/types/mailbox/inbox.d.ts +52 -0
- package/types/mailbox/index.d.ts +111 -0
- package/types/mailbox/post.d.ts +37 -0
- package/types/network/discovery.d.ts +21 -5
- package/types/network/index.d.ts +64 -48
- package/types/network/presence.d.ts +17 -4
- package/types/pairing/index.d.ts +143 -134
- package/types/pairing/invite.d.ts +70 -114
- package/types/pairing/request.d.ts +58 -98
- package/types/rpc/index.d.ts +26 -44
- package/types/rpc/peer.d.ts +10 -7
- package/types/storage/index.d.ts +64 -54
- package/src/pairing/candidate.js +0 -123
- package/types/pairing/candidate.d.ts +0 -36
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import ReadyResource from 'ready-resource'
|
|
2
|
+
import safetyCatch from 'safety-catch'
|
|
3
|
+
import crypto from 'hypercore-crypto'
|
|
4
|
+
import b4a from 'b4a'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One sealed message on its way to an address: written to a fresh core, announced to peers
|
|
8
|
+
* receiving at the address, and deposited on `mirrors` for an owner who is offline.
|
|
9
|
+
* `delivered` resolves on the first read, by the owner or by a mirror holding it for them.
|
|
10
|
+
* Given a `core` instead, it announces that core as it is.
|
|
11
|
+
*/
|
|
12
|
+
export class Post extends ReadyResource {
|
|
13
|
+
/**
|
|
14
|
+
* @param {import('../network/index.js').Network} network
|
|
15
|
+
* @param {Uint8Array} address
|
|
16
|
+
* @param {Uint8Array | null} message
|
|
17
|
+
* @param {{ mirrors?: Uint8Array[], core?: import('hypercore') }} [opts]
|
|
18
|
+
*/
|
|
19
|
+
constructor(network, address, message, { mirrors = [], core = null } = {}) {
|
|
20
|
+
super()
|
|
21
|
+
this.network = network
|
|
22
|
+
this.address = address
|
|
23
|
+
this.message = message
|
|
24
|
+
this.mirrors = mirrors
|
|
25
|
+
/** @type {Promise<void>} */
|
|
26
|
+
this.delivered = new Promise((resolve) => {
|
|
27
|
+
/** @private */
|
|
28
|
+
this._ondelivered = resolve
|
|
29
|
+
})
|
|
30
|
+
/** @private */
|
|
31
|
+
this._core = core
|
|
32
|
+
/** @private */
|
|
33
|
+
this._session = null
|
|
34
|
+
/** @private */
|
|
35
|
+
this._discovery = null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @private */
|
|
39
|
+
async _open() {
|
|
40
|
+
const { network, address } = this
|
|
41
|
+
const core = this._core || network.store.get({ name: b4a.toHex(crypto.randomBytes(32)) })
|
|
42
|
+
this._core = core
|
|
43
|
+
await core.ready()
|
|
44
|
+
core.once('upload', () => this._ondelivered())
|
|
45
|
+
if (this.message) await core.append(crypto.encrypt(this.message, address))
|
|
46
|
+
|
|
47
|
+
const wakeup = [{ key: core.key, length: core.length }]
|
|
48
|
+
const session = network.wakeup.session(address, {
|
|
49
|
+
discoveryKey: crypto.discoveryKey(address),
|
|
50
|
+
onpeeractive: (peer) => session.announce(peer, wakeup)
|
|
51
|
+
})
|
|
52
|
+
this._session = session
|
|
53
|
+
// a topic this network already joined has active peers that will not fire onpeeractive again
|
|
54
|
+
for (const peer of session.peers) if (peer.active) session.announce(peer, wakeup)
|
|
55
|
+
this._discovery = network.join(crypto.discoveryKey(address))
|
|
56
|
+
if (this.mirrors.length) {
|
|
57
|
+
const opts = { keys: this.mirrors, pick: this.mirrors.length, referrer: address }
|
|
58
|
+
network.peering().addCoreBackground(core, opts)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** @private */
|
|
63
|
+
async _close() {
|
|
64
|
+
this._session?.destroy()
|
|
65
|
+
// the unannounce is a DHT round trip, never waited on
|
|
66
|
+
this._discovery?.destroy().catch(safetyCatch)
|
|
67
|
+
await this._core?.close()
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/network/discovery.js
CHANGED
|
@@ -7,17 +7,29 @@ import { CeroError } from '../lib/errors.js'
|
|
|
7
7
|
* Handle for a single topic membership on a {@link Network}. Wraps a hyperswarm
|
|
8
8
|
* PeerDiscovery session and lets it switch between active/passive announce/lookup.
|
|
9
9
|
*/
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {{ refresh(opts?: object): Promise<void>, flushed(): Promise<boolean>, destroy(): Promise<void> }} Session
|
|
12
|
+
* The hyperswarm session `swarm.join()` returns, as far as this file uses it.
|
|
13
|
+
*/
|
|
14
|
+
|
|
10
15
|
export class Discovery {
|
|
11
16
|
/**
|
|
12
17
|
* @param {import('./index.js').Network} network Owning network.
|
|
13
|
-
* @param {
|
|
18
|
+
* @param {Session} session Hyperswarm PeerDiscovery session from `swarm.join()`.
|
|
14
19
|
* @param {'active' | 'passive'} mode
|
|
15
20
|
*/
|
|
16
21
|
constructor(network, session, mode) {
|
|
17
22
|
this.network = network
|
|
23
|
+
/** @type {Session} */
|
|
18
24
|
this.session = session
|
|
25
|
+
/** @private */
|
|
19
26
|
this._mode = mode
|
|
27
|
+
/** @private */
|
|
20
28
|
this._destroyed = false
|
|
29
|
+
/** @private */
|
|
30
|
+
this._timer = null
|
|
31
|
+
// a passive join only announces: the active peer's second lookup finds it
|
|
32
|
+
if (mode === ACTIVE) this._requery().catch(safetyCatch)
|
|
21
33
|
}
|
|
22
34
|
|
|
23
35
|
/** @returns {'active' | 'passive'} */
|
|
@@ -62,6 +74,18 @@ export class Discovery {
|
|
|
62
74
|
await this.session.flushed()
|
|
63
75
|
}
|
|
64
76
|
|
|
77
|
+
// peers joining at once look up before either announce lands; hyperswarm looks again in 10 min
|
|
78
|
+
/** @private */
|
|
79
|
+
async _requery() {
|
|
80
|
+
await this.session.flushed()
|
|
81
|
+
if (this._destroyed) return
|
|
82
|
+
this._timer = setTimeout(
|
|
83
|
+
() => this.session.refresh().catch(safetyCatch),
|
|
84
|
+
1000 + Math.random() * 2000
|
|
85
|
+
)
|
|
86
|
+
this._timer.unref()
|
|
87
|
+
}
|
|
88
|
+
|
|
65
89
|
/**
|
|
66
90
|
* Leave the topic and tear down the session. Idempotent.
|
|
67
91
|
*
|
|
@@ -70,6 +94,7 @@ export class Discovery {
|
|
|
70
94
|
async destroy() {
|
|
71
95
|
if (this._destroyed) return
|
|
72
96
|
this._destroyed = true
|
|
97
|
+
clearTimeout(this._timer)
|
|
73
98
|
this.network._discoveries.delete(this)
|
|
74
99
|
try {
|
|
75
100
|
await this.session.destroy()
|
package/src/network/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import NoiseSecretStream from '@hyperswarm/secret-stream'
|
|
2
2
|
import Autobee from 'autobee'
|
|
3
|
-
import BlindPairing from 'blind-pairing'
|
|
4
3
|
import BlindPeering from 'blind-peering'
|
|
5
4
|
import Protomux from 'protomux'
|
|
6
5
|
import ProtomuxWakeup from 'protomux-wakeup'
|
|
@@ -8,6 +7,7 @@ import ReadyResource from 'ready-resource'
|
|
|
8
7
|
import safetyCatch from 'safety-catch'
|
|
9
8
|
import { decode as decodeKey } from 'hypercore-id-encoding'
|
|
10
9
|
import { hash } from 'hypercore-crypto'
|
|
10
|
+
import HyperDHT from 'hyperdht'
|
|
11
11
|
import Hyperswarm from 'hyperswarm'
|
|
12
12
|
import b4a from 'b4a'
|
|
13
13
|
import c from 'compact-encoding'
|
|
@@ -17,6 +17,11 @@ import { CeroError } from '../lib/errors.js'
|
|
|
17
17
|
import { Discovery } from './discovery.js'
|
|
18
18
|
import { Presence } from './presence.js'
|
|
19
19
|
|
|
20
|
+
// a live remote answers our header in milliseconds; udx gives up on a silent one only after ~13 s
|
|
21
|
+
const SILENT_TIMEOUT = 3000
|
|
22
|
+
const RELOOKUPS = [5000, 20000, 60000]
|
|
23
|
+
|
|
24
|
+
/** @type {(topic: Uint8Array, channel: string | null) => Uint8Array} */
|
|
20
25
|
export function channelTopic(topic, channel) {
|
|
21
26
|
return channel ? hash([topic, b4a.from(channel)]) : topic
|
|
22
27
|
}
|
|
@@ -25,16 +30,16 @@ export function channelTopic(topic, channel) {
|
|
|
25
30
|
* @typedef {object} NetworkOpts
|
|
26
31
|
* @property {import('../identity/index.js').Identity} [identity] Long-lived keypair used as the swarm identity.
|
|
27
32
|
* @property {Array<{ host: string, port: number }>} [bootstrap] Custom DHT bootstrap nodes.
|
|
28
|
-
* @property {(remotePublicKey: Uint8Array, payload:
|
|
33
|
+
* @property {(remotePublicKey: Uint8Array, payload: unknown) => boolean} [firewall] Incoming-connection filter.
|
|
29
34
|
* @property {Uint8Array[]} [relayThrough] Relay public keys to tunnel through.
|
|
30
35
|
* @property {number[]} [backoffs] Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
|
|
31
36
|
* @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
|
|
32
|
-
* @property {
|
|
37
|
+
* @property {import('corestore')} [store] Corestore; required for mirrors (blind peers replicate its cores).
|
|
33
38
|
* @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
|
|
34
|
-
* @property {(err:
|
|
39
|
+
* @property {(err: Error) => void} [onerror] Background-task error handler.
|
|
35
40
|
* @property {{ active?: number, announced?: number, idle?: number }} [presence] Swarm budget for attached databases: how many search, how many only announce, and the idle ms before the rest leave.
|
|
36
41
|
*
|
|
37
|
-
* @typedef {{ replicate: (stream:
|
|
42
|
+
* @typedef {{ replicate: (stream: import('@hyperswarm/secret-stream')) => unknown }} Replicable
|
|
38
43
|
*/
|
|
39
44
|
|
|
40
45
|
/**
|
|
@@ -62,36 +67,50 @@ export class Network extends ReadyResource {
|
|
|
62
67
|
this.relayThrough = relayThrough || null
|
|
63
68
|
this.backoffs = backoffs || null
|
|
64
69
|
this.channel = channel || null
|
|
70
|
+
/** @type {import('corestore') | null} */
|
|
65
71
|
this.store = store || null
|
|
72
|
+
/** @type {Uint8Array[]} */
|
|
66
73
|
this.mirrors = (mirrors || []).map((k) => (typeof k === 'string' ? decodeKey(k) : k))
|
|
67
74
|
|
|
75
|
+
/** @private */
|
|
68
76
|
this._swarm = null
|
|
77
|
+
/** @type {import('protomux-wakeup')} */
|
|
69
78
|
this.wakeup = new ProtomuxWakeup()
|
|
70
79
|
this.presence = new Presence(this, presence)
|
|
71
80
|
|
|
72
81
|
this.info = null
|
|
82
|
+
/** @private */
|
|
73
83
|
this._peerInfo = new Map()
|
|
84
|
+
/** @private */
|
|
74
85
|
this._infoSenders = new Set()
|
|
75
86
|
|
|
87
|
+
/** @private */
|
|
76
88
|
this._replicateables = new Set()
|
|
89
|
+
/** @private */
|
|
77
90
|
this._discoveries = new Set()
|
|
91
|
+
/** @private */
|
|
78
92
|
this._injected = new Set()
|
|
79
|
-
|
|
93
|
+
/** @private */
|
|
80
94
|
this._blindPeering = null
|
|
95
|
+
/** @private */
|
|
96
|
+
this._lost = new Set()
|
|
97
|
+
/** @private */
|
|
98
|
+
this._relookup = null
|
|
99
|
+
/** @private */
|
|
81
100
|
this._onerror = onerror
|
|
82
101
|
}
|
|
83
102
|
|
|
84
|
-
/** @returns {
|
|
103
|
+
/** @returns {import('hyperswarm') | null} The underlying hyperswarm, or null before ready / after close. */
|
|
85
104
|
get swarm() {
|
|
86
105
|
return this._swarm
|
|
87
106
|
}
|
|
88
107
|
|
|
89
|
-
/** @returns {Map<string,
|
|
108
|
+
/** @returns {Map<string, object>} Known peers keyed by public-key string. */
|
|
90
109
|
get peers() {
|
|
91
110
|
return this.swarm ? this.swarm.peers : new Map()
|
|
92
111
|
}
|
|
93
112
|
|
|
94
|
-
/** @returns {Set<
|
|
113
|
+
/** @returns {Set<import('@hyperswarm/secret-stream')>} Live connection streams — swarm and injected. */
|
|
95
114
|
get connections() {
|
|
96
115
|
if (!this._injected.size) return this.swarm ? this.swarm.connections : new Set()
|
|
97
116
|
return new Set([...(this.swarm ? this.swarm.connections : []), ...this._injected])
|
|
@@ -102,38 +121,51 @@ export class Network extends ReadyResource {
|
|
|
102
121
|
return this.swarm?.suspended === true
|
|
103
122
|
}
|
|
104
123
|
|
|
124
|
+
/** @private */
|
|
105
125
|
async _open() {
|
|
106
|
-
|
|
126
|
+
// timeouts follow each node's measured round trip, not a flat second a try; the swarm destroys it
|
|
127
|
+
const dht = new HyperDHT({
|
|
128
|
+
bootstrap: this.bootstrap,
|
|
129
|
+
adaptiveTimeout: { min: 150, max: 1000 }
|
|
130
|
+
})
|
|
131
|
+
const opts = { dht }
|
|
107
132
|
if (this.identity) {
|
|
108
133
|
opts.keyPair = { publicKey: this.identity.publicKey, secretKey: this.identity.secretKey }
|
|
109
134
|
}
|
|
110
|
-
if (this.bootstrap) opts.bootstrap = this.bootstrap
|
|
111
135
|
if (this.firewall) opts.firewall = this.firewall
|
|
112
136
|
if (this.relayThrough) opts.relayThrough = this.relayThrough
|
|
113
137
|
if (this.backoffs) opts.backoffs = this.backoffs
|
|
114
138
|
|
|
115
139
|
const swarm = (this._swarm = new Hyperswarm(opts))
|
|
140
|
+
// back online after a NAT rebind or a router restart, peers may hold a stale address for us
|
|
141
|
+
dht.on('network-update', () => {
|
|
142
|
+
// a tick later: the dht's own wake-up in this event would clear a refresh made now
|
|
143
|
+
if (dht.online) setTimeout(() => this.closing || swarm.server.refresh(), 0)
|
|
144
|
+
})
|
|
116
145
|
swarm.on('connection', (stream, info) => {
|
|
117
146
|
if (this.closing || this.closed) {
|
|
118
147
|
stream.destroy()
|
|
119
148
|
return
|
|
120
149
|
}
|
|
150
|
+
dropIfSilent(stream)
|
|
151
|
+
stream.on('close', () => this._lose(stream.remotePublicKey))
|
|
121
152
|
this.wakeup.addStream(stream)
|
|
122
|
-
this._attachInfo(stream)
|
|
123
153
|
for (const r of this._replicateables) replicateInto(r, stream)
|
|
124
154
|
this.emit('connection', stream, info)
|
|
125
155
|
})
|
|
126
156
|
|
|
127
|
-
if (this.store
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
})
|
|
157
|
+
if (this.store) {
|
|
158
|
+
// mailbox cores live in the store: every connection replicates it, so a listener can fetch them
|
|
159
|
+
const store = this.store
|
|
160
|
+
this._replicateables.add({ store, replicate: (stream) => store.replicate(stream) })
|
|
161
|
+
if (this.mirrors.length) this.peering()
|
|
133
162
|
}
|
|
134
163
|
}
|
|
135
164
|
|
|
165
|
+
/** @private */
|
|
136
166
|
async _close() {
|
|
167
|
+
clearTimeout(this._relookup)
|
|
168
|
+
this._lost.clear()
|
|
137
169
|
for (const conn of [...this._injected]) {
|
|
138
170
|
try {
|
|
139
171
|
conn.destroy()
|
|
@@ -168,15 +200,6 @@ export class Network extends ReadyResource {
|
|
|
168
200
|
this._blindPeering = null
|
|
169
201
|
}
|
|
170
202
|
|
|
171
|
-
if (this._blind) {
|
|
172
|
-
try {
|
|
173
|
-
await (await this._blind).close()
|
|
174
|
-
} catch (err) {
|
|
175
|
-
safetyCatch(err)
|
|
176
|
-
}
|
|
177
|
-
this._blind = null
|
|
178
|
-
}
|
|
179
|
-
|
|
180
203
|
if (this.wakeup) {
|
|
181
204
|
try {
|
|
182
205
|
await this.wakeup.destroy()
|
|
@@ -190,14 +213,14 @@ export class Network extends ReadyResource {
|
|
|
190
213
|
* Feed an externally-established connection — a Bluetooth L2CAP channel, a serial link, an
|
|
191
214
|
* in-process pair, any duplex — into the network.
|
|
192
215
|
*
|
|
193
|
-
* @param {
|
|
216
|
+
* @param {import('streamx').Duplex} stream Duplex transport, or a ready NoiseSecretStream.
|
|
194
217
|
* @param {{ isInitiator?: boolean }} [opts] Which side initiates the noise handshake (raw duplexes only).
|
|
195
|
-
* @returns {
|
|
218
|
+
* @returns {import('@hyperswarm/secret-stream')} The encrypted connection stream.
|
|
196
219
|
*/
|
|
197
220
|
inject(stream, { isInitiator } = {}) {
|
|
198
221
|
if (this.closing || this.closed) throw CeroError.CLOSED('Network')
|
|
199
222
|
if (!stream) throw CeroError.REQUIRED('stream')
|
|
200
|
-
//
|
|
223
|
+
// a raw duplex handshakes as this identity
|
|
201
224
|
const keyPair = this.identity
|
|
202
225
|
? { publicKey: this.identity.publicKey, secretKey: this.identity.secretKey }
|
|
203
226
|
: (this.swarm?.keyPair ?? undefined)
|
|
@@ -206,9 +229,6 @@ export class Network extends ReadyResource {
|
|
|
206
229
|
? stream
|
|
207
230
|
: new NoiseSecretStream(isInitiator === true, stream, keyPair ? { keyPair } : undefined)
|
|
208
231
|
|
|
209
|
-
// blind-pairing sends on the lowest-rtt channel; an injected duplex has no rtt, so give it one
|
|
210
|
-
if (conn.rawStream && conn.rawStream.rtt === undefined) conn.rawStream.rtt = 0
|
|
211
|
-
|
|
212
232
|
this._injected.add(conn)
|
|
213
233
|
conn.on('close', () => this._injected.delete(conn))
|
|
214
234
|
conn.on('error', safetyCatch) // a dropped radio link must not crash the host
|
|
@@ -221,41 +241,26 @@ export class Network extends ReadyResource {
|
|
|
221
241
|
}
|
|
222
242
|
|
|
223
243
|
/**
|
|
224
|
-
*
|
|
244
|
+
* The blind-peering client, built on first use: a mailbox post names mirrors this network
|
|
245
|
+
* may not have been given.
|
|
225
246
|
*
|
|
226
|
-
* @returns {
|
|
247
|
+
* @returns {import('blind-peering')}
|
|
227
248
|
*/
|
|
228
|
-
|
|
249
|
+
peering() {
|
|
229
250
|
if (this.closing || this.closed) throw CeroError.CLOSED('Network')
|
|
230
|
-
if (!this.
|
|
231
|
-
|
|
232
|
-
this.
|
|
233
|
-
|
|
234
|
-
this.
|
|
235
|
-
|
|
236
|
-
})
|
|
237
|
-
for (const conn of this._injected) blind._onconnection(conn)
|
|
238
|
-
return blind
|
|
251
|
+
if (!this.store) throw CeroError.REQUIRED('store')
|
|
252
|
+
if (!this._blindPeering) {
|
|
253
|
+
this._blindPeering = new BlindPeering(this.swarm.dht, this.store, {
|
|
254
|
+
blindPeers: this.mirrors.map((key) => ({ key })),
|
|
255
|
+
wakeup: this.wakeup,
|
|
256
|
+
pick: 2
|
|
239
257
|
})
|
|
240
258
|
}
|
|
241
|
-
return this.
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Re-attach pairing channels on injected connections. blind-pairing only auto-attaches
|
|
246
|
-
* refs that existed when a connection arrived — swarm peers meet again over topic joins,
|
|
247
|
-
* injected links (Bluetooth,.
|
|
248
|
-
*
|
|
249
|
-
* @returns {Promise<void>}
|
|
250
|
-
*/
|
|
251
|
-
async refreshInjected() {
|
|
252
|
-
if (!this._blind || !this._injected.size) return
|
|
253
|
-
const blind = await this._blind
|
|
254
|
-
for (const conn of this._injected) blind._onconnection(conn)
|
|
259
|
+
return this._blindPeering
|
|
255
260
|
}
|
|
256
261
|
|
|
257
262
|
/**
|
|
258
|
-
* Declare this peer's self-reported info ({ name, ... }).
|
|
263
|
+
* Declare this peer's self-reported info ({ name, ... }) to the peers of injected streams.
|
|
259
264
|
*
|
|
260
265
|
* @param {object | null} info
|
|
261
266
|
*/
|
|
@@ -272,7 +277,7 @@ export class Network extends ReadyResource {
|
|
|
272
277
|
* @returns {object | null}
|
|
273
278
|
*/
|
|
274
279
|
getInfo(key) {
|
|
275
|
-
const hex = typeof key === 'string' ? key : b4a.
|
|
280
|
+
const hex = typeof key === 'string' ? key : b4a.toHex(key)
|
|
276
281
|
return this._peerInfo.get(hex) ?? null
|
|
277
282
|
}
|
|
278
283
|
|
|
@@ -296,6 +301,7 @@ export class Network extends ReadyResource {
|
|
|
296
301
|
if (this.closing || this.closed) return
|
|
297
302
|
await this._blindPeering?.suspend()
|
|
298
303
|
await this._swarm?.suspend()
|
|
304
|
+
clearTimeout(this._relookup)
|
|
299
305
|
}
|
|
300
306
|
|
|
301
307
|
/**
|
|
@@ -304,9 +310,11 @@ export class Network extends ReadyResource {
|
|
|
304
310
|
* @returns {Promise<void>}
|
|
305
311
|
*/
|
|
306
312
|
async resume() {
|
|
307
|
-
if (this.closing || this.closed) return
|
|
313
|
+
if (this.closing || this.closed || !this.suspended) return
|
|
308
314
|
await this._swarm?.resume()
|
|
309
315
|
await this._blindPeering?.resume()
|
|
316
|
+
// the lookups hyperswarm runs at resume can go out before the network is back
|
|
317
|
+
this._relook(0)
|
|
310
318
|
}
|
|
311
319
|
|
|
312
320
|
/**
|
|
@@ -352,6 +360,7 @@ export class Network extends ReadyResource {
|
|
|
352
360
|
}
|
|
353
361
|
}
|
|
354
362
|
|
|
363
|
+
/** @private */
|
|
355
364
|
_mirror(bee) {
|
|
356
365
|
const peering = this._blindPeering
|
|
357
366
|
if (!peering || bee.closing) return
|
|
@@ -392,7 +401,31 @@ export class Network extends ReadyResource {
|
|
|
392
401
|
for (const stream of this.connections) replicateInto(target, stream)
|
|
393
402
|
}
|
|
394
403
|
|
|
395
|
-
//
|
|
404
|
+
// hyperswarm stops redialing a peer after a few failed tries and looks it up again only every 10 min
|
|
405
|
+
/** @private */
|
|
406
|
+
_lose(key) {
|
|
407
|
+
if (this.closing || this.closed) return
|
|
408
|
+
this._lost.add(b4a.toHex(key))
|
|
409
|
+
this._relook(0)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** @private */
|
|
413
|
+
_relook(i) {
|
|
414
|
+
clearTimeout(this._relookup)
|
|
415
|
+
this._relookup = setTimeout(() => {
|
|
416
|
+
for (const conn of this.swarm.connections) this._lost.delete(b4a.toHex(conn.remotePublicKey))
|
|
417
|
+
if (!this._lost.size && this.swarm.connections.size) return
|
|
418
|
+
for (const d of this._discoveries) {
|
|
419
|
+
if (d.mode === ACTIVE) d.session.refresh().catch(safetyCatch)
|
|
420
|
+
}
|
|
421
|
+
if (i + 1 < RELOOKUPS.length) this._relook(i + 1)
|
|
422
|
+
else this._lost.clear()
|
|
423
|
+
}, RELOOKUPS[i])
|
|
424
|
+
this._relookup.unref()
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// injected streams only: bytes on a fresh swarm connection trip hyperswarm's duplicate guard
|
|
428
|
+
/** @private */
|
|
396
429
|
_attachInfo(conn) {
|
|
397
430
|
const mux = Protomux.from(conn)
|
|
398
431
|
let message = null
|
|
@@ -404,7 +437,7 @@ export class Network extends ReadyResource {
|
|
|
404
437
|
encoding: c.json,
|
|
405
438
|
onmessage: (info) => {
|
|
406
439
|
if (!info || typeof info !== 'object') return
|
|
407
|
-
const hex = b4a.
|
|
440
|
+
const hex = b4a.toHex(conn.remotePublicKey)
|
|
408
441
|
this._peerInfo.set(hex, info)
|
|
409
442
|
this.emit('peer-info', hex, info)
|
|
410
443
|
}
|
|
@@ -420,6 +453,14 @@ export class Network extends ReadyResource {
|
|
|
420
453
|
}
|
|
421
454
|
}
|
|
422
455
|
|
|
456
|
+
function dropIfSilent(conn) {
|
|
457
|
+
const timer = setTimeout(() => {
|
|
458
|
+
if (conn.rawStream.packetsReceived === 0) conn.destroy()
|
|
459
|
+
}, SILENT_TIMEOUT)
|
|
460
|
+
timer.unref()
|
|
461
|
+
conn.once('close', () => clearTimeout(timer))
|
|
462
|
+
}
|
|
463
|
+
|
|
423
464
|
// corestore replication is store-wide: replicate each root once per connection
|
|
424
465
|
const replicatedRoots = new WeakMap()
|
|
425
466
|
|
package/src/network/presence.js
CHANGED
|
@@ -22,6 +22,7 @@ export class Presence {
|
|
|
22
22
|
this.active = active
|
|
23
23
|
this.announced = announced
|
|
24
24
|
this.idle = idle
|
|
25
|
+
/** @type {Map<string, { topic: Uint8Array, pinned: boolean, refs: number, touched: number, off: boolean, discovery: import('./discovery.js').Discovery | null, timer: ReturnType<typeof setTimeout> | null, pending: 'active' | 'passive' | null }>} */
|
|
25
26
|
this.entries = new Map()
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -34,7 +35,7 @@ export class Presence {
|
|
|
34
35
|
* @returns {Slot}
|
|
35
36
|
*/
|
|
36
37
|
add(topic, { pinned = false } = {}) {
|
|
37
|
-
const id = b4a.
|
|
38
|
+
const id = b4a.toHex(topic)
|
|
38
39
|
let e = this.entries.get(id)
|
|
39
40
|
if (!e) {
|
|
40
41
|
e = {
|
|
@@ -78,7 +79,7 @@ export class Presence {
|
|
|
78
79
|
* @returns {'active' | 'passive' | null}
|
|
79
80
|
*/
|
|
80
81
|
mode(topic) {
|
|
81
|
-
const e = this.entries.get(b4a.
|
|
82
|
+
const e = this.entries.get(b4a.toHex(topic))
|
|
82
83
|
return e?.discovery ? e.discovery.mode : null
|
|
83
84
|
}
|
|
84
85
|
|
|
@@ -102,6 +103,7 @@ export class Presence {
|
|
|
102
103
|
}
|
|
103
104
|
|
|
104
105
|
// up at once, down after idle: a room at the edge of the budget must not churn the DHT
|
|
106
|
+
/** @private */
|
|
105
107
|
_want(e, mode) {
|
|
106
108
|
const current = e.discovery ? e.discovery.mode : null
|
|
107
109
|
if (rank(mode) < rank(current)) {
|
|
@@ -116,12 +118,14 @@ export class Presence {
|
|
|
116
118
|
else e.discovery.activate().catch(safetyCatch)
|
|
117
119
|
}
|
|
118
120
|
|
|
121
|
+
/** @private */
|
|
119
122
|
_settle(e) {
|
|
120
123
|
e.timer = null
|
|
121
124
|
if (e.pending === PASSIVE) e.discovery.deactivate().catch(safetyCatch)
|
|
122
125
|
else this._leave(e)
|
|
123
126
|
}
|
|
124
127
|
|
|
128
|
+
/** @private */
|
|
125
129
|
_leave(e) {
|
|
126
130
|
clearTimeout(e.timer)
|
|
127
131
|
e.timer = null
|