@cero-base/core 2.10.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.10.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,
@@ -146,32 +146,32 @@
146
146
  "pretest": "npm run build:test",
147
147
  "prepublishOnly": "npm run build:types",
148
148
  "test": "npm run test:bare",
149
- "test:bare": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-bare",
149
+ "test:bare": "find test -name '*.test.js' ! -name network.test.js | sort | xargs -P4 -n1 brittle-bare && brittle-bare test/network/network.test.js",
150
150
  "pretest:bare": "npm run build:test",
151
151
  "test:node": "find test -name '*.test.js' | sort | xargs -P4 -n1 brittle-node"
152
152
  },
153
153
  "dependencies": {
154
154
  "@hyperswarm/secret-stream": "^6.9.2",
155
- "autobee": "2.13.0",
155
+ "autobee": "2.13.3",
156
156
  "b4a": "^1.9.0",
157
157
  "bare-crypto": "^1.15.4",
158
158
  "bare-fs": "^4.8.4",
159
159
  "bare-path": "^3.1.2",
160
160
  "bip39-mnemonic": "^2.5.0",
161
- "blind-peering": "^2.12.2",
162
- "blind-push": "^1.0.2",
161
+ "blind-peering": "^2.13.1",
162
+ "blind-push": "^1.2.0",
163
163
  "compact-encoding": "^3.5.2",
164
164
  "corestore": "^7.16.1",
165
165
  "framed-stream": "^1.0.1",
166
166
  "hrpc": "^4.3.1",
167
167
  "hyperblobs": "^2.13.0",
168
- "hypercore": "^11.39.6",
168
+ "hypercore": "^11.40.0",
169
169
  "hypercore-blob-server": "^1.15.1",
170
170
  "hypercore-crypto": "^3.7.0",
171
171
  "hypercore-id-encoding": "^1.3.0",
172
172
  "hypercore-storage": "^3.4.0",
173
173
  "hyperdb": "^6.11.1",
174
- "hyperdht": "^6.35.0",
174
+ "hyperdht": "^6.36.0",
175
175
  "hyperdispatch": "^1.6.0",
176
176
  "hyperschema": "^1.26.2",
177
177
  "hyperswarm": "^4.17.2",
@@ -191,8 +191,8 @@
191
191
  "bare-fetch": "^3.4.1",
192
192
  "bare-process": "^4.5.1",
193
193
  "bare-subprocess": "^6.2.2",
194
- "bare-url": "^2.5.5",
195
- "blind-peer": "^3.16.0",
194
+ "bare-url": "^2.5.6",
195
+ "blind-peer": "^3.18.1",
196
196
  "blind-push-gateway": "^1.0.0",
197
197
  "brittle": "^4.5.0",
198
198
  "protomux": "^3.13.0",
@@ -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.
@@ -447,7 +450,9 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
447
450
  const existing = await getMember(ctx.view, op.id)
448
451
  if (!existing) return
449
452
  let decided = null
450
- if (op.id !== (await getSignerMember(ctx.view, ctx.key))) {
453
+ // leaving is hers: only someone else taking her out turns her older codes away
454
+ const leaving = op.id === (await getSignerMember(ctx.view, ctx.key))
455
+ if (!leaving) {
451
456
  // the last owner stays, whatever a rule grants: without her nobody outranks the rest
452
457
  if (existing.role === OWNER && (await orphans(ctx.view, op.id))) {
453
458
  throw CeroError.INVALID(
@@ -474,9 +479,11 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
474
479
  } else {
475
480
  await drop(ctx, 'members', op.id)
476
481
  }
477
- // an invite they could hold, or saw, does not bring them back
482
+ // an invite they could hold, or saw, does not bring them back on its own: after a removal it is turned away, after
483
+ // leaving it waits for a member to accept her again, so leaving first never dodges a removal. Either row re-keys
484
+ // the handle at its first removal (rotation.js)
478
485
  const minted = (await ctx.view.get(countersCol, { name: 'invites' }))?.value ?? 0
479
- await ctx.view.insert(removals, { id: op.id, index: minted })
486
+ await ctx.view.insert(removals, { id: leaving ? leftId(op.id) : op.id, index: minted })
480
487
  })
481
488
 
482
489
  add('rotate-key', async (op, ctx) => {
@@ -690,7 +697,9 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
690
697
  (removal && (invite.index ?? 0) <= removal.index) || (await revoked(ctx.view, join.proof))
691
698
  if (out) return refuse(ctx, { id, reply: join.reply, expires: invite.expires || 0 }, 'removed')
692
699
  await ctx.view.delete(refusals, { id })
693
- if (invite.confirm && !known) {
700
+ const gone = known ? null : await ctx.view.get(removals, { id: leftId(memberId) })
701
+ const older = gone && (invite.index ?? 0) <= gone.index
702
+ if ((invite.confirm || older) && !known) {
694
703
  const waiting = request(invite, join.identity, signed.device, ctx.key, join.reply, ts)
695
704
  return save(ctx, 'put', 'requests', waiting)
696
705
  }
@@ -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
@@ -245,6 +246,9 @@ export class Database extends ReadyResource {
245
246
  })
246
247
  // without a listener autobee escalates apply/view errors to a process crash
247
248
  this.bee.on('error', this._onerror)
249
+ // a failed background remap is only a warning: the store reads its old cores until a reopen or a fast-forward
250
+ // tries again
251
+ this.bee.on('warning', this._onerror)
248
252
 
249
253
  // with no mirror to hold the database, every member holds it: any member serves a joiner, offline
250
254
  if (this.network && !this.network.mirrors.length) {
@@ -533,6 +537,23 @@ export class Database extends ReadyResource {
533
537
  return this._read(this.view, name, query)
534
538
  }
535
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
+
536
557
  // the same read against any view: hooks read the transaction they run in
537
558
  /** @private */
538
559
  async _read(view, name, query) {
@@ -980,7 +1001,15 @@ export class Database extends ReadyResource {
980
1001
  checkRequired(name, this.refs[name], stored)
981
1002
  if (ref.kind === COLLECTION && !stored.id) stored.id = genId()
982
1003
  const insert = ref.kind === COLLECTION && upsert && !BESPOKE.has(ref.verb)
983
- 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)
984
1013
  }
985
1014
 
986
1015
  /** @private */
@@ -120,10 +120,18 @@ export class Mailbox extends ReadyResource {
120
120
  * @returns {Promise<void>}
121
121
  */
122
122
  async send(address, message, { mirrors = this.network.mirrors, keep = true } = {}) {
123
+ // a send racing the close fails as the mailbox closing, never as the storage under it
124
+ if (this.closing) throw CeroError.CLOSED('Mailbox')
123
125
  if (!this.opened) await this.ready()
124
126
  const name = b4a.toHex(crypto.randomBytes(32))
125
127
  const mail = { id: b4a.toHex(crypto.randomBytes(16)), name, address, message, mirrors, keep }
126
- await this.outbox.put(mail)
128
+ try {
129
+ await this.outbox.put(mail)
130
+ } catch (err) {
131
+ if (this.closing) throw CeroError.CLOSED('Mailbox')
132
+ throw err
133
+ }
134
+ if (this.closing) throw CeroError.CLOSED('Mailbox')
127
135
  this._deliver(mail)
128
136
  }
129
137
 
@@ -83,6 +83,8 @@ export class Network extends ReadyResource {
83
83
  /** @private */
84
84
  this._blindPeering = null
85
85
  /** @private */
86
+ this._peeringClosed = false
87
+ /** @private */
86
88
  this._lost = new Set()
87
89
  /** @private */
88
90
  this._relookup = null
@@ -222,6 +224,18 @@ export class Network extends ReadyResource {
222
224
  this.emit('connection', conn, info)
223
225
  }
224
226
 
227
+ /**
228
+ * Stop mirroring, before the databases close: blind peering reads storage for every database it
229
+ * mirrors, so once it is closed none of its work outlives storage. `peering()` throws CLOSED
230
+ * after it; closing the network does it too.
231
+ *
232
+ * @returns {Promise<void>}
233
+ */
234
+ async closePeering() {
235
+ this._peeringClosed = true
236
+ await this._blindPeering?.close()
237
+ }
238
+
225
239
  /**
226
240
  * The blind-peering client, built on first use: a mailbox post names mirrors this network
227
241
  * may not have been given.
@@ -229,7 +243,7 @@ export class Network extends ReadyResource {
229
243
  * @returns {import('blind-peering')}
230
244
  */
231
245
  peering() {
232
- if (this.closing || this.closed) throw CeroError.CLOSED('Network')
246
+ if (this.closing || this.closed || this._peeringClosed) throw CeroError.CLOSED('Network')
233
247
  if (!this._blindPeering) {
234
248
  this._blindPeering = new BlindPeering(this.swarm.dht, this.store, {
235
249
  blindPeers: this.mirrors.map((key) => ({ key })),
@@ -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
  /**
@@ -89,6 +89,8 @@ export declare class Network extends ReadyResource {
89
89
  /** @private */
90
90
  _blindPeering;
91
91
  /** @private */
92
+ _peeringClosed;
93
+ /** @private */
92
94
  _lost;
93
95
  /** @private */
94
96
  _relookup;
@@ -121,6 +123,14 @@ export declare class Network extends ReadyResource {
121
123
  }): import('@hyperswarm/secret-stream');
122
124
  /** @private */
123
125
  private _add;
126
+ /**
127
+ * Stop mirroring, before the databases close: blind peering reads storage for every database it
128
+ * mirrors, so once it is closed none of its work outlives storage. `peering()` throws CLOSED
129
+ * after it; closing the network does it too.
130
+ *
131
+ * @returns {Promise<void>}
132
+ */
133
+ closePeering(): Promise<void>;
124
134
  /**
125
135
  * The blind-peering client, built on first use: a mailbox post names mirrors this network
126
136
  * may not have been given.