@cero-base/cero 2.11.0 → 3.0.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.
@@ -68,9 +68,9 @@ export type GetResult = {
68
68
  * @typedef {{ data: import('../lib/spec.js').Row | import('../lib/spec.js').Row[] | null, total?: number, size?: number }} GetResult Single-ref get omits `total`/`size`; list/handle refs include them.
69
69
  */
70
70
  /**
71
- * IPC-side RPC server for cero. Bridges an `hrpc` channel to a live `Handle` tree: boots the
72
- * root via `cero()` as soon as it opens, so the network is up while the UI still loads, then
73
- * exposes data ops, pairing, and handle lifecycle.
71
+ * IPC-side RPC server for cero. Bridges an `hrpc` channel to a live `Handle` tree: a store that holds an identity
72
+ * boots its root via `cero()` as soon as it opens, so the network is up while the UI still loads; a new one waits
73
+ * for the client to say how, by init, restore or link. Then it exposes data ops, pairing, and handle lifecycle.
74
74
  */
75
75
  export declare class Server extends RPCServer {
76
76
  storage: string;
@@ -82,6 +82,8 @@ export declare class Server extends RPCServer {
82
82
  };
83
83
  /** @type {Set<object>} open error streams, one per connected client */
84
84
  _errors: Set<object>;
85
+ /** @private */
86
+ _reported;
85
87
  /** @type {Context | null} */
86
88
  me: Context | null;
87
89
  /** @type {Map<string, Context>} */
@@ -92,6 +94,10 @@ export declare class Server extends RPCServer {
92
94
  _held: Map<string, number>;
93
95
  /** @private */
94
96
  _booting;
97
+ /** @private */
98
+ _adopting;
99
+ /** @private */
100
+ _stop;
95
101
  /**
96
102
  * @param {import('streamx').Duplex} ipc Framed IPC stream (must be writable).
97
103
  * @param {Spec} spec
@@ -112,10 +118,20 @@ export declare class Server extends RPCServer {
112
118
  get identity(): import('@cero-base/core/identity').Identity | undefined;
113
119
  /** @private */
114
120
  private _open;
121
+ /**
122
+ * Open the root, as `how` says on a new store: a phrase recovers, an invite links, neither creates.
123
+ * @private
124
+ */
125
+ private _start;
115
126
  /** @private */
116
127
  private _boot;
117
128
  /** @private */
118
129
  private _close;
130
+ /**
131
+ * The root a restore or a link resolves with is the one served from here on; with the server closing it closes.
132
+ * @private
133
+ */
134
+ private _adopt;
119
135
  /**
120
136
  * End every watch stream bound to a handle (e.g. when it closes or leaves).
121
137
  * @private
@@ -159,6 +175,11 @@ export declare class Server extends RPCServer {
159
175
  * @private
160
176
  */
161
177
  private _wireVerbs;
178
+ /**
179
+ * Register a request handler whose error fails only its call.
180
+ * @private
181
+ */
182
+ private _on;
162
183
  /**
163
184
  * Look up a live handle by id, throwing if unknown. Binds the handle's
164
185
  * codec on first use.
@@ -1,139 +0,0 @@
1
- import AbortController from 'bare-abort-controller'
2
- import b4a from 'b4a'
3
- import c from 'compact-encoding'
4
- import safetyCatch from 'safety-catch'
5
-
6
- import { Pairing } from '@cero-base/core/pairing'
7
- import { Identity } from '@cero-base/core/identity'
8
- import { CeroError } from '@cero-base/core/errors'
9
- import { epochEntries } from '@cero-base/core/database/encryption'
10
-
11
- /**
12
- * One handle being joined. It joins with the latest invite and waits as long as it takes: the
13
- * caller's timeout only ends the caller's wait. What it learns is saved as it goes, the writer
14
- * before the join is written and the keys once the reply lands, so a join resumed after a restart
15
- * picks up where it stopped. It ends admitted, denied, expired or cancelled; a close only pauses
16
- * it. How it ends reaches the caller, or `onerror` once nobody waits.
17
- */
18
- export class Join {
19
- /**
20
- * @param {import('./index.js').Handle} root
21
- * @param {{ type: string, spec: object, discoveryKey: Uint8Array, onend: () => void }} opts
22
- */
23
- constructor(root, { type, spec, discoveryKey, onend }) {
24
- this.root = root
25
- this.type = type
26
- this.spec = spec
27
- this.id = b4a.toHex(discoveryKey)
28
- this.invite = null
29
- this.waiting = 0
30
- this.cancelled = false
31
- /** @private */
32
- this._running = null // aborts the attempt in flight
33
- /** @private */
34
- this._row = null
35
- /** @type {Promise<import('./index.js').Handle>} */
36
- this.done = new Promise((resolve, reject) => {
37
- /** @private */
38
- this._resolve = resolve
39
- /** @private */
40
- this._reject = reject
41
- })
42
- this.done.catch(safetyCatch)
43
- /** @private */
44
- this._onend = onend
45
- }
46
-
47
- /**
48
- * Join with `invite`, taking over from an older attempt: the writer stays, so a reply to the
49
- * older one still lands.
50
- *
51
- * @param {string} invite
52
- */
53
- async start(invite) {
54
- this.invite = invite
55
- this._running?.abort()
56
- const running = new AbortController()
57
- this._running = running
58
- try {
59
- const row = await this._save({ invite })
60
- const writer = { publicKey: row.publicKey, secretKey: row.secretKey }
61
- const { mailbox, identity } = this.root
62
- const keep = (answer) => (answer instanceof Error ? this._forget() : this._keep(answer))
63
- const opts = { identity, spec: this.spec, writer, keep, timeout: 0, signal: running.signal }
64
- const reply = row.key ? unpack(row) : await Pairing.join(mailbox, invite, opts)
65
- this._end(this._resolve, await this.root._enter(this.type, reply))
66
- await this._forget()
67
- } catch (err) {
68
- if (running !== this._running) return
69
- // a close pauses the join, anything else ends it
70
- if (err.code !== 'CLOSED' || this.cancelled) await this._forget()
71
- if (err.code !== 'CLOSED' && !this.waiting) this.root._onerror(err)
72
- this._end(this._reject, err)
73
- }
74
- }
75
-
76
- close() {
77
- this._running?.abort()
78
- }
79
-
80
- // ends it for good: the attempt forgets its row as it unwinds, so no restart resumes it
81
- async cancel() {
82
- this.cancelled = true
83
- this._running?.abort()
84
- await this.done.catch(safetyCatch)
85
- }
86
-
87
- // the first save fixes the writer, later ones add what was learned
88
- /** @private */
89
- async _save(fields) {
90
- const store = this.root.local.store
91
- this._row ??= (await store.get('joins', this.id)).data || Identity.randomKeyPair()
92
- this._row = { ...this._row, id: this.id, type: this.type, ...fields }
93
- await store.put('joins', this._row)
94
- return this._row
95
- }
96
-
97
- // the reply, kept so a restart opens the handle without joining again
98
- /** @private */
99
- async _keep(reply) {
100
- const { key, encryptionKey, epochs } = reply
101
- await this._save({ key, encryptionKey, epochs: c.encode(epochEntries, epochs) })
102
- }
103
-
104
- /** @private */
105
- async _forget() {
106
- await this.root.local.store.del('joins', this.id).catch(safetyCatch)
107
- }
108
-
109
- /** @private */
110
- _end(settle, value) {
111
- this._onend()
112
- settle(value)
113
- }
114
- }
115
-
116
- /**
117
- * Wait for a join, but stop waiting after `ms`; the join itself goes on.
118
- *
119
- * @param {Promise<import('./index.js').Handle>} done
120
- * @param {number} ms `0` waits as long as the join takes.
121
- * @returns {Promise<import('./index.js').Handle>}
122
- */
123
- export async function wait(done, ms) {
124
- if (!ms) return done
125
- let timer
126
- const timeout = new Promise((resolve, reject) => {
127
- timer = setTimeout(() => reject(CeroError.TIMEOUT('join')), ms)
128
- })
129
- try {
130
- return await Promise.race([done, timeout])
131
- } finally {
132
- clearTimeout(timer)
133
- }
134
- }
135
-
136
- function unpack({ key, encryptionKey, epochs, publicKey, secretKey }) {
137
- const writer = { publicKey, secretKey }
138
- return { key, encryptionKey, epochs: epochs ? c.decode(epochEntries, epochs) : [], writer }
139
- }
@@ -1,63 +0,0 @@
1
- /**
2
- * One handle being joined. It joins with the latest invite and waits as long as it takes: the
3
- * caller's timeout only ends the caller's wait. What it learns is saved as it goes, the writer
4
- * before the join is written and the keys once the reply lands, so a join resumed after a restart
5
- * picks up where it stopped. It ends admitted, denied, expired or cancelled; a close only pauses
6
- * it. How it ends reaches the caller, or `onerror` once nobody waits.
7
- */
8
- export declare class Join {
9
- root: import("./index.js").Handle;
10
- type: string;
11
- spec: object;
12
- id: string;
13
- invite: string;
14
- waiting: number;
15
- cancelled: boolean;
16
- /** @private */
17
- _running;
18
- /** @private */
19
- _row;
20
- /** @type {Promise<import('./index.js').Handle>} */
21
- done: Promise<import('./index.js').Handle>;
22
- /** @private */
23
- _resolve;
24
- /** @private */
25
- _reject;
26
- /** @private */
27
- _onend;
28
- /**
29
- * @param {import('./index.js').Handle} root
30
- * @param {{ type: string, spec: object, discoveryKey: Uint8Array, onend: () => void }} opts
31
- */
32
- constructor(root: import('./index.js').Handle, { type, spec, discoveryKey, onend }: {
33
- type: string;
34
- spec: object;
35
- discoveryKey: Uint8Array;
36
- onend: () => void;
37
- });
38
- /**
39
- * Join with `invite`, taking over from an older attempt: the writer stays, so a reply to the
40
- * older one still lands.
41
- *
42
- * @param {string} invite
43
- */
44
- start(invite: string): Promise<void>;
45
- close(): void;
46
- cancel(): Promise<void>;
47
- /** @private */
48
- private _save;
49
- /** @private */
50
- private _keep;
51
- /** @private */
52
- private _forget;
53
- /** @private */
54
- private _end;
55
- }
56
- /**
57
- * Wait for a join, but stop waiting after `ms`; the join itself goes on.
58
- *
59
- * @param {Promise<import('./index.js').Handle>} done
60
- * @param {number} ms `0` waits as long as the join takes.
61
- * @returns {Promise<import('./index.js').Handle>}
62
- */
63
- export declare function wait(done: Promise<import('./index.js').Handle>, ms: number): Promise<import('./index.js').Handle>;