@cero-base/core 1.19.0 → 2.0.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/package.json +8 -5
- package/src/blobs/index.js +15 -15
- package/src/blobs/server.js +0 -2
- package/src/database/changes.js +3 -7
- package/src/database/dispatch.js +128 -203
- package/src/database/encryption.js +22 -52
- package/src/database/envelope.js +1 -3
- package/src/database/index.js +336 -387
- 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 +138 -166
- 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 +25 -31
- 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 +2 -6
- package/types/database/encryption.d.ts +8 -20
- package/types/database/index.d.ts +79 -96
- 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 +16 -26
- 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 -20
package/src/network/index.js
CHANGED
|
@@ -79,13 +79,108 @@ export class Network extends ReadyResource {
|
|
|
79
79
|
return this._swarm
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/** @returns {Map<string, any>} Known peers keyed by public-key string. */
|
|
83
|
+
get peers() {
|
|
84
|
+
return this.swarm ? this.swarm.peers : new Map()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @returns {Set<any>} Live connection streams — swarm and injected. */
|
|
88
|
+
get connections() {
|
|
89
|
+
if (!this._injected.size) return this.swarm ? this.swarm.connections : new Set()
|
|
90
|
+
return new Set([...(this.swarm ? this.swarm.connections : []), ...this._injected])
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @returns {boolean} */
|
|
94
|
+
get suspended() {
|
|
95
|
+
return this.swarm?.suspended === true
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async _open() {
|
|
99
|
+
const opts = {}
|
|
100
|
+
if (this.identity) {
|
|
101
|
+
opts.keyPair = { publicKey: this.identity.publicKey, secretKey: this.identity.secretKey }
|
|
102
|
+
}
|
|
103
|
+
if (this.bootstrap) opts.bootstrap = this.bootstrap
|
|
104
|
+
if (this.firewall) opts.firewall = this.firewall
|
|
105
|
+
if (this.relayThrough) opts.relayThrough = this.relayThrough
|
|
106
|
+
if (this.backoffs) opts.backoffs = this.backoffs
|
|
107
|
+
|
|
108
|
+
const swarm = (this._swarm = new Hyperswarm(opts))
|
|
109
|
+
swarm.on('connection', (stream, info) => {
|
|
110
|
+
if (this.closing || this.closed) {
|
|
111
|
+
stream.destroy()
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
this.wakeup.addStream(stream)
|
|
115
|
+
this._attachInfo(stream)
|
|
116
|
+
for (const r of this._replicateables) replicateInto(r, stream)
|
|
117
|
+
this.emit('connection', stream, info)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
if (this.store && this.mirrors.length) {
|
|
121
|
+
this._blindPeering = new BlindPeering(swarm.dht, this.store, {
|
|
122
|
+
blindPeers: this.mirrors.map((key) => ({ key })),
|
|
123
|
+
wakeup: this.wakeup,
|
|
124
|
+
pick: 2
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async _close() {
|
|
130
|
+
for (const conn of [...this._injected]) {
|
|
131
|
+
try {
|
|
132
|
+
conn.destroy()
|
|
133
|
+
} catch (err) {
|
|
134
|
+
safetyCatch(err)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
this._injected.clear()
|
|
138
|
+
|
|
139
|
+
// stop discovery now, never await the DHT unannounce round-trips
|
|
140
|
+
for (const d of [...this._discoveries]) d.destroy().catch(safetyCatch)
|
|
141
|
+
this._discoveries.clear()
|
|
142
|
+
|
|
143
|
+
// the swarm dies first with force, so per-topic unannounces become no-ops
|
|
144
|
+
const swarm = this._swarm
|
|
145
|
+
this._swarm = null
|
|
146
|
+
if (swarm) {
|
|
147
|
+
// force destroy still awaits a server.close round-trip; cap it
|
|
148
|
+
await Promise.race([
|
|
149
|
+
swarm.destroy({ force: true }).catch(safetyCatch),
|
|
150
|
+
new Promise((r) => setTimeout(r, 750))
|
|
151
|
+
])
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (this._blindPeering) {
|
|
155
|
+
try {
|
|
156
|
+
await this._blindPeering.close()
|
|
157
|
+
} catch (err) {
|
|
158
|
+
safetyCatch(err)
|
|
159
|
+
}
|
|
160
|
+
this._blindPeering = null
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (this._blind) {
|
|
164
|
+
try {
|
|
165
|
+
await (await this._blind).close()
|
|
166
|
+
} catch (err) {
|
|
167
|
+
safetyCatch(err)
|
|
168
|
+
}
|
|
169
|
+
this._blind = null
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (this.wakeup) {
|
|
173
|
+
try {
|
|
174
|
+
await this.wakeup.destroy()
|
|
175
|
+
} catch (err) {
|
|
176
|
+
safetyCatch(err)
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
82
181
|
/**
|
|
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.
|
|
182
|
+
* Feed an externally-established connection — a Bluetooth L2CAP channel, a serial link, an
|
|
183
|
+
* in-process pair, any duplex — into the network.
|
|
89
184
|
*
|
|
90
185
|
* @param {any} stream Duplex transport, or a ready NoiseSecretStream.
|
|
91
186
|
* @param {{ isInitiator?: boolean }} [opts] Which side initiates the noise handshake (raw duplexes only).
|
|
@@ -94,9 +189,7 @@ export class Network extends ReadyResource {
|
|
|
94
189
|
inject(stream, { isInitiator } = {}) {
|
|
95
190
|
if (this.closing || this.closed) throw CeroError.CLOSED('Network')
|
|
96
191
|
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).
|
|
192
|
+
// the swarm identity, so two radios between one pair dedupe to one peer
|
|
100
193
|
const keyPair = this.identity
|
|
101
194
|
? { publicKey: this.identity.publicKey, secretKey: this.identity.secretKey }
|
|
102
195
|
: (this.swarm?.keyPair ?? undefined)
|
|
@@ -105,10 +198,7 @@ export class Network extends ReadyResource {
|
|
|
105
198
|
? stream
|
|
106
199
|
: new NoiseSecretStream(isInitiator === true, stream, keyPair ? { keyPair } : undefined)
|
|
107
200
|
|
|
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.
|
|
201
|
+
// blind-pairing sends on the lowest-rtt channel; an injected duplex has no rtt, so give it one
|
|
112
202
|
if (conn.rawStream && conn.rawStream.rtt === undefined) conn.rawStream.rtt = 0
|
|
113
203
|
|
|
114
204
|
this._injected.add(conn)
|
|
@@ -123,9 +213,7 @@ export class Network extends ReadyResource {
|
|
|
123
213
|
}
|
|
124
214
|
|
|
125
215
|
/**
|
|
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.
|
|
216
|
+
* Lazily create the network-shared BlindPairing.
|
|
129
217
|
*
|
|
130
218
|
* @returns {Promise<any>}
|
|
131
219
|
*/
|
|
@@ -134,9 +222,7 @@ export class Network extends ReadyResource {
|
|
|
134
222
|
if (!this._blind) {
|
|
135
223
|
const blind = new BlindPairing(this.swarm)
|
|
136
224
|
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.
|
|
225
|
+
// blind-pairing only watches the swarm; injected connections must reach it too
|
|
140
226
|
this.on('connection', (conn, info) => {
|
|
141
227
|
if (info?.injected) blind._onconnection(conn)
|
|
142
228
|
})
|
|
@@ -148,11 +234,9 @@ export class Network extends ReadyResource {
|
|
|
148
234
|
}
|
|
149
235
|
|
|
150
236
|
/**
|
|
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.
|
|
237
|
+
* Re-attach pairing channels on injected connections. blind-pairing only auto-attaches
|
|
238
|
+
* refs that existed when a connection arrived — swarm peers meet again over topic joins,
|
|
239
|
+
* injected links (Bluetooth,.
|
|
156
240
|
*
|
|
157
241
|
* @returns {Promise<void>}
|
|
158
242
|
*/
|
|
@@ -163,9 +247,7 @@ export class Network extends ReadyResource {
|
|
|
163
247
|
}
|
|
164
248
|
|
|
165
249
|
/**
|
|
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.
|
|
250
|
+
* Declare this peer's self-reported info ({ name, ... }).
|
|
169
251
|
*
|
|
170
252
|
* @param {object | null} info
|
|
171
253
|
*/
|
|
@@ -186,82 +268,6 @@ export class Network extends ReadyResource {
|
|
|
186
268
|
return this._peerInfo.get(hex) ?? null
|
|
187
269
|
}
|
|
188
270
|
|
|
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
271
|
/**
|
|
266
272
|
* Wait for pending DHT announces and lookups to settle, bounded by timeout.
|
|
267
273
|
*
|
|
@@ -303,63 +309,6 @@ export class Network extends ReadyResource {
|
|
|
303
309
|
await this._blindPeering?.resume()
|
|
304
310
|
}
|
|
305
311
|
|
|
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
312
|
/**
|
|
364
313
|
* Announce/lookup on a 32-byte topic and return a Discovery handle.
|
|
365
314
|
*
|
|
@@ -396,8 +345,7 @@ export class Network extends ReadyResource {
|
|
|
396
345
|
if (!core) throw CeroError.REQUIRED('core')
|
|
397
346
|
this._replicateables.add(core)
|
|
398
347
|
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
|
|
348
|
+
// mirror the core so it stays available while its writers are offline
|
|
401
349
|
if (this._blindPeering) {
|
|
402
350
|
if (Autobee.isAutobee(core)) {
|
|
403
351
|
this._blindPeering.addAutobaseBackground(core)
|
|
@@ -434,12 +382,36 @@ export class Network extends ReadyResource {
|
|
|
434
382
|
}
|
|
435
383
|
for (const stream of this.connections) replicateInto(target, stream)
|
|
436
384
|
}
|
|
385
|
+
|
|
386
|
+
// silent until a side has info: bytes on a fresh connection trip hyperswarm's duplicate guard
|
|
387
|
+
_attachInfo(conn) {
|
|
388
|
+
const mux = Protomux.from(conn)
|
|
389
|
+
let message = null
|
|
390
|
+
const send = () => {
|
|
391
|
+
if (!message) {
|
|
392
|
+
const channel = mux.createChannel({ protocol: 'cero/info' })
|
|
393
|
+
if (channel === null) return
|
|
394
|
+
message = channel.addMessage({
|
|
395
|
+
encoding: c.json,
|
|
396
|
+
onmessage: (info) => {
|
|
397
|
+
if (!info || typeof info !== 'object') return
|
|
398
|
+
const hex = b4a.toString(conn.remotePublicKey, 'hex')
|
|
399
|
+
this._peerInfo.set(hex, info)
|
|
400
|
+
this.emit('peer-info', hex, info)
|
|
401
|
+
}
|
|
402
|
+
})
|
|
403
|
+
channel.open()
|
|
404
|
+
}
|
|
405
|
+
if (this.info) message.send(this.info)
|
|
406
|
+
}
|
|
407
|
+
mux.pair({ protocol: 'cero/info' }, send)
|
|
408
|
+
this._infoSenders.add(send)
|
|
409
|
+
conn.on('close', () => this._infoSenders.delete(send))
|
|
410
|
+
if (this.info) send()
|
|
411
|
+
}
|
|
437
412
|
}
|
|
438
413
|
|
|
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.
|
|
414
|
+
// corestore replication is store-wide: replicate each root once per connection
|
|
443
415
|
const replicatedRoots = new WeakMap()
|
|
444
416
|
|
|
445
417
|
function replicateInto(core, stream) {
|
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':
|
package/src/pairing/index.js
CHANGED
|
@@ -35,10 +35,8 @@ import { CeroError } from '../lib/errors.js'
|
|
|
35
35
|
*/
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Blind-pairing membership wrapper. Hosts invites, dispatches incoming
|
|
39
|
-
*
|
|
40
|
-
* side of the handshake. All wire payloads are signed and routed through
|
|
41
|
-
* the supplied network.
|
|
38
|
+
* Blind-pairing membership wrapper. Hosts invites, dispatches incoming candidates to the
|
|
39
|
+
* application for accept/deny, and provides the joiner side of the handshake.
|
|
42
40
|
*/
|
|
43
41
|
export class Pairing extends ReadyResource {
|
|
44
42
|
/** @param {PairingOpts} [opts] */
|
|
@@ -63,8 +61,7 @@ export class Pairing extends ReadyResource {
|
|
|
63
61
|
this.inviteEncoding = inviteEncoding
|
|
64
62
|
this.joinerEncoding = joinerEncoding
|
|
65
63
|
this._onerror = onerror
|
|
66
|
-
//
|
|
67
|
-
// (single-use settled, or expired) — lets the owner drop its persisted row.
|
|
64
|
+
// fired when this instance stops serving an invite, so the owner drops its row
|
|
68
65
|
this.onconsume = onconsume
|
|
69
66
|
|
|
70
67
|
this._blind = null
|
|
@@ -73,14 +70,21 @@ export class Pairing extends ReadyResource {
|
|
|
73
70
|
this._candidates = new Set() // active joiner candidates
|
|
74
71
|
}
|
|
75
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Whether the underlying blind-pairing layer is suspended.
|
|
75
|
+
*
|
|
76
|
+
* @returns {boolean}
|
|
77
|
+
*/
|
|
78
|
+
get suspended() {
|
|
79
|
+
return this._blind?.suspended === true
|
|
80
|
+
}
|
|
81
|
+
|
|
76
82
|
async _open() {
|
|
77
83
|
await this.network.ready()
|
|
78
84
|
// shared instance — one set of swarm/DHT listeners for every handle
|
|
79
85
|
this._blind = await this.network.blind()
|
|
80
86
|
|
|
81
|
-
// blind-pairing allows
|
|
82
|
-
// pairing must not register one, or concurrent joins under the same
|
|
83
|
-
// identity throw 'Active member already exist'
|
|
87
|
+
// blind-pairing allows one member listener per discovery key
|
|
84
88
|
if (this.host) {
|
|
85
89
|
this._member = this._blind.addMember({
|
|
86
90
|
// channel-scoped; the candidate hashes identically (no channel → identity)
|
|
@@ -104,22 +108,6 @@ export class Pairing extends ReadyResource {
|
|
|
104
108
|
this._invites.clear()
|
|
105
109
|
}
|
|
106
110
|
|
|
107
|
-
/**
|
|
108
|
-
* Resolve the resource discovery key an invite targets — without pairing.
|
|
109
|
-
* Lets callers detect "already joined" before any handshake. Returns `null`
|
|
110
|
-
* for an unparseable invite.
|
|
111
|
-
*
|
|
112
|
-
* @param {string} invite
|
|
113
|
-
* @returns {Uint8Array | null}
|
|
114
|
-
*/
|
|
115
|
-
static inviteTopic(invite) {
|
|
116
|
-
try {
|
|
117
|
-
return Invite.parse(invite).discoveryKey
|
|
118
|
-
} catch {
|
|
119
|
-
return null
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
111
|
/**
|
|
124
112
|
* Mint a new pairing invite. Returns its canonical wire form (z32 string).
|
|
125
113
|
*
|
|
@@ -128,13 +116,11 @@ export class Pairing extends ReadyResource {
|
|
|
128
116
|
*/
|
|
129
117
|
async createInvite({ role = '', expiresIn = 0, data = null, reuse = false } = {}) {
|
|
130
118
|
if (this.closing || this.closed) throw CeroError.CLOSED('Pairing')
|
|
131
|
-
// a join-only pairing
|
|
132
|
-
// never be served, so fail loudly instead of handing out a dead invite
|
|
119
|
+
// a join-only pairing serves no invites
|
|
133
120
|
if (!this.host) throw CeroError.INVALID('cannot create invites on a join-only Pairing')
|
|
134
121
|
if (!this.opened) await this.ready()
|
|
135
122
|
|
|
136
|
-
//
|
|
137
|
-
// Pairing instance (per-handle keys avoid identity-wide collisions).
|
|
123
|
+
// the rendezvous key matches the member topic
|
|
138
124
|
const blind = BlindPairing.createInvite(this.topic)
|
|
139
125
|
|
|
140
126
|
const invite = Invite.create({
|
|
@@ -154,8 +140,7 @@ export class Pairing extends ReadyResource {
|
|
|
154
140
|
publicKey: blind.publicKey,
|
|
155
141
|
invite,
|
|
156
142
|
reuse,
|
|
157
|
-
// minted here
|
|
158
|
-
// never drop it for being absent from the store
|
|
143
|
+
// minted here, maybe not persisted yet: syncRows must keep it
|
|
159
144
|
local: true
|
|
160
145
|
})
|
|
161
146
|
|
|
@@ -176,11 +161,7 @@ export class Pairing extends ReadyResource {
|
|
|
176
161
|
}
|
|
177
162
|
|
|
178
163
|
/**
|
|
179
|
-
* Reconcile the served-invite set with persisted rows (the room's `invites`
|
|
180
|
-
* collection). Adds unknown rows so ANY member replica can serve them across
|
|
181
|
-
* restarts; drops replicated records whose row disappeared (revoked or
|
|
182
|
-
* consumed elsewhere). Locally-minted records not yet visible as rows are
|
|
183
|
-
* kept.
|
|
164
|
+
* Reconcile the served-invite set with persisted rows (the room's `invites` collection).
|
|
184
165
|
*
|
|
185
166
|
* @param {Array<{ id: string, invite: Uint8Array, publicKey: Uint8Array, seed: Uint8Array, reuse?: boolean }>} rows
|
|
186
167
|
*/
|
|
@@ -232,16 +213,6 @@ export class Pairing extends ReadyResource {
|
|
|
232
213
|
return false
|
|
233
214
|
}
|
|
234
215
|
|
|
235
|
-
/**
|
|
236
|
-
* Cheap structural test for a candidate invite string.
|
|
237
|
-
*
|
|
238
|
-
* @param {unknown} str
|
|
239
|
-
* @returns {boolean}
|
|
240
|
-
*/
|
|
241
|
-
static isInvite(str) {
|
|
242
|
-
return Invite.isInvite(str)
|
|
243
|
-
}
|
|
244
|
-
|
|
245
216
|
/**
|
|
246
217
|
* Joiner side — start a pairing handshake against `inviteStr` and resolve
|
|
247
218
|
* with the host's response when the host confirms.
|
|
@@ -269,15 +240,6 @@ export class Pairing extends ReadyResource {
|
|
|
269
240
|
return candidate.start()
|
|
270
241
|
}
|
|
271
242
|
|
|
272
|
-
/**
|
|
273
|
-
* Whether the underlying blind-pairing layer is suspended.
|
|
274
|
-
*
|
|
275
|
-
* @returns {boolean}
|
|
276
|
-
*/
|
|
277
|
-
get suspended() {
|
|
278
|
-
return this._blind?.suspended === true
|
|
279
|
-
}
|
|
280
|
-
|
|
281
243
|
/**
|
|
282
244
|
* Pause blind-pairing — keeps state, drops sockets. Idempotent.
|
|
283
245
|
*
|
|
@@ -343,6 +305,30 @@ export class Pairing extends ReadyResource {
|
|
|
343
305
|
|
|
344
306
|
this.emit('candidate', request)
|
|
345
307
|
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Resolve the resource discovery key an invite targets — without pairing.
|
|
311
|
+
*
|
|
312
|
+
* @param {string} invite
|
|
313
|
+
* @returns {Uint8Array | null}
|
|
314
|
+
*/
|
|
315
|
+
static inviteTopic(invite) {
|
|
316
|
+
try {
|
|
317
|
+
return Invite.parse(invite).discoveryKey
|
|
318
|
+
} catch {
|
|
319
|
+
return null
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Cheap structural test for a candidate invite string.
|
|
325
|
+
*
|
|
326
|
+
* @param {unknown} str
|
|
327
|
+
* @returns {boolean}
|
|
328
|
+
*/
|
|
329
|
+
static isInvite(str) {
|
|
330
|
+
return Invite.isInvite(str)
|
|
331
|
+
}
|
|
346
332
|
}
|
|
347
333
|
|
|
348
334
|
function encodeUserData(data, encoding) {
|
package/src/pairing/invite.js
CHANGED
|
@@ -35,9 +35,9 @@ const SignBody = getEncoding('@cero/invite-body')
|
|
|
35
35
|
*/
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Signed, expirable pairing invite. Wraps a blind-pairing invite with a
|
|
39
|
-
*
|
|
40
|
-
*
|
|
38
|
+
* Signed, expirable pairing invite. Wraps a blind-pairing invite with a role, optional
|
|
39
|
+
* payload and an Ed25519 signature so the host can be authenticated by the joiner before
|
|
40
|
+
* any handshake happens.
|
|
41
41
|
*
|
|
42
42
|
* @property {any} [_rawData] Decoded payload kept alongside the encoded `data` for convenience.
|
|
43
43
|
* @property {Uint8Array} _discoveryKey Cached discovery key of the wrapped blind invite.
|
|
@@ -54,7 +54,6 @@ export class Invite {
|
|
|
54
54
|
this.sig = fields.sig
|
|
55
55
|
this._str = fields._str || null
|
|
56
56
|
|
|
57
|
-
// cached discovery key of the wrapped blind invite
|
|
58
57
|
this._discoveryKey = null
|
|
59
58
|
}
|
|
60
59
|
|
package/src/pairing/request.js
CHANGED
|
@@ -8,10 +8,7 @@ import { CeroError } from '../lib/errors.js'
|
|
|
8
8
|
export const STATUS_ACCEPTED = 0
|
|
9
9
|
export const STATUS_DENIED = 1
|
|
10
10
|
|
|
11
|
-
//
|
|
12
|
-
// We piggyback on the confirm path for *both* success and deny — blind-pairing's
|
|
13
|
-
// native deny() has no payload field, and its discoveryKey check pins the wire
|
|
14
|
-
// `key` to the invite, so the real resource key flows through this envelope.
|
|
11
|
+
// success and deny both ride the confirm response
|
|
15
12
|
export const Response = getEncoding('@cero/confirm')
|
|
16
13
|
|
|
17
14
|
/**
|
|
@@ -31,9 +28,7 @@ export const Response = getEncoding('@cero/confirm')
|
|
|
31
28
|
|
|
32
29
|
/**
|
|
33
30
|
* Internal — a pairing request from an incoming candidate, awaiting the host's
|
|
34
|
-
* accept/deny.
|
|
35
|
-
* the host can decide via `confirm` / `deny`. (The candidate is the joiner; this
|
|
36
|
-
* is the host's handle to answer it.)
|
|
31
|
+
* accept/deny.
|
|
37
32
|
*
|
|
38
33
|
* @property {boolean} _settled Whether confirm/deny has already run.
|
|
39
34
|
*/
|
|
@@ -86,8 +81,7 @@ export class Request {
|
|
|
86
81
|
})
|
|
87
82
|
}
|
|
88
83
|
|
|
89
|
-
//
|
|
90
|
-
// (callers guard idempotency before this runs)
|
|
84
|
+
// signed with the per-invite seed
|
|
91
85
|
_respond(envelope) {
|
|
92
86
|
this._settled = true
|
|
93
87
|
this._onsettle()
|
package/src/rpc/index.js
CHANGED
|
@@ -9,8 +9,7 @@ export { RPCClient } from './client.js'
|
|
|
9
9
|
|
|
10
10
|
const EMPTY = b4a.alloc(0)
|
|
11
11
|
|
|
12
|
-
//
|
|
13
|
-
// spec does not provide its own rows/query/create types in its schema.
|
|
12
|
+
// fallback envelope encodings when the spec has none
|
|
14
13
|
const DEFAULT_ROWS = getEncoding('@cero/rows')
|
|
15
14
|
const DEFAULT_CHANGES = getEncoding('@cero/changes')
|
|
16
15
|
const DEFAULT_QUERY = getEncoding('@cero/query')
|
|
@@ -39,10 +38,7 @@ const DEFAULT_CREATE = getEncoding('@cero/create')
|
|
|
39
38
|
*/
|
|
40
39
|
|
|
41
40
|
/**
|
|
42
|
-
* Attach a `codec` namespace to a hyperschema-shaped `spec`.
|
|
43
|
-
* envelope types using the spec's namespace (`@<ns>/rows` etc.) and falls
|
|
44
|
-
* back to the bundled `@cero/*` envelopes when the spec has not declared
|
|
45
|
-
* its own. Returns the spec for chaining.
|
|
41
|
+
* Attach a `codec` namespace to a hyperschema-shaped `spec`.
|
|
46
42
|
*
|
|
47
43
|
* @param {Spec} spec
|
|
48
44
|
* @returns {Spec}
|
|
@@ -107,11 +103,7 @@ export function bindCodec(spec) {
|
|
|
107
103
|
if (!buf || buf.length === 0) return undefined
|
|
108
104
|
const env = decodeEnvelope(schema, QUERY, DEFAULT_QUERY, buf)
|
|
109
105
|
const out = {}
|
|
110
|
-
//
|
|
111
|
-
// recover fields the encoder actually wrote (truthy). KNOWN LIMITATION: a
|
|
112
|
-
// `limit: 0` query (degenerate — "return nothing") is indistinguishable from
|
|
113
|
-
// "no limit" over the wire and is treated as unset. Falsy cursors (gt: '')
|
|
114
|
-
// are harmless — an empty-string bound matches everything anyway.
|
|
106
|
+
// hyperschema fills optional fields with 0/false, so only truthy fields are recovered; limit: 0 is lost
|
|
115
107
|
if (env.gt) out.gt = env.gt
|
|
116
108
|
if (env.gte) out.gte = env.gte
|
|
117
109
|
if (env.lt) out.lt = env.lt
|
|
@@ -145,9 +137,7 @@ export function bindCodec(spec) {
|
|
|
145
137
|
return spec
|
|
146
138
|
}
|
|
147
139
|
|
|
148
|
-
//
|
|
149
|
-
// encoding bundled with this primitive. This lets callers point a foreign spec
|
|
150
|
-
// at the codec without re-declaring the rows/query/create envelopes.
|
|
140
|
+
// the spec's own envelope type first, the built-in @cero encoding as fallback
|
|
151
141
|
function encodeEnvelope(schema, fqn, fallback, value) {
|
|
152
142
|
if (fqn && schemaHas(schema, fqn)) return schema.encode(fqn, value)
|
|
153
143
|
return c.encode(fallback, value)
|