@cero-base/cero 3.2.0 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/cero",
3
- "version": "3.2.0",
3
+ "version": "3.3.1",
4
4
  "description": "The ideal p2p API — everything is a handle, handles contain refs, refs contain rows.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -113,7 +113,7 @@
113
113
  "test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
114
114
  },
115
115
  "dependencies": {
116
- "@cero-base/core": "^3.2.0",
116
+ "@cero-base/core": "^3.3.1",
117
117
  "b4a": "^1.9.0",
118
118
  "bare-abort-controller": "^1.1.2",
119
119
  "bare-crypto": "^1.15.4",
@@ -22,7 +22,7 @@ import { Blobs } from '@cero-base/core/blobs'
22
22
  import { decodeId, servedType } from '@cero-base/core/blobs/codec'
23
23
  import { FileServer } from '@cero-base/core/blobs/server'
24
24
 
25
- import { KEEP_OPEN, NS, PASSES, REFUSALS, SPENT, TIMEOUT } from '../lib/constants.js'
25
+ import { KEEP_OPEN, LOOKS, NS, PASSES, REFUSALS, SPENT, TIMEOUT } from '../lib/constants.js'
26
26
 
27
27
  import { Ref } from '../lib/refs.js'
28
28
  import { box } from '../local/index.js'
@@ -138,9 +138,7 @@ export class Handle extends ReadyResource {
138
138
  /** @private */
139
139
  this._taking = parent ? null : new Set() // the requests being taken back, by writer
140
140
  /** @private */
141
- this._held = parent ? null : new Map() // pass invite → when it is tried again, its join having settled nothing
142
- /** @private */
143
- this._retries = parent ? null : new Map() // room open here → the timer that reads its passes again
141
+ this._looks = parent ? null : new Map() // room open here → its reads of its passes, and the look at each room they name
144
142
  /** @private */
145
143
  this._coreKeys = parent ? null : new Map()
146
144
  /** @private */
@@ -459,6 +457,7 @@ export class Handle extends ReadyResource {
459
457
  // device checks its chain against
460
458
  /** @private */
461
459
  async _invite(opts = {}) {
460
+ if (opts.from != null) return this._seat(opts)
462
461
  if (this.parent) {
463
462
  const invite = await this._pair.invite(opts)
464
463
  // a close right after the invite must not drop the row that reopens this handle at boot
@@ -497,20 +496,24 @@ export class Handle extends ReadyResource {
497
496
  // an invite made to her, sealed with this room's keys to her devices in a room both are in, where
498
497
  // they find it. `from` crosses the bridge as its id
499
498
  /** @private */
500
- async _seat(id, { from, role = 'member', ttl } = {}) {
501
- if (!this.parent) throw CeroError.INVALID('a device is linked, not seated')
499
+ async _seat({ from, ...opts }) {
500
+ const id = opts.to
501
+ if (!this.parent) throw CeroError.INVALID("the root's invite links a device: no 'from'")
502
+ if (id == null) throw CeroError.INVALID("an invite 'from' a handle is made 'to' a member of it")
503
+ // its pass is sized before the invite is minted, as one that carries none
504
+ if (opts.data != null) throw CeroError.INVALID("an invite 'from' a handle carries no data")
502
505
  if (typeof from === 'string') {
503
506
  const { data: row } = await this.root.store.get('handles', from)
504
507
  if (!row) throw CeroError.UNKNOWN('handle', from)
505
508
  const shared = await this.root._load(row.type, from)
506
509
  try {
507
- return await this._seat(id, { from: shared, role, ttl })
510
+ return await this._seat({ ...opts, from: shared })
508
511
  } finally {
509
512
  await shared.close()
510
513
  }
511
514
  }
512
515
  if (!(from instanceof Handle) || !from.parent) {
513
- throw CeroError.INVALID('seat takes { from }: a room she is in')
516
+ throw CeroError.INVALID("'from' is a handle you both are in")
514
517
  }
515
518
  const { store } = from
516
519
  const { data: me } = await store.get('members', this.identity.id)
@@ -524,7 +527,7 @@ export class Handle extends ReadyResource {
524
527
  }
525
528
  const { data: invites } = await this.store.get('invites')
526
529
  if (invites.some((i) => i.to === id && !(i.expires && i.expires <= Date.now()))) {
527
- throw CeroError.INVALID(`${id} has a seat already`)
530
+ throw CeroError.INVALID(`${id} has an invite here already`)
528
531
  }
529
532
  // what would keep its pass out of `from` is checked first: an invite minted is live
530
533
  const at = Math.min(store.version, await store.dispatcher.version(store.view))
@@ -538,10 +541,17 @@ export class Handle extends ReadyResource {
538
541
  `this room has too many keys to seal to her ${devices.length} devices`
539
542
  )
540
543
  }
541
- const invite = await this._invite({ role, ttl, to: id, keys: devices.map((d) => d.key) })
542
- const pass = Pass.seal(devices, { ...fields, invite })
544
+ const invite = await this._invite({ ...opts, keys: devices.map((d) => d.key) })
545
+ // a rotation under way as the invite went in may not name her: its key is read once it ends
546
+ await this.store.rotation.settled()
547
+ const epochs = keyring.all()
548
+ // spent or revoked since: the rotation after that leaves her out, and so does this
549
+ const { data: live } = await this.store.get('invites', b4a.toHex(Invite.parse(invite).id))
550
+ if (!live) return invite
551
+ const pass = Pass.seal(devices, { ...fields, epochs, invite })
543
552
  const row = { id: b4a.toHex(Identity.randomBytes(16)), to: id, pass, createdAt: Date.now() }
544
553
  await store.call('add-pass', row)
554
+ return invite
545
555
  }
546
556
 
547
557
  // the same context, its refs writing into one batch; a fn that ignores it would write outside
@@ -617,27 +627,19 @@ export class Handle extends ReadyResource {
617
627
  }
618
628
 
619
629
  // a device that applies its own identity's removal writes it on the row, with who removed her; `out`, a seat
620
- // she holds no more with no removal to read, as a seat of hers that never counted there
630
+ // she holds no more with no removal to read, as a seat of hers that never counted there, with why
631
+ // the room turned it away
621
632
  /** @private */
622
633
  async _ousted(child, out = false) {
623
634
  const { data: row } = await this.store.get('handles', child.id)
624
635
  // only a seat held is lost: a row asking or joining is a rejoin under way, one ended is over
625
636
  if (!row || !['in', 'leaving'].includes(row.state || 'in')) return
626
- const lost = out || (await refused(child.store, this.identity))
627
- const removal = (await child.store.removal(this.identity.id)) ?? (lost ? { by: null } : null)
628
- if (!removal) return
629
- const fields = { by: removal.by, reason: null, encryptionKey: null, epochs: null }
630
- await this.store.set('handles', { id: child.id, state: 'removed', ...fields })
631
- // a seat waiting for the room this one held is taken now
632
- this._repass()
633
- }
634
-
635
- // the passes in every room open here, read again
636
- /** @private */
637
- _repass() {
638
- for (const child of this.children) {
639
- this._passes(child).catch((err) => this.closing || this._onerror(err))
640
- }
637
+ const removal = await child.store.removal(this.identity.id)
638
+ const refusal = removal ? null : await refused(child.store, this.identity)
639
+ if (!removal && !refusal && !out) return
640
+ const fields = { by: removal?.by ?? null, reason: refusal?.reason || null }
641
+ const ended = { state: 'removed', ...fields, encryptionKey: null, epochs: null }
642
+ await this.store.set('handles', { id: child.id, ...ended })
641
643
  }
642
644
 
643
645
  /**
@@ -1268,8 +1270,7 @@ export class Handle extends ReadyResource {
1268
1270
  writer: null,
1269
1271
  waiting: 0,
1270
1272
  end: null,
1271
- running: null,
1272
- read: false
1273
+ running: null
1273
1274
  }
1274
1275
  join.done = new Promise((resolve, reject) => {
1275
1276
  join.resolve = resolve
@@ -1302,12 +1303,6 @@ export class Handle extends ReadyResource {
1302
1303
  try {
1303
1304
  const { pass } = join
1304
1305
  const taken = pass && (await this._take(join, invite, running.signal))
1305
- // a pass for a room she sits in already: the room is hers as it is, let go like one entered
1306
- if (taken === false) {
1307
- const child = await this._load(join.type, join.id)
1308
- child.close().catch(this._onerror)
1309
- return settle(join.resolve, child)
1310
- }
1311
1306
  const id = pass ? hid.encode(pass.key) : await this._ask(join.type, invite)
1312
1307
  const writer = (join.writer = await this._loadKeyPair(id))
1313
1308
  const { data: row } = await this.store.get('handles', id)
@@ -1341,55 +1336,33 @@ export class Handle extends ReadyResource {
1341
1336
  if (end.code !== 'CLOSED') {
1342
1337
  await this._over(join.id, end, { invite, writer: join.writer }).catch(report)
1343
1338
  }
1344
- // a pass whose room did not show in time is tried again, nothing on the root
1345
- if (err.code !== 'CLOSED' && err.code !== 'TIMEOUT' && alone) this._onerror(err)
1339
+ if (err.code !== 'CLOSED' && alone) this._onerror(err)
1346
1340
  settle(join.reject, end)
1347
1341
  }
1348
1342
  }
1349
1343
 
1350
- // a pass's join: its invite read in the room its keys open, as nobody, before anything is written;
1351
- // then asked, written and seated by this device's own claim. Null when that takes longer: it goes
1352
- // on as a plain join
1344
+ // a room as a pass's keys open it, apart from any open here
1345
+ /** @private */
1346
+ _apart(type, { key, encryptionKey, epochs }, keyPair) {
1347
+ const spec = pickHandle(this.spec, type)
1348
+ const namespace = `${NS}/handle/${type}/${randomNs()}`
1349
+ return this._room({ spec, namespace, key, encryptionKey, epochs, keyPair })
1350
+ }
1351
+
1352
+ // a pass's join, its invite seen in its room by the look that took it: asked, written and seated
1353
+ // by this device's own claim. Null when that takes longer: it goes on as a plain join
1353
1354
  /** @private */
1354
1355
  async _take(join, invite, signal) {
1355
1356
  const { type, pass } = join
1356
1357
  const id = hid.encode(pass.key)
1357
1358
  const spec = pickHandle(this.spec, type)
1358
- const { key, encryptionKey, epochs } = pass
1359
- const room = (keyPair) => {
1360
- const namespace = `${NS}/handle/${type}/${randomNs()}`
1361
- return this._room({ spec, namespace, key, encryptionKey, epochs, keyPair })
1362
- }
1363
- const reader = room(Identity.randomKeyPair())
1364
- let moot = false
1365
- try {
1366
- await reader.ready()
1367
- const { store } = reader
1368
- const inviteId = b4a.toHex(Invite.parse(invite).id)
1369
- const shows = async () =>
1370
- !!(await store.get('invites', inviteId)).data ||
1371
- !!(await store.view.get(`@${store.ns}/${SPENT}`, { id: inviteId }))
1372
- if (!(await until(store, shows, TIMEOUT, signal))) {
1373
- throw signal.aborted ? CeroError.CLOSED('join') : CeroError.TIMEOUT('join')
1374
- }
1375
- // a seat lost, or one that never counted there, ends on the root before she asks again: the
1376
- // room holds neither her nor a promise to her at a seat made after she was out
1377
- const seated = await promised(store, this.identity)
1378
- await this._ousted(reader, !seated)
1379
- // a room she reads as in under another invite takes nothing now, read again in its turn
1380
- const { data: row } = await this.store.get('handles', id)
1381
- moot = seated && !!row && (row.state || 'in') === 'in'
1382
- } finally {
1383
- await reader._end().catch(safetyCatch)
1384
- }
1385
- if (moot) return false
1386
1359
  const { data: kept } = await this.local.store.get('handle-keypairs', id)
1387
1360
  const { publicKey, secretKey } = kept?.invite === invite ? kept : Identity.randomKeyPair()
1388
1361
  const writer = (join.writer = { publicKey, secretKey })
1389
1362
  // asked before its join is written: a retry, or the latest ask, finds its writer
1390
1363
  await this._ask(type, invite, writer)
1391
1364
  await Pairing.write(this.mailbox, invite, { identity: this.identity, spec, writer })
1392
- const child = room(writer)
1365
+ const child = this._apart(type, pass, writer)
1393
1366
  try {
1394
1367
  await child.ready()
1395
1368
  const { store } = child
@@ -1411,48 +1384,125 @@ export class Handle extends ReadyResource {
1411
1384
  return null
1412
1385
  }
1413
1386
 
1414
- // the passes for this identity in a room open here, read whenever its seats change, a join of one
1415
- // ends, a seat of hers ends, and every TIMEOUT while one waits: one join a room at a time, the
1416
- // newest pass first, and one whose join settled nothing waits TIMEOUT before its next try
1387
+ // the passes for this identity in a room open here, read as it opens, whenever its seats change
1388
+ // and when a join one of them began ends: each room they name is looked at once, for all of them
1417
1389
  /** @private */
1418
1390
  async _passes(from) {
1391
+ const mine = this._looks.get(from)
1392
+ if (!mine) return
1393
+ // a read begun since has the newer rows: it sets the looks
1394
+ const turn = ++mine.turn
1419
1395
  const { store } = from
1420
1396
  const rows = await store.view.find(`@${store.ns}/${PASSES}`, {}).toArray()
1421
1397
  rows.sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0))
1422
- let waiting = false
1398
+ const rooms = new Map()
1399
+ const left = new Map()
1423
1400
  for (const row of rows) {
1424
1401
  if (row.to !== this.identity.id) continue
1425
1402
  const pass = opens(this.identity, row.pass)
1426
1403
  const type = pass?.data && b4a.toString(pass.data)
1427
1404
  if (!type || !this.spec.handles?.[type]) continue
1428
1405
  if (await this._spent(from, row, pass)) continue
1429
- waiting = true
1430
- if ((this._held.get(pass.invite) ?? 0) > Date.now()) continue
1431
- const join = this._start(pass.invite, type, pass)
1432
- // another pass's join for the room ends first, and the read after it starts this one
1433
- if (join.invite !== pass.invite || join.read) continue
1434
- join.read = true
1435
- const next = async () => {
1436
- if (!(await this._spent(from, row, pass))) this._held.set(pass.invite, Date.now() + TIMEOUT)
1437
- if (!from.closing && !from.closed) await this._passes(from)
1406
+ // each opens a room: the newest of one member, so that none holds this device or another's passes up
1407
+ const count = (left.get(row.by) || 0) + 1
1408
+ left.set(row.by, count)
1409
+ if (count > LOOKS) continue
1410
+ const id = hid.encode(pass.key)
1411
+ rooms.set(id, [...(rooms.get(id) || []), { row, pass, type }])
1412
+ }
1413
+ if (turn !== mine.turn || this._looks.get(from) !== mine) return
1414
+ for (const [id, look] of mine.rooms) {
1415
+ if (rooms.has(id)) continue
1416
+ look.stop.abort()
1417
+ mine.rooms.delete(id)
1418
+ }
1419
+ for (const [id, passes] of rooms) this._look(from, id, passes)
1420
+ }
1421
+
1422
+ // one look a room for the passes that name it, begun again only once they are others
1423
+ /** @private */
1424
+ _look(from, id, passes) {
1425
+ const { rooms } = this._looks.get(from)
1426
+ const rows = passes.map((entry) => entry.row.id).join()
1427
+ const was = rooms.get(id)
1428
+ if (was?.rows === rows) return
1429
+ was?.stop.abort()
1430
+ const look = { rows, stop: new AbortController() }
1431
+ rooms.set(id, look)
1432
+ this._watch(from, id, passes, look).catch((err) => this.closing || this._onerror(err))
1433
+ }
1434
+
1435
+ // the room its passes name, open as nobody until each has its end, each through the keys it
1436
+ // holds: its invite is read there before anything is written. The newest she can still take is
1437
+ // joined; one she sits in already, or whose invite ended with her in, goes; one that ended waits
1438
+ // for every newer one to show; one that opens nothing keeps no other waiting
1439
+ /** @private */
1440
+ async _watch(from, id, passes, look) {
1441
+ const { signal } = look.stop
1442
+ const readers = passes.map(({ type, pass }) =>
1443
+ this._apart(type, pass, Identity.randomKeyPair())
1444
+ )
1445
+ const left = new Map()
1446
+ let taken = null
1447
+ try {
1448
+ const ready = await Promise.allSettled(readers.map((reader) => reader.ready()))
1449
+ for (const [i, { status, reason }] of ready.entries()) {
1450
+ if (status === 'fulfilled') left.set(passes[i], readers[i])
1451
+ else if (!this.closing) this._onerror(reason)
1452
+ }
1453
+ const read = async () => {
1454
+ let unseen = false
1455
+ for (const [entry, reader] of left) {
1456
+ const { store } = reader
1457
+ // a join of the room that ended since, under its invite, is its end
1458
+ if (await this._spent(from, entry.row, entry.pass)) {
1459
+ left.delete(entry)
1460
+ continue
1461
+ }
1462
+ const inviteId = b4a.toHex(Invite.parse(entry.pass.invite).id)
1463
+ const live = !!(await store.get('invites', inviteId)).data
1464
+ if (!live && !(await store.view.get(`@${store.ns}/${SPENT}`, { id: inviteId }))) {
1465
+ unseen = true
1466
+ continue
1467
+ }
1468
+ // a seat lost, or one that never counted there, ends on the root before she asks again: the
1469
+ // room holds neither her nor a promise to her at a seat made after she was out. Only an invite
1470
+ // she can still take says so: one that ended is read in a room that may not hold her join yet
1471
+ const seated = await promised(store, this.identity)
1472
+ await this._ousted(reader, live && !seated)
1473
+ const { data: row } = await this.store.get('handles', id)
1474
+ if (row && (row.state || 'in') === 'in' && (seated || !live)) {
1475
+ left.delete(entry)
1476
+ await this._delPass(from, entry.row)
1477
+ } else if (live || !unseen) {
1478
+ taken = entry
1479
+ return true
1480
+ }
1481
+ }
1482
+ return left.size === 0
1438
1483
  }
1439
- join.done.then(next, next).catch((err) => this.closing || this._onerror(err))
1484
+ const stores = [...left.values()].map((reader) => reader.store)
1485
+ await until(stores, read, signal)
1486
+ } finally {
1487
+ await Promise.all(readers.map((reader) => reader._end().catch(safetyCatch)))
1440
1488
  }
1441
- if (waiting) this._retry(from)
1489
+ if (!taken || signal.aborted) return
1490
+ const join = this._start(taken.pass.invite, taken.type, taken.pass)
1491
+ const again = () => {
1492
+ const mine = this._looks.get(from)
1493
+ if (!mine) return
1494
+ // the room's join ended under another invite: these are looked at again
1495
+ if (join.invite !== taken.pass.invite && mine.rooms.get(id) === look) mine.rooms.delete(id)
1496
+ return this._passes(from)
1497
+ }
1498
+ join.done.then(again, again).catch((err) => this.closing || this._onerror(err))
1442
1499
  }
1443
1500
 
1444
- // a pass waiting in a room is read again in TIMEOUT: its room may show late, or its join have
1445
- // settled nothing
1446
1501
  /** @private */
1447
- _retry(from) {
1448
- if (this._retries.has(from) || from.closing || from.closed) return
1449
- const timer = setTimeout(() => {
1450
- this._retries.delete(from)
1451
- if (from.closing || from.closed) return
1452
- this._passes(from).catch((err) => this.closing || this._onerror(err))
1453
- }, TIMEOUT)
1454
- timer.unref?.()
1455
- this._retries.set(from, timer)
1502
+ async _delPass(from, row) {
1503
+ if (from.store.writable && !from.closing && !from.closed) {
1504
+ await from.store.call('del-pass', { id: row.id })
1505
+ }
1456
1506
  }
1457
1507
 
1458
1508
  // a pass goes once its join is in or ended, or it ran out. A room in under another invite is a
@@ -1463,9 +1513,7 @@ export class Handle extends ReadyResource {
1463
1513
  const state = was && (was.state || 'in')
1464
1514
  const done = was?.invite === pass.invite && (state === 'in' || SWEPT.includes(state))
1465
1515
  if (!done && !Invite.parse(pass.invite).expired) return false
1466
- if (from.store.writable && !from.closing && !from.closed) {
1467
- await from.store.call('del-pass', { id: row.id })
1468
- }
1516
+ await this._delPass(from, row)
1469
1517
  return true
1470
1518
  }
1471
1519
 
@@ -1901,11 +1949,13 @@ export class Handle extends ReadyResource {
1901
1949
  child._onclose(() => child.store.off('writable', prune))
1902
1950
  prune()
1903
1951
  this._seats(child)
1952
+ const looks = { turn: 0, rooms: new Map() }
1953
+ this._looks.set(child, looks)
1904
1954
  const take = () => this._passes(child).catch((err) => this.closing || this._onerror(err))
1905
1955
  child._onclose(child.store.onUpdate(PASSES, take))
1906
1956
  child._onclose(() => {
1907
- clearTimeout(this._retries.get(child))
1908
- this._retries.delete(child)
1957
+ this._looks.delete(child)
1958
+ for (const look of looks.rooms.values()) look.stop.abort()
1909
1959
  })
1910
1960
  take()
1911
1961
  // what a listener ties to the handle as it runs is no holder's: it stays through their last close
@@ -2508,27 +2558,30 @@ function listed(rows, query = {}) {
2508
2558
  return { data, total: data.length, size: data.length }
2509
2559
  }
2510
2560
 
2511
- // whether `check` holds of a room within `ms` as its view updates: false once past, or stopped
2512
- function until(db, check, ms, signal) {
2513
- return new Promise((resolve, reject) => {
2514
- const done = (err, found) => {
2515
- clearTimeout(timer)
2516
- db.off('update', test)
2517
- db.off('close', closed)
2518
- signal.removeEventListener('abort', stop)
2519
- if (err) reject(err)
2520
- else resolve(found)
2521
- }
2522
- const test = () => check().then((found) => found && done(null, true), done)
2523
- const stop = () => done(null, false)
2524
- const closed = () => done(CeroError.CLOSED('Database'))
2525
- const timer = setTimeout(stop, ms)
2526
- db.on('update', test)
2527
- db.once('close', closed)
2528
- signal.addEventListener('abort', stop)
2529
- if (signal.aborted) stop()
2530
- else test()
2531
- })
2561
+ // whether `check` comes to hold as the views of these rooms update, read one at a time: false once stopped
2562
+ async function until(dbs, check, signal) {
2563
+ let wake = null
2564
+ const heard = () => wake?.()
2565
+ for (const db of dbs) {
2566
+ db.on('update', heard)
2567
+ db.on('close', heard)
2568
+ }
2569
+ signal.addEventListener('abort', heard)
2570
+ try {
2571
+ while (!signal.aborted) {
2572
+ // listened for before the read: an update during it is read after
2573
+ const next = new Promise((resolve) => (wake = resolve))
2574
+ if (await check()) return true
2575
+ await next
2576
+ }
2577
+ return false
2578
+ } finally {
2579
+ for (const db of dbs) {
2580
+ db.off('update', heard)
2581
+ db.off('close', heard)
2582
+ }
2583
+ signal.removeEventListener('abort', heard)
2584
+ }
2532
2585
  }
2533
2586
 
2534
2587
  // the room turned this device's writer away, under `invite` when one is named
@@ -2537,10 +2590,10 @@ async function turned(db, invite = null) {
2537
2590
  return refusal && (!invite || !refusal.invite || refusal.invite === invite) ? refusal : null
2538
2591
  }
2539
2592
 
2540
- // a seat this device took that never counted: no member row, and its writer turned away
2593
+ // how the room turned away a seat this device took that never counted: no member row, and its writer refused
2541
2594
  async function refused(db, identity) {
2542
- if ((await db.get('members', identity.id)).data) return false
2543
- return !!(await turned(db))
2595
+ if ((await db.get('members', identity.id)).data) return null
2596
+ return turned(db)
2544
2597
  }
2545
2598
 
2546
2599
  // whether a room let this identity in, by any of its devices
@@ -12,3 +12,6 @@ export const TIMEOUT = 30000
12
12
 
13
13
  // How many handles nobody holds stay open for a reopen.
14
14
  export const KEEP_OPEN = 16
15
+
16
+ // How many passes left by one member a device looks at, at once.
17
+ export const LOOKS = 16
@@ -383,10 +383,14 @@ export async function open(ref, arg = {}) {
383
383
  const OPENS = new Set(['invite', 'id', 'name'])
384
384
 
385
385
  /**
386
- * Mint an invite code into a handle.
386
+ * Mint an invite code into a handle. With `from`, a handle you and the member it is made `to` are
387
+ * both in, nobody passes the code on: her devices find it in `from` while they have it open, join
388
+ * on their own, and the handle shows up in her list. Taken back with `revoke`. With `from` it
389
+ * rejects `DENIED` when you cannot write in `from`, `UNKNOWN` when she or none of her devices is in
390
+ * `from`, `INVALID` when she is a member already or has one waiting.
387
391
  *
388
392
  * @param {Context} ctx
389
- * @param {import('@cero-base/core/pairing').InviteOpts} [opts]
393
+ * @param {import('@cero-base/core/pairing').InviteOpts & { from?: Context }} [opts]
390
394
  * @returns {Promise<string>}
391
395
  */
392
396
  export async function invite(ctx, opts = {}) {
@@ -469,24 +473,6 @@ export function removed(ctx, id) {
469
473
  return ctx._removed(id)
470
474
  }
471
475
 
472
- /**
473
- * Let a member of another handle in, with no invite to pass on: her devices find it in `from` while
474
- * they have it open, join on their own, and the handle shows up in her list. Taken back with
475
- * `revoke` on the `invites` row whose `to` is her id. Rejects `DENIED` for a role above your own or
476
- * when you cannot write in `from`, `UNKNOWN` when she or none of her devices is in `from`, `INVALID`
477
- * when she is a member already.
478
- *
479
- * @param {Context} ctx The handle she is let into.
480
- * @param {string} id Her member id.
481
- * @param {{ from: Context, role?: string, ttl?: number | string }} opts `from`: a handle both are in. `role` is `member` by default.
482
- * @returns {Promise<void>}
483
- */
484
- export async function seat(ctx, id, opts) {
485
- // checked before anything crosses to the worker, which carries `from` as its id
486
- if (!isOptions(opts)) throw CeroError.INVALID('seat options are an object: { from }')
487
- return ctx._seat(id, opts)
488
- }
489
-
490
476
  /**
491
477
  * Quit a handle for good: its row reads `leaving`, then `left` once the handle holds it, and it closes.
492
478
  *
package/src/rpc/client.js CHANGED
@@ -31,7 +31,6 @@ export const {
31
31
  deny,
32
32
  leave,
33
33
  removed,
34
- seat,
35
34
  close,
36
35
  cancel,
37
36
  suspend,
@@ -350,9 +349,10 @@ const operators = {
350
349
 
351
350
  // the verbs in lib/operators.js land here, and cross to the worker, which runs the same operator
352
351
 
352
+ // the handle it is delivered `from` crosses as its id
353
353
  /** @private */
354
354
  _invite(opts) {
355
- return this._verb('invite', opts)
355
+ return this._verb('invite', { ...opts, from: opts.from?.id ?? opts.from })
356
356
  },
357
357
 
358
358
  /** @private */
@@ -380,11 +380,6 @@ const operators = {
380
380
  return this._verb('removed', id)
381
381
  },
382
382
 
383
- /** @private */
384
- _seat(id, { from, ...opts }) {
385
- return this._verb('seat', id, { ...opts, from: from?.id ?? from })
386
- },
387
-
388
383
  /** @private */
389
384
  _cancel(invite) {
390
385
  return this._verb('cancel', invite)
package/src/rpc/server.js CHANGED
@@ -18,7 +18,6 @@ import {
18
18
  revoke,
19
19
  rotate,
20
20
  removed,
21
- seat,
22
21
  accept,
23
22
  deny,
24
23
  cancel,
@@ -40,7 +39,6 @@ const VERBS = new Map(
40
39
  revoke,
41
40
  rotate,
42
41
  removed,
43
- seat,
44
42
  accept,
45
43
  deny,
46
44
  cancel,
@@ -29,7 +29,9 @@ export declare const cero: {
29
29
  id?: string;
30
30
  name?: string;
31
31
  } | undefined): Promise<operators.Context>;
32
- invite(ctx: operators.Context, opts?: import("@cero-base/core").InviteOpts): Promise<string>;
32
+ invite(ctx: operators.Context, opts?: import("@cero-base/core").InviteOpts & {
33
+ from?: operators.Context;
34
+ }): Promise<string>;
33
35
  revoke(ctx: operators.Context, code: string): Promise<boolean>;
34
36
  rotate(ctx: operators.Context): Promise<{
35
37
  epoch: number;
@@ -44,11 +46,6 @@ export declare const cero: {
44
46
  id: string;
45
47
  }, reason?: string): Promise<void>;
46
48
  removed(ctx: operators.Context, id: string): Promise<'removed' | 'left' | null>;
47
- seat(ctx: operators.Context, id: string, opts: {
48
- from: operators.Context;
49
- role?: string;
50
- ttl?: number | string;
51
- }): Promise<void>;
52
49
  leave(ctx: operators.Context): Promise<void>;
53
50
  close(ctx: operators.Context): Promise<void>;
54
51
  cancel(me: operators.Context, code: string): Promise<boolean>;
@@ -191,9 +191,7 @@ export declare class Handle extends ReadyResource {
191
191
  /** @private */
192
192
  _taking;
193
193
  /** @private */
194
- _held;
195
- /** @private */
196
- _retries;
194
+ _looks;
197
195
  /** @private */
198
196
  _coreKeys;
199
197
  /** @private */
@@ -335,8 +333,6 @@ export declare class Handle extends ReadyResource {
335
333
  private _depart;
336
334
  /** @private */
337
335
  private _ousted;
338
- /** @private */
339
- private _repass;
340
336
  /**
341
337
  * One holder done with it: a child stays open for the others. With its last, what they tied to it ends and it
342
338
  * stays open, idle, so a reopen finds it as it is; the root keeps `keepOpen` of them (16 by default) and one more
@@ -462,11 +458,17 @@ export declare class Handle extends ReadyResource {
462
458
  /** @private */
463
459
  private _run;
464
460
  /** @private */
461
+ private _apart;
462
+ /** @private */
465
463
  private _take;
466
464
  /** @private */
467
465
  private _passes;
468
466
  /** @private */
469
- private _retry;
467
+ private _look;
468
+ /** @private */
469
+ private _watch;
470
+ /** @private */
471
+ private _delPass;
470
472
  /** @private */
471
473
  private _spent;
472
474
  /** @private */
@@ -8,3 +8,4 @@ export declare const ASKS = "owed";
8
8
  export declare const PASSES = "seats";
9
9
  export declare const TIMEOUT = 30000;
10
10
  export declare const KEEP_OPEN = 16;
11
+ export declare const LOOKS = 16;
@@ -126,13 +126,19 @@ export declare function open(ref: Ref, arg?: string | {
126
126
  name?: string;
127
127
  } | undefined): Promise<Context>;
128
128
  /**
129
- * Mint an invite code into a handle.
129
+ * Mint an invite code into a handle. With `from`, a handle you and the member it is made `to` are
130
+ * both in, nobody passes the code on: her devices find it in `from` while they have it open, join
131
+ * on their own, and the handle shows up in her list. Taken back with `revoke`. With `from` it
132
+ * rejects `DENIED` when you cannot write in `from`, `UNKNOWN` when she or none of her devices is in
133
+ * `from`, `INVALID` when she is a member already or has one waiting.
130
134
  *
131
135
  * @param {Context} ctx
132
- * @param {import('@cero-base/core/pairing').InviteOpts} [opts]
136
+ * @param {import('@cero-base/core/pairing').InviteOpts & { from?: Context }} [opts]
133
137
  * @returns {Promise<string>}
134
138
  */
135
- export declare function invite(ctx: Context, opts?: import('@cero-base/core/pairing').InviteOpts): Promise<string>;
139
+ export declare function invite(ctx: Context, opts?: import('@cero-base/core/pairing').InviteOpts & {
140
+ from?: Context;
141
+ }): Promise<string>;
136
142
  /**
137
143
  * Kill an invite, for every member.
138
144
  *
@@ -197,23 +203,6 @@ export declare function deny(ctx: Context, request: {
197
203
  * @returns {Promise<'removed' | 'left' | null>}
198
204
  */
199
205
  export declare function removed(ctx: Context, id: string): Promise<'removed' | 'left' | null>;
200
- /**
201
- * Let a member of another handle in, with no invite to pass on: her devices find it in `from` while
202
- * they have it open, join on their own, and the handle shows up in her list. Taken back with
203
- * `revoke` on the `invites` row whose `to` is her id. Rejects `DENIED` for a role above your own or
204
- * when you cannot write in `from`, `UNKNOWN` when she or none of her devices is in `from`, `INVALID`
205
- * when she is a member already.
206
- *
207
- * @param {Context} ctx The handle she is let into.
208
- * @param {string} id Her member id.
209
- * @param {{ from: Context, role?: string, ttl?: number | string }} opts `from`: a handle both are in. `role` is `member` by default.
210
- * @returns {Promise<void>}
211
- */
212
- export declare function seat(ctx: Context, id: string, opts: {
213
- from: Context;
214
- role?: string;
215
- ttl?: number | string;
216
- }): Promise<void>;
217
206
  /**
218
207
  * Quit a handle for good: its row reads `leaving`, then `left` once the handle holds it, and it closes.
219
208
  *
@@ -22,7 +22,9 @@ declare const remote: {
22
22
  id?: string;
23
23
  name?: string;
24
24
  } | undefined): Promise<verbs.Context>;
25
- invite(ctx: verbs.Context, opts?: import("@cero-base/core").InviteOpts): Promise<string>;
25
+ invite(ctx: verbs.Context, opts?: import("@cero-base/core").InviteOpts & {
26
+ from?: verbs.Context;
27
+ }): Promise<string>;
26
28
  revoke(ctx: verbs.Context, code: string): Promise<boolean>;
27
29
  rotate(ctx: verbs.Context): Promise<{
28
30
  epoch: number;
@@ -36,11 +38,6 @@ declare const remote: {
36
38
  id: string;
37
39
  }, reason?: string): Promise<void>;
38
40
  removed(ctx: verbs.Context, id: string): Promise<'removed' | 'left' | null>;
39
- seat(ctx: verbs.Context, id: string, opts: {
40
- from: verbs.Context;
41
- role?: string;
42
- ttl?: number | string;
43
- }): Promise<void>;
44
41
  leave(ctx: verbs.Context): Promise<void>;
45
42
  close(ctx: verbs.Context): Promise<void>;
46
43
  cancel(me: verbs.Context, code: string): Promise<boolean>;
@@ -71,7 +68,7 @@ declare const remote: {
71
68
  topic?: string;
72
69
  }): Promise<void>;
73
70
  };
74
- export declare const put: typeof verbs.put, set: typeof verbs.set, get: typeof verbs.get, del: typeof verbs.del, watch: typeof verbs.watch, call: typeof verbs.call, supports: typeof verbs.supports, open: typeof verbs.open, invite: typeof verbs.invite, revoke: typeof verbs.revoke, rotate: typeof verbs.rotate, accept: typeof verbs.accept, deny: typeof verbs.deny, leave: typeof verbs.leave, removed: typeof verbs.removed, seat: typeof verbs.seat, close: typeof verbs.close, cancel: typeof verbs.cancel, suspend: typeof verbs.suspend, resume: typeof verbs.resume, activate: typeof verbs.activate, deactivate: typeof verbs.deactivate, phrase: typeof verbs.phrase, nearby: typeof verbs.nearby;
71
+ export declare const put: typeof verbs.put, set: typeof verbs.set, get: typeof verbs.get, del: typeof verbs.del, watch: typeof verbs.watch, call: typeof verbs.call, supports: typeof verbs.supports, open: typeof verbs.open, invite: typeof verbs.invite, revoke: typeof verbs.revoke, rotate: typeof verbs.rotate, accept: typeof verbs.accept, deny: typeof verbs.deny, leave: typeof verbs.leave, removed: typeof verbs.removed, close: typeof verbs.close, cancel: typeof verbs.cancel, suspend: typeof verbs.suspend, resume: typeof verbs.resume, activate: typeof verbs.activate, deactivate: typeof verbs.deactivate, phrase: typeof verbs.phrase, nearby: typeof verbs.nearby;
75
72
  export { t, schema };
76
73
  export type BaseRPCClient = import('@cero-base/core/rpc').RPCClient;
77
74
  export type RefInfo = import('../lib/spec.js').RefInfo;