@cero-base/core 2.4.0 → 2.5.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.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -73,6 +73,10 @@
73
73
  "types": "./types/pairing/index.d.ts",
74
74
  "default": "./src/pairing/index.js"
75
75
  },
76
+ "./push": {
77
+ "types": "./types/push/index.d.ts",
78
+ "default": "./src/push/index.js"
79
+ },
76
80
  "./invite": {
77
81
  "types": "./types/pairing/invite.d.ts",
78
82
  "default": "./src/pairing/invite.js"
@@ -151,22 +155,23 @@
151
155
  "autobee": "2.12.1",
152
156
  "b4a": "^1.9.0",
153
157
  "bare-crypto": "^1.15.4",
154
- "bare-fs": "^4.8.2",
158
+ "bare-fs": "^4.8.4",
155
159
  "bare-path": "^3.1.2",
156
160
  "bip39-mnemonic": "^2.5.0",
157
- "blind-peering": "^2.9.5",
161
+ "blind-peering": "^2.12.0",
162
+ "blind-push": "^1.0.2",
158
163
  "compact-encoding": "^3.5.2",
159
- "corestore": "^7.13.0",
164
+ "corestore": "^7.16.1",
160
165
  "framed-stream": "^1.0.1",
161
166
  "hrpc": "^4.3.1",
162
- "hyperblobs": "^2.12.1",
163
- "hypercore": "^11.37.1",
164
- "hypercore-blob-server": "^1.15.0",
167
+ "hyperblobs": "^2.13.0",
168
+ "hypercore": "^11.39.3",
169
+ "hypercore-blob-server": "^1.15.1",
165
170
  "hypercore-crypto": "^3.7.0",
166
171
  "hypercore-id-encoding": "^1.3.0",
167
172
  "hypercore-storage": "^3.3.1",
168
- "hyperdb": "^6.9.0",
169
- "hyperdht": "^6.34.0",
173
+ "hyperdb": "^6.11.0",
174
+ "hyperdht": "^6.34.1",
170
175
  "hyperdispatch": "^1.6.0",
171
176
  "hyperschema": "^1.26.2",
172
177
  "hyperswarm": "^4.17.2",
@@ -183,13 +188,15 @@
183
188
  "@hyperswarm/testnet": "^3.1.4",
184
189
  "bare-abort-controller": "^1.1.2",
185
190
  "bare-events": "^2.9.2",
186
- "bare-fetch": "^3.4.0",
191
+ "bare-fetch": "^3.4.1",
187
192
  "bare-process": "^4.5.1",
188
- "bare-subprocess": "^6.2.0",
189
- "bare-url": "^2.5.4",
190
- "blind-peer": "^3.15.1",
191
- "brittle": "^4.2.0",
192
- "protomux": "^3.12.1",
193
+ "bare-subprocess": "^6.2.2",
194
+ "bare-url": "^2.5.5",
195
+ "blind-peer": "^3.16.0",
196
+ "blind-push-gateway": "^1.0.0",
197
+ "brittle": "^4.5.0",
198
+ "protomux": "^3.13.0",
199
+ "protomux-rpc-router": "^1.3.0",
193
200
  "typescript": "^7.0.2",
194
201
  "which-runtime": "^1.4.0"
195
202
  },
@@ -47,6 +47,7 @@ import { keyPair } from '../mailbox/inbox.js'
47
47
  * @property {Array<{ epoch: number, entropy: Uint8Array }> | null} [epochs] Rotation epochs to prime the keyring with (delivered at join).
48
48
  * @property {Uint8Array | null} [key] Existing autobee key to reopen.
49
49
  * @property {boolean} [pinned] Always search and announce, outside the network's presence budget. The root.
50
+ * @property {boolean} [push] Push a notification through the mirrors after each local write, so members learn of it while their app is dead.
50
51
  * @property {import('../identity/index.js').KeyPair} [keyPair] This device's writer keypair; a fresh random one by default. Never the identity's, never the database key.
51
52
  * @property {(err: Error) => void} [onerror] Called when a node is skipped or refused, or the bee errors.
52
53
  *
@@ -113,6 +114,7 @@ export class Database extends ReadyResource {
113
114
  this.rotation = new Rotation(this)
114
115
  this.key = opts.key || null
115
116
  this.pinned = opts.pinned === true
117
+ this.push = opts.push === true
116
118
  this.keyPair = opts.keyPair || Identity.randomKeyPair()
117
119
  if (b4a.equals(this.keyPair.publicKey, this.identity.publicKey)) {
118
120
  throw CeroError.INVALID('the identity keypair is never a writer — use a device keypair')
@@ -128,6 +130,18 @@ export class Database extends ReadyResource {
128
130
  /** @private */
129
131
  this._presence = null
130
132
  /** @private */
133
+ this._pushing = null
134
+ /** @private */
135
+ this._pushAgain = false
136
+ /** @private */
137
+ this._pushed = -1
138
+ /** @private */
139
+ this._onResume = () => {
140
+ if (!this._pushAgain) return
141
+ this._pushAgain = false
142
+ this._push()
143
+ }
144
+ /** @private */
131
145
  this._txChain = null
132
146
  /** @private */
133
147
  this._held = new Map()
@@ -228,6 +242,7 @@ export class Database extends ReadyResource {
228
242
  /** @private */
229
243
  async _close() {
230
244
  this.rotation.close()
245
+ this.network?.off('resume', this._onResume)
231
246
  if (this._presence) {
232
247
  this._presence.remove()
233
248
  this._presence = null
@@ -420,6 +435,57 @@ export class Database extends ReadyResource {
420
435
  if (this.closing || this.closed) throw CeroError.CLOSED('Database')
421
436
  throw err
422
437
  }
438
+ if (this.push) this._push()
439
+ }
440
+
441
+ /** @returns {boolean} Whether a push notification is on its way to the mirrors. */
442
+ get pushing() {
443
+ return this._pushing !== null
444
+ }
445
+
446
+ // one in flight, then once more at the latest block: a burst of writes is at most two pushes;
447
+ // one due while the network sleeps goes at resume
448
+ /** @private */
449
+ _push() {
450
+ if (!this.network?.mirrors.length || this.closing) return
451
+ if (this._pushing || this.network.suspended) {
452
+ this._pushAgain = true
453
+ return
454
+ }
455
+ this._pushing = this._send().finally(() => {
456
+ this._pushing = null
457
+ if (!this._pushAgain) return
458
+ this._pushAgain = false
459
+ this._push()
460
+ })
461
+ }
462
+
463
+ // a fresh writer core reaches the mirror a few seconds after its first block: one more try
464
+ /** @private */
465
+ async _send() {
466
+ for (let tries = 0; ; tries++) {
467
+ if (this.closing || !this.bee) return
468
+ if (this.network.suspended) {
469
+ this._pushAgain = true
470
+ return
471
+ }
472
+ const core = this.bee.local
473
+ const index = core.length - 1
474
+ if (index === this._pushed) return
475
+ try {
476
+ await this.network.peering().sendNotification(core, {
477
+ roomKey: this.key,
478
+ index,
479
+ target: this.key
480
+ })
481
+ this._pushed = index
482
+ return
483
+ } catch (err) {
484
+ safetyCatch(err)
485
+ if (tries) return
486
+ await new Promise((resolve) => setTimeout(resolve, PUSH_RETRY).unref())
487
+ }
488
+ }
423
489
  }
424
490
 
425
491
  /**
@@ -737,6 +803,7 @@ export class Database extends ReadyResource {
737
803
  /** @private */
738
804
  _joinSwarm(bee, discoveryKey) {
739
805
  this.network.attach(bee)
806
+ if (this.push && !this._presence) this.network.on('resume', this._onResume)
740
807
  this._presence ??= this.network.presence.add(discoveryKey, { pinned: this.pinned })
741
808
  }
742
809
 
@@ -758,7 +825,12 @@ export class Database extends ReadyResource {
758
825
  const id = b4a.toHex(key)
759
826
  const held = this._held.get(id)
760
827
  if (this.closing || (held && (held.end === -1 || end <= held.end))) return
761
- const core = held ? held.core : this.store.get({ key, active: false })
828
+ // another device's writer is held in the bee's wakeup group, as autobee opens its writers: once autobee lets go of
829
+ // it, a block landing on it still wakes the bee. A view stays out, or autobee would take it for a writer, and so
830
+ // does this device's own writer, as autobee keeps it
831
+ const other = end !== -1 && !b4a.equals(key, this.bee.local.key)
832
+ const group = other ? this.bee.wakeupCapability?.discoveryKey || null : null
833
+ const core = held ? held.core : this.store.get({ key, active: false, group })
762
834
  held?.range.destroy()
763
835
  this._held.set(id, { core, end, range: core.download({ start: 0, end }) })
764
836
  }
@@ -995,6 +1067,7 @@ function bounded(promise, ms = 2000) {
995
1067
  }
996
1068
 
997
1069
  const NO_HOOKS = []
1070
+ const PUSH_RETRY = 5000
998
1071
 
999
1072
  function addHook(map, op, fn) {
1000
1073
  if (typeof fn !== 'function') throw CeroError.INVALID('hook fn must be a function')
@@ -278,6 +278,7 @@ export class Network extends ReadyResource {
278
278
  await this._blindPeering?.resume()
279
279
  // the lookups hyperswarm runs at resume can go out before the network is back
280
280
  this._relook(0)
281
+ this.emit('resume')
281
282
  }
282
283
 
283
284
  /**
@@ -0,0 +1,36 @@
1
+ import { decode, readNotification } from 'blind-push'
2
+ import crypto from 'hypercore-crypto'
3
+ import b4a from 'b4a'
4
+
5
+ /**
6
+ * @typedef {{ id: string, key: Uint8Array }} Handle A handle this device holds: its id and its database key.
7
+ * @typedef {Handle & { length: number, newer: boolean }} Hit The handle a push is about, the length it proves, and whether this device is behind it.
8
+ */
9
+
10
+ /**
11
+ * Verify a push payload against this device's storage: the one the gateway sent, decoded from the
12
+ * platform's message. Null when it is about no handle given, is forged, or does not decode.
13
+ *
14
+ * @param {import('hypercore-storage')} storage The corestore's storage; may be opened read-only.
15
+ * @param {Uint8Array} payload
16
+ * @param {Handle[]} handles
17
+ * @returns {Promise<Hit | null>}
18
+ */
19
+ export async function receive(storage, payload, handles) {
20
+ let push
21
+ try {
22
+ push = decode(payload)
23
+ } catch {
24
+ return null
25
+ }
26
+ const handle = handles.find((h) => b4a.equals(crypto.discoveryKey(h.key), push.discoveryKey))
27
+ if (!handle) return null
28
+ let read
29
+ try {
30
+ read = await readNotification(storage, handle.key, push.payload)
31
+ } catch {
32
+ return null
33
+ }
34
+ if (!read) return null
35
+ return { ...handle, length: read.result.length, newer: read.result.newer }
36
+ }
@@ -60,6 +60,10 @@ export type DatabaseOpts = {
60
60
  * Always search and announce, outside the network's presence budget. The root.
61
61
  */
62
62
  pinned?: boolean;
63
+ /**
64
+ * Push a notification through the mirrors after each local write, so members learn of it while their app is dead.
65
+ */
66
+ push?: boolean;
63
67
  /**
64
68
  * This device's writer keypair; a fresh random one by default. Never the identity's, never the database key.
65
69
  */
@@ -154,6 +158,7 @@ export type HookFn = (ctx: HookContext) => unknown;
154
158
  * @property {Array<{ epoch: number, entropy: Uint8Array }> | null} [epochs] Rotation epochs to prime the keyring with (delivered at join).
155
159
  * @property {Uint8Array | null} [key] Existing autobee key to reopen.
156
160
  * @property {boolean} [pinned] Always search and announce, outside the network's presence budget. The root.
161
+ * @property {boolean} [push] Push a notification through the mirrors after each local write, so members learn of it while their app is dead.
157
162
  * @property {import('../identity/index.js').KeyPair} [keyPair] This device's writer keypair; a fresh random one by default. Never the identity's, never the database key.
158
163
  * @property {(err: Error) => void} [onerror] Called when a node is skipped or refused, or the bee errors.
159
164
  *
@@ -231,6 +236,7 @@ export declare class Database extends ReadyResource {
231
236
  rotation: Rotation;
232
237
  key: Uint8Array<ArrayBufferLike>;
233
238
  pinned: boolean;
239
+ push: boolean;
234
240
  keyPair: import("../index.js").KeyPair;
235
241
  /** @private */
236
242
  _onerror;
@@ -247,6 +253,14 @@ export declare class Database extends ReadyResource {
247
253
  /** @private */
248
254
  _presence;
249
255
  /** @private */
256
+ _pushing;
257
+ /** @private */
258
+ _pushAgain;
259
+ /** @private */
260
+ _pushed;
261
+ /** @private */
262
+ _onResume;
263
+ /** @private */
250
264
  _txChain;
251
265
  /** @private */
252
266
  _held;
@@ -372,6 +386,12 @@ export declare class Database extends ReadyResource {
372
386
  * @returns {Promise<void>}
373
387
  */
374
388
  write(ops: Array<[string, unknown]>): Promise<void>;
389
+ /** @returns {boolean} Whether a push notification is on its way to the mirrors. */
390
+ get pushing(): boolean;
391
+ /** @private */
392
+ private _push;
393
+ /** @private */
394
+ private _send;
375
395
  /**
376
396
  * Read a row. With no `query`: list all (collection) or fetch the one
377
397
  * record (single). With a string id: fetch that specific row.
@@ -0,0 +1,22 @@
1
+ export type Handle = {
2
+ id: string;
3
+ key: Uint8Array;
4
+ };
5
+ export type Hit = Handle & {
6
+ length: number;
7
+ newer: boolean;
8
+ };
9
+ /**
10
+ * @typedef {{ id: string, key: Uint8Array }} Handle A handle this device holds: its id and its database key.
11
+ * @typedef {Handle & { length: number, newer: boolean }} Hit The handle a push is about, the length it proves, and whether this device is behind it.
12
+ */
13
+ /**
14
+ * Verify a push payload against this device's storage: the one the gateway sent, decoded from the
15
+ * platform's message. Null when it is about no handle given, is forged, or does not decode.
16
+ *
17
+ * @param {import('hypercore-storage')} storage The corestore's storage; may be opened read-only.
18
+ * @param {Uint8Array} payload
19
+ * @param {Handle[]} handles
20
+ * @returns {Promise<Hit | null>}
21
+ */
22
+ export declare function receive(storage: import('hypercore-storage'), payload: Uint8Array, handles: Handle[]): Promise<Hit | null>;