@cero-base/cero 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/cero",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
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.0.0",
116
+ "@cero-base/core": "^3.1.0",
117
117
  "b4a": "^1.9.0",
118
118
  "bare-abort-controller": "^1.1.2",
119
119
  "bare-crypto": "^1.15.4",
@@ -1,5 +1,5 @@
1
1
  import { CeroError } from '@cero-base/core/errors'
2
- import { COUNTERS, EPOCHS, REMOVALS, REFUSALS, SPENT, ASKS } from '../lib/constants.js'
2
+ import { COUNTERS, EPOCHS, REMOVALS, REFUSALS, SPENT, ASKS, PASSES } from '../lib/constants.js'
3
3
  import * as schemas from './schemas.js'
4
4
 
5
5
  // internal refs by scope; kind defaults to collection
@@ -175,7 +175,10 @@ export function collections(ns, scope) {
175
175
  // collections cero added once apps shipped: numbered after the app's collections and indexes, so
176
176
  // a new spec gives each of those the id an older release gave it
177
177
  export function added(ns) {
178
- return [{ name: ASKS, schema: `@${ns}/owed`, key: ['id'] }]
178
+ return [
179
+ { name: ASKS, schema: `@${ns}/owed`, key: ['id'] },
180
+ { name: PASSES, schema: `@${ns}/pass`, key: ['id'] }
181
+ ]
179
182
  }
180
183
 
181
184
  /**
@@ -227,7 +230,9 @@ export function appended(ns) {
227
230
  { name: 'revoke', requestType: `@${ns}/revoke` },
228
231
  { name: 'ask-key', requestType: `@${ns}/owed` },
229
232
  { name: 'del-owed', requestType: `@${ns}/del-by-id` },
230
- { name: 'cancel-join', requestType: `@${ns}/del-by-id` }
233
+ { name: 'cancel-join', requestType: `@${ns}/del-by-id` },
234
+ { name: 'add-pass', requestType: `@${ns}/pass` },
235
+ { name: 'del-pass', requestType: `@${ns}/del-by-id` }
231
236
  ]
232
237
  }
233
238
 
@@ -57,7 +57,9 @@ export const main = {
57
57
  confirm: bool,
58
58
  // the secret behind its own address: every device that has the row opens its joins and proves
59
59
  // their answers
60
- secret: bytes
60
+ secret: bytes,
61
+ // the member it was made for: nobody else joins with it
62
+ to: string
61
63
  },
62
64
  handle: {
63
65
  id: required(string),
@@ -222,6 +224,15 @@ export const main = {
222
224
  removed: bytes,
223
225
  standing: bytes,
224
226
  proof: bytes
227
+ },
228
+ // an invite into another room and that room's keys, sealed to the devices of the member it is
229
+ // `to`, waiting here until one of them takes it. `by` is the member who wrote it
230
+ pass: {
231
+ id: required(string),
232
+ to: required(string),
233
+ by: string,
234
+ pass: required(bytes),
235
+ createdAt: uint
225
236
  }
226
237
  }
227
238
 
@@ -335,7 +346,7 @@ export const local = {
335
346
 
336
347
  // what a write means to apply. A release that changes it without changing what is stored bumps
337
348
  // this, so an app's next build raises its contract and devices on the old release skip the new writes
338
- export const contract = { version: 4 }
349
+ export const contract = { version: 5 }
339
350
 
340
351
  export const rpc = {
341
352
  'req-empty': {
@@ -29,6 +29,9 @@ export async function mirror(from, row, fields, to, id) {
29
29
  changed ||= (was[k] ?? null) !== (row[k] ?? null)
30
30
  } else if (prev && (await get(to.handle.files, prev)).data?.from === file) {
31
31
  out[k] = prev
32
+ } else if (!to.handle.store.writable) {
33
+ // a file takes a seat to add: a reader's row keeps the one it has, and takes the rest
34
+ out[k] = prev
32
35
  } else {
33
36
  const data = await from.handle._bytes(file)
34
37
  const { type } = decodeId(file)
@@ -10,7 +10,7 @@ import z32 from 'z32'
10
10
  import { Identity } from '@cero-base/core/identity'
11
11
  import { Database } from '@cero-base/core/database'
12
12
  import { blobEpochKey, epochEntries } from '@cero-base/core/database/encryption'
13
- import { Pairing } from '@cero-base/core/pairing'
13
+ import { Pairing, Pass } from '@cero-base/core/pairing'
14
14
  import { read } from '../push/index.js'
15
15
  import { Invite } from '@cero-base/core/invite'
16
16
  import { Mailbox } from '@cero-base/core/mailbox'
@@ -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, TIMEOUT } from '../lib/constants.js'
25
+ import { KEEP_OPEN, NS, PASSES, SPENT, TIMEOUT } from '../lib/constants.js'
26
26
 
27
27
  import { Ref } from '../lib/refs.js'
28
28
  import { box } from '../local/index.js'
@@ -479,6 +479,56 @@ export class Handle extends ReadyResource {
479
479
  return this.store.removed(id)
480
480
  }
481
481
 
482
+ // an invite made to her, sealed with this room's keys to her devices in a room both are in, where
483
+ // they find it. `from` crosses the bridge as its id
484
+ /** @private */
485
+ async _seat(id, { from, role = 'member', ttl } = {}) {
486
+ if (!this.parent) throw CeroError.INVALID('a device is linked, not seated')
487
+ if (typeof from === 'string') {
488
+ const { data: row } = await this.root.store.get('handles', from)
489
+ if (!row) throw CeroError.UNKNOWN('handle', from)
490
+ const shared = await this.root._load(row.type, from)
491
+ try {
492
+ return await this._seat(id, { from: shared, role, ttl })
493
+ } finally {
494
+ await shared.close()
495
+ }
496
+ }
497
+ if (!(from instanceof Handle) || !from.parent) {
498
+ throw CeroError.INVALID('seat takes { from }: a room she is in')
499
+ }
500
+ const { store } = from
501
+ const { data: me } = await store.get('members', this.identity.id)
502
+ if (!can(me?.role, WRITE)) throw CeroError.DENIED(null, `you cannot write in ${from.id}`)
503
+ // a member's devices, which a removal takes with her
504
+ const devices = (await store.get('devices')).data.filter((d) => d.memberId === id && d.key)
505
+ if (!devices.length) throw CeroError.UNKNOWN('member', id)
506
+ // one seat at a time: in, let in and not yet claimed, or seated and not come yet
507
+ if (await promised(this.store, { id, publicKey: hid.decode(id) })) {
508
+ throw CeroError.INVALID(`${id} is in already`)
509
+ }
510
+ const { data: invites } = await this.store.get('invites')
511
+ if (invites.some((i) => i.to === id && !(i.expires && i.expires <= Date.now()))) {
512
+ throw CeroError.INVALID(`${id} has a seat already`)
513
+ }
514
+ // what would keep its pass out of `from` is checked first: an invite minted is live
515
+ const at = Math.min(store.version, await store.dispatcher.version(store.view))
516
+ if ((store.meta.since?.routes[`@${store.ns}/add-pass`] ?? 0) > at) {
517
+ throw CeroError.UNSUPPORTED(`'add-pass' in ${from.id}`)
518
+ }
519
+ const { key, encryptionKey, keyring } = this.store
520
+ const fields = { key, encryptionKey, epochs: keyring.all(), data: b4a.from(this.type) }
521
+ if (!Pass.fits(devices, fields)) {
522
+ throw CeroError.INVALID(
523
+ `this room has too many keys to seal to her ${devices.length} devices`
524
+ )
525
+ }
526
+ const invite = await this._invite({ role, ttl, to: id })
527
+ const pass = Pass.seal(devices, { ...fields, invite })
528
+ const row = { id: b4a.toHex(Identity.randomBytes(16)), to: id, pass, createdAt: Date.now() }
529
+ await store.call('add-pass', row)
530
+ }
531
+
482
532
  // the same context, its refs writing into one batch; a fn that ignores it would write outside
483
533
  /** @private */
484
534
  async _tx(fn) {
@@ -1179,11 +1229,11 @@ export class Handle extends ReadyResource {
1179
1229
 
1180
1230
  // one join a handle, whoever waits on it: it goes on until its row says how it ended, and a close only pauses it
1181
1231
  /** @private */
1182
- _start(invite, type) {
1232
+ _start(invite, type, pass = null) {
1183
1233
  const id = hid.encode(Invite.parse(invite).key)
1184
1234
  let join = this._joining.get(id)
1185
1235
  if (!join) {
1186
- join = { id, type, invite: null, writer: null, waiting: 0, end: null, running: null }
1236
+ join = { id, type, invite: null, pass, writer: null, waiting: 0, end: null, running: null }
1187
1237
  join.done = new Promise((resolve, reject) => {
1188
1238
  join.resolve = resolve
1189
1239
  join.reject = reject
@@ -1191,7 +1241,12 @@ export class Handle extends ReadyResource {
1191
1241
  join.done.catch(safetyCatch)
1192
1242
  this._joining.set(id, join)
1193
1243
  }
1194
- if (join.invite !== invite) this._run(join, invite)
1244
+ // a join past its reply enters with those keys: a newer invite waits on it, and one waiting
1245
+ // for a reply takes the keys a pass hands it
1246
+ if ((join.invite !== invite || (pass && !join.pass)) && !join.entering) {
1247
+ join.pass = pass
1248
+ this._run(join, invite)
1249
+ }
1195
1250
  return join
1196
1251
  }
1197
1252
 
@@ -1207,7 +1262,9 @@ export class Handle extends ReadyResource {
1207
1262
  end(value)
1208
1263
  }
1209
1264
  try {
1210
- const id = await this._ask(join.type, invite)
1265
+ const { pass } = join
1266
+ const taken = pass && (await this._take(join, invite, running.signal))
1267
+ const id = pass ? hid.encode(pass.key) : await this._ask(join.type, invite)
1211
1268
  const writer = (join.writer = await this._loadKeyPair(id))
1212
1269
  const { data: row } = await this.store.get('handles', id)
1213
1270
  // the keys are on the row before the reply is let go: a restart opens the handle without joining again
@@ -1217,11 +1274,16 @@ export class Handle extends ReadyResource {
1217
1274
  const spec = pickHandle(this.spec, join.type)
1218
1275
  const { identity, mailbox } = this
1219
1276
  const opts = { identity, spec, writer, keep, timeout: 0, signal: running.signal }
1220
- const reply = row.encryptionKey
1221
- ? { ...row, epochs: row.epochs ? c.decode(epochEntries, row.epochs) : [], writer }
1222
- : await Pairing.join(mailbox, invite, opts)
1277
+ const reply = taken
1278
+ ? { ...pass, writer }
1279
+ : row.encryptionKey
1280
+ ? { ...row, epochs: row.epochs ? c.decode(epochEntries, row.epochs) : [], writer }
1281
+ : await Pairing.join(mailbox, invite, opts)
1282
+ // an abort stops no reply already in hand: the newer attempt enters
1283
+ if (running !== join.running) return taken?._end()
1284
+ join.entering = true
1223
1285
  await this._stand(id, 'joining', keys(reply))
1224
- const child = await this._enter(join.type, reply, invite)
1286
+ const child = await this._enter(join.type, reply, invite, taken)
1225
1287
  // every waiter takes its own hold once the row reads in: this one is let go, idle until the app opens it
1226
1288
  child.close().catch(this._onerror)
1227
1289
  settle(join.resolve, child)
@@ -1235,11 +1297,97 @@ export class Handle extends ReadyResource {
1235
1297
  if (end.code !== 'CLOSED') {
1236
1298
  await this._over(join.id, end, { invite, writer: join.writer }).catch(report)
1237
1299
  }
1238
- if (err.code !== 'CLOSED' && alone) this._onerror(err)
1300
+ // a pass whose room did not show in time is tried again, nothing on the root
1301
+ if (err.code !== 'CLOSED' && err.code !== 'TIMEOUT' && alone) this._onerror(err)
1239
1302
  settle(join.reject, end)
1240
1303
  }
1241
1304
  }
1242
1305
 
1306
+ // a pass's join: its invite read in the room its keys open, as nobody, before anything is written;
1307
+ // then asked, written and seated by this device's own claim. Null when that takes longer: it goes
1308
+ // on as a plain join
1309
+ /** @private */
1310
+ async _take(join, invite, signal) {
1311
+ const { type, pass } = join
1312
+ const id = hid.encode(pass.key)
1313
+ const spec = pickHandle(this.spec, type)
1314
+ const { key, encryptionKey, epochs } = pass
1315
+ const room = (keyPair) => {
1316
+ const namespace = `${NS}/handle/${type}/${randomNs()}`
1317
+ return this._room({ spec, namespace, key, encryptionKey, epochs, keyPair })
1318
+ }
1319
+ const reader = room(Identity.randomKeyPair())
1320
+ try {
1321
+ await reader.ready()
1322
+ const { store } = reader
1323
+ const inviteId = b4a.toHex(Invite.parse(invite).id)
1324
+ const shows = async () =>
1325
+ !!(await store.get('invites', inviteId)).data ||
1326
+ !!(await store.view.get(`@${store.ns}/${SPENT}`, { id: inviteId }))
1327
+ if (!(await until(store, shows, TIMEOUT, signal))) {
1328
+ throw signal.aborted ? CeroError.CLOSED('join') : CeroError.TIMEOUT('join')
1329
+ }
1330
+ } finally {
1331
+ await reader._end().catch(safetyCatch)
1332
+ }
1333
+ const { data: kept } = await this.local.store.get('handle-keypairs', id)
1334
+ const { publicKey, secretKey } = kept?.invite === invite ? kept : Identity.randomKeyPair()
1335
+ const writer = (join.writer = { publicKey, secretKey })
1336
+ // asked before its join is written: a retry, or the latest ask, finds its writer
1337
+ await this._ask(type, invite, writer)
1338
+ await Pairing.write(this.mailbox, invite, { identity: this.identity, spec, writer })
1339
+ const child = room(writer)
1340
+ try {
1341
+ await child.ready()
1342
+ const { store } = child
1343
+ const err = await store.claim({ timeout: TIMEOUT }).catch((err) => err)
1344
+ if (signal.aborted) throw CeroError.CLOSED('join')
1345
+ if (!err) return child
1346
+ // another device of hers took the invite first: this one sits beside it, on a writer of its own
1347
+ if (err.code === 'DENIED' && (await promised(store, this.identity))) {
1348
+ await this._saveKeyPair(id, Identity.randomKeyPair(), invite)
1349
+ await this._stand(id, 'joining', keys(pass))
1350
+ } else if (err.code !== 'TIMEOUT') {
1351
+ throw err
1352
+ }
1353
+ } catch (err) {
1354
+ await child._end().catch(safetyCatch)
1355
+ throw err
1356
+ }
1357
+ await child._end()
1358
+ return null
1359
+ }
1360
+
1361
+ // the passes this device opens in a room open here: each starts its join
1362
+ /** @private */
1363
+ async _passes(from) {
1364
+ const { store } = from
1365
+ const rows = await store.view.find(`@${store.ns}/${PASSES}`, {}).toArray()
1366
+ for (const row of rows) {
1367
+ if (row.to !== this.identity.id) continue
1368
+ const pass = opens(this.identity, row.pass)
1369
+ const type = pass?.data && b4a.toString(pass.data)
1370
+ if (!type || !this.spec.handles?.[type]) continue
1371
+ if (await this._spent(from, row, pass)) continue
1372
+ const spend = () =>
1373
+ this._spent(from, row, pass).catch((err) => this.closing || this._onerror(err))
1374
+ this._start(pass.invite, type, pass).done.then(spend, spend)
1375
+ }
1376
+ }
1377
+
1378
+ // a pass goes once its room is on the root, the join under its invite ended, or it ran out
1379
+ /** @private */
1380
+ async _spent(from, row, pass) {
1381
+ const { data: was } = await this.store.get('handles', hid.encode(pass.key))
1382
+ const state = was && (was.state || 'in')
1383
+ const ended = SWEPT.includes(state) && was.invite === pass.invite
1384
+ if (state !== 'in' && !ended && !Invite.parse(pass.invite).expired) return false
1385
+ if (from.store.writable && !from.closing && !from.closed) {
1386
+ await from.store.call('del-pass', { id: row.id })
1387
+ }
1388
+ return true
1389
+ }
1390
+
1243
1391
  // ends it for good from any device: its row reads cancelled, so no device and no restart carries it on
1244
1392
  /** @private */
1245
1393
  async _cancel(invite) {
@@ -1267,12 +1415,24 @@ export class Handle extends ReadyResource {
1267
1415
  if (mine && asks && row.writer === ask.id) return
1268
1416
  // a seated row names no writer
1269
1417
  const ended = row && !asks && state !== 'cancelled' && (!row.writer || row.writer === ask.id)
1270
- if (ended && row.invite === ask.invite) return this.local.store.del('asks', ask.id)
1418
+ const same = ended && row.invite === ask.invite
1419
+ // let in on another device, this one's join stops and its writer, which may never have claimed,
1420
+ // goes: the room opens here as on any device of hers
1421
+ const elsewhere =
1422
+ same &&
1423
+ state === 'in' &&
1424
+ !this._loading.has(ask.handle) &&
1425
+ ![...this.children].some((c) => c.id === ask.handle)
1426
+ if (same && !elsewhere) return this.local.store.del('asks', ask.id)
1271
1427
  // only the join that asks with this writer stops: one still choosing its writer may ask anew
1272
1428
  const join = this._joining.get(ask.handle)
1273
1429
  if (join?.writer && b4a.equals(join.writer.publicKey, ask.publicKey)) {
1274
1430
  this._stop(join, CeroError.CANCELLED())
1275
1431
  }
1432
+ if (elsewhere) {
1433
+ await this._forget(ask.handle, ask)
1434
+ return this.local.store.del('asks', ask.id)
1435
+ }
1276
1436
  if (this._taking.has(ask.id)) return
1277
1437
  this._taking.add(ask.id)
1278
1438
  this._takeBack(ask)
@@ -1327,7 +1487,7 @@ export class Handle extends ReadyResource {
1327
1487
 
1328
1488
  // a join's row: `asking` with its invite and no keys. A join that ended under this invite ends the same way again
1329
1489
  /** @private */
1330
- async _ask(type, invite) {
1490
+ async _ask(type, invite, given = null) {
1331
1491
  const { key } = Invite.parse(invite)
1332
1492
  const id = hid.encode(key)
1333
1493
  const { data: row } = await this.store.get('handles', id)
@@ -1342,7 +1502,8 @@ export class Handle extends ReadyResource {
1342
1502
  const mine = !!kept?.invite && (state === 'joining' || latest)
1343
1503
  if (mine && (state === 'joining' || row.invite === invite)) return id
1344
1504
  if (ENDS[state] && row.invite === invite) throw ENDS[state](row.reason)
1345
- const writer = mine ? kept : Identity.randomKeyPair()
1505
+ // a pass's join asks with the writer its join is already written with
1506
+ const writer = mine ? kept : given || Identity.randomKeyPair()
1346
1507
  await this._saveKeyPair(id, writer, invite)
1347
1508
  // a room another device of this identity already has the keys of is joined with them
1348
1509
  if (state === 'joining') return id
@@ -1427,18 +1588,20 @@ export class Handle extends ReadyResource {
1427
1588
  * @returns {Promise<Handle>}
1428
1589
  * @private
1429
1590
  */
1430
- async _enter(type, reply, invite) {
1591
+ async _enter(type, reply, invite, taken = null) {
1431
1592
  const id = hid.encode(reply.key)
1432
1593
  // a room this identity lost its seat in stays open on its old writer: it ends before the new seat opens it
1433
1594
  const stale = [...this.children].find((c) => c.id === id && !c.closing)
1434
1595
  const out = stale && (await stale.store.removed(this.identity.id))
1435
1596
  if (out) await stale._end().catch(safetyCatch)
1597
+ // a pass's join is already open, seated
1436
1598
  const child = /** @type {Child} */ (
1437
- this._room({
1438
- spec: pickHandle(this.spec, type),
1439
- namespace: `${NS}/handle/${type}/${randomNs()}`,
1440
- ...delivered(reply)
1441
- })
1599
+ taken ||
1600
+ this._room({
1601
+ spec: pickHandle(this.spec, type),
1602
+ namespace: `${NS}/handle/${type}/${randomNs()}`,
1603
+ ...delivered(reply)
1604
+ })
1442
1605
  )
1443
1606
  // its row is listed while it joins: an open by id shares this child, a second on the same core deadlocks
1444
1607
  let publish = null
@@ -1460,6 +1623,8 @@ export class Handle extends ReadyResource {
1460
1623
  }
1461
1624
 
1462
1625
  await this._saveKeyPair(id, child.store.keyPair, invite)
1626
+ // seated on its own writer: its ask is answered, never read as one let in on another device
1627
+ await this.local.store.del('asks', hid.encode(child.store.keyPair.publicKey))
1463
1628
 
1464
1629
  const ts = Date.now()
1465
1630
  await this.store.call('add-handle', {
@@ -1648,6 +1813,9 @@ export class Handle extends ReadyResource {
1648
1813
  child.once('close', () => child.store.off('writable', prune))
1649
1814
  prune()
1650
1815
  this._seats(child)
1816
+ const take = () => this._passes(child).catch((err) => this.closing || this._onerror(err))
1817
+ child.once('close', child.store.onUpdate(PASSES, take))
1818
+ take()
1651
1819
  // what a listener ties to the handle as it runs is no holder's: it stays through their last close
1652
1820
  child._arriving = true
1653
1821
  try {
@@ -1664,7 +1832,7 @@ export class Handle extends ReadyResource {
1664
1832
  const onchange = (touched) => {
1665
1833
  if (touched.has('*') || touched.has('devices') || touched.has('members')) {
1666
1834
  this._index(child).catch(this._onerror)
1667
- this._ousted(child).catch(this._onerror)
1835
+ this._ousted(child).catch((err) => this.closing || this._onerror(err))
1668
1836
  }
1669
1837
  }
1670
1838
  child.store.on('update', onchange)
@@ -2245,6 +2413,45 @@ function listed(rows, query = {}) {
2245
2413
  return { data, total: data.length, size: data.length }
2246
2414
  }
2247
2415
 
2416
+ // whether `check` holds of a room within `ms` as its view updates: false once past, or stopped
2417
+ function until(db, check, ms, signal) {
2418
+ return new Promise((resolve, reject) => {
2419
+ const done = (err, found) => {
2420
+ clearTimeout(timer)
2421
+ db.off('update', test)
2422
+ db.off('close', closed)
2423
+ signal.removeEventListener('abort', stop)
2424
+ if (err) reject(err)
2425
+ else resolve(found)
2426
+ }
2427
+ const test = () => check().then((found) => found && done(null, true), done)
2428
+ const stop = () => done(null, false)
2429
+ const closed = () => done(CeroError.CLOSED('Database'))
2430
+ const timer = setTimeout(stop, ms)
2431
+ db.on('update', test)
2432
+ db.once('close', closed)
2433
+ signal.addEventListener('abort', stop)
2434
+ if (signal.aborted) stop()
2435
+ else test()
2436
+ })
2437
+ }
2438
+
2439
+ // whether a room let this identity in, by any of its devices
2440
+ async function promised(db, identity) {
2441
+ if ((await db.get('members', identity.id)).data) return true
2442
+ const { data: requests } = await db.get('requests')
2443
+ return requests.some((row) => row.promised && b4a.equals(row.identity, identity.publicKey))
2444
+ }
2445
+
2446
+ // what a pass holds for this device; a malformed one, or none of its boxes, is nothing
2447
+ function opens(identity, pass) {
2448
+ try {
2449
+ return Pass.open(identity, pass)
2450
+ } catch {
2451
+ return null
2452
+ }
2453
+ }
2454
+
2248
2455
  function admitted(db, id) {
2249
2456
  return new Promise((resolve, reject) => {
2250
2457
  const done = (err, member) => {
@@ -5,6 +5,7 @@ export const REMOVALS = 'removals'
5
5
  export const REFUSALS = 'refusals'
6
6
  export const SPENT = 'spent'
7
7
  export const ASKS = 'owed'
8
+ export const PASSES = 'seats'
8
9
 
9
10
  // Writer-admission / pairing timeout (ms).
10
11
  export const TIMEOUT = 30000
@@ -469,6 +469,24 @@ export function removed(ctx, id) {
469
469
  return ctx._removed(id)
470
470
  }
471
471
 
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
+
472
490
  /**
473
491
  * Quit a handle for good: its row reads `leaving`, then `left` once the handle holds it, and it closes.
474
492
  *
package/src/rpc/client.js CHANGED
@@ -31,6 +31,7 @@ export const {
31
31
  deny,
32
32
  leave,
33
33
  removed,
34
+ seat,
34
35
  close,
35
36
  cancel,
36
37
  suspend,
@@ -379,6 +380,11 @@ const operators = {
379
380
  return this._verb('removed', id)
380
381
  },
381
382
 
383
+ /** @private */
384
+ _seat(id, { from, ...opts }) {
385
+ return this._verb('seat', id, { ...opts, from: from?.id ?? from })
386
+ },
387
+
382
388
  /** @private */
383
389
  _cancel(invite) {
384
390
  return this._verb('cancel', invite)
package/src/rpc/server.js CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  revoke,
19
19
  rotate,
20
20
  removed,
21
+ seat,
21
22
  accept,
22
23
  deny,
23
24
  cancel,
@@ -39,6 +40,7 @@ const VERBS = new Map(
39
40
  revoke,
40
41
  rotate,
41
42
  removed,
43
+ seat,
42
44
  accept,
43
45
  deny,
44
46
  cancel,
@@ -45,6 +45,7 @@ export declare const main: {
45
45
  index: import("@cero-base/core").Prim;
46
46
  confirm: import("@cero-base/core").Prim;
47
47
  secret: import("@cero-base/core").Prim;
48
+ to: import("@cero-base/core").Prim;
48
49
  };
49
50
  handle: {
50
51
  id: import("@cero-base/core").Prim;
@@ -166,6 +167,13 @@ export declare const main: {
166
167
  standing: import("@cero-base/core").Prim;
167
168
  proof: import("@cero-base/core").Prim;
168
169
  };
170
+ pass: {
171
+ id: import("@cero-base/core").Prim;
172
+ to: import("@cero-base/core").Prim;
173
+ by: import("@cero-base/core").Prim;
174
+ pass: import("@cero-base/core").Prim;
175
+ createdAt: import("@cero-base/core").Prim;
176
+ };
169
177
  };
170
178
  export declare const local: {
171
179
  master: {
@@ -44,6 +44,11 @@ export declare const cero: {
44
44
  id: string;
45
45
  }, reason?: string): Promise<void>;
46
46
  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>;
47
52
  leave(ctx: operators.Context): Promise<void>;
48
53
  close(ctx: operators.Context): Promise<void>;
49
54
  cancel(me: operators.Context, code: string): Promise<boolean>;
@@ -318,6 +318,8 @@ export declare class Handle extends ReadyResource {
318
318
  /** @private */
319
319
  private _removed;
320
320
  /** @private */
321
+ private _seat;
322
+ /** @private */
321
323
  private _tx;
322
324
  /** @private */
323
325
  private _leave;
@@ -450,6 +452,12 @@ export declare class Handle extends ReadyResource {
450
452
  /** @private */
451
453
  private _run;
452
454
  /** @private */
455
+ private _take;
456
+ /** @private */
457
+ private _passes;
458
+ /** @private */
459
+ private _spent;
460
+ /** @private */
453
461
  private _cancel;
454
462
  /** @private */
455
463
  private _check;
@@ -5,5 +5,6 @@ export declare const REMOVALS = "removals";
5
5
  export declare const REFUSALS = "refusals";
6
6
  export declare const SPENT = "spent";
7
7
  export declare const ASKS = "owed";
8
+ export declare const PASSES = "seats";
8
9
  export declare const TIMEOUT = 30000;
9
10
  export declare const KEEP_OPEN = 16;
@@ -197,6 +197,23 @@ export declare function deny(ctx: Context, request: {
197
197
  * @returns {Promise<'removed' | 'left' | null>}
198
198
  */
199
199
  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>;
200
217
  /**
201
218
  * Quit a handle for good: its row reads `leaving`, then `left` once the handle holds it, and it closes.
202
219
  *
@@ -36,6 +36,11 @@ declare const remote: {
36
36
  id: string;
37
37
  }, reason?: string): Promise<void>;
38
38
  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>;
39
44
  leave(ctx: verbs.Context): Promise<void>;
40
45
  close(ctx: verbs.Context): Promise<void>;
41
46
  cancel(me: verbs.Context, code: string): Promise<boolean>;
@@ -66,7 +71,7 @@ declare const remote: {
66
71
  topic?: string;
67
72
  }): Promise<void>;
68
73
  };
69
- 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;
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;
70
75
  export { t, schema };
71
76
  export type BaseRPCClient = import('@cero-base/core/rpc').RPCClient;
72
77
  export type RefInfo = import('../lib/spec.js').RefInfo;