@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/lib/operators.js
CHANGED
|
@@ -2,6 +2,7 @@ import { Readable } from 'streamx'
|
|
|
2
2
|
|
|
3
3
|
import { encodeId, decodeId } from '@cero-base/core/blobs/codec'
|
|
4
4
|
import { onAbort, subscribe } from '@cero-base/core/utils'
|
|
5
|
+
import { CeroError } from '@cero-base/core/errors'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* @typedef {import('./refs.js').Ref} Ref
|
|
@@ -99,9 +100,9 @@ const WRITES = { single: ['set', 'del'], collection: ['put', 'set', 'del'] }
|
|
|
99
100
|
* peer, inside the op's transaction. Return `false` to refuse it: the writer's own call rejects
|
|
100
101
|
* with `REFUSED`. `ctx` is `{ op, name, row, existing, id, memberId, role, get, put, set, del }`;
|
|
101
102
|
* mutate `ctx.row` to rewrite what is stored. `op` is the op as it applies, so an upsert on a
|
|
102
|
-
* collection is a `put`. The four operators on `ctx` read and write the
|
|
103
|
+
* collection is a `put`. The four operators on `ctx` read and write the handle as it stands at this
|
|
103
104
|
* op, inside the transaction. Must be deterministic: read only `ctx`, never a clock or local
|
|
104
|
-
* state. On a type ref (`me.room.notes`) it reaches every
|
|
105
|
+
* state. On a type ref (`me.room.notes`) it reaches every handle of the type before the handle
|
|
105
106
|
* opens; register in an extension's `setup` to see every op the root applies too. The imported
|
|
106
107
|
* operators throw inside a hook; use the ones on `ctx`. Not available over RPC.
|
|
107
108
|
*
|
|
@@ -153,11 +154,9 @@ function hook(phase, ref, fn, opts) {
|
|
|
153
154
|
// handle refs read their rows from the parent's `handles` collection, filtered by type
|
|
154
155
|
const parentStore = (ref) => (ref.handle.root ? ref.handle.root.store : ref.handle.store)
|
|
155
156
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return { data, total: data.length, size: data.length }
|
|
160
|
-
}
|
|
157
|
+
// a list asks the store for the type, so limit and total count only it; an id is checked after
|
|
158
|
+
const ofType = (ref, q) => (typeof q === 'string' ? q : { ...q, type: ref.name })
|
|
159
|
+
const typed = (row, name) => ({ data: row?.type === name ? row : null })
|
|
161
160
|
|
|
162
161
|
function resolveResult(ref, res) {
|
|
163
162
|
if (res == null || res.data == null) return res
|
|
@@ -201,8 +200,8 @@ export async function get(ref, q) {
|
|
|
201
200
|
const live = ref.handle._live?.[ref.name]
|
|
202
201
|
if (live) return live.get(q)
|
|
203
202
|
if (ref.kind === 'handle') {
|
|
204
|
-
const
|
|
205
|
-
return
|
|
203
|
+
const res = await parentStore(ref).get('handles', ofType(ref, q))
|
|
204
|
+
return typeof q === 'string' ? typed(res.data, ref.name) : res
|
|
206
205
|
}
|
|
207
206
|
const res = await ref.handle.store.get(ref.name, q)
|
|
208
207
|
return resolveResult(ref, res)
|
|
@@ -244,10 +243,8 @@ function source(ref, q) {
|
|
|
244
243
|
return { src: subscribe({ get: () => live.get(q), watch: live.watch }), map: (res) => res }
|
|
245
244
|
}
|
|
246
245
|
if (ref.kind === 'handle') {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
map: (snap) => normalize(snap?.data, ref.name)
|
|
250
|
-
}
|
|
246
|
+
const map = typeof q === 'string' ? (snap) => typed(snap?.data, ref.name) : (res) => res
|
|
247
|
+
return { src: parentStore(ref).watch('handles', ofType(ref, q)), map }
|
|
251
248
|
}
|
|
252
249
|
return { src: ref.handle.store.watch(ref.name, q), map: (res) => resolveResult(ref, res) }
|
|
253
250
|
}
|
|
@@ -327,15 +324,23 @@ function same(a, b) {
|
|
|
327
324
|
* @param {string | { invite?: string, id?: string, name?: string } | undefined} [arg]
|
|
328
325
|
* @returns {Promise<Context>} The resolved child handle.
|
|
329
326
|
*/
|
|
330
|
-
export function open(ref, arg) {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
327
|
+
export async function open(ref, arg = {}) {
|
|
328
|
+
const opts = typeof arg === 'string' ? { invite: arg } : arg
|
|
329
|
+
const [key, ...more] = opts && typeof opts === 'object' ? Object.keys(opts) : [null]
|
|
330
|
+
// one of the three, or nothing: a typo never quietly creates a handle
|
|
331
|
+
const known = key === undefined || (OPENS.has(key) && typeof opts[key] === 'string')
|
|
332
|
+
if (more.length || !known) {
|
|
333
|
+
throw CeroError.INVALID('open takes an invite, { invite }, { id }, { name } or nothing')
|
|
334
|
+
}
|
|
335
|
+
if (key === 'invite') return ref.handle._join(opts.invite, ref.name)
|
|
336
|
+
if (key === 'id') return ref.handle._load(ref.name, opts.id)
|
|
337
|
+
return ref.handle._create(ref.name, opts)
|
|
335
338
|
}
|
|
336
339
|
|
|
340
|
+
const OPENS = new Set(['invite', 'id', 'name'])
|
|
341
|
+
|
|
337
342
|
/**
|
|
338
|
-
* Mint an invite code into a
|
|
343
|
+
* Mint an invite code into a handle.
|
|
339
344
|
*
|
|
340
345
|
* @param {Context} ctx
|
|
341
346
|
* @param {import('@cero-base/core/pairing').InviteOpts} [opts]
|
|
@@ -357,7 +362,7 @@ export function revoke(ctx, code) {
|
|
|
357
362
|
}
|
|
358
363
|
|
|
359
364
|
/**
|
|
360
|
-
* Re-key a
|
|
365
|
+
* Re-key a handle now: a new epoch sealed to its members, so one removed before it reads nothing
|
|
361
366
|
* written after. Needs the remove permission. A removal also re-keys on its own shortly after it
|
|
362
367
|
* lands; this is the one to await.
|
|
363
368
|
*
|
|
@@ -370,7 +375,7 @@ export function rotate(ctx) {
|
|
|
370
375
|
|
|
371
376
|
/**
|
|
372
377
|
* Write atomically: every write `fn` makes through `tx`, the context it is handed, lands as one
|
|
373
|
-
* batch, or none does. Reads through `tx` see the
|
|
378
|
+
* batch, or none does. Reads through `tx` see the handle as it was before the batch. Not available
|
|
374
379
|
* over RPC.
|
|
375
380
|
*
|
|
376
381
|
* @template T
|
|
@@ -407,7 +412,7 @@ export function deny(ctx, request, reason = '') {
|
|
|
407
412
|
}
|
|
408
413
|
|
|
409
414
|
/**
|
|
410
|
-
* Quit a
|
|
415
|
+
* Quit a handle for good: it leaves your list and closes on this device.
|
|
411
416
|
*
|
|
412
417
|
* @param {Context} ctx
|
|
413
418
|
* @returns {Promise<void>}
|
|
@@ -459,24 +464,24 @@ export function resume(me) {
|
|
|
459
464
|
}
|
|
460
465
|
|
|
461
466
|
/**
|
|
462
|
-
* Mark a
|
|
467
|
+
* Mark a handle as the one in use: it ranks first on the swarm, searching and announcing. Handles are
|
|
463
468
|
* otherwise ranked by their last update.
|
|
464
469
|
*
|
|
465
|
-
* @param {Context}
|
|
470
|
+
* @param {Context} ctx
|
|
466
471
|
* @returns {Promise<void>}
|
|
467
472
|
*/
|
|
468
|
-
export async function activate(
|
|
469
|
-
return
|
|
473
|
+
export async function activate(ctx) {
|
|
474
|
+
return ctx._active(true)
|
|
470
475
|
}
|
|
471
476
|
|
|
472
477
|
/**
|
|
473
|
-
* Take a
|
|
478
|
+
* Take a handle off the swarm until something lands in it.
|
|
474
479
|
*
|
|
475
|
-
* @param {Context}
|
|
480
|
+
* @param {Context} ctx
|
|
476
481
|
* @returns {Promise<void>}
|
|
477
482
|
*/
|
|
478
|
-
export async function deactivate(
|
|
479
|
-
return
|
|
483
|
+
export async function deactivate(ctx) {
|
|
484
|
+
return ctx._active(false)
|
|
480
485
|
}
|
|
481
486
|
|
|
482
487
|
/**
|
|
@@ -490,14 +495,16 @@ export function phrase(me) {
|
|
|
490
495
|
}
|
|
491
496
|
|
|
492
497
|
/**
|
|
493
|
-
*
|
|
494
|
-
*
|
|
495
|
-
*
|
|
498
|
+
* Turn the device's Bluetooth radio on or off, on `opts.topic` or else the default topic: the one
|
|
499
|
+
* `cero()` opened with, or the channel's own. On, it links with the devices in range on the same
|
|
500
|
+
* channel and topic, and everything syncs and joins over those links as over the internet.
|
|
501
|
+
* Nothing is stored: the app sets it at every start.
|
|
496
502
|
*
|
|
497
503
|
* @param {Context} ctx
|
|
498
|
-
* @param {boolean
|
|
504
|
+
* @param {boolean} on
|
|
505
|
+
* @param {{ topic?: string }} [opts]
|
|
499
506
|
* @returns {Promise<void>}
|
|
500
507
|
*/
|
|
501
|
-
export function nearby(ctx,
|
|
502
|
-
return ctx._nearby(
|
|
508
|
+
export async function nearby(ctx, on, opts = {}) {
|
|
509
|
+
return ctx._nearby(on, opts)
|
|
503
510
|
}
|
package/src/lib/refs.js
CHANGED
|
@@ -12,7 +12,7 @@ import { CeroError } from '@cero-base/core/errors'
|
|
|
12
12
|
*/
|
|
13
13
|
export class Ref {
|
|
14
14
|
/**
|
|
15
|
-
* @param {Owner} handle The root, a
|
|
15
|
+
* @param {Owner} handle The root, a handle or the local store the ref lives on.
|
|
16
16
|
* @param {string} name Ref name as declared in the schema.
|
|
17
17
|
* @param {string} kind Ref kind: `'collection'`, `'single'`, `'action'`, or `'handle'`.
|
|
18
18
|
* @param {string | null} [schema] Fully-qualified schema id, if any.
|
|
@@ -32,7 +32,7 @@ export class Ref {
|
|
|
32
32
|
*
|
|
33
33
|
* @param {Owner} target
|
|
34
34
|
* @param {Record<string, RefInfo>} refs
|
|
35
|
-
* @param {Record<string, Spec>} [handles] The handle types, so `target.room.notes` names every
|
|
35
|
+
* @param {Record<string, Spec>} [handles] The handle types, so `target.room.notes` names every handle's notes.
|
|
36
36
|
*/
|
|
37
37
|
static attach(target, refs, handles) {
|
|
38
38
|
for (const [name, info] of Object.entries(refs || {})) {
|
package/src/rpc/client.js
CHANGED
|
@@ -53,7 +53,7 @@ export { t, schema }
|
|
|
53
53
|
* @property {string} type
|
|
54
54
|
* @property {string|null} name
|
|
55
55
|
*
|
|
56
|
-
* @typedef {(Client | Handle) & Record<string, Ref>} Context A UI's root or
|
|
56
|
+
* @typedef {(Client | Handle) & Record<string, Ref>} Context A UI's root or handle: the verbs take it like an in-process one.
|
|
57
57
|
*/
|
|
58
58
|
|
|
59
59
|
const blobIdEnc = {
|
|
@@ -337,36 +337,26 @@ const operators = {
|
|
|
337
337
|
await this.rpc.call({ handle: this.id, op, data: encoded })
|
|
338
338
|
},
|
|
339
339
|
|
|
340
|
-
// the verbs in lib/operators.js land here, and cross to the worker
|
|
340
|
+
// the verbs in lib/operators.js land here, and cross to the worker, which runs the same operator
|
|
341
341
|
|
|
342
342
|
/** @private */
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
handle: this.id,
|
|
346
|
-
role: role || '',
|
|
347
|
-
ttl: ttl ? String(ttl) : '',
|
|
348
|
-
reuse: reuse === true,
|
|
349
|
-
confirm: confirm === true,
|
|
350
|
-
data: data || null
|
|
351
|
-
})
|
|
352
|
-
return invite
|
|
343
|
+
_invite(opts) {
|
|
344
|
+
return this._verb('invite', opts)
|
|
353
345
|
},
|
|
354
346
|
|
|
355
347
|
/** @private */
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
return ok
|
|
348
|
+
_revoke(invite) {
|
|
349
|
+
return this._verb('revoke', invite)
|
|
359
350
|
},
|
|
360
351
|
|
|
361
352
|
/** @private */
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
return { epoch }
|
|
353
|
+
_rotate() {
|
|
354
|
+
return this._verb('rotate')
|
|
365
355
|
},
|
|
366
356
|
|
|
367
357
|
/** @private */
|
|
368
|
-
|
|
369
|
-
|
|
358
|
+
_answer(id, { accept, role, reason }) {
|
|
359
|
+
return accept ? this._verb('accept', { id }, { role }) : this._verb('deny', { id }, reason)
|
|
370
360
|
},
|
|
371
361
|
|
|
372
362
|
/** @private */
|
|
@@ -375,34 +365,43 @@ const operators = {
|
|
|
375
365
|
},
|
|
376
366
|
|
|
377
367
|
/** @private */
|
|
378
|
-
|
|
379
|
-
return
|
|
368
|
+
_cancel(invite) {
|
|
369
|
+
return this._verb('cancel', invite)
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
/** @private */
|
|
373
|
+
_suspend() {
|
|
374
|
+
return this._verb('suspend')
|
|
380
375
|
},
|
|
381
376
|
|
|
382
377
|
/** @private */
|
|
383
|
-
|
|
384
|
-
|
|
378
|
+
_resume() {
|
|
379
|
+
return this._verb('resume')
|
|
385
380
|
},
|
|
386
381
|
|
|
387
382
|
/** @private */
|
|
388
|
-
|
|
389
|
-
|
|
383
|
+
_active(on) {
|
|
384
|
+
return this._verb(on ? 'activate' : 'deactivate')
|
|
390
385
|
},
|
|
391
386
|
|
|
392
387
|
/** @private */
|
|
393
|
-
|
|
394
|
-
|
|
388
|
+
_phrase() {
|
|
389
|
+
return this._verb('phrase')
|
|
395
390
|
},
|
|
396
391
|
|
|
397
392
|
/** @private */
|
|
398
|
-
|
|
399
|
-
return
|
|
393
|
+
_nearby(on, opts) {
|
|
394
|
+
return this._verb('nearby', on, opts)
|
|
400
395
|
},
|
|
401
396
|
|
|
402
397
|
/** @private */
|
|
403
|
-
async
|
|
404
|
-
const
|
|
405
|
-
|
|
398
|
+
async _verb(verb, ...args) {
|
|
399
|
+
const { data } = await this.rpc.verb({
|
|
400
|
+
handle: this.id,
|
|
401
|
+
verb,
|
|
402
|
+
args: c.encode(c.any, given(args))
|
|
403
|
+
})
|
|
404
|
+
return data ? c.decode(c.any, data) : undefined
|
|
406
405
|
}
|
|
407
406
|
}
|
|
408
407
|
|
|
@@ -415,9 +414,7 @@ const operators = {
|
|
|
415
414
|
* @returns {Promise<Client>}
|
|
416
415
|
*/
|
|
417
416
|
export async function restore(me, phrase) {
|
|
418
|
-
|
|
419
|
-
me.id = res.id
|
|
420
|
-
me.device = device(res)
|
|
417
|
+
me._identify(await me.rpc.restore({ phrase }))
|
|
421
418
|
return me
|
|
422
419
|
}
|
|
423
420
|
|
|
@@ -502,18 +499,21 @@ export class Client extends RPCClient {
|
|
|
502
499
|
/** @private */
|
|
503
500
|
async _open() {
|
|
504
501
|
await super._open()
|
|
505
|
-
|
|
506
|
-
const { id, fileBase, fileToken } = res
|
|
507
|
-
this.id = id
|
|
508
|
-
this.device = device(res)
|
|
509
|
-
/** @private */
|
|
510
|
-
this._fileBase = fileBase || ''
|
|
511
|
-
/** @private */
|
|
512
|
-
this._fileToken = fileToken || ''
|
|
502
|
+
this._identify(await this.rpc.init({}))
|
|
513
503
|
Ref.attach(this, /** @type {Spec} */ (this.spec).meta.refs)
|
|
514
504
|
if (/** @type {Spec} */ (this.spec).meta.local?.refs) this.local = new LocalRefs(this)
|
|
515
505
|
this._pumpErrors()
|
|
516
506
|
}
|
|
507
|
+
// the worker's answer to init and to restore: who this UI is, and where its files are served
|
|
508
|
+
/** @private */
|
|
509
|
+
_identify(res) {
|
|
510
|
+
this.id = res.id
|
|
511
|
+
this.device = device(res)
|
|
512
|
+
/** @private */
|
|
513
|
+
this._fileBase = res.fileBase || ''
|
|
514
|
+
/** @private */
|
|
515
|
+
this._fileToken = res.fileToken || ''
|
|
516
|
+
}
|
|
517
517
|
|
|
518
518
|
/**
|
|
519
519
|
* Create a new child handle of the given type.
|
|
@@ -541,7 +541,7 @@ export class Client extends RPCClient {
|
|
|
541
541
|
* @private
|
|
542
542
|
*/
|
|
543
543
|
async _load(type, id) {
|
|
544
|
-
const stub = await this.rpc.openHandle({ parent: this.id, row: id })
|
|
544
|
+
const stub = await this.rpc.openHandle({ parent: this.id, row: id, type })
|
|
545
545
|
return new Handle(this, stub.id, stub.type, stub.name || null)
|
|
546
546
|
}
|
|
547
547
|
|
|
@@ -654,9 +654,16 @@ const ZERO = {
|
|
|
654
654
|
// c.any carries undefined as null, and in a query undefined is absent
|
|
655
655
|
function toWire(query) {
|
|
656
656
|
if (query === undefined) return null
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
657
|
+
return c.encode(c.any, query && typeof query === 'object' ? defined(query) : query)
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// an option or a last argument left undefined stays absent, so the operator's default applies
|
|
661
|
+
function given(args) {
|
|
662
|
+
const out = args.map((arg) => (arg?.constructor === Object ? defined(arg) : arg))
|
|
663
|
+
while (out.length && out[out.length - 1] === undefined) out.pop()
|
|
664
|
+
return out
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
function defined(obj) {
|
|
668
|
+
return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined))
|
|
662
669
|
}
|
package/src/rpc/server.js
CHANGED
|
@@ -4,7 +4,7 @@ import safetyCatch from 'safety-catch'
|
|
|
4
4
|
import { RPCServer, bindCodec } from '@cero-base/core/rpc'
|
|
5
5
|
import { CeroError } from '@cero-base/core/errors'
|
|
6
6
|
|
|
7
|
-
import { cero, restore
|
|
7
|
+
import { cero, restore } from '../index.js'
|
|
8
8
|
import {
|
|
9
9
|
put,
|
|
10
10
|
set,
|
|
@@ -15,6 +15,8 @@ import {
|
|
|
15
15
|
invite,
|
|
16
16
|
revoke,
|
|
17
17
|
rotate,
|
|
18
|
+
accept,
|
|
19
|
+
deny,
|
|
18
20
|
cancel,
|
|
19
21
|
leave,
|
|
20
22
|
suspend,
|
|
@@ -25,6 +27,24 @@ import {
|
|
|
25
27
|
nearby
|
|
26
28
|
} from '../lib/operators.js'
|
|
27
29
|
|
|
30
|
+
// what a UI reaches by name: these operators, never close or a handle's privates
|
|
31
|
+
const VERBS = new Map(
|
|
32
|
+
Object.entries({
|
|
33
|
+
invite,
|
|
34
|
+
revoke,
|
|
35
|
+
rotate,
|
|
36
|
+
accept,
|
|
37
|
+
deny,
|
|
38
|
+
cancel,
|
|
39
|
+
suspend,
|
|
40
|
+
resume,
|
|
41
|
+
activate,
|
|
42
|
+
deactivate,
|
|
43
|
+
phrase,
|
|
44
|
+
nearby
|
|
45
|
+
})
|
|
46
|
+
)
|
|
47
|
+
|
|
28
48
|
/**
|
|
29
49
|
* @typedef {import('@cero-base/core/rpc').RPCServer} BaseRPCServer
|
|
30
50
|
*
|
|
@@ -37,7 +57,7 @@ import {
|
|
|
37
57
|
*
|
|
38
58
|
* @typedef {object} Identity
|
|
39
59
|
* @property {string} id Long-lived cero identity id.
|
|
40
|
-
* @property {string} deviceId Per-device id
|
|
60
|
+
* @property {string} deviceId Per-device id.
|
|
41
61
|
* @property {string} deviceName This device's name, empty when it has none.
|
|
42
62
|
*
|
|
43
63
|
* @typedef {{ ref: import('../lib/refs.js').Ref, codec: import('@cero-base/core/rpc').Codec, info?: import('../lib/spec.js').RefInfo }} RefAndCodec
|
|
@@ -114,10 +134,9 @@ export class Server extends RPCServer {
|
|
|
114
134
|
this.handles.set(this.me.id, this.me)
|
|
115
135
|
await this.me.fileServer.listen()
|
|
116
136
|
this._wireData()
|
|
117
|
-
this._wirePairing()
|
|
118
137
|
this._wireHandles()
|
|
138
|
+
this._wireVerbs()
|
|
119
139
|
this._wireRestore()
|
|
120
|
-
this._wireSeed()
|
|
121
140
|
}
|
|
122
141
|
|
|
123
142
|
/** @private */
|
|
@@ -173,13 +192,13 @@ export class Server extends RPCServer {
|
|
|
173
192
|
}
|
|
174
193
|
|
|
175
194
|
/**
|
|
176
|
-
* Wire the `restore` handler.
|
|
195
|
+
* Wire the `restore` handler.
|
|
177
196
|
* @private
|
|
178
197
|
*/
|
|
179
198
|
_wireRestore() {
|
|
180
199
|
this.rpc.onRestore(async ({ phrase }) => {
|
|
181
200
|
if (!this.me) throw CeroError.NOT_READY('Server', 'server')
|
|
182
|
-
this.me = await restore(this.me,
|
|
201
|
+
this.me = await restore(this.me, phrase)
|
|
183
202
|
this.handles = new Map([[this.me.id, this.me]])
|
|
184
203
|
return this._identity()
|
|
185
204
|
})
|
|
@@ -279,71 +298,7 @@ export class Server extends RPCServer {
|
|
|
279
298
|
}
|
|
280
299
|
|
|
281
300
|
/**
|
|
282
|
-
* Register
|
|
283
|
-
* @private
|
|
284
|
-
*/
|
|
285
|
-
_wirePairing() {
|
|
286
|
-
this.rpc.onInvite(async ({ handle, role, ttl, reuse, confirm, data }) => {
|
|
287
|
-
const code = await invite(this._resolve(handle), {
|
|
288
|
-
role: role || undefined,
|
|
289
|
-
ttl: ttl || undefined,
|
|
290
|
-
reuse: reuse === true,
|
|
291
|
-
confirm: confirm === true,
|
|
292
|
-
data: data || null
|
|
293
|
-
})
|
|
294
|
-
return { invite: code }
|
|
295
|
-
})
|
|
296
|
-
|
|
297
|
-
this.rpc.onRevoke(async ({ handle, invite: code }) => {
|
|
298
|
-
return { ok: await revoke(this._resolve(handle), code) }
|
|
299
|
-
})
|
|
300
|
-
|
|
301
|
-
this.rpc.onRotate(({ handle }) => rotate(this._resolve(handle)))
|
|
302
|
-
|
|
303
|
-
this.rpc.onAnswer(async ({ handle, id, accept, role, reason }) => {
|
|
304
|
-
await this._resolve(handle)._answer(id, { accept, role: role || undefined, reason })
|
|
305
|
-
return {}
|
|
306
|
-
})
|
|
307
|
-
|
|
308
|
-
this.rpc.onSuspend(async ({ handle }) => {
|
|
309
|
-
await suspend(this._resolve(handle))
|
|
310
|
-
return {}
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
this.rpc.onResume(async ({ handle }) => {
|
|
314
|
-
await resume(this._resolve(handle))
|
|
315
|
-
return {}
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
this.rpc.onSetActive(async ({ handle, active: on }) => {
|
|
319
|
-
await (on ? activate : deactivate)(this._resolve(handle))
|
|
320
|
-
return {}
|
|
321
|
-
})
|
|
322
|
-
|
|
323
|
-
this.rpc.onNearby(async ({ on, invite }) => {
|
|
324
|
-
await nearby(this.me, invite || on)
|
|
325
|
-
return {}
|
|
326
|
-
})
|
|
327
|
-
|
|
328
|
-
this.rpc.onCancel(async ({ invite: code }) => ({ ok: await cancel(this.me, code) }))
|
|
329
|
-
|
|
330
|
-
this.rpc.onJoin(async ({ parent, ref, invite }) => {
|
|
331
|
-
if (this._resolve(parent) !== this.me) throw CeroError.UNSUPPORTED('nested handles')
|
|
332
|
-
let child
|
|
333
|
-
try {
|
|
334
|
-
child = await this.me._join(invite, ref)
|
|
335
|
-
} catch (err) {
|
|
336
|
-
if (err.code !== 'DENIED') throw err
|
|
337
|
-
return { id: '', type: ref, denied: true, reason: err.reason || '' }
|
|
338
|
-
}
|
|
339
|
-
const id = child.id
|
|
340
|
-
this.handles.set(id, child)
|
|
341
|
-
return { id, type: ref, name: '' }
|
|
342
|
-
})
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Register add/open/close/leave RPC handlers for child handles.
|
|
301
|
+
* Register add/open/join/close/leave RPC handlers for child handles.
|
|
347
302
|
* @private
|
|
348
303
|
*/
|
|
349
304
|
_wireHandles() {
|
|
@@ -359,15 +314,29 @@ export class Server extends RPCServer {
|
|
|
359
314
|
return { id, type: ref, name: opts.name || '' }
|
|
360
315
|
})
|
|
361
316
|
|
|
362
|
-
this.rpc.onOpenHandle(async ({ parent, row }) => {
|
|
317
|
+
this.rpc.onOpenHandle(async ({ parent, row, type }) => {
|
|
363
318
|
if (this._resolve(parent) !== this.me) throw CeroError.UNSUPPORTED('nested handles')
|
|
364
319
|
const { data } = await this.me.store.get('handles', row)
|
|
365
320
|
if (!data) throw CeroError.UNKNOWN('handle', row)
|
|
366
|
-
const child = await this.me._load(
|
|
321
|
+
const child = await this.me._load(type, row)
|
|
367
322
|
this.handles.set(row, child)
|
|
368
323
|
return { id: row, type: data.type, name: data.name || '' }
|
|
369
324
|
})
|
|
370
325
|
|
|
326
|
+
this.rpc.onJoin(async ({ parent, ref, invite }) => {
|
|
327
|
+
if (this._resolve(parent) !== this.me) throw CeroError.UNSUPPORTED('nested handles')
|
|
328
|
+
let child
|
|
329
|
+
try {
|
|
330
|
+
child = await this.me._join(invite, ref)
|
|
331
|
+
} catch (err) {
|
|
332
|
+
if (err.code !== 'DENIED') throw err
|
|
333
|
+
return { id: '', type: ref, denied: true, reason: err.reason || '' }
|
|
334
|
+
}
|
|
335
|
+
const id = child.id
|
|
336
|
+
this.handles.set(id, child)
|
|
337
|
+
return { id, type: ref, name: '' }
|
|
338
|
+
})
|
|
339
|
+
|
|
371
340
|
this.rpc.onCloseHandle(async ({ handle }) => {
|
|
372
341
|
const h = this.handles.get(handle)
|
|
373
342
|
if (!h || h === this.me) return {}
|
|
@@ -385,6 +354,25 @@ export class Server extends RPCServer {
|
|
|
385
354
|
})
|
|
386
355
|
}
|
|
387
356
|
|
|
357
|
+
/**
|
|
358
|
+
* Register the `verb` handler: the operator by its name, on the handle, as in the worker.
|
|
359
|
+
* @private
|
|
360
|
+
*/
|
|
361
|
+
_wireVerbs() {
|
|
362
|
+
this.rpc.onVerb(async ({ handle, verb, args }) => {
|
|
363
|
+
const run = VERBS.get(verb)
|
|
364
|
+
if (!run) throw CeroError.UNKNOWN('verb', verb)
|
|
365
|
+
let data
|
|
366
|
+
try {
|
|
367
|
+
data = await run(this._resolve(handle), ...c.decode(c.any, args))
|
|
368
|
+
} catch (err) {
|
|
369
|
+
// bare-rpc rethrows a TypeError and ends the worker: a bad argument fails only the call
|
|
370
|
+
throw Object.assign(new Error(err.message), { code: err.code })
|
|
371
|
+
}
|
|
372
|
+
return { data: data === undefined ? null : c.encode(c.any, data) }
|
|
373
|
+
})
|
|
374
|
+
}
|
|
375
|
+
|
|
388
376
|
/**
|
|
389
377
|
* Look up a live handle by id, throwing if unknown. Binds the handle's
|
|
390
378
|
* codec on first use.
|
|
@@ -413,9 +401,7 @@ export class Server extends RPCServer {
|
|
|
413
401
|
if (local) {
|
|
414
402
|
const info = this.spec.meta.local?.refs?.[name]
|
|
415
403
|
if (!info || info.internal) throw CeroError.UNKNOWN('local ref', name)
|
|
416
|
-
|
|
417
|
-
if (!r) throw CeroError.UNKNOWN('local ref', name)
|
|
418
|
-
return { ref: r, codec: this.spec.local.codec, info }
|
|
404
|
+
return { ref: this.me.local[name], codec: this.spec.local.codec, info }
|
|
419
405
|
}
|
|
420
406
|
const h = this._resolve(id)
|
|
421
407
|
const r = h[name]
|
|
@@ -439,17 +425,6 @@ export class Server extends RPCServer {
|
|
|
439
425
|
fileToken: fs.server.token || ''
|
|
440
426
|
}
|
|
441
427
|
}
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* Wire the on-demand `seed` handler — surfaces the recovery phrase only when asked.
|
|
445
|
-
* @private
|
|
446
|
-
*/
|
|
447
|
-
_wireSeed() {
|
|
448
|
-
this.rpc.onSeed(async () => {
|
|
449
|
-
if (!this.me) throw CeroError.NOT_READY('Server', 'server')
|
|
450
|
-
return { phrase: await phrase(this.me) }
|
|
451
|
-
})
|
|
452
|
-
}
|
|
453
428
|
}
|
|
454
429
|
|
|
455
430
|
/**
|
|
@@ -477,13 +452,27 @@ function fromWire(buf) {
|
|
|
477
452
|
*/
|
|
478
453
|
function encodeGet(r, codec, query, { data, total, size }) {
|
|
479
454
|
const one = typeof query === 'string' || r.kind === 'single'
|
|
455
|
+
const wire = toIds(r)
|
|
480
456
|
return {
|
|
481
|
-
data: one ? codec.encodeRow(r.schema, data) : codec.encodeRows(r.schema, data),
|
|
457
|
+
data: one ? codec.encodeRow(r.schema, wire(data)) : codec.encodeRows(r.schema, data?.map(wire)),
|
|
482
458
|
total: total ?? -1,
|
|
483
459
|
size: size ?? 0
|
|
484
460
|
}
|
|
485
461
|
}
|
|
486
462
|
|
|
463
|
+
// a read resolves file fields to { id, type, size, url }, which a typed row cannot carry: the wire
|
|
464
|
+
// takes the id, and the client builds the same file from it
|
|
465
|
+
function toIds(r) {
|
|
466
|
+
const fields = r.name === 'files' ? null : r.handle.store?.refs?.[r.name]?.files
|
|
467
|
+
if (!fields?.length) return (row) => row
|
|
468
|
+
return (row) => {
|
|
469
|
+
if (!row) return row
|
|
470
|
+
const out = { ...row }
|
|
471
|
+
for (const f of fields) if (out[f] && typeof out[f] === 'object') out[f] = out[f].id
|
|
472
|
+
return out
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
487
476
|
// a typed row decodes every field, the ones the client left out as defaults: keep what was sent
|
|
488
477
|
function received(row, info, sent = null) {
|
|
489
478
|
const declared = info?.fields
|