@cero-base/cero 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -8
- package/src/build/schemas.js +4 -2
- package/src/handle/index.js +147 -37
- package/types/build/schemas.d.ts +1 -0
- package/types/handle/index.d.ts +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cero-base/cero",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.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,18 +113,18 @@
|
|
|
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.
|
|
116
|
+
"@cero-base/core": "^3.2.0",
|
|
117
117
|
"b4a": "^1.9.0",
|
|
118
118
|
"bare-abort-controller": "^1.1.2",
|
|
119
119
|
"bare-crypto": "^1.15.4",
|
|
120
120
|
"bare-fs": "^4.8.4",
|
|
121
|
-
"bare-path": "^3.1.
|
|
121
|
+
"bare-path": "^3.1.3",
|
|
122
122
|
"ble-swarm": "^2.4.0",
|
|
123
|
-
"compact-encoding": "^3.
|
|
123
|
+
"compact-encoding": "^3.7.0",
|
|
124
124
|
"corestore": "^7.16.1",
|
|
125
125
|
"fd-lock": "^2.2.0",
|
|
126
126
|
"hrpc": "^4.3.1",
|
|
127
|
-
"hypercore": "^11.40.
|
|
127
|
+
"hypercore": "^11.40.4",
|
|
128
128
|
"hypercore-crypto": "^3.7.0",
|
|
129
129
|
"hypercore-id-encoding": "^1.3.0",
|
|
130
130
|
"hypercore-storage": "^3.4.0",
|
|
@@ -141,10 +141,10 @@
|
|
|
141
141
|
"devDependencies": {
|
|
142
142
|
"@hyperswarm/testnet": "^3.1.4",
|
|
143
143
|
"bare-events": "^2.9.2",
|
|
144
|
-
"bare-fetch": "^3.
|
|
144
|
+
"bare-fetch": "^3.5.0",
|
|
145
145
|
"bare-process": "^4.5.1",
|
|
146
|
-
"bare-url": "^2.5.
|
|
147
|
-
"blind-peer": "^3.18.
|
|
146
|
+
"bare-url": "^2.5.7",
|
|
147
|
+
"blind-peer": "^3.18.2",
|
|
148
148
|
"brittle": "^4.5.0",
|
|
149
149
|
"typescript": "^7.0.2"
|
|
150
150
|
},
|
package/src/build/schemas.js
CHANGED
|
@@ -59,7 +59,9 @@ export const main = {
|
|
|
59
59
|
// their answers
|
|
60
60
|
secret: bytes,
|
|
61
61
|
// the member it was made for: nobody else joins with it
|
|
62
|
-
to: string
|
|
62
|
+
to: string,
|
|
63
|
+
// her device keys, 32 bytes each, back to back: a rotation before she comes is sealed to them
|
|
64
|
+
keys: bytes
|
|
63
65
|
},
|
|
64
66
|
handle: {
|
|
65
67
|
id: required(string),
|
|
@@ -346,7 +348,7 @@ export const local = {
|
|
|
346
348
|
|
|
347
349
|
// what a write means to apply. A release that changes it without changing what is stored bumps
|
|
348
350
|
// this, so an app's next build raises its contract and devices on the old release skip the new writes
|
|
349
|
-
export const contract = { version:
|
|
351
|
+
export const contract = { version: 6 }
|
|
350
352
|
|
|
351
353
|
export const rpc = {
|
|
352
354
|
'req-empty': {
|
package/src/handle/index.js
CHANGED
|
@@ -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, SPENT, TIMEOUT } from '../lib/constants.js'
|
|
25
|
+
import { KEEP_OPEN, 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,6 +138,10 @@ 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
|
|
144
|
+
/** @private */
|
|
141
145
|
this._coreKeys = parent ? null : new Map()
|
|
142
146
|
/** @private */
|
|
143
147
|
this._offJoins = null
|
|
@@ -238,7 +242,7 @@ export class Handle extends ReadyResource {
|
|
|
238
242
|
/** @private */
|
|
239
243
|
this._ac = new AbortController()
|
|
240
244
|
if (this.closed) this._ac.abort()
|
|
241
|
-
else this.
|
|
245
|
+
else this._onclose(() => this._ac.abort())
|
|
242
246
|
}
|
|
243
247
|
return this._ac.signal
|
|
244
248
|
}
|
|
@@ -250,6 +254,17 @@ export class Handle extends ReadyResource {
|
|
|
250
254
|
return h
|
|
251
255
|
}
|
|
252
256
|
|
|
257
|
+
// what is tied to this handle while it is open is undone as it closes, by one listener however many there are
|
|
258
|
+
/** @private */
|
|
259
|
+
_onclose(fn) {
|
|
260
|
+
if (!this._offs) {
|
|
261
|
+
/** @private */
|
|
262
|
+
this._offs = []
|
|
263
|
+
this.once('close', () => this._offs.forEach((off) => off()))
|
|
264
|
+
}
|
|
265
|
+
this._offs.push(fn)
|
|
266
|
+
}
|
|
267
|
+
|
|
253
268
|
/**
|
|
254
269
|
* Lazily-built file server for this identity. Root-only — child handles
|
|
255
270
|
* reach it through `this.root.fileServer`.
|
|
@@ -325,10 +340,10 @@ export class Handle extends ReadyResource {
|
|
|
325
340
|
this._pruneAll().catch(this._onerror)
|
|
326
341
|
this._tellClosed()
|
|
327
342
|
})
|
|
328
|
-
this.
|
|
343
|
+
this._onclose(prune)
|
|
329
344
|
if (this._bluetooth) {
|
|
330
345
|
const off = this.store.onUpdate('profile', () => this._tell().catch(this._onerror))
|
|
331
|
-
this.
|
|
346
|
+
this._onclose(off)
|
|
332
347
|
await this._tell()
|
|
333
348
|
}
|
|
334
349
|
// the joins on their way, at launch and as their rows move
|
|
@@ -340,7 +355,7 @@ export class Handle extends ReadyResource {
|
|
|
340
355
|
this._tellClosed()
|
|
341
356
|
// the handles rows, keyed by push topic, for a cold push process that cannot read the root
|
|
342
357
|
const off = this.store.onUpdate('handles', () => this._index().catch(this._onerror))
|
|
343
|
-
this.
|
|
358
|
+
this._onclose(off)
|
|
344
359
|
await this._index()
|
|
345
360
|
}
|
|
346
361
|
}
|
|
@@ -523,7 +538,7 @@ export class Handle extends ReadyResource {
|
|
|
523
538
|
`this room has too many keys to seal to her ${devices.length} devices`
|
|
524
539
|
)
|
|
525
540
|
}
|
|
526
|
-
const invite = await this._invite({ role, ttl, to: id })
|
|
541
|
+
const invite = await this._invite({ role, ttl, to: id, keys: devices.map((d) => d.key) })
|
|
527
542
|
const pass = Pass.seal(devices, { ...fields, invite })
|
|
528
543
|
const row = { id: b4a.toHex(Identity.randomBytes(16)), to: id, pass, createdAt: Date.now() }
|
|
529
544
|
await store.call('add-pass', row)
|
|
@@ -601,16 +616,28 @@ export class Handle extends ReadyResource {
|
|
|
601
616
|
this._departing.set(id, going)
|
|
602
617
|
}
|
|
603
618
|
|
|
604
|
-
// a device that applies its own identity's removal writes it on the row, with who removed her
|
|
619
|
+
// 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
|
|
605
621
|
/** @private */
|
|
606
|
-
async _ousted(child) {
|
|
607
|
-
const removal = await child.store.removal(this.identity.id)
|
|
608
|
-
if (!removal) return
|
|
622
|
+
async _ousted(child, out = false) {
|
|
609
623
|
const { data: row } = await this.store.get('handles', child.id)
|
|
610
624
|
// only a seat held is lost: a row asking or joining is a rejoin under way, one ended is over
|
|
611
625
|
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
|
|
612
629
|
const fields = { by: removal.by, reason: null, encryptionKey: null, epochs: null }
|
|
613
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
|
+
}
|
|
614
641
|
}
|
|
615
642
|
|
|
616
643
|
/**
|
|
@@ -1233,7 +1260,17 @@ export class Handle extends ReadyResource {
|
|
|
1233
1260
|
const id = hid.encode(Invite.parse(invite).key)
|
|
1234
1261
|
let join = this._joining.get(id)
|
|
1235
1262
|
if (!join) {
|
|
1236
|
-
join = {
|
|
1263
|
+
join = {
|
|
1264
|
+
id,
|
|
1265
|
+
type,
|
|
1266
|
+
invite: null,
|
|
1267
|
+
pass,
|
|
1268
|
+
writer: null,
|
|
1269
|
+
waiting: 0,
|
|
1270
|
+
end: null,
|
|
1271
|
+
running: null,
|
|
1272
|
+
read: false
|
|
1273
|
+
}
|
|
1237
1274
|
join.done = new Promise((resolve, reject) => {
|
|
1238
1275
|
join.resolve = resolve
|
|
1239
1276
|
join.reject = reject
|
|
@@ -1242,8 +1279,9 @@ export class Handle extends ReadyResource {
|
|
|
1242
1279
|
this._joining.set(id, join)
|
|
1243
1280
|
}
|
|
1244
1281
|
// 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
|
-
|
|
1282
|
+
// for a reply takes the keys a pass hands it. A pass waits for another pass's join to end
|
|
1283
|
+
const other = join.invite !== invite && !(pass && join.pass && join.invite)
|
|
1284
|
+
if ((other || (pass && !join.pass)) && !join.entering) {
|
|
1247
1285
|
join.pass = pass
|
|
1248
1286
|
this._run(join, invite)
|
|
1249
1287
|
}
|
|
@@ -1264,6 +1302,12 @@ export class Handle extends ReadyResource {
|
|
|
1264
1302
|
try {
|
|
1265
1303
|
const { pass } = join
|
|
1266
1304
|
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
|
+
}
|
|
1267
1311
|
const id = pass ? hid.encode(pass.key) : await this._ask(join.type, invite)
|
|
1268
1312
|
const writer = (join.writer = await this._loadKeyPair(id))
|
|
1269
1313
|
const { data: row } = await this.store.get('handles', id)
|
|
@@ -1317,6 +1361,7 @@ export class Handle extends ReadyResource {
|
|
|
1317
1361
|
return this._room({ spec, namespace, key, encryptionKey, epochs, keyPair })
|
|
1318
1362
|
}
|
|
1319
1363
|
const reader = room(Identity.randomKeyPair())
|
|
1364
|
+
let moot = false
|
|
1320
1365
|
try {
|
|
1321
1366
|
await reader.ready()
|
|
1322
1367
|
const { store } = reader
|
|
@@ -1327,9 +1372,17 @@ export class Handle extends ReadyResource {
|
|
|
1327
1372
|
if (!(await until(store, shows, TIMEOUT, signal))) {
|
|
1328
1373
|
throw signal.aborted ? CeroError.CLOSED('join') : CeroError.TIMEOUT('join')
|
|
1329
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'
|
|
1330
1382
|
} finally {
|
|
1331
1383
|
await reader._end().catch(safetyCatch)
|
|
1332
1384
|
}
|
|
1385
|
+
if (moot) return false
|
|
1333
1386
|
const { data: kept } = await this.local.store.get('handle-keypairs', id)
|
|
1334
1387
|
const { publicKey, secretKey } = kept?.invite === invite ? kept : Identity.randomKeyPair()
|
|
1335
1388
|
const writer = (join.writer = { publicKey, secretKey })
|
|
@@ -1358,30 +1411,58 @@ export class Handle extends ReadyResource {
|
|
|
1358
1411
|
return null
|
|
1359
1412
|
}
|
|
1360
1413
|
|
|
1361
|
-
// the passes this
|
|
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
|
|
1362
1417
|
/** @private */
|
|
1363
1418
|
async _passes(from) {
|
|
1364
1419
|
const { store } = from
|
|
1365
1420
|
const rows = await store.view.find(`@${store.ns}/${PASSES}`, {}).toArray()
|
|
1421
|
+
rows.sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0))
|
|
1422
|
+
let waiting = false
|
|
1366
1423
|
for (const row of rows) {
|
|
1367
1424
|
if (row.to !== this.identity.id) continue
|
|
1368
1425
|
const pass = opens(this.identity, row.pass)
|
|
1369
1426
|
const type = pass?.data && b4a.toString(pass.data)
|
|
1370
1427
|
if (!type || !this.spec.handles?.[type]) continue
|
|
1371
1428
|
if (await this._spent(from, row, pass)) continue
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
this._start(pass.invite, type, pass)
|
|
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)
|
|
1438
|
+
}
|
|
1439
|
+
join.done.then(next, next).catch((err) => this.closing || this._onerror(err))
|
|
1375
1440
|
}
|
|
1441
|
+
if (waiting) this._retry(from)
|
|
1442
|
+
}
|
|
1443
|
+
|
|
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
|
+
/** @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)
|
|
1376
1456
|
}
|
|
1377
1457
|
|
|
1378
|
-
// a pass goes once its
|
|
1458
|
+
// a pass goes once its join is in or ended, or it ran out. A room in under another invite is a
|
|
1459
|
+
// seat she may have lost since, unheard on a device that keeps it closed
|
|
1379
1460
|
/** @private */
|
|
1380
1461
|
async _spent(from, row, pass) {
|
|
1381
1462
|
const { data: was } = await this.store.get('handles', hid.encode(pass.key))
|
|
1382
1463
|
const state = was && (was.state || 'in')
|
|
1383
|
-
const
|
|
1384
|
-
if (
|
|
1464
|
+
const done = was?.invite === pass.invite && (state === 'in' || SWEPT.includes(state))
|
|
1465
|
+
if (!done && !Invite.parse(pass.invite).expired) return false
|
|
1385
1466
|
if (from.store.writable && !from.closing && !from.closed) {
|
|
1386
1467
|
await from.store.call('del-pass', { id: row.id })
|
|
1387
1468
|
}
|
|
@@ -1502,8 +1583,8 @@ export class Handle extends ReadyResource {
|
|
|
1502
1583
|
const mine = !!kept?.invite && (state === 'joining' || latest)
|
|
1503
1584
|
if (mine && (state === 'joining' || row.invite === invite)) return id
|
|
1504
1585
|
if (ENDS[state] && row.invite === invite) throw ENDS[state](row.reason)
|
|
1505
|
-
// a pass's join asks with the writer its join is
|
|
1506
|
-
const writer = mine ? kept :
|
|
1586
|
+
// a pass's join asks with the writer its join is written with, whatever ask stood before
|
|
1587
|
+
const writer = given || (mine ? kept : Identity.randomKeyPair())
|
|
1507
1588
|
await this._saveKeyPair(id, writer, invite)
|
|
1508
1589
|
// a room another device of this identity already has the keys of is joined with them
|
|
1509
1590
|
if (state === 'joining') return id
|
|
@@ -1590,10 +1671,13 @@ export class Handle extends ReadyResource {
|
|
|
1590
1671
|
*/
|
|
1591
1672
|
async _enter(type, reply, invite, taken = null) {
|
|
1592
1673
|
const id = hid.encode(reply.key)
|
|
1593
|
-
//
|
|
1674
|
+
// open here on another writer than the seat it enters on, the room is one this identity lost, or
|
|
1675
|
+
// whose seat never counted: it ends before the new seat opens it
|
|
1594
1676
|
const stale = [...this.children].find((c) => c.id === id && !c.closing)
|
|
1595
|
-
const
|
|
1596
|
-
if (
|
|
1677
|
+
const writer = taken ? taken.store.writerKey : reply.writer?.publicKey
|
|
1678
|
+
if (stale && writer && !b4a.equals(stale.store.writerKey, writer)) {
|
|
1679
|
+
await stale._end().catch(safetyCatch)
|
|
1680
|
+
}
|
|
1597
1681
|
// a pass's join is already open, seated
|
|
1598
1682
|
const child = /** @type {Child} */ (
|
|
1599
1683
|
taken ||
|
|
@@ -1617,7 +1701,11 @@ export class Handle extends ReadyResource {
|
|
|
1617
1701
|
// let in by a promise, this device seats itself: its claim writes the member row
|
|
1618
1702
|
await child.store.claim({ timeout: 0 })
|
|
1619
1703
|
// admitted once our member row lands; the same apply seats a writer, a reader has no seat
|
|
1620
|
-
const member = await admitted(
|
|
1704
|
+
const member = await admitted(
|
|
1705
|
+
child.store,
|
|
1706
|
+
this.identity.id,
|
|
1707
|
+
b4a.toHex(Invite.parse(invite).id)
|
|
1708
|
+
)
|
|
1621
1709
|
if (member.role !== 'reader' && !child.store.writable) {
|
|
1622
1710
|
await child.store.whenWritable({ timeout: 0 })
|
|
1623
1711
|
}
|
|
@@ -1776,7 +1864,7 @@ export class Handle extends ReadyResource {
|
|
|
1776
1864
|
const child = new Handle({ parent: this, ...opts })
|
|
1777
1865
|
for (const hook of this._typeHooks) hook.apply(child)
|
|
1778
1866
|
this._pending.add(child)
|
|
1779
|
-
child.
|
|
1867
|
+
child._onclose(() => this._pending.delete(child))
|
|
1780
1868
|
return child
|
|
1781
1869
|
}
|
|
1782
1870
|
|
|
@@ -1810,11 +1898,15 @@ export class Handle extends ReadyResource {
|
|
|
1810
1898
|
// it tells a handle nothing before it has a seat there
|
|
1811
1899
|
const prune = () => this._prune(child).catch(this._onerror)
|
|
1812
1900
|
child.store.on('writable', prune)
|
|
1813
|
-
child.
|
|
1901
|
+
child._onclose(() => child.store.off('writable', prune))
|
|
1814
1902
|
prune()
|
|
1815
1903
|
this._seats(child)
|
|
1816
1904
|
const take = () => this._passes(child).catch((err) => this.closing || this._onerror(err))
|
|
1817
|
-
child.
|
|
1905
|
+
child._onclose(child.store.onUpdate(PASSES, take))
|
|
1906
|
+
child._onclose(() => {
|
|
1907
|
+
clearTimeout(this._retries.get(child))
|
|
1908
|
+
this._retries.delete(child)
|
|
1909
|
+
})
|
|
1818
1910
|
take()
|
|
1819
1911
|
// what a listener ties to the handle as it runs is no holder's: it stays through their last close
|
|
1820
1912
|
child._arriving = true
|
|
@@ -1832,11 +1924,12 @@ export class Handle extends ReadyResource {
|
|
|
1832
1924
|
const onchange = (touched) => {
|
|
1833
1925
|
if (touched.has('*') || touched.has('devices') || touched.has('members')) {
|
|
1834
1926
|
this._index(child).catch(this._onerror)
|
|
1835
|
-
this._ousted(child).catch((err) => this.closing || this._onerror(err))
|
|
1836
1927
|
}
|
|
1928
|
+
// whatever a batch touched: a reorder takes back a seat it read before
|
|
1929
|
+
this._ousted(child).catch((err) => this.closing || this._onerror(err))
|
|
1837
1930
|
}
|
|
1838
1931
|
child.store.on('update', onchange)
|
|
1839
|
-
child.
|
|
1932
|
+
child._onclose(() => child.store.off('update', onchange))
|
|
1840
1933
|
this._index(child).catch(this._onerror)
|
|
1841
1934
|
}
|
|
1842
1935
|
|
|
@@ -2165,13 +2258,15 @@ export class Handle extends ReadyResource {
|
|
|
2165
2258
|
}
|
|
2166
2259
|
const touches = (name) => (touched) => touched.has('*') || touched.has(name)
|
|
2167
2260
|
const onepoch = () => save().catch(this._onerror)
|
|
2168
|
-
|
|
2169
|
-
const
|
|
2261
|
+
// a room that closed under the read says nothing
|
|
2262
|
+
const report = (err) => err.code === 'CLOSED' || this._onerror(err)
|
|
2263
|
+
const onroot = (touched) => touches('handles')(touched) && read().catch(report)
|
|
2264
|
+
const onchild = (touched) => touches('epochs')(touched) && read().catch(report)
|
|
2170
2265
|
child.store.on('epoch', onepoch)
|
|
2171
2266
|
child.store.on('update', onchild)
|
|
2172
2267
|
this.store.on('update', onroot)
|
|
2173
2268
|
this.store.on('epoch', onepoch)
|
|
2174
|
-
child.
|
|
2269
|
+
child._onclose(() => {
|
|
2175
2270
|
child.store.off('epoch', onepoch)
|
|
2176
2271
|
child.store.off('update', onchild)
|
|
2177
2272
|
this.store.off('update', onroot)
|
|
@@ -2189,7 +2284,7 @@ export class Handle extends ReadyResource {
|
|
|
2189
2284
|
apply: (child) => {
|
|
2190
2285
|
if (child.type !== ref.type) return
|
|
2191
2286
|
offs.set(child, attach(child))
|
|
2192
|
-
child.
|
|
2287
|
+
child._onclose(() => offs.delete(child))
|
|
2193
2288
|
}
|
|
2194
2289
|
}
|
|
2195
2290
|
this._typeHooks.add(hook)
|
|
@@ -2436,6 +2531,18 @@ function until(db, check, ms, signal) {
|
|
|
2436
2531
|
})
|
|
2437
2532
|
}
|
|
2438
2533
|
|
|
2534
|
+
// the room turned this device's writer away, under `invite` when one is named
|
|
2535
|
+
async function turned(db, invite = null) {
|
|
2536
|
+
const refusal = await db.view.get(`@${db.ns}/${REFUSALS}`, { id: hid.encode(db.writerKey) })
|
|
2537
|
+
return refusal && (!invite || !refusal.invite || refusal.invite === invite) ? refusal : null
|
|
2538
|
+
}
|
|
2539
|
+
|
|
2540
|
+
// a seat this device took that never counted: no member row, and its writer turned away
|
|
2541
|
+
async function refused(db, identity) {
|
|
2542
|
+
if ((await db.get('members', identity.id)).data) return false
|
|
2543
|
+
return !!(await turned(db))
|
|
2544
|
+
}
|
|
2545
|
+
|
|
2439
2546
|
// whether a room let this identity in, by any of its devices
|
|
2440
2547
|
async function promised(db, identity) {
|
|
2441
2548
|
if ((await db.get('members', identity.id)).data) return true
|
|
@@ -2452,7 +2559,8 @@ function opens(identity, pass) {
|
|
|
2452
2559
|
}
|
|
2453
2560
|
}
|
|
2454
2561
|
|
|
2455
|
-
|
|
2562
|
+
// its member row once it lands, or why the room turned this device's writer away under `invite`
|
|
2563
|
+
function admitted(db, id, invite) {
|
|
2456
2564
|
return new Promise((resolve, reject) => {
|
|
2457
2565
|
const done = (err, member) => {
|
|
2458
2566
|
db.off('update', onupdate)
|
|
@@ -2462,7 +2570,9 @@ function admitted(db, id) {
|
|
|
2462
2570
|
}
|
|
2463
2571
|
const check = async () => {
|
|
2464
2572
|
const { data } = await db.get('members', id)
|
|
2465
|
-
if (data) done(null, data)
|
|
2573
|
+
if (data) return done(null, data)
|
|
2574
|
+
const refusal = await turned(db, invite)
|
|
2575
|
+
if (refusal) done(CeroError.DENIED(refusal.reason || null))
|
|
2466
2576
|
}
|
|
2467
2577
|
const onupdate = () => check().catch(done)
|
|
2468
2578
|
const onclose = () => done(CeroError.CLOSED('Database'))
|
package/types/build/schemas.d.ts
CHANGED
package/types/handle/index.d.ts
CHANGED
|
@@ -191,6 +191,10 @@ export declare class Handle extends ReadyResource {
|
|
|
191
191
|
/** @private */
|
|
192
192
|
_taking;
|
|
193
193
|
/** @private */
|
|
194
|
+
_held;
|
|
195
|
+
/** @private */
|
|
196
|
+
_retries;
|
|
197
|
+
/** @private */
|
|
194
198
|
_coreKeys;
|
|
195
199
|
/** @private */
|
|
196
200
|
_offJoins;
|
|
@@ -241,6 +245,8 @@ export declare class Handle extends ReadyResource {
|
|
|
241
245
|
_live;
|
|
242
246
|
/** @private */
|
|
243
247
|
_ac;
|
|
248
|
+
/** @private */
|
|
249
|
+
_offs;
|
|
244
250
|
_queued: {
|
|
245
251
|
root: boolean;
|
|
246
252
|
children: Set<any>;
|
|
@@ -256,6 +262,8 @@ export declare class Handle extends ReadyResource {
|
|
|
256
262
|
get signal(): AbortSignal;
|
|
257
263
|
/** The top-most handle in the parent chain — itself for a root handle. */
|
|
258
264
|
get root(): this;
|
|
265
|
+
/** @private */
|
|
266
|
+
private _onclose;
|
|
259
267
|
/**
|
|
260
268
|
* Lazily-built file server for this identity. Root-only — child handles
|
|
261
269
|
* reach it through `this.root.fileServer`.
|
|
@@ -327,6 +335,8 @@ export declare class Handle extends ReadyResource {
|
|
|
327
335
|
private _depart;
|
|
328
336
|
/** @private */
|
|
329
337
|
private _ousted;
|
|
338
|
+
/** @private */
|
|
339
|
+
private _repass;
|
|
330
340
|
/**
|
|
331
341
|
* One holder done with it: a child stays open for the others. With its last, what they tied to it ends and it
|
|
332
342
|
* stays open, idle, so a reopen finds it as it is; the root keeps `keepOpen` of them (16 by default) and one more
|
|
@@ -456,6 +466,8 @@ export declare class Handle extends ReadyResource {
|
|
|
456
466
|
/** @private */
|
|
457
467
|
private _passes;
|
|
458
468
|
/** @private */
|
|
469
|
+
private _retry;
|
|
470
|
+
/** @private */
|
|
459
471
|
private _spent;
|
|
460
472
|
/** @private */
|
|
461
473
|
private _cancel;
|