@cero-base/cero 2.3.4 → 2.4.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 +1 -1
- package/package.json +3 -2
- package/src/build/index.js +32 -21
- package/src/build/internal.js +14 -16
- package/src/build/schemas.js +13 -50
- package/src/extensions/profile-sync.js +8 -2
- package/src/handle/index.js +54 -140
- package/src/handle/join.js +7 -12
- package/src/index.js +43 -76
- package/src/lib/bluetooth.js +96 -84
- package/src/lib/operators.js +43 -36
- package/src/lib/refs.js +2 -2
- package/src/rpc/client.js +57 -50
- package/src/rpc/server.js +77 -88
- package/types/build/schemas.d.ts +8 -43
- package/types/extensions/index.d.ts +5 -3
- package/types/handle/index.d.ts +7 -63
- package/types/index.d.ts +17 -40
- package/types/lib/bluetooth.d.ts +29 -24
- package/types/lib/operators.d.ts +21 -17
- package/types/lib/refs.d.ts +2 -2
- package/types/rpc/client.d.ts +7 -3
- package/types/rpc/server.d.ts +7 -12
package/src/handle/index.js
CHANGED
|
@@ -40,7 +40,7 @@ export { Ref } from '../lib/refs.js'
|
|
|
40
40
|
* @property {Network} [network] Shared swarm. Inherited from `parent` if omitted.
|
|
41
41
|
* @property {import('corestore')} [store] Pre-existing Corestore. Falls back to `parent.store.store`.
|
|
42
42
|
* @property {Spec} [spec] Built cero spec.
|
|
43
|
-
* @property {Local} [local]
|
|
43
|
+
* @property {Local} [local] Root only, and required there: the device store for handle keypairs, joins and mail.
|
|
44
44
|
* @property {import('hypercore-storage')} [storage] Owned HypercoreStorage to close on shutdown.
|
|
45
45
|
* @property {{ destroy(): Promise<void> }} [discovery] Owned identity discovery to destroy on shutdown.
|
|
46
46
|
* @property {string} [dir] Data directory (root handles only).
|
|
@@ -50,7 +50,6 @@ export { Ref } from '../lib/refs.js'
|
|
|
50
50
|
* @property {Array<{ epoch: number, entropy: Uint8Array }>} [epochs] Rotation epochs delivered at join.
|
|
51
51
|
* @property {string} [namespace] Corestore namespace.
|
|
52
52
|
* @property {KeyPair} [keyPair] Writer keypair.
|
|
53
|
-
* @property {boolean} [pair] When `false`, skips creating a `Pairing` session.
|
|
54
53
|
* @property {import('../lib/bluetooth.js').Bluetooth | null} [bluetooth] Root only: the radio, up before the root opens.
|
|
55
54
|
* @property {{ name?: string | null, isMobile?: boolean, recovering?: boolean, timeout?: number } | null} [bootstrap] Root only: provision this device as it opens, its genesis or its recovery.
|
|
56
55
|
*
|
|
@@ -60,16 +59,6 @@ export { Ref } from '../lib/refs.js'
|
|
|
60
59
|
* @typedef {object} JoinChildOpts
|
|
61
60
|
* @property {number} [timeout]
|
|
62
61
|
*
|
|
63
|
-
* @typedef {object} StaticJoinOpts
|
|
64
|
-
* @property {Handle} [parent]
|
|
65
|
-
* @property {Network} [network]
|
|
66
|
-
* @property {Identity} [identity]
|
|
67
|
-
* @property {import('corestore')} [store]
|
|
68
|
-
* @property {Spec} [spec]
|
|
69
|
-
* @property {string} [namespace]
|
|
70
|
-
* @property {KeyPair} [writer] Writer keypair in the joined handle; a fresh one by default.
|
|
71
|
-
* @property {number} [timeout]
|
|
72
|
-
*
|
|
73
62
|
* @typedef {object} HandleExtra
|
|
74
63
|
* @property {string | null} [name] Display name; set on child handles by the owner flow.
|
|
75
64
|
* @property {import('../lib/refs.js').Ref} [profile] `profile` ref, attached dynamically when the schema declares one.
|
|
@@ -77,7 +66,7 @@ export { Ref } from '../lib/refs.js'
|
|
|
77
66
|
*
|
|
78
67
|
* @typedef {Handle & HandleExtra} Child A child handle plus its dynamically-attached refs.
|
|
79
68
|
*
|
|
80
|
-
* @typedef {Handle & Record<string, import('../lib/refs.js').Ref>} Context
|
|
69
|
+
* @typedef {Handle & Record<string, import('../lib/refs.js').Ref>} Context The root or an opened handle, with its refs (`me.profile`, `room.messages`).
|
|
81
70
|
*/
|
|
82
71
|
|
|
83
72
|
/**
|
|
@@ -98,6 +87,7 @@ export class Handle extends ReadyResource {
|
|
|
98
87
|
if (!network) throw CeroError.REQUIRED('network')
|
|
99
88
|
if (!store) throw CeroError.REQUIRED('store')
|
|
100
89
|
if (!spec) throw CeroError.REQUIRED('spec')
|
|
90
|
+
if (!parent && !opts.local) throw CeroError.REQUIRED('local')
|
|
101
91
|
|
|
102
92
|
/** @type {Identity} */
|
|
103
93
|
this.identity = identity
|
|
@@ -109,7 +99,12 @@ export class Handle extends ReadyResource {
|
|
|
109
99
|
this.local = opts.local || null
|
|
110
100
|
// one per device: the root's, its boxes in the local store
|
|
111
101
|
/** @type {Mailbox} */
|
|
112
|
-
this.mailbox = parent
|
|
102
|
+
this.mailbox = parent
|
|
103
|
+
? parent.root.mailbox
|
|
104
|
+
: new Mailbox(network, {
|
|
105
|
+
inbox: box(this.local.store, 'inbox'),
|
|
106
|
+
outbox: box(this.local.store, 'outbox')
|
|
107
|
+
})
|
|
113
108
|
/** @private */
|
|
114
109
|
this._storage = opts.storage || null
|
|
115
110
|
/** @private */
|
|
@@ -155,7 +150,8 @@ export class Handle extends ReadyResource {
|
|
|
155
150
|
network,
|
|
156
151
|
spec,
|
|
157
152
|
key: opts.key,
|
|
158
|
-
|
|
153
|
+
// the root stays seed-derived: a phrase recovers it, nothing delivers it
|
|
154
|
+
encryptionKey: parent ? opts.encryptionKey : identity.encryptionKey,
|
|
159
155
|
epochs: opts.epochs,
|
|
160
156
|
namespace: opts.namespace,
|
|
161
157
|
keyPair: opts.keyPair,
|
|
@@ -165,8 +161,6 @@ export class Handle extends ReadyResource {
|
|
|
165
161
|
/** @private */
|
|
166
162
|
this._pair = null
|
|
167
163
|
/** @private */
|
|
168
|
-
this._wantsPair = opts.pair !== false
|
|
169
|
-
/** @private */
|
|
170
164
|
this._boot = opts.bootstrap || null
|
|
171
165
|
/** @private */
|
|
172
166
|
this._bluetooth = opts.bluetooth || null
|
|
@@ -177,7 +171,7 @@ export class Handle extends ReadyResource {
|
|
|
177
171
|
status: { get: () => this._status(), watch: (fn) => this._onStatus(fn) },
|
|
178
172
|
...(!this.parent && {
|
|
179
173
|
joins: { get: (q) => this._joins(q), watch: (fn) => this._onJoins(fn) },
|
|
180
|
-
nearby: { get: () => this._peers(), watch: (fn) => this.
|
|
174
|
+
nearby: { get: () => this._peers(), watch: (fn) => this._onRadio(fn) }
|
|
181
175
|
})
|
|
182
176
|
}
|
|
183
177
|
}
|
|
@@ -244,7 +238,7 @@ export class Handle extends ReadyResource {
|
|
|
244
238
|
}
|
|
245
239
|
|
|
246
240
|
/**
|
|
247
|
-
* Canonical id: the identity id on the root, the database key on a
|
|
241
|
+
* Canonical id: the identity id on the root, the database key on a handle.
|
|
248
242
|
*
|
|
249
243
|
* @returns {string | null}
|
|
250
244
|
*/
|
|
@@ -254,7 +248,7 @@ export class Handle extends ReadyResource {
|
|
|
254
248
|
}
|
|
255
249
|
|
|
256
250
|
/**
|
|
257
|
-
* This device's id and name. `null` on a
|
|
251
|
+
* This device's id and name. `null` on a handle.
|
|
258
252
|
*
|
|
259
253
|
* @returns {{ id: string, name: string | null } | null}
|
|
260
254
|
*/
|
|
@@ -269,7 +263,7 @@ export class Handle extends ReadyResource {
|
|
|
269
263
|
await this.store.ready()
|
|
270
264
|
// a fresh device takes writes only once provisioned, and an operator waits for the open
|
|
271
265
|
if (this._boot) await this.store.bootstrap(this._boot)
|
|
272
|
-
if (this.
|
|
266
|
+
if (this.parent) {
|
|
273
267
|
this._pair = new Pairing({ mailbox: this.mailbox, db: this.store })
|
|
274
268
|
await this._pair.ready()
|
|
275
269
|
}
|
|
@@ -318,7 +312,7 @@ export class Handle extends ReadyResource {
|
|
|
318
312
|
steps.push(
|
|
319
313
|
() => this.mailbox.close(),
|
|
320
314
|
() => this._bluetooth?.close(),
|
|
321
|
-
() => this.local
|
|
315
|
+
() => this.local.close(),
|
|
322
316
|
async () => {
|
|
323
317
|
await this._fileServer?.close()
|
|
324
318
|
this._fileServer = null
|
|
@@ -357,16 +351,6 @@ export class Handle extends ReadyResource {
|
|
|
357
351
|
return this.root.fileServer.getLink(id)
|
|
358
352
|
}
|
|
359
353
|
|
|
360
|
-
/**
|
|
361
|
-
* Claim writer capability on an existing database (paired-device flow).
|
|
362
|
-
* Forwards to `Database.claim`.
|
|
363
|
-
*
|
|
364
|
-
* @returns {Promise<void>}
|
|
365
|
-
*/
|
|
366
|
-
claim() {
|
|
367
|
-
return this.store.claim()
|
|
368
|
-
}
|
|
369
|
-
|
|
370
354
|
// the verbs in lib/operators.js land here
|
|
371
355
|
|
|
372
356
|
/** @private */
|
|
@@ -381,8 +365,8 @@ export class Handle extends ReadyResource {
|
|
|
381
365
|
|
|
382
366
|
/** @private */
|
|
383
367
|
async _answer(id, { accept, role, reason }) {
|
|
384
|
-
const
|
|
385
|
-
return accept ?
|
|
368
|
+
const pairing = this._pairing()
|
|
369
|
+
return accept ? pairing.accept(id, { role }) : pairing.deny(id, reason)
|
|
386
370
|
}
|
|
387
371
|
|
|
388
372
|
/** @private */
|
|
@@ -408,7 +392,7 @@ export class Handle extends ReadyResource {
|
|
|
408
392
|
|
|
409
393
|
/** @private */
|
|
410
394
|
_pairing() {
|
|
411
|
-
if (!this._pair) throw CeroError.INVALID('the root has no invites, open a
|
|
395
|
+
if (!this._pair) throw CeroError.INVALID('the root has no invites, open a handle first')
|
|
412
396
|
return this._pair
|
|
413
397
|
}
|
|
414
398
|
|
|
@@ -436,12 +420,14 @@ export class Handle extends ReadyResource {
|
|
|
436
420
|
/** @private */
|
|
437
421
|
_lifecycle() {
|
|
438
422
|
if (!this._sus) {
|
|
439
|
-
throw CeroError.INVALID(
|
|
423
|
+
throw CeroError.INVALID(
|
|
424
|
+
"suspend and resume are the app's, on me: a handle uses cero.activate"
|
|
425
|
+
)
|
|
440
426
|
}
|
|
441
427
|
return this._sus
|
|
442
428
|
}
|
|
443
429
|
|
|
444
|
-
// true ranks a
|
|
430
|
+
// true ranks a handle as just used, false takes it off the swarm until something lands in it
|
|
445
431
|
/** @private */
|
|
446
432
|
_active(on) {
|
|
447
433
|
this.store.setActive(on)
|
|
@@ -480,17 +466,9 @@ export class Handle extends ReadyResource {
|
|
|
480
466
|
}
|
|
481
467
|
}
|
|
482
468
|
|
|
483
|
-
// the radio is the device's: the mesh (true), off (false), or one invite's rendezvous (a code)
|
|
484
|
-
// until the next call or the invite expires
|
|
485
469
|
/** @private */
|
|
486
|
-
async _nearby(
|
|
487
|
-
|
|
488
|
-
if (!bt) throw CeroError.INVALID('nearby needs cero() to open with the bluetooth option')
|
|
489
|
-
this.root._rendezvous?.()
|
|
490
|
-
this.root._rendezvous = null
|
|
491
|
-
if (mode === false) return bt.stop()
|
|
492
|
-
await bt.start()
|
|
493
|
-
if (typeof mode === 'string') this.root._rendezvous = bt.announce(mode)
|
|
470
|
+
async _nearby(on, opts) {
|
|
471
|
+
return this.root._bluetooth.set(on, opts)
|
|
494
472
|
}
|
|
495
473
|
|
|
496
474
|
// what a Bluetooth peer hears: the profile's name and whether this device is a phone
|
|
@@ -513,7 +491,7 @@ export class Handle extends ReadyResource {
|
|
|
513
491
|
return { data, total: data.length, size: data.length }
|
|
514
492
|
}
|
|
515
493
|
|
|
516
|
-
// a peer's name as its member row shows it in a
|
|
494
|
+
// a peer's name as its member row shows it in a handle open here; a stranger has none
|
|
517
495
|
/** @private */
|
|
518
496
|
async _nameOf(id) {
|
|
519
497
|
for (const room of this.children) {
|
|
@@ -523,17 +501,6 @@ export class Handle extends ReadyResource {
|
|
|
523
501
|
return null
|
|
524
502
|
}
|
|
525
503
|
|
|
526
|
-
// a link opening, closing, or a peer telling a new name
|
|
527
|
-
/** @private */
|
|
528
|
-
_onPeers(fn) {
|
|
529
|
-
const off = this._onRadio(fn)
|
|
530
|
-
this.network.on('peer-info', fn)
|
|
531
|
-
return () => {
|
|
532
|
-
off()
|
|
533
|
-
this.network.off('peer-info', fn)
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
|
|
537
504
|
/** @private */
|
|
538
505
|
_onRadio(fn) {
|
|
539
506
|
const bt = this.root._bluetooth
|
|
@@ -545,7 +512,7 @@ export class Handle extends ReadyResource {
|
|
|
545
512
|
// the joins this device waits on, without the keys the local store keeps for them
|
|
546
513
|
/** @private */
|
|
547
514
|
async _joins(query = {}) {
|
|
548
|
-
const rows =
|
|
515
|
+
const rows = (await this.local.store.get('joins')).data
|
|
549
516
|
const data = filter(
|
|
550
517
|
rows.map(({ id, type, invite }) => ({ id, type, invite })),
|
|
551
518
|
query
|
|
@@ -555,7 +522,6 @@ export class Handle extends ReadyResource {
|
|
|
555
522
|
|
|
556
523
|
/** @private */
|
|
557
524
|
_onJoins(fn) {
|
|
558
|
-
if (!this.local) return () => {}
|
|
559
525
|
this.local.store.db.watch(fn)
|
|
560
526
|
return () => this.local.store.db.unwatch(fn)
|
|
561
527
|
}
|
|
@@ -660,7 +626,7 @@ export class Handle extends ReadyResource {
|
|
|
660
626
|
}
|
|
661
627
|
}
|
|
662
628
|
|
|
663
|
-
// base-era cores use the OWNING handle's key,
|
|
629
|
+
// base-era cores use the OWNING handle's key, handles have their own
|
|
664
630
|
/** @private */
|
|
665
631
|
_blobCoreKey(stamp) {
|
|
666
632
|
if (!stamp) return this.store.encryptionKey
|
|
@@ -684,7 +650,7 @@ export class Handle extends ReadyResource {
|
|
|
684
650
|
spec: pickHandle(this.spec, type),
|
|
685
651
|
namespace: `${NS}/handle/${type}/${writer.id}`,
|
|
686
652
|
keyPair: writer,
|
|
687
|
-
// inheriting identity.encryptionKey would let any member decrypt every
|
|
653
|
+
// inheriting identity.encryptionKey would let any member decrypt every handle
|
|
688
654
|
encryptionKey: Identity.randomBytes(32)
|
|
689
655
|
})
|
|
690
656
|
)
|
|
@@ -709,7 +675,7 @@ export class Handle extends ReadyResource {
|
|
|
709
675
|
|
|
710
676
|
const ts = Date.now()
|
|
711
677
|
const writerKey = child.store.writerKey
|
|
712
|
-
// one batch, so a
|
|
678
|
+
// one batch, so a handle's first two ops land together
|
|
713
679
|
await child.store.tx(async (tx) => {
|
|
714
680
|
await tx.call('add-writer', {
|
|
715
681
|
master: this.identity.publicKey,
|
|
@@ -880,7 +846,6 @@ export class Handle extends ReadyResource {
|
|
|
880
846
|
// outbox kept
|
|
881
847
|
/** @private */
|
|
882
848
|
async _carryOn() {
|
|
883
|
-
if (!this.local) return
|
|
884
849
|
for (const { id, invite, type } of (await this.local.store.get('joins')).data) {
|
|
885
850
|
if (await this._joined(type, Invite.parse(invite).discoveryKey)) {
|
|
886
851
|
await this.local.store.del('joins', id)
|
|
@@ -900,21 +865,25 @@ export class Handle extends ReadyResource {
|
|
|
900
865
|
* @private
|
|
901
866
|
*/
|
|
902
867
|
async _load(type, id) {
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
868
|
+
const loading = this._loading.get(id)
|
|
869
|
+
// no await unless one is loading: a concurrent open must see this one's entry, not race it
|
|
870
|
+
const open = [...this.children].find((c) => c.id === id) || (loading && (await loading))
|
|
871
|
+
if (open && open.type !== type) {
|
|
872
|
+
throw CeroError.INVALID(`handle ${id} is type ${open.type}, not ${type}`)
|
|
873
|
+
}
|
|
874
|
+
if (open) return open
|
|
875
|
+
const reopening = this._reopen(type, id)
|
|
876
|
+
this._loading.set(id, reopening)
|
|
908
877
|
try {
|
|
909
|
-
return await
|
|
878
|
+
return await reopening
|
|
910
879
|
} finally {
|
|
911
880
|
this._loading.delete(id)
|
|
912
881
|
}
|
|
913
882
|
}
|
|
914
883
|
|
|
915
884
|
/**
|
|
916
|
-
* Reconstruct a child handle by id. Reuses the stored writer keypair if
|
|
917
|
-
*
|
|
885
|
+
* Reconstruct a child handle by id. Reuses the stored writer keypair if available; otherwise
|
|
886
|
+
* stores a fresh one, and a device that may write claims its seat with it.
|
|
918
887
|
*
|
|
919
888
|
* @param {string} type
|
|
920
889
|
* @param {string} id
|
|
@@ -928,9 +897,10 @@ export class Handle extends ReadyResource {
|
|
|
928
897
|
throw CeroError.INVALID(`handle ${id} is type ${data.type}, not ${type}`)
|
|
929
898
|
}
|
|
930
899
|
let writer = await this._loadKeyPair(id)
|
|
931
|
-
|
|
932
|
-
if (firstTime) {
|
|
900
|
+
if (!writer) {
|
|
933
901
|
writer = Identity.randomKeyPair()
|
|
902
|
+
// stored before its claim is on the log: a retry claims with it, never with another
|
|
903
|
+
await this._saveKeyPair(id, writer)
|
|
934
904
|
}
|
|
935
905
|
const child = this._room({
|
|
936
906
|
spec: pickHandle(this.spec, type),
|
|
@@ -941,11 +911,9 @@ export class Handle extends ReadyResource {
|
|
|
941
911
|
})
|
|
942
912
|
try {
|
|
943
913
|
await child.ready()
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
if (firstTime) {
|
|
948
|
-
await this._saveKeyPair(id, writer)
|
|
914
|
+
// a writer that never wrote has not claimed yet
|
|
915
|
+
if (!child.store.writable && child.store.length === 0) {
|
|
916
|
+
await child.store.claim({ timeout: TIMEOUT })
|
|
949
917
|
}
|
|
950
918
|
this._adopt(child, {})
|
|
951
919
|
} catch (err) {
|
|
@@ -955,7 +923,7 @@ export class Handle extends ReadyResource {
|
|
|
955
923
|
return child
|
|
956
924
|
}
|
|
957
925
|
|
|
958
|
-
// a
|
|
926
|
+
// a handle has its type's hooks before it opens, so no op it applies runs without them
|
|
959
927
|
/** @private */
|
|
960
928
|
_room(opts) {
|
|
961
929
|
const child = new Handle({ parent: this, ...opts })
|
|
@@ -991,7 +959,7 @@ export class Handle extends ReadyResource {
|
|
|
991
959
|
this.emit('handle', child, info)
|
|
992
960
|
}
|
|
993
961
|
|
|
994
|
-
// before(me.room.notes, fn): on every
|
|
962
|
+
// before(me.room.notes, fn): on every handle open now and every one opened later
|
|
995
963
|
/** @private */
|
|
996
964
|
_hookType(ref, attach, opts) {
|
|
997
965
|
const offs = new Map()
|
|
@@ -1013,11 +981,11 @@ export class Handle extends ReadyResource {
|
|
|
1013
981
|
return off
|
|
1014
982
|
}
|
|
1015
983
|
|
|
1016
|
-
// a
|
|
1017
|
-
// not only while the app has the
|
|
984
|
+
// a handle with invites is reopened at boot, so its joins are answered whenever we are online,
|
|
985
|
+
// not only while the app has the handle open
|
|
1018
986
|
/** @private */
|
|
1019
987
|
_serve(child) {
|
|
1020
|
-
if (!
|
|
988
|
+
if (!child._pair) return
|
|
1021
989
|
const save = async () => {
|
|
1022
990
|
const { serving } = child._pair
|
|
1023
991
|
if (serving === child._serving) return
|
|
@@ -1031,7 +999,6 @@ export class Handle extends ReadyResource {
|
|
|
1031
999
|
|
|
1032
1000
|
/** @private */
|
|
1033
1001
|
async _reserve() {
|
|
1034
|
-
if (!this.local) return
|
|
1035
1002
|
for (const { id, type } of (await this.local.store.get('serving')).data) {
|
|
1036
1003
|
this._load(type, id).catch((err) => {
|
|
1037
1004
|
if (err.code === 'UNKNOWN') return this.local.store.del('serving', id).catch(safetyCatch)
|
|
@@ -1047,7 +1014,7 @@ export class Handle extends ReadyResource {
|
|
|
1047
1014
|
* @private
|
|
1048
1015
|
*/
|
|
1049
1016
|
async _saveKeyPair(id, keyPair) {
|
|
1050
|
-
if (!
|
|
1017
|
+
if (!keyPair) return
|
|
1051
1018
|
await this.local.store.put('handle-keypairs', {
|
|
1052
1019
|
id,
|
|
1053
1020
|
publicKey: keyPair.publicKey,
|
|
@@ -1061,7 +1028,6 @@ export class Handle extends ReadyResource {
|
|
|
1061
1028
|
* @private
|
|
1062
1029
|
*/
|
|
1063
1030
|
async _loadKeyPair(id) {
|
|
1064
|
-
if (!this.local) return null
|
|
1065
1031
|
const { data } = await this.local.store.get('handle-keypairs', id)
|
|
1066
1032
|
if (!data) return null
|
|
1067
1033
|
return {
|
|
@@ -1069,52 +1035,6 @@ export class Handle extends ReadyResource {
|
|
|
1069
1035
|
secretKey: data.secretKey
|
|
1070
1036
|
}
|
|
1071
1037
|
}
|
|
1072
|
-
|
|
1073
|
-
/**
|
|
1074
|
-
* Pair into an existing handle via an invite, returning a brand-new `Handle` opened with the
|
|
1075
|
-
* delivered keys. A one-shot join: the root's `_join` is the one that survives restarts.
|
|
1076
|
-
*
|
|
1077
|
-
* @param {string} invite
|
|
1078
|
-
* @param {StaticJoinOpts} [opts]
|
|
1079
|
-
* @returns {Promise<Handle>}
|
|
1080
|
-
*/
|
|
1081
|
-
static async join(
|
|
1082
|
-
invite,
|
|
1083
|
-
{ parent, network, identity, store, spec, namespace, writer, timeout = TIMEOUT } = {}
|
|
1084
|
-
) {
|
|
1085
|
-
const net = network || parent?.network
|
|
1086
|
-
const id = identity || parent?.identity
|
|
1087
|
-
if (!net) throw CeroError.REQUIRED('network')
|
|
1088
|
-
if (!id) throw CeroError.REQUIRED('identity')
|
|
1089
|
-
if (!store && !parent) throw CeroError.REQUIRED('store')
|
|
1090
|
-
if (!spec) throw CeroError.REQUIRED('spec')
|
|
1091
|
-
|
|
1092
|
-
// standalone, a mailbox of its own for this one join
|
|
1093
|
-
const mailbox = parent ? null : new Mailbox(net)
|
|
1094
|
-
try {
|
|
1095
|
-
const reply = await Pairing.join(mailbox || parent.mailbox, invite, {
|
|
1096
|
-
identity: id,
|
|
1097
|
-
spec,
|
|
1098
|
-
writer,
|
|
1099
|
-
timeout
|
|
1100
|
-
})
|
|
1101
|
-
const opts = { parent, store, identity: id, network: net, spec, namespace }
|
|
1102
|
-
return Handle.fromReply(reply, opts)
|
|
1103
|
-
} finally {
|
|
1104
|
-
await mailbox?.close()
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
|
|
1108
|
-
/**
|
|
1109
|
-
* A handle opened with the keys a pairing reply delivered.
|
|
1110
|
-
*
|
|
1111
|
-
* @param {import('@cero-base/core/pairing').JoinResult} reply
|
|
1112
|
-
* @param {Omit<HandleOpts, 'key' | 'encryptionKey' | 'epochs' | 'keyPair'>} opts
|
|
1113
|
-
* @returns {Handle}
|
|
1114
|
-
*/
|
|
1115
|
-
static fromReply(reply, opts) {
|
|
1116
|
-
return new Handle({ ...opts, ...delivered(reply) })
|
|
1117
|
-
}
|
|
1118
1038
|
}
|
|
1119
1039
|
|
|
1120
1040
|
// the keys a pairing reply delivered, as a handle opens with them
|
|
@@ -1142,12 +1062,6 @@ function admitted(db, id) {
|
|
|
1142
1062
|
})
|
|
1143
1063
|
}
|
|
1144
1064
|
|
|
1145
|
-
// a device without a local store keeps its mail in memory
|
|
1146
|
-
function boxes(local) {
|
|
1147
|
-
if (!local) return {}
|
|
1148
|
-
return { inbox: box(local.store, 'inbox'), outbox: box(local.store, 'outbox') }
|
|
1149
|
-
}
|
|
1150
|
-
|
|
1151
1065
|
function pickHandle(spec, type) {
|
|
1152
1066
|
const h = spec.handles?.[type]
|
|
1153
1067
|
if (!h) throw CeroError.UNKNOWN('handle type', type)
|
package/src/handle/join.js
CHANGED
|
@@ -55,15 +55,13 @@ export class Join {
|
|
|
55
55
|
this._running?.abort()
|
|
56
56
|
const running = new AbortController()
|
|
57
57
|
this._running = running
|
|
58
|
-
const nearby = this.root._bluetooth?.announce(invite)
|
|
59
58
|
try {
|
|
60
59
|
const row = await this._save({ invite })
|
|
61
60
|
const writer = { publicKey: row.publicKey, secretKey: row.secretKey }
|
|
62
61
|
const { mailbox, identity } = this.root
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
: await this._keep(await Pairing.join(mailbox, invite, opts))
|
|
62
|
+
const keep = (answer) => (answer instanceof Error ? this._forget() : this._keep(answer))
|
|
63
|
+
const opts = { identity, spec: this.spec, writer, keep, timeout: 0, signal: running.signal }
|
|
64
|
+
const reply = row.key ? unpack(row) : await Pairing.join(mailbox, invite, opts)
|
|
67
65
|
this._end(this._resolve, await this.root._enter(this.type, reply))
|
|
68
66
|
await this._forget()
|
|
69
67
|
} catch (err) {
|
|
@@ -72,8 +70,6 @@ export class Join {
|
|
|
72
70
|
if (err.code !== 'CLOSED' || this.cancelled) await this._forget()
|
|
73
71
|
if (err.code !== 'CLOSED' && !this.waiting) this.root._onerror(err)
|
|
74
72
|
this._end(this._reject, err)
|
|
75
|
-
} finally {
|
|
76
|
-
nearby?.()
|
|
77
73
|
}
|
|
78
74
|
}
|
|
79
75
|
|
|
@@ -91,10 +87,10 @@ export class Join {
|
|
|
91
87
|
// the first save fixes the writer, later ones add what was learned
|
|
92
88
|
/** @private */
|
|
93
89
|
async _save(fields) {
|
|
94
|
-
const store = this.root.local
|
|
95
|
-
this._row ??= (
|
|
90
|
+
const store = this.root.local.store
|
|
91
|
+
this._row ??= (await store.get('joins', this.id)).data || Identity.randomKeyPair()
|
|
96
92
|
this._row = { ...this._row, id: this.id, type: this.type, ...fields }
|
|
97
|
-
await store
|
|
93
|
+
await store.put('joins', this._row)
|
|
98
94
|
return this._row
|
|
99
95
|
}
|
|
100
96
|
|
|
@@ -103,12 +99,11 @@ export class Join {
|
|
|
103
99
|
async _keep(reply) {
|
|
104
100
|
const { key, encryptionKey, epochs } = reply
|
|
105
101
|
await this._save({ key, encryptionKey, epochs: c.encode(epochEntries, epochs) })
|
|
106
|
-
return reply
|
|
107
102
|
}
|
|
108
103
|
|
|
109
104
|
/** @private */
|
|
110
105
|
async _forget() {
|
|
111
|
-
await this.root.local
|
|
106
|
+
await this.root.local.store.del('joins', this.id).catch(safetyCatch)
|
|
112
107
|
}
|
|
113
108
|
|
|
114
109
|
/** @private */
|