@cero-base/core 2.9.0 → 2.11.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.9.0",
3
+ "version": "2.11.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",
@@ -378,6 +378,11 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
378
378
  const minted = (await ctx.view.get(countersCol, { name: 'invites' }))?.value ?? 0
379
379
  await ctx.view.insert(removals, { id: hid.encode(device.key), index: minted })
380
380
  }
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
+
381
386
  // the devices of the chain, not the identity it starts with: a member's removal is ordered by
382
387
  // its invites, a device's is for good
383
388
  async function revoked(view, proof) {
@@ -447,7 +452,9 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
447
452
  const existing = await getMember(ctx.view, op.id)
448
453
  if (!existing) return
449
454
  let decided = null
450
- if (op.id !== (await getSignerMember(ctx.view, ctx.key))) {
455
+ // leaving is hers: only someone else taking her out turns her older codes away
456
+ const leaving = op.id === (await getSignerMember(ctx.view, ctx.key))
457
+ if (!leaving) {
451
458
  // the last owner stays, whatever a rule grants: without her nobody outranks the rest
452
459
  if (existing.role === OWNER && (await orphans(ctx.view, op.id))) {
453
460
  throw CeroError.INVALID(
@@ -474,9 +481,11 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
474
481
  } else {
475
482
  await drop(ctx, 'members', op.id)
476
483
  }
477
- // an invite they could hold, or saw, does not bring them back
484
+ // an invite they could hold, or saw, does not bring them back on its own: after a removal it is turned away, after
485
+ // leaving it waits for a member to accept her again, so leaving first never dodges a removal. Either row re-keys
486
+ // the handle at its first removal (rotation.js)
478
487
  const minted = (await ctx.view.get(countersCol, { name: 'invites' }))?.value ?? 0
479
- await ctx.view.insert(removals, { id: op.id, index: minted })
488
+ await ctx.view.insert(removals, { id: leaving ? leftId(op.id) : op.id, index: minted })
480
489
  })
481
490
 
482
491
  add('rotate-key', async (op, ctx) => {
@@ -690,7 +699,9 @@ export function makeDispatcher({ spec, ns, onerror, key, onepoch, room, hooks, t
690
699
  (removal && (invite.index ?? 0) <= removal.index) || (await revoked(ctx.view, join.proof))
691
700
  if (out) return refuse(ctx, { id, reply: join.reply, expires: invite.expires || 0 }, 'removed')
692
701
  await ctx.view.delete(refusals, { id })
693
- if (invite.confirm && !known) {
702
+ const gone = known ? null : await ctx.view.get(removals, { id: leftId(memberId) })
703
+ const older = gone && (invite.index ?? 0) <= gone.index
704
+ if ((invite.confirm || older) && !known) {
694
705
  const waiting = request(invite, join.identity, signed.device, ctx.key, join.reply, ts)
695
706
  return save(ctx, 'put', 'requests', waiting)
696
707
  }
@@ -245,6 +245,9 @@ export class Database extends ReadyResource {
245
245
  })
246
246
  // without a listener autobee escalates apply/view errors to a process crash
247
247
  this.bee.on('error', this._onerror)
248
+ // a failed background remap is only a warning: the store reads its old cores until a reopen or a fast-forward
249
+ // tries again
250
+ this.bee.on('warning', this._onerror)
248
251
 
249
252
  // with no mirror to hold the database, every member holds it: any member serves a joiner, offline
250
253
  if (this.network && !this.network.mirrors.length) {
@@ -64,6 +64,17 @@ export class Mailbox extends ReadyResource {
64
64
  return keyPair(secret).publicKey
65
65
  }
66
66
 
67
+ /**
68
+ * The key pair behind the address: seal to `publicKey` with `crypto.encrypt`, open with
69
+ * `crypto.decrypt(sealed, pair)`.
70
+ *
71
+ * @param {Uint8Array} secret
72
+ * @returns {{ publicKey: Uint8Array, secretKey: Uint8Array }}
73
+ */
74
+ static keyPair(secret) {
75
+ return keyPair(secret)
76
+ }
77
+
67
78
  /** @private */
68
79
  async _open() {
69
80
  await this.network.ready()
@@ -109,10 +120,18 @@ export class Mailbox extends ReadyResource {
109
120
  * @returns {Promise<void>}
110
121
  */
111
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')
112
125
  if (!this.opened) await this.ready()
113
126
  const name = b4a.toHex(crypto.randomBytes(32))
114
127
  const mail = { id: b4a.toHex(crypto.randomBytes(16)), name, address, message, mirrors, keep }
115
- 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')
116
135
  this._deliver(mail)
117
136
  }
118
137
 
@@ -19,6 +19,10 @@ import { Presence } from './presence.js'
19
19
  const SILENT_TIMEOUT = 3000
20
20
  const RELOOKUPS = [5000, 20000, 60000]
21
21
 
22
+ function toKey(k) {
23
+ return typeof k === 'string' ? decodeKey(k) : k
24
+ }
25
+
22
26
  /** @type {(topic: Uint8Array, channel: string | null) => Uint8Array} */
23
27
  export function channelTopic(topic, channel) {
24
28
  return channel ? hash([topic, b4a.from(channel)]) : topic
@@ -28,7 +32,7 @@ export function channelTopic(topic, channel) {
28
32
  * @typedef {object} NetworkOpts
29
33
  * @property {Array<{ host: string, port: number }>} [bootstrap] Custom DHT bootstrap nodes.
30
34
  * @property {(remotePublicKey: Uint8Array, payload: unknown) => boolean} [firewall] Incoming-connection filter.
31
- * @property {Uint8Array[]} [relayThrough] Relay public keys to tunnel through.
35
+ * @property {Array<string | Uint8Array>} [relayThrough] Blind relay public keys; a connection goes through one only when a direct one fails or this device sits behind a randomizing NAT.
32
36
  * @property {number[]} [backoffs] Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
33
37
  * @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
34
38
  * @property {import('corestore')} store Corestore; every connection replicates it, so every core it holds rides every peer.
@@ -58,13 +62,13 @@ export class Network extends ReadyResource {
58
62
  if (!store) throw CeroError.REQUIRED('store')
59
63
  this.bootstrap = bootstrap || null
60
64
  this.firewall = firewall || null
61
- this.relayThrough = relayThrough || null
65
+ this.relayThrough = relayThrough?.map(toKey) || null
62
66
  this.backoffs = backoffs || null
63
67
  this.channel = channel || null
64
68
  /** @type {import('corestore')} */
65
69
  this.store = store
66
70
  /** @type {Uint8Array[]} */
67
- this.mirrors = (mirrors || []).map((k) => (typeof k === 'string' ? decodeKey(k) : k))
71
+ this.mirrors = (mirrors || []).map(toKey)
68
72
 
69
73
  /** @private */
70
74
  this._swarm = null
@@ -79,6 +83,8 @@ export class Network extends ReadyResource {
79
83
  /** @private */
80
84
  this._blindPeering = null
81
85
  /** @private */
86
+ this._peeringClosed = false
87
+ /** @private */
82
88
  this._lost = new Set()
83
89
  /** @private */
84
90
  this._relookup = null
@@ -218,6 +224,18 @@ export class Network extends ReadyResource {
218
224
  this.emit('connection', conn, info)
219
225
  }
220
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
+
221
239
  /**
222
240
  * The blind-peering client, built on first use: a mailbox post names mirrors this network
223
241
  * may not have been given.
@@ -225,7 +243,7 @@ export class Network extends ReadyResource {
225
243
  * @returns {import('blind-peering')}
226
244
  */
227
245
  peering() {
228
- if (this.closing || this.closed) throw CeroError.CLOSED('Network')
246
+ if (this.closing || this.closed || this._peeringClosed) throw CeroError.CLOSED('Network')
229
247
  if (!this._blindPeering) {
230
248
  this._blindPeering = new BlindPeering(this.swarm.dht, this.store, {
231
249
  blindPeers: this.mirrors.map((key) => ({ key })),
@@ -91,6 +91,17 @@ export declare class Mailbox extends ReadyResource {
91
91
  * @returns {Uint8Array}
92
92
  */
93
93
  static getAddress(secret: Uint8Array): Uint8Array;
94
+ /**
95
+ * The key pair behind the address: seal to `publicKey` with `crypto.encrypt`, open with
96
+ * `crypto.decrypt(sealed, pair)`.
97
+ *
98
+ * @param {Uint8Array} secret
99
+ * @returns {{ publicKey: Uint8Array, secretKey: Uint8Array }}
100
+ */
101
+ static keyPair(secret: Uint8Array): {
102
+ publicKey: Uint8Array;
103
+ secretKey: Uint8Array;
104
+ };
94
105
  /** @private */
95
106
  private _open;
96
107
  /** @private */
@@ -16,9 +16,9 @@ export type NetworkOpts = {
16
16
  */
17
17
  firewall?: (remotePublicKey: Uint8Array, payload: unknown) => boolean;
18
18
  /**
19
- * Relay public keys to tunnel through.
19
+ * Blind relay public keys; a connection goes through one only when a direct one fails or this device sits behind a randomizing NAT.
20
20
  */
21
- relayThrough?: Uint8Array[];
21
+ relayThrough?: Array<string | Uint8Array>;
22
22
  /**
23
23
  * Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
24
24
  */
@@ -52,7 +52,7 @@ export type NetworkOpts = {
52
52
  * @typedef {object} NetworkOpts
53
53
  * @property {Array<{ host: string, port: number }>} [bootstrap] Custom DHT bootstrap nodes.
54
54
  * @property {(remotePublicKey: Uint8Array, payload: unknown) => boolean} [firewall] Incoming-connection filter.
55
- * @property {Uint8Array[]} [relayThrough] Relay public keys to tunnel through.
55
+ * @property {Array<string | Uint8Array>} [relayThrough] Blind relay public keys; a connection goes through one only when a direct one fails or this device sits behind a randomizing NAT.
56
56
  * @property {number[]} [backoffs] Reconnect backoff tiers in ms; the default escalates to ~10min, far too slow for local nets.
57
57
  * @property {string} [channel] Optional network-isolation label; only same-channel peers meet.
58
58
  * @property {import('corestore')} store Corestore; every connection replicates it, so every core it holds rides every peer.
@@ -70,7 +70,7 @@ export declare class Network extends ReadyResource {
70
70
  port: number;
71
71
  }[];
72
72
  firewall: (remotePublicKey: Uint8Array, payload: unknown) => boolean;
73
- relayThrough: Uint8Array<ArrayBufferLike>[];
73
+ relayThrough: any[];
74
74
  backoffs: number[];
75
75
  channel: string;
76
76
  /** @type {import('corestore')} */
@@ -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.