@cero-base/core 1.19.0 → 2.1.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/README.md +1 -1
- package/package.json +9 -6
- package/src/blobs/index.js +15 -15
- package/src/blobs/server.js +0 -2
- package/src/database/changes.js +4 -8
- package/src/database/dispatch.js +296 -227
- package/src/database/encryption.js +22 -52
- package/src/database/envelope.js +1 -3
- package/src/database/index.js +406 -476
- package/src/database/rotation.js +56 -70
- package/src/identity/index.js +5 -9
- package/src/index.js +5 -9
- package/src/lib/constants.js +3 -12
- package/src/lib/errors.js +6 -10
- package/src/lib/schema.js +2 -5
- package/src/lib/utils.js +9 -24
- package/src/network/index.js +144 -167
- package/src/network/presence.js +131 -0
- package/src/pairing/candidate.js +2 -4
- package/src/pairing/index.js +41 -55
- package/src/pairing/invite.js +3 -4
- package/src/pairing/request.js +3 -9
- package/src/rpc/index.js +4 -14
- package/src/rpc/peer.js +3 -7
- package/src/storage/index.js +29 -50
- package/types/blobs/index.d.ts +5 -5
- package/types/blobs/server.d.ts +0 -2
- package/types/database/changes.d.ts +2 -5
- package/types/database/dispatch.d.ts +11 -7
- package/types/database/encryption.d.ts +8 -20
- package/types/database/index.d.ts +164 -141
- package/types/database/rotation.d.ts +14 -20
- package/types/identity/index.d.ts +5 -9
- package/types/index.d.ts +4 -3
- package/types/lib/errors.d.ts +6 -10
- package/types/lib/schema.d.ts +2 -5
- package/types/lib/utils.d.ts +3 -7
- package/types/network/index.d.ts +28 -27
- package/types/network/presence.d.ts +50 -0
- package/types/pairing/candidate.d.ts +1 -2
- package/types/pairing/index.d.ts +22 -30
- package/types/pairing/invite.d.ts +3 -3
- package/types/pairing/request.d.ts +1 -3
- package/types/rpc/index.d.ts +1 -4
- package/types/rpc/peer.d.ts +2 -4
- package/types/storage/index.d.ts +17 -30
package/src/network/index.js
CHANGED
|
@@ -15,6 +15,7 @@ import c from 'compact-encoding'
|
|
|
15
15
|
import { ACTIVE, PASSIVE } from '../lib/constants.js'
|
|
16
16
|
import { CeroError } from '../lib/errors.js'
|
|
17
17
|
import { Discovery } from './discovery.js'
|
|
18
|
+
import { Presence } from './presence.js'
|
|
18
19
|
|
|
19
20
|
export function channelTopic(topic, channel) {
|
|
20
21
|
return channel ? hash([topic, b4a.from(channel)]) : topic
|
|
@@ -30,6 +31,7 @@ export function channelTopic(topic, channel) {
|
|
|
30
31
|
* @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
|
|
31
32
|
* @property {any} [store] Corestore; required for mirrors (blind peers replicate its cores).
|
|
32
33
|
* @property {Array<string | Uint8Array>} [mirrors] Blind-peer public keys; each attached room/blob core is mirrored through them for offline sync.
|
|
34
|
+
* @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.
|
|
33
35
|
*
|
|
34
36
|
* @typedef {{ replicate: (stream: any) => any }} Replicable
|
|
35
37
|
*/
|
|
@@ -48,7 +50,8 @@ export class Network extends ReadyResource {
|
|
|
48
50
|
backoffs,
|
|
49
51
|
channel,
|
|
50
52
|
store,
|
|
51
|
-
mirrors
|
|
53
|
+
mirrors,
|
|
54
|
+
presence
|
|
52
55
|
} = {}) {
|
|
53
56
|
super()
|
|
54
57
|
this.identity = identity || null
|
|
@@ -62,6 +65,7 @@ export class Network extends ReadyResource {
|
|
|
62
65
|
|
|
63
66
|
this._swarm = null
|
|
64
67
|
this.wakeup = new ProtomuxWakeup()
|
|
68
|
+
this.presence = new Presence(this, presence)
|
|
65
69
|
|
|
66
70
|
this.info = null
|
|
67
71
|
this._peerInfo = new Map()
|
|
@@ -79,13 +83,109 @@ export class Network extends ReadyResource {
|
|
|
79
83
|
return this._swarm
|
|
80
84
|
}
|
|
81
85
|
|
|
86
|
+
/** @returns {Map<string, any>} Known peers keyed by public-key string. */
|
|
87
|
+
get peers() {
|
|
88
|
+
return this.swarm ? this.swarm.peers : new Map()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** @returns {Set<any>} Live connection streams — swarm and injected. */
|
|
92
|
+
get connections() {
|
|
93
|
+
if (!this._injected.size) return this.swarm ? this.swarm.connections : new Set()
|
|
94
|
+
return new Set([...(this.swarm ? this.swarm.connections : []), ...this._injected])
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @returns {boolean} */
|
|
98
|
+
get suspended() {
|
|
99
|
+
return this.swarm?.suspended === true
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async _open() {
|
|
103
|
+
const opts = {}
|
|
104
|
+
if (this.identity) {
|
|
105
|
+
opts.keyPair = { publicKey: this.identity.publicKey, secretKey: this.identity.secretKey }
|
|
106
|
+
}
|
|
107
|
+
if (this.bootstrap) opts.bootstrap = this.bootstrap
|
|
108
|
+
if (this.firewall) opts.firewall = this.firewall
|
|
109
|
+
if (this.relayThrough) opts.relayThrough = this.relayThrough
|
|
110
|
+
if (this.backoffs) opts.backoffs = this.backoffs
|
|
111
|
+
|
|
112
|
+
const swarm = (this._swarm = new Hyperswarm(opts))
|
|
113
|
+
swarm.on('connection', (stream, info) => {
|
|
114
|
+
if (this.closing || this.closed) {
|
|
115
|
+
stream.destroy()
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
this.wakeup.addStream(stream)
|
|
119
|
+
this._attachInfo(stream)
|
|
120
|
+
for (const r of this._replicateables) replicateInto(r, stream)
|
|
121
|
+
this.emit('connection', stream, info)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
if (this.store && this.mirrors.length) {
|
|
125
|
+
this._blindPeering = new BlindPeering(swarm.dht, this.store, {
|
|
126
|
+
blindPeers: this.mirrors.map((key) => ({ key })),
|
|
127
|
+
wakeup: this.wakeup,
|
|
128
|
+
pick: 2
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async _close() {
|
|
134
|
+
for (const conn of [...this._injected]) {
|
|
135
|
+
try {
|
|
136
|
+
conn.destroy()
|
|
137
|
+
} catch (err) {
|
|
138
|
+
safetyCatch(err)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
this._injected.clear()
|
|
142
|
+
|
|
143
|
+
// stop discovery now, never await the DHT unannounce round-trips
|
|
144
|
+
this.presence.close()
|
|
145
|
+
for (const d of [...this._discoveries]) d.destroy().catch(safetyCatch)
|
|
146
|
+
this._discoveries.clear()
|
|
147
|
+
|
|
148
|
+
// the swarm dies first with force, so per-topic unannounces become no-ops
|
|
149
|
+
const swarm = this._swarm
|
|
150
|
+
this._swarm = null
|
|
151
|
+
if (swarm) {
|
|
152
|
+
// force destroy still awaits a server.close round-trip; cap it
|
|
153
|
+
await Promise.race([
|
|
154
|
+
swarm.destroy({ force: true }).catch(safetyCatch),
|
|
155
|
+
new Promise((r) => setTimeout(r, 750))
|
|
156
|
+
])
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (this._blindPeering) {
|
|
160
|
+
try {
|
|
161
|
+
await this._blindPeering.close()
|
|
162
|
+
} catch (err) {
|
|
163
|
+
safetyCatch(err)
|
|
164
|
+
}
|
|
165
|
+
this._blindPeering = null
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (this._blind) {
|
|
169
|
+
try {
|
|
170
|
+
await (await this._blind).close()
|
|
171
|
+
} catch (err) {
|
|
172
|
+
safetyCatch(err)
|
|
173
|
+
}
|
|
174
|
+
this._blind = null
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (this.wakeup) {
|
|
178
|
+
try {
|
|
179
|
+
await this.wakeup.destroy()
|
|
180
|
+
} catch (err) {
|
|
181
|
+
safetyCatch(err)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
82
186
|
/**
|
|
83
|
-
* Feed an externally-established connection — a Bluetooth L2CAP channel, a
|
|
84
|
-
*
|
|
85
|
-
* duplex is wrapped in NoiseSecretStream (pass `isInitiator`); a stream
|
|
86
|
-
* that already IS one is used as-is. From here it gets the exact same
|
|
87
|
-
* treatment as a swarm connection: wakeup, replication of every attached
|
|
88
|
-
* core, pairing, and the 'connection' event.
|
|
187
|
+
* Feed an externally-established connection — a Bluetooth L2CAP channel, a serial link, an
|
|
188
|
+
* in-process pair, any duplex — into the network.
|
|
89
189
|
*
|
|
90
190
|
* @param {any} stream Duplex transport, or a ready NoiseSecretStream.
|
|
91
191
|
* @param {{ isInitiator?: boolean }} [opts] Which side initiates the noise handshake (raw duplexes only).
|
|
@@ -94,9 +194,7 @@ export class Network extends ReadyResource {
|
|
|
94
194
|
inject(stream, { isInitiator } = {}) {
|
|
95
195
|
if (this.closing || this.closed) throw CeroError.CLOSED('Network')
|
|
96
196
|
if (!stream) throw CeroError.REQUIRED('stream')
|
|
97
|
-
//
|
|
98
|
-
// swarm — an ephemeral key per link would defeat duplicate-peer detection
|
|
99
|
-
// (two radios between one pair would look like two different peers).
|
|
197
|
+
// the swarm identity, so two radios between one pair dedupe to one peer
|
|
100
198
|
const keyPair = this.identity
|
|
101
199
|
? { publicKey: this.identity.publicKey, secretKey: this.identity.secretKey }
|
|
102
200
|
: (this.swarm?.keyPair ?? undefined)
|
|
@@ -105,10 +203,7 @@ export class Network extends ReadyResource {
|
|
|
105
203
|
? stream
|
|
106
204
|
: new NoiseSecretStream(isInitiator === true, stream, keyPair ? { keyPair } : undefined)
|
|
107
205
|
|
|
108
|
-
// blind-pairing
|
|
109
|
-
// field only exists on real udx sockets. A raw injected duplex has none,
|
|
110
|
-
// so `rtt < Infinity` is always false and pairing never sends a request.
|
|
111
|
-
// Zero is also the semantically correct RTT for a direct injected link.
|
|
206
|
+
// blind-pairing sends on the lowest-rtt channel; an injected duplex has no rtt, so give it one
|
|
112
207
|
if (conn.rawStream && conn.rawStream.rtt === undefined) conn.rawStream.rtt = 0
|
|
113
208
|
|
|
114
209
|
this._injected.add(conn)
|
|
@@ -123,9 +218,7 @@ export class Network extends ReadyResource {
|
|
|
123
218
|
}
|
|
124
219
|
|
|
125
220
|
/**
|
|
126
|
-
* Lazily create the network-shared BlindPairing.
|
|
127
|
-
* handle's pairing member — per-handle instances each added their own swarm
|
|
128
|
-
* and DHT listeners plus a protomux channel per connection.
|
|
221
|
+
* Lazily create the network-shared BlindPairing.
|
|
129
222
|
*
|
|
130
223
|
* @returns {Promise<any>}
|
|
131
224
|
*/
|
|
@@ -134,9 +227,7 @@ export class Network extends ReadyResource {
|
|
|
134
227
|
if (!this._blind) {
|
|
135
228
|
const blind = new BlindPairing(this.swarm)
|
|
136
229
|
this._blind = blind.ready().then(() => {
|
|
137
|
-
// blind-pairing only watches the swarm
|
|
138
|
-
// reach it too, or offline (e.g. Bluetooth) pairing never completes.
|
|
139
|
-
// _onconnection is upstream-private; the offline-pairing test pins it.
|
|
230
|
+
// blind-pairing only watches the swarm; injected connections must reach it too
|
|
140
231
|
this.on('connection', (conn, info) => {
|
|
141
232
|
if (info?.injected) blind._onconnection(conn)
|
|
142
233
|
})
|
|
@@ -148,11 +239,9 @@ export class Network extends ReadyResource {
|
|
|
148
239
|
}
|
|
149
240
|
|
|
150
241
|
/**
|
|
151
|
-
* Re-attach pairing channels on injected connections. blind-pairing only
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* a member/candidate added later must re-run the attach. Idempotent:
|
|
155
|
-
* protomux refuses duplicate channels.
|
|
242
|
+
* Re-attach pairing channels on injected connections. blind-pairing only auto-attaches
|
|
243
|
+
* refs that existed when a connection arrived — swarm peers meet again over topic joins,
|
|
244
|
+
* injected links (Bluetooth,.
|
|
156
245
|
*
|
|
157
246
|
* @returns {Promise<void>}
|
|
158
247
|
*/
|
|
@@ -163,9 +252,7 @@ export class Network extends ReadyResource {
|
|
|
163
252
|
}
|
|
164
253
|
|
|
165
254
|
/**
|
|
166
|
-
* Declare this peer's self-reported info ({ name, ... }).
|
|
167
|
-
* encrypted channel on every live and future connection — never on the
|
|
168
|
-
* radio advertisement or the DHT.
|
|
255
|
+
* Declare this peer's self-reported info ({ name, ... }).
|
|
169
256
|
*
|
|
170
257
|
* @param {object | null} info
|
|
171
258
|
*/
|
|
@@ -186,82 +273,6 @@ export class Network extends ReadyResource {
|
|
|
186
273
|
return this._peerInfo.get(hex) ?? null
|
|
187
274
|
}
|
|
188
275
|
|
|
189
|
-
// The channel stays silent until a side has info to send: bytes on a fresh
|
|
190
|
-
// connection make hyperswarm's duplicate-connection guard treat it as the
|
|
191
|
-
// stale one and drop it when both peers dialed each other at once.
|
|
192
|
-
_attachInfo(conn) {
|
|
193
|
-
const mux = Protomux.from(conn)
|
|
194
|
-
let message = null
|
|
195
|
-
const send = () => {
|
|
196
|
-
if (!message) {
|
|
197
|
-
const channel = mux.createChannel({ protocol: 'cero/info' })
|
|
198
|
-
if (channel === null) return
|
|
199
|
-
message = channel.addMessage({
|
|
200
|
-
encoding: c.json,
|
|
201
|
-
onmessage: (info) => {
|
|
202
|
-
if (!info || typeof info !== 'object') return
|
|
203
|
-
const hex = b4a.toString(conn.remotePublicKey, 'hex')
|
|
204
|
-
this._peerInfo.set(hex, info)
|
|
205
|
-
this.emit('peer-info', hex, info)
|
|
206
|
-
}
|
|
207
|
-
})
|
|
208
|
-
channel.open()
|
|
209
|
-
}
|
|
210
|
-
if (this.info) message.send(this.info)
|
|
211
|
-
}
|
|
212
|
-
mux.pair({ protocol: 'cero/info' }, send)
|
|
213
|
-
this._infoSenders.add(send)
|
|
214
|
-
conn.on('close', () => this._infoSenders.delete(send))
|
|
215
|
-
if (this.info) send()
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/** @returns {Map<string, any>} Known peers keyed by public-key string. */
|
|
219
|
-
get peers() {
|
|
220
|
-
return this.swarm ? this.swarm.peers : new Map()
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/** @returns {Set<any>} Live connection streams — swarm and injected. */
|
|
224
|
-
get connections() {
|
|
225
|
-
if (!this._injected.size) return this.swarm ? this.swarm.connections : new Set()
|
|
226
|
-
return new Set([...(this.swarm ? this.swarm.connections : []), ...this._injected])
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/** @returns {boolean} */
|
|
230
|
-
get suspended() {
|
|
231
|
-
return this.swarm?.suspended === true
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async _open() {
|
|
235
|
-
const opts = {}
|
|
236
|
-
if (this.identity) {
|
|
237
|
-
opts.keyPair = { publicKey: this.identity.publicKey, secretKey: this.identity.secretKey }
|
|
238
|
-
}
|
|
239
|
-
if (this.bootstrap) opts.bootstrap = this.bootstrap
|
|
240
|
-
if (this.firewall) opts.firewall = this.firewall
|
|
241
|
-
if (this.relayThrough) opts.relayThrough = this.relayThrough
|
|
242
|
-
if (this.backoffs) opts.backoffs = this.backoffs
|
|
243
|
-
|
|
244
|
-
const swarm = (this._swarm = new Hyperswarm(opts))
|
|
245
|
-
swarm.on('connection', (stream, info) => {
|
|
246
|
-
if (this.closing || this.closed) {
|
|
247
|
-
stream.destroy()
|
|
248
|
-
return
|
|
249
|
-
}
|
|
250
|
-
this.wakeup.addStream(stream)
|
|
251
|
-
this._attachInfo(stream)
|
|
252
|
-
for (const r of this._replicateables) replicateInto(r, stream)
|
|
253
|
-
this.emit('connection', stream, info)
|
|
254
|
-
})
|
|
255
|
-
|
|
256
|
-
if (this.store && this.mirrors.length) {
|
|
257
|
-
this._blindPeering = new BlindPeering(swarm.dht, this.store, {
|
|
258
|
-
blindPeers: this.mirrors.map((key) => ({ key })),
|
|
259
|
-
wakeup: this.wakeup,
|
|
260
|
-
pick: 2
|
|
261
|
-
})
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
276
|
/**
|
|
266
277
|
* Wait for pending DHT announces and lookups to settle, bounded by timeout.
|
|
267
278
|
*
|
|
@@ -303,63 +314,6 @@ export class Network extends ReadyResource {
|
|
|
303
314
|
await this._blindPeering?.resume()
|
|
304
315
|
}
|
|
305
316
|
|
|
306
|
-
async _close() {
|
|
307
|
-
for (const conn of [...this._injected]) {
|
|
308
|
-
try {
|
|
309
|
-
conn.destroy()
|
|
310
|
-
} catch (err) {
|
|
311
|
-
safetyCatch(err)
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
this._injected.clear()
|
|
315
|
-
|
|
316
|
-
// stop every discovery session NOW (timers die synchronously) but never
|
|
317
|
-
// await the DHT unannounce round-trips — the force destroy below skips
|
|
318
|
-
// upstream's own clear(), so un-destroyed sessions would leak live timers
|
|
319
|
-
for (const d of [...this._discoveries]) d.destroy().catch(safetyCatch)
|
|
320
|
-
this._discoveries.clear()
|
|
321
|
-
|
|
322
|
-
// the swarm dies FIRST, with force: every polite per-topic DHT unannounce
|
|
323
|
-
// (blind-pairing's included) becomes an instant no-op instead of a network
|
|
324
|
-
// round-trip — a vanished node ages out of the DHT regardless
|
|
325
|
-
const swarm = this._swarm
|
|
326
|
-
this._swarm = null
|
|
327
|
-
if (swarm) {
|
|
328
|
-
// even force destroy awaits a graceful server.close DHT round-trip
|
|
329
|
-
// upstream — cap the wait and let the teardown finish in the background
|
|
330
|
-
await Promise.race([
|
|
331
|
-
swarm.destroy({ force: true }).catch(safetyCatch),
|
|
332
|
-
new Promise((r) => setTimeout(r, 750))
|
|
333
|
-
])
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
if (this._blindPeering) {
|
|
337
|
-
try {
|
|
338
|
-
await this._blindPeering.close()
|
|
339
|
-
} catch (err) {
|
|
340
|
-
safetyCatch(err)
|
|
341
|
-
}
|
|
342
|
-
this._blindPeering = null
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
if (this._blind) {
|
|
346
|
-
try {
|
|
347
|
-
await (await this._blind).close()
|
|
348
|
-
} catch (err) {
|
|
349
|
-
safetyCatch(err)
|
|
350
|
-
}
|
|
351
|
-
this._blind = null
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (this.wakeup) {
|
|
355
|
-
try {
|
|
356
|
-
await this.wakeup.destroy()
|
|
357
|
-
} catch (err) {
|
|
358
|
-
safetyCatch(err)
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
317
|
/**
|
|
364
318
|
* Announce/lookup on a 32-byte topic and return a Discovery handle.
|
|
365
319
|
*
|
|
@@ -396,8 +350,7 @@ export class Network extends ReadyResource {
|
|
|
396
350
|
if (!core) throw CeroError.REQUIRED('core')
|
|
397
351
|
this._replicateables.add(core)
|
|
398
352
|
for (const stream of this.connections) replicateInto(core, stream)
|
|
399
|
-
// mirror the core so it stays available
|
|
400
|
-
// autobees announce their whole writer set, plain cores (blobs) just themselves
|
|
353
|
+
// mirror the core so it stays available while its writers are offline
|
|
401
354
|
if (this._blindPeering) {
|
|
402
355
|
if (Autobee.isAutobee(core)) {
|
|
403
356
|
this._blindPeering.addAutobaseBackground(core)
|
|
@@ -434,12 +387,36 @@ export class Network extends ReadyResource {
|
|
|
434
387
|
}
|
|
435
388
|
for (const stream of this.connections) replicateInto(target, stream)
|
|
436
389
|
}
|
|
390
|
+
|
|
391
|
+
// silent until a side has info: bytes on a fresh connection trip hyperswarm's duplicate guard
|
|
392
|
+
_attachInfo(conn) {
|
|
393
|
+
const mux = Protomux.from(conn)
|
|
394
|
+
let message = null
|
|
395
|
+
const send = () => {
|
|
396
|
+
if (!message) {
|
|
397
|
+
const channel = mux.createChannel({ protocol: 'cero/info' })
|
|
398
|
+
if (channel === null) return
|
|
399
|
+
message = channel.addMessage({
|
|
400
|
+
encoding: c.json,
|
|
401
|
+
onmessage: (info) => {
|
|
402
|
+
if (!info || typeof info !== 'object') return
|
|
403
|
+
const hex = b4a.toString(conn.remotePublicKey, 'hex')
|
|
404
|
+
this._peerInfo.set(hex, info)
|
|
405
|
+
this.emit('peer-info', hex, info)
|
|
406
|
+
}
|
|
407
|
+
})
|
|
408
|
+
channel.open()
|
|
409
|
+
}
|
|
410
|
+
if (this.info) message.send(this.info)
|
|
411
|
+
}
|
|
412
|
+
mux.pair({ protocol: 'cero/info' }, send)
|
|
413
|
+
this._infoSenders.add(send)
|
|
414
|
+
conn.on('close', () => this._infoSenders.delete(send))
|
|
415
|
+
if (this.info) send()
|
|
416
|
+
}
|
|
437
417
|
}
|
|
438
418
|
|
|
439
|
-
//
|
|
440
|
-
// re-attach every core AND add N duplicate StreamTracker records per
|
|
441
|
-
// connection. Replicate each root store once per stream; wakeup streams are
|
|
442
|
-
// added at the network level, so skipped bees lose nothing.
|
|
419
|
+
// corestore replication is store-wide: replicate each root once per connection
|
|
443
420
|
const replicatedRoots = new WeakMap()
|
|
444
421
|
|
|
445
422
|
function replicateInto(core, stream) {
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import safetyCatch from 'safety-catch'
|
|
2
|
+
import b4a from 'b4a'
|
|
3
|
+
|
|
4
|
+
import { ACTIVE, PASSIVE } from '../lib/constants.js'
|
|
5
|
+
|
|
6
|
+
const rank = (mode) => (mode === ACTIVE ? 2 : mode === PASSIVE ? 1 : 0)
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Which handles swarm. Ranked by last touch: the latest `active` search and announce, the
|
|
10
|
+
* next `announced` announce only, the rest leave their topic. A handle moves up at once and
|
|
11
|
+
* down only after `idle` ms. A pinned handle always searches, outside the budget.
|
|
12
|
+
*
|
|
13
|
+
* @typedef {{ mode: 'active' | 'passive' | null, touch: () => void, off: () => void, remove: () => void }} Slot
|
|
14
|
+
*/
|
|
15
|
+
export class Presence {
|
|
16
|
+
/**
|
|
17
|
+
* @param {import('./index.js').Network} network
|
|
18
|
+
* @param {{ active?: number, announced?: number, idle?: number }} [opts]
|
|
19
|
+
*/
|
|
20
|
+
constructor(network, { active = 8, announced = 8, idle = 30_000 } = {}) {
|
|
21
|
+
this.network = network
|
|
22
|
+
this.active = active
|
|
23
|
+
this.announced = announced
|
|
24
|
+
this.idle = idle
|
|
25
|
+
this.entries = new Map()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Register a topic. One entry per topic, shared by every slot on it until all are removed;
|
|
30
|
+
* a new entry starts untouched, at the bottom.
|
|
31
|
+
*
|
|
32
|
+
* @param {Uint8Array} topic
|
|
33
|
+
* @param {{ pinned?: boolean }} [opts]
|
|
34
|
+
* @returns {Slot}
|
|
35
|
+
*/
|
|
36
|
+
add(topic, { pinned = false } = {}) {
|
|
37
|
+
const id = b4a.toString(topic, 'hex')
|
|
38
|
+
let e = this.entries.get(id)
|
|
39
|
+
if (!e) {
|
|
40
|
+
e = {
|
|
41
|
+
topic,
|
|
42
|
+
pinned,
|
|
43
|
+
refs: 0,
|
|
44
|
+
touched: 0,
|
|
45
|
+
off: false,
|
|
46
|
+
discovery: null,
|
|
47
|
+
timer: null,
|
|
48
|
+
pending: null
|
|
49
|
+
}
|
|
50
|
+
this.entries.set(id, e)
|
|
51
|
+
}
|
|
52
|
+
e.refs++
|
|
53
|
+
this.refresh()
|
|
54
|
+
const presence = this
|
|
55
|
+
return {
|
|
56
|
+
get mode() {
|
|
57
|
+
return e.discovery ? e.discovery.mode : null
|
|
58
|
+
},
|
|
59
|
+
touch() {
|
|
60
|
+
e.touched = Date.now()
|
|
61
|
+
e.off = false
|
|
62
|
+
presence.refresh()
|
|
63
|
+
},
|
|
64
|
+
off() {
|
|
65
|
+
e.off = true
|
|
66
|
+
presence.refresh()
|
|
67
|
+
},
|
|
68
|
+
remove() {
|
|
69
|
+
if (--e.refs > 0) return
|
|
70
|
+
presence.entries.delete(id)
|
|
71
|
+
presence._leave(e)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {Uint8Array} topic
|
|
78
|
+
* @returns {'active' | 'passive' | null}
|
|
79
|
+
*/
|
|
80
|
+
mode(topic) {
|
|
81
|
+
const e = this.entries.get(b4a.toString(topic, 'hex'))
|
|
82
|
+
return e?.discovery ? e.discovery.mode : null
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
refresh() {
|
|
86
|
+
if (this.network.closing || this.network.closed) return
|
|
87
|
+
const ranked = []
|
|
88
|
+
for (const e of this.entries.values()) {
|
|
89
|
+
if (e.pinned) this._want(e, ACTIVE)
|
|
90
|
+
else if (e.off) this._want(e, null)
|
|
91
|
+
else ranked.push(e)
|
|
92
|
+
}
|
|
93
|
+
ranked.sort((a, b) => b.touched - a.touched)
|
|
94
|
+
ranked.forEach((e, i) => {
|
|
95
|
+
this._want(e, i < this.active ? ACTIVE : i < this.active + this.announced ? PASSIVE : null)
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
close() {
|
|
100
|
+
for (const e of this.entries.values()) clearTimeout(e.timer)
|
|
101
|
+
this.entries.clear()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// up at once, down after idle: a room at the edge of the budget must not churn the DHT
|
|
105
|
+
_want(e, mode) {
|
|
106
|
+
const current = e.discovery ? e.discovery.mode : null
|
|
107
|
+
if (rank(mode) < rank(current)) {
|
|
108
|
+
e.pending = mode
|
|
109
|
+
if (!e.timer) e.timer = setTimeout(() => this._settle(e), this.idle)
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
clearTimeout(e.timer)
|
|
113
|
+
e.timer = null
|
|
114
|
+
if (mode === current) return
|
|
115
|
+
if (!e.discovery) e.discovery = this.network.join(e.topic, { mode })
|
|
116
|
+
else e.discovery.activate().catch(safetyCatch)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
_settle(e) {
|
|
120
|
+
e.timer = null
|
|
121
|
+
if (e.pending === PASSIVE) e.discovery.deactivate().catch(safetyCatch)
|
|
122
|
+
else this._leave(e)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
_leave(e) {
|
|
126
|
+
clearTimeout(e.timer)
|
|
127
|
+
e.timer = null
|
|
128
|
+
e.discovery?.destroy().catch(safetyCatch)
|
|
129
|
+
e.discovery = null
|
|
130
|
+
}
|
|
131
|
+
}
|
package/src/pairing/candidate.js
CHANGED
|
@@ -7,8 +7,7 @@ import { Response, STATUS_DENIED } from './request.js'
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Internal — the joiner side: a candidate's handshake state machine (this *is* the
|
|
10
|
-
* blind-pairing `addCandidate`).
|
|
11
|
-
* exactly once via `_done` or `_fail`.
|
|
10
|
+
* blind-pairing `addCandidate`).
|
|
12
11
|
*
|
|
13
12
|
* @property {any} _candidate blind-pairing addCandidate handle (null until started).
|
|
14
13
|
* @property {ReturnType<typeof setTimeout> | null} _timer Pending timeout, if any.
|
|
@@ -109,8 +108,7 @@ export class Candidate {
|
|
|
109
108
|
}
|
|
110
109
|
|
|
111
110
|
function fromBlindError(err) {
|
|
112
|
-
// code-less
|
|
113
|
-
// mismatch, bad signature) — never a host decision, don't report DENIED
|
|
111
|
+
// a code-less rejection is a protocol failure, never a host decision
|
|
114
112
|
if (!err || !err.code) return CeroError.NETWORK_ERROR(err?.message || 'pairing failed')
|
|
115
113
|
switch (err.code) {
|
|
116
114
|
case 'INVITE_EXPIRED':
|