@cero-base/core 2.11.0 → 2.12.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/core",
3
- "version": "2.11.0",
3
+ "version": "2.12.0",
4
4
  "description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -43,6 +43,9 @@ const isKey = (b) => b?.byteLength === 32
43
43
  // member and device have their own set handlers, every other collection upserts via add-
44
44
  export const BESPOKE = new Set(['member', 'device'])
45
45
 
46
+ // a member who left, apart from one removed and from a revoked device, in the removals: ids never hold ':'
47
+ export const leftId = (memberId) => `left:${memberId}`
48
+
46
49
  /**
47
50
  * Build the hyperdispatch router for a spec: wires membership, builtin, and
48
51
  * spec-defined collection/action ops, returning the router plus an apply loop.
@@ -378,11 +381,6 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
378
381
  const minted = (await ctx.view.get(countersCol, { name: 'invites' }))?.value ?? 0
379
382
  await ctx.view.insert(removals, { id: hid.encode(device.key), index: minted })
380
383
  }
381
- // a member who left, apart from one removed and from a revoked device: ids never hold ':'
382
- function leftId(memberId) {
383
- return `left:${memberId}`
384
- }
385
-
386
384
  // the devices of the chain, not the identity it starts with: a member's removal is ordered by
387
385
  // its invites, a device's is for good
388
386
  async function revoked(view, proof) {
@@ -15,7 +15,8 @@ import {
15
15
  QUERY_RESERVED,
16
16
  RANK,
17
17
  ADMIN,
18
- WRITE
18
+ WRITE,
19
+ REMOVALS
19
20
  } from '../lib/constants.js'
20
21
  import { genId } from '../lib/ids.js'
21
22
  import {
@@ -33,7 +34,7 @@ import { CeroError } from '../lib/errors.js'
33
34
  import { Identity } from '../identity/index.js'
34
35
  import { Rotation } from './rotation.js'
35
36
  import { makeChanges } from './changes.js'
36
- import { makeDispatcher, BESPOKE } from './dispatch.js'
37
+ import { makeDispatcher, BESPOKE, leftId } from './dispatch.js'
37
38
  import { keyPair } from '../mailbox/inbox.js'
38
39
 
39
40
  // autobee remaps an older store once a recovery peer, a mirror, serves it; unset, it closes the store
@@ -536,6 +537,23 @@ export class Database extends ReadyResource {
536
537
  return this._read(this.view, name, query)
537
538
  }
538
539
 
540
+ /**
541
+ * Whether a member is out for good, as this device has seen it: `'removed'` by another member, `'left'` on her
542
+ * own, or null while she is a member, never was one, or her going has not reached this device. A member let in
543
+ * again after is a member: null.
544
+ *
545
+ * @param {string} memberId
546
+ * @returns {Promise<'removed' | 'left' | null>}
547
+ */
548
+ async removed(memberId) {
549
+ this.guard()
550
+ if ((await this.get('members', memberId)).data) return null
551
+ const col = `@${this.ns}/${REMOVALS}`
552
+ if (await this.view.get(col, { id: memberId })) return 'removed'
553
+ if (await this.view.get(col, { id: leftId(memberId) })) return 'left'
554
+ return null
555
+ }
556
+
539
557
  // the same read against any view: hooks read the transaction they run in
540
558
  /** @private */
541
559
  async _read(view, name, query) {
@@ -983,7 +1001,15 @@ export class Database extends ReadyResource {
983
1001
  checkRequired(name, this.refs[name], stored)
984
1002
  if (ref.kind === COLLECTION && !stored.id) stored.id = genId()
985
1003
  const insert = ref.kind === COLLECTION && upsert && !BESPOKE.has(ref.verb)
986
- return this._append(stored, `${insert ? 'add' : 'set'}-${ref.verb}`)
1004
+ const verb = `${insert ? 'add' : 'set'}-${ref.verb}`
1005
+ // a member's rank travels only when the caller names one: this device's view of it may predate a change another
1006
+ // device made, which a merged copy would undo or get refused for. An empty rank is no change, and apply keeps
1007
+ // the stored one
1008
+ if (ref.verb === 'member' && !('role' in row)) {
1009
+ await this._append({ ...stored, role: '' }, verb)
1010
+ return { row: stored }
1011
+ }
1012
+ return this._append(stored, verb)
987
1013
  }
988
1014
 
989
1015
  /** @private */
@@ -101,6 +101,7 @@ export class Pairing extends ReadyResource {
101
101
  async _close() {
102
102
  this.db.off('update', this._onupdate)
103
103
  clearTimeout(this._expiry)
104
+ this._expiry = null
104
105
  const posts = [...this._replies.values(), ...this._denials.values()]
105
106
  this._replies.clear()
106
107
  this._denials.clear()
@@ -232,6 +233,9 @@ export class Pairing extends ReadyResource {
232
233
  /** @private */
233
234
  _arm(rows) {
234
235
  clearTimeout(this._expiry)
236
+ this._expiry = null
237
+ // a sync that awaited past close arms nothing: _close cleared the timer already
238
+ if (this.closing || this.closed) return
235
239
  const live = rows.filter((row) => row.expires > 0 && !expired(row))
236
240
  const next = Math.min(...live.map((row) => row.expires))
237
241
  if (!Number.isFinite(next)) return
@@ -1,4 +1,5 @@
1
1
  export declare const BESPOKE: Set<string>;
2
+ export declare const leftId: (memberId: any) => string;
2
3
  /**
3
4
  * Build the hyperdispatch router for a spec: wires membership, builtin, and
4
5
  * spec-defined collection/action ops, returning the router plus an apply loop.
@@ -413,6 +413,15 @@ export declare class Database extends ReadyResource {
413
413
  * @returns {Promise<SingleResult | ListResult>}
414
414
  */
415
415
  get(name: string, query?: string | Query): Promise<SingleResult | ListResult>;
416
+ /**
417
+ * Whether a member is out for good, as this device has seen it: `'removed'` by another member, `'left'` on her
418
+ * own, or null while she is a member, never was one, or her going has not reached this device. A member let in
419
+ * again after is a member: null.
420
+ *
421
+ * @param {string} memberId
422
+ * @returns {Promise<'removed' | 'left' | null>}
423
+ */
424
+ removed(memberId: string): Promise<'removed' | 'left' | null>;
416
425
  /** @private */
417
426
  private _read;
418
427
  /**