@cero-base/cero 2.2.1 → 2.3.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.
Files changed (42) hide show
  1. package/README.md +24 -16
  2. package/package.json +7 -6
  3. package/src/build/index.js +38 -34
  4. package/src/build/internal.js +96 -11
  5. package/src/build/schemas.js +119 -19
  6. package/src/extensions/handle-sync.js +8 -12
  7. package/src/extensions/index.js +17 -63
  8. package/src/extensions/mirror.js +44 -0
  9. package/src/extensions/profile-sync.js +10 -12
  10. package/src/handle/index.js +499 -374
  11. package/src/handle/join.js +144 -0
  12. package/src/index.js +89 -118
  13. package/src/lib/bluetooth.js +83 -19
  14. package/src/lib/constants.js +1 -0
  15. package/src/lib/operators.js +298 -128
  16. package/src/lib/peek.js +1 -1
  17. package/src/lib/refs.js +7 -7
  18. package/src/lib/spec.js +32 -0
  19. package/src/local/index.js +27 -3
  20. package/src/rpc/client.js +240 -202
  21. package/src/rpc/server.js +186 -133
  22. package/types/build/index.d.ts +3 -8
  23. package/types/build/internal.d.ts +86 -27
  24. package/types/build/schemas.d.ts +91 -12
  25. package/types/extensions/handle-sync.d.ts +4 -12
  26. package/types/extensions/index.d.ts +59 -59
  27. package/types/extensions/mirror.d.ts +13 -0
  28. package/types/extensions/profile-sync.d.ts +4 -13
  29. package/types/handle/index.d.ts +229 -198
  30. package/types/handle/join.d.ts +63 -0
  31. package/types/index.d.ts +27 -55
  32. package/types/lib/bluetooth.d.ts +57 -19
  33. package/types/lib/constants.d.ts +1 -0
  34. package/types/lib/operators.d.ts +166 -81
  35. package/types/lib/peek.d.ts +2 -2
  36. package/types/lib/refs.d.ts +14 -15
  37. package/types/lib/spec.d.ts +87 -0
  38. package/types/local/index.d.ts +19 -9
  39. package/types/rpc/client.d.ts +55 -112
  40. package/types/rpc/server.d.ts +91 -50
  41. package/src/rpc/index.js +0 -8
  42. package/types/rpc/index.d.ts +0 -7
@@ -1,13 +1,13 @@
1
- import AbortController from 'bare-abort-controller';
2
1
  import ReadyResource from 'ready-resource';
3
2
  import { Identity } from '@cero-base/core/identity';
4
3
  import { Database } from '@cero-base/core/database';
5
- import { Pairing } from '@cero-base/core/pairing';
4
+ import { Mailbox } from '@cero-base/core/mailbox';
6
5
  import { Blobs } from '@cero-base/core/blobs';
7
6
  import { FileServer } from '@cero-base/core/blobs/server';
8
7
  export { Ref } from '../lib/refs.js';
9
8
  export type Network = import('@cero-base/core/network').Network;
10
9
  export type Local = import('../local/index.js').Local;
10
+ export type Spec = import('../lib/spec.js').Spec;
11
11
  export type KeyPair = import('@cero-base/core/identity').KeyPair;
12
12
  export type HandleOpts = {
13
13
  /**
@@ -25,11 +25,11 @@ export type HandleOpts = {
25
25
  /**
26
26
  * Pre-existing Corestore. Falls back to `parent.store.store`.
27
27
  */
28
- store?: any;
28
+ store?: import('corestore');
29
29
  /**
30
30
  * Built cero spec.
31
31
  */
32
- spec?: any;
32
+ spec?: Spec;
33
33
  /**
34
34
  * Local store for per-handle keypairs.
35
35
  */
@@ -37,11 +37,13 @@ export type HandleOpts = {
37
37
  /**
38
38
  * Owned HypercoreStorage to close on shutdown.
39
39
  */
40
- storage?: any;
40
+ storage?: import('hypercore-storage');
41
41
  /**
42
- * Owned identity Discovery to destroy on shutdown.
42
+ * Owned identity discovery to destroy on shutdown.
43
43
  */
44
- discovery?: any;
44
+ discovery?: {
45
+ destroy(): Promise<void>;
46
+ };
45
47
  /**
46
48
  * Data directory (root handles only).
47
49
  */
@@ -49,8 +51,7 @@ export type HandleOpts = {
49
51
  /**
50
52
  * Pass-through cero(...) options.
51
53
  */
52
- opts?: any;
53
- routes?: Record<string, Function>;
54
+ opts?: import('../index.js').CeroOpts;
54
55
  /**
55
56
  * Existing database key.
56
57
  */
@@ -78,33 +79,38 @@ export type HandleOpts = {
78
79
  * When `false`, skips creating a `Pairing` session.
79
80
  */
80
81
  pair?: boolean;
82
+ /**
83
+ * Root only: the radio, up before the root opens.
84
+ */
85
+ bluetooth?: import('../lib/bluetooth.js').Bluetooth | null;
86
+ /**
87
+ * Root only: provision this device as it opens, its genesis or its recovery.
88
+ */
89
+ bootstrap?: {
90
+ name?: string | null;
91
+ isMobile?: boolean;
92
+ recovering?: boolean;
93
+ timeout?: number;
94
+ } | null;
81
95
  };
82
96
  export type CreateChildOpts = {
83
97
  name?: string | null;
84
- routes?: Record<string, Function>;
85
- role?: string;
86
- accept?: boolean;
87
98
  };
88
99
  export type JoinChildOpts = {
89
- routes?: Record<string, Function>;
90
100
  timeout?: number;
91
101
  };
92
102
  export type StaticJoinOpts = {
93
103
  parent?: Handle;
94
104
  network?: Network;
95
105
  identity?: Identity;
96
- store?: any;
97
- spec?: any;
106
+ store?: import('corestore');
107
+ spec?: Spec;
98
108
  namespace?: string;
99
- routes?: Record<string, Function>;
100
- timeout?: number;
101
- };
102
- export type AcceptOpts = {
103
109
  /**
104
- * Role to grant the joining peer. Falls back to the invite's role, then `'member'`.
110
+ * Writer keypair in the joined handle; a fresh one by default.
105
111
  */
106
- role?: string;
107
- name?: string | null;
112
+ writer?: KeyPair;
113
+ timeout?: number;
108
114
  };
109
115
  export type HandleExtra = {
110
116
  /**
@@ -121,55 +127,49 @@ export type HandleExtra = {
121
127
  members?: import('../lib/refs.js').Ref;
122
128
  };
123
129
  export type Child = Handle & HandleExtra;
124
- export type CeroHandle = Handle & Record<string, import('../lib/refs.js').Ref>;
130
+ export type Context = Handle & Record<string, import('../lib/refs.js').Ref>;
125
131
  /**
126
132
  * @typedef {import('@cero-base/core/network').Network} Network
127
133
  * @typedef {import('../local/index.js').Local} Local
134
+ * @typedef {import('../lib/spec.js').Spec} Spec
128
135
  * @typedef {import('@cero-base/core/identity').KeyPair} KeyPair
129
136
  *
130
137
  * @typedef {object} HandleOpts
131
138
  * @property {Handle} [parent] Parent handle when this is a child slot.
132
139
  * @property {Identity} [identity] Long-lived user identity. Inherited from `parent` if omitted.
133
140
  * @property {Network} [network] Shared swarm. Inherited from `parent` if omitted.
134
- * @property {any} [store] Pre-existing Corestore. Falls back to `parent.store.store`.
135
- * @property {any} [spec] Built cero spec.
141
+ * @property {import('corestore')} [store] Pre-existing Corestore. Falls back to `parent.store.store`.
142
+ * @property {Spec} [spec] Built cero spec.
136
143
  * @property {Local} [local] Local store for per-handle keypairs.
137
- * @property {any} [storage] Owned HypercoreStorage to close on shutdown.
138
- * @property {any} [discovery] Owned identity Discovery to destroy on shutdown.
144
+ * @property {import('hypercore-storage')} [storage] Owned HypercoreStorage to close on shutdown.
145
+ * @property {{ destroy(): Promise<void> }} [discovery] Owned identity discovery to destroy on shutdown.
139
146
  * @property {string} [dir] Data directory (root handles only).
140
- * @property {any} [opts] Pass-through cero(...) options.
141
- * @property {Record<string, Function>} [routes]
147
+ * @property {import('../index.js').CeroOpts} [opts] Pass-through cero(...) options.
142
148
  * @property {Uint8Array} [key] Existing database key.
143
149
  * @property {Uint8Array} [encryptionKey] Existing encryption key.
144
150
  * @property {Array<{ epoch: number, entropy: Uint8Array }>} [epochs] Rotation epochs delivered at join.
145
151
  * @property {string} [namespace] Corestore namespace.
146
152
  * @property {KeyPair} [keyPair] Writer keypair.
147
153
  * @property {boolean} [pair] When `false`, skips creating a `Pairing` session.
154
+ * @property {import('../lib/bluetooth.js').Bluetooth | null} [bluetooth] Root only: the radio, up before the root opens.
155
+ * @property {{ name?: string | null, isMobile?: boolean, recovering?: boolean, timeout?: number } | null} [bootstrap] Root only: provision this device as it opens, its genesis or its recovery.
148
156
  *
149
157
  * @typedef {object} CreateChildOpts
150
158
  * @property {string | null} [name]
151
- * @property {Record<string, Function>} [routes]
152
- * @property {string} [role]
153
- * @property {boolean} [accept]
154
159
  *
155
160
  * @typedef {object} JoinChildOpts
156
- * @property {Record<string, Function>} [routes]
157
161
  * @property {number} [timeout]
158
162
  *
159
163
  * @typedef {object} StaticJoinOpts
160
164
  * @property {Handle} [parent]
161
165
  * @property {Network} [network]
162
166
  * @property {Identity} [identity]
163
- * @property {any} [store]
164
- * @property {any} [spec]
167
+ * @property {import('corestore')} [store]
168
+ * @property {Spec} [spec]
165
169
  * @property {string} [namespace]
166
- * @property {Record<string, Function>} [routes]
170
+ * @property {KeyPair} [writer] Writer keypair in the joined handle; a fresh one by default.
167
171
  * @property {number} [timeout]
168
172
  *
169
- * @typedef {object} AcceptOpts
170
- * @property {string} [role] Role to grant the joining peer. Falls back to the invite's role, then `'member'`.
171
- * @property {string | null} [name]
172
- *
173
173
  * @typedef {object} HandleExtra
174
174
  * @property {string | null} [name] Display name; set on child handles by the owner flow.
175
175
  * @property {import('../lib/refs.js').Ref} [profile] `profile` ref, attached dynamically when the schema declares one.
@@ -177,39 +177,66 @@ export type CeroHandle = Handle & Record<string, import('../lib/refs.js').Ref>;
177
177
  *
178
178
  * @typedef {Handle & HandleExtra} Child A child handle plus its dynamically-attached refs.
179
179
  *
180
- * @typedef {Handle & Record<string, import('../lib/refs.js').Ref>} CeroHandle A handle with every schema ref reachable as a `Ref` property (e.g. `me.profile`, `room.messages`).
180
+ * @typedef {Handle & Record<string, import('../lib/refs.js').Ref>} Context A root or a room: a handle with its refs (`me.profile`, `room.messages`).
181
181
  */
182
182
  /**
183
183
  * A cero handle — a single writable database session attached to a swarm.
184
184
  */
185
185
  export declare class Handle extends ReadyResource {
186
- identity: any;
187
- network: any;
188
- spec: any;
186
+ /** @type {Identity} */
187
+ identity: Identity;
188
+ /** @type {Network} */
189
+ network: Network;
190
+ spec: import("../lib/spec.js").Spec;
189
191
  parent: Handle;
190
192
  local: import("../index.js").Local;
191
- _storage: any;
192
- _discovery: any;
193
- _dir: string;
194
- _opts: any;
195
- extensions: any;
196
- operators: any;
197
- _onerror: any;
198
- children: Set<any>;
199
- _typeHooks: Set<any>;
200
- _loading: Map<any, any>;
201
- _joining: Map<any, any>;
202
- _coreKeys: Map<any, any>;
203
- _fileServer: FileServer;
204
- _blobs: Blobs;
205
- _epochBlobs: any;
206
- _sus: any;
207
- _owned: Set<any>;
193
+ /** @type {Mailbox} */
194
+ mailbox: Mailbox;
195
+ /** @private */
196
+ _storage;
197
+ /** @private */
198
+ _discovery;
199
+ /** @private */
200
+ _dir;
201
+ /** @private */
202
+ _opts;
203
+ /** @type {import('../extensions/index.js').Extension[]} */
204
+ extensions: import('../extensions/index.js').Extension[];
205
+ /** @private */
206
+ _onerror;
207
+ /** @type {Set<Handle> | null} */
208
+ children: Set<Handle> | null;
209
+ /** @private */
210
+ _typeHooks;
211
+ /** @private */
212
+ _loading;
213
+ /** @private */
214
+ _joining;
215
+ /** @private */
216
+ _coreKeys;
217
+ /** @private */
218
+ _fileServer;
219
+ /** @private */
220
+ _blobs;
221
+ /** @private */
222
+ _epochBlobs;
223
+ /** @private */
224
+ _sus;
225
+ /** @private */
226
+ _owned;
208
227
  store: Database;
209
- pair: Pairing;
210
- _wantsPair: boolean;
211
- _ac: AbortController;
212
- _invitesSync: (touched: any) => void;
228
+ /** @private */
229
+ _pair;
230
+ /** @private */
231
+ _wantsPair;
232
+ /** @private */
233
+ _boot;
234
+ /** @private */
235
+ _bluetooth;
236
+ /** @private */
237
+ _live;
238
+ /** @private */
239
+ _ac;
213
240
  /** @param {HandleOpts} [opts] */
214
241
  constructor(opts?: HandleOpts);
215
242
  /**
@@ -234,17 +261,26 @@ export declare class Handle extends ReadyResource {
234
261
  */
235
262
  get blobs(): Blobs;
236
263
  /** The handle type of a child, null on the root. */
237
- get type(): any;
238
- /** Canonical id — identity id for the root handle, store key for children. */
239
- get id(): any;
240
- /** This device's id + name. `null` on child handles. */
264
+ get type(): string;
265
+ /**
266
+ * Canonical id: the identity id on the root, the database key on a room.
267
+ *
268
+ * @returns {string | null}
269
+ */
270
+ get id(): string | null;
271
+ /**
272
+ * This device's id and name. `null` on a room.
273
+ *
274
+ * @returns {{ id: string, name: string | null } | null}
275
+ */
241
276
  get device(): {
242
- id: any;
243
- name: any;
244
- };
245
- get suspended(): boolean;
246
- _open(): Promise<void>;
247
- _close(): Promise<void>;
277
+ id: string;
278
+ name: string | null;
279
+ } | null;
280
+ /** @private */
281
+ private _open;
282
+ /** @private */
283
+ private _close;
248
284
  /**
249
285
  * Tie a destroyable resource (a `watch` stream, a timer, any `{ destroy }`) to this
250
286
  * handle's lifecycle — it's destroyed automatically on close, so callers don't track
@@ -258,34 +294,8 @@ export declare class Handle extends ReadyResource {
258
294
  destroy?: Function;
259
295
  once?: Function;
260
296
  }>(resource: T): T;
261
- /**
262
- * `EventEmitter.on` plus an optional `{ signal }` that removes the listener
263
- * when the signal aborts — e.g. `me.on('handle', fn, { signal: me.signal })`.
264
- *
265
- * @param {string} event
266
- * @param {(...args: any[]) => void} fn
267
- * @param {{ signal?: AbortSignal }} [opts]
268
- * @returns {this}
269
- */
270
- on(event: string, fn: (...args: any[]) => void, opts?: {
271
- signal?: AbortSignal;
272
- }): this;
273
- /**
274
- * Resolve a durable file id to an ephemeral download url via this identity's
275
- * file server.
276
- *
277
- * @param {string} id
278
- * @returns {string}
279
- */
280
- getLink(id: string): string;
281
- /**
282
- * Initialise a fresh database: write the genesis claim, derive the writer.
283
- * Forwards to `Database.bootstrap`.
284
- *
285
- * @param {any} [opts]
286
- * @returns {Promise<any>}
287
- */
288
- bootstrap(opts?: any): Promise<any>;
297
+ /** @private */
298
+ private _link;
289
299
  /**
290
300
  * Claim writer capability on an existing database (paired-device flow).
291
301
  * Forwards to `Database.claim`.
@@ -293,80 +303,79 @@ export declare class Handle extends ReadyResource {
293
303
  * @returns {Promise<void>}
294
304
  */
295
305
  claim(): Promise<void>;
296
- /**
297
- * `true` ranks this handle as just touched, `false` takes it out of the swarm until the
298
- * next update lands in it.
299
- *
300
- * @param {boolean} active
301
- */
302
- setActive(active: boolean): void;
303
- /**
304
- * Mint a pairing invite for this handle.
305
- *
306
- * @param {{ role?: string, expiresIn?: number, data?: any }} [opts]
307
- * @returns {Promise<string>} Z32-encoded invite string.
308
- */
309
- invite(opts?: {
310
- role?: string;
311
- expiresIn?: number;
312
- data?: any;
313
- }): Promise<string>;
314
- /**
315
- * Revoke a previously-minted invite by its string form.
316
- *
317
- * @param {string} invite
318
- * @returns {Promise<boolean>} `true` if the invite was found and removed.
319
- */
320
- revoke(invite: string): Promise<boolean>;
321
- /**
322
- * Accept a paired candidate — adds them as a writer (or read-only member)
323
- * and confirms the pairing so they receive this handle's keys.
324
- *
325
- * @param {any} candidate
326
- * @param {AcceptOpts} [opts]
327
- * @returns {Promise<void>}
328
- */
329
- accept(candidate: any, { role, name }?: AcceptOpts): Promise<void>;
330
- /**
331
- * Leave a child handle — removes it from the parent's `handles` collection
332
- * and closes the session. No-op on root handles.
333
- *
334
- * @returns {Promise<void>}
335
- */
336
- leave(): Promise<void>;
337
- /**
338
- * Pause networking + storage. Idempotent; no-op on child handles.
339
- *
340
- * @returns {Promise<void>}
341
- */
342
- suspend(): Promise<void>;
343
- /**
344
- * Resume a suspended root handle. Idempotent; no-op on child handles.
345
- *
346
- * @returns {Promise<void>}
347
- */
348
- resume(): Promise<void>;
306
+ /** @private */
307
+ private _invite;
308
+ /** @private */
309
+ private _revoke;
310
+ /** @private */
311
+ private _answer;
312
+ /** @private */
313
+ private _rotate;
314
+ /** @private */
315
+ private _tx;
316
+ /** @private */
317
+ private _pairing;
318
+ /** @private */
319
+ private _leave;
320
+ /** @private */
321
+ private _suspend;
322
+ /** @private */
323
+ private _resume;
324
+ /** @private */
325
+ private _lifecycle;
326
+ /** @private */
327
+ private _active;
328
+ /** @private */
329
+ private _status;
330
+ /** @private */
331
+ private _onStatus;
332
+ /** @private */
333
+ private _nearby;
334
+ /** @private */
335
+ private _tell;
336
+ /** @private */
337
+ private _peers;
338
+ /** @private */
339
+ private _nameOf;
340
+ /** @private */
341
+ private _onPeers;
342
+ /** @private */
343
+ private _onRadio;
344
+ /** @private */
345
+ private _joins;
346
+ /** @private */
347
+ private _onJoins;
348
+ /** @private */
349
+ private _phrase;
349
350
  /**
350
351
  * @param {Uint8Array} coreKey
351
352
  * @param {object} info
352
353
  * @returns {{ key: Uint8Array, encryptionKey: Uint8Array } | null}
354
+ * @private
353
355
  */
354
- _resolveCore(coreKey: Uint8Array, info: object): {
355
- key: Uint8Array;
356
- encryptionKey: Uint8Array;
357
- } | null;
358
- _baseBlobs(): Blobs;
359
- _makeBlobs(name: any, encryptionKey: any, stamp: any): Blobs;
356
+ private _resolveCore;
357
+ /** @private */
358
+ private _baseBlobs;
359
+ /** @private */
360
+ private _makeBlobs;
360
361
  /**
361
362
  * Remember the blob-core key a file id points at so the file server can open the core.
362
363
  *
363
364
  * @param {string} id
364
365
  * @param {number} [stamp]
366
+ * @private
365
367
  */
366
- _registerBlobCore(id: string, stamp?: number): void;
367
- _blobCoreKey(stamp: any): Uint8Array<ArrayBufferLike>;
368
- _syncInvites(): Promise<void>;
369
- _checkGrant(role: any): Promise<void>;
368
+ private _registerBlobCore;
369
+ /**
370
+ * The bytes of a file this handle holds, fetched from a peer when this device lacks them.
371
+ *
372
+ * @param {string} id
373
+ * @returns {Promise<Uint8Array>}
374
+ * @private
375
+ */
376
+ private _bytes;
377
+ /** @private */
378
+ private _blobCoreKey;
370
379
  /**
371
380
  * Create a new child handle of `type`. Owner-flow — generates a fresh writer, adds it as a
372
381
  * writer + member, and registers the child on the parent's `handles` collection.
@@ -374,25 +383,38 @@ export declare class Handle extends ReadyResource {
374
383
  * @param {string} type
375
384
  * @param {CreateChildOpts} [opts]
376
385
  * @returns {Promise<Handle>}
386
+ * @private
377
387
  */
378
- _create(type: string, { name, routes, role, accept }?: CreateChildOpts): Promise<Handle>;
388
+ private _create;
379
389
  /**
380
- * Join a child handle by invite (joiner-flow).
390
+ * Join a child handle by invite. The caller waits up to `timeout`; the join itself goes on
391
+ * until it is admitted, denied or expired, across restarts, and the handle then arrives
392
+ * with the `handle` event.
381
393
  *
382
394
  * @param {string} invite
383
395
  * @param {string} type
384
396
  * @param {JoinChildOpts} [opts]
385
397
  * @returns {Promise<Handle>}
398
+ * @private
386
399
  */
387
- _join(invite: string, type: string, opts?: JoinChildOpts): Promise<Handle>;
400
+ private _join;
401
+ /** @private */
402
+ private _start;
403
+ /** @private */
404
+ private _cancel;
405
+ /** @private */
406
+ private _joined;
388
407
  /**
389
- * @param {string} invite
408
+ * Open a joined handle with the keys its reply delivered, once this writer is admitted.
409
+ *
390
410
  * @param {string} type
391
- * @param {JoinChildOpts} [opts]
392
- * @param {Uint8Array | null} [target]
411
+ * @param {import('@cero-base/core/pairing').JoinResult} reply
393
412
  * @returns {Promise<Handle>}
413
+ * @private
394
414
  */
395
- _pair(invite: string, type: string, { routes, timeout }?: JoinChildOpts, target?: Uint8Array | null): Promise<Handle>;
415
+ private _enter;
416
+ /** @private */
417
+ private _carryOn;
396
418
  /**
397
419
  * Get an open child by id, or re-open it. Concurrent calls for the same id share one
398
420
  * in-flight load, so the child is built — and `handle` emitted — exactly once.
@@ -400,8 +422,9 @@ export declare class Handle extends ReadyResource {
400
422
  * @param {string} type
401
423
  * @param {string} id
402
424
  * @returns {Promise<Handle>}
425
+ * @private
403
426
  */
404
- _load(type: string, id: string, opts: any): Promise<Handle>;
427
+ private _load;
405
428
  /**
406
429
  * Reconstruct a child handle by id. Reuses the stored writer keypair if
407
430
  * available; otherwise generates a fresh one and claims writer capability.
@@ -409,43 +432,51 @@ export declare class Handle extends ReadyResource {
409
432
  * @param {string} type
410
433
  * @param {string} id
411
434
  * @returns {Promise<Handle>}
412
- */
413
- _reopen(type: string, id: string, opts: any): Promise<Handle>;
414
- _suspend(): Promise<void>;
415
- _resume(): Promise<void>;
416
- _adopt(child: any, info: any): void;
417
- _hookType(op: any, ref: any, fn: any, opts: any): () => void;
418
- /**
419
- * @param {Handle} child
420
- * @param {{ role?: string }} [opts]
421
- */
422
- _wireAccept(child: Handle, { role }?: {
423
- role?: string;
424
- }): void;
435
+ * @private
436
+ */
437
+ private _reopen;
438
+ /** @private */
439
+ private _room;
440
+ /** @private */
441
+ private _sleep;
442
+ /** @private */
443
+ private _wake;
444
+ /** @private */
445
+ private _adopt;
446
+ /** @private */
447
+ private _hookType;
448
+ /** @private */
449
+ private _serve;
450
+ /** @private */
451
+ private _reserve;
425
452
  /**
426
453
  * @param {string} id
427
454
  * @param {KeyPair | { publicKey: Uint8Array, secretKey: Uint8Array } | null} keyPair
428
455
  * @returns {Promise<void>}
456
+ * @private
429
457
  */
430
- _saveKeyPair(id: string, keyPair: KeyPair | {
431
- publicKey: Uint8Array;
432
- secretKey: Uint8Array;
433
- } | null): Promise<void>;
458
+ private _saveKeyPair;
434
459
  /**
435
460
  * @param {string} id
436
461
  * @returns {Promise<{ publicKey: Uint8Array, secretKey: Uint8Array } | null>}
462
+ * @private
437
463
  */
438
- _loadKeyPair(id: string): Promise<{
439
- publicKey: Uint8Array;
440
- secretKey: Uint8Array;
441
- } | null>;
464
+ private _loadKeyPair;
442
465
  /**
443
- * Pair into an existing handle via an invite, returning a brand-new
444
- * `Handle` already configured with the resolved key + encryption key.
466
+ * Pair into an existing handle via an invite, returning a brand-new `Handle` opened with the
467
+ * delivered keys. A one-shot join: the root's `_join` is the one that survives restarts.
445
468
  *
446
469
  * @param {string} invite
447
470
  * @param {StaticJoinOpts} [opts]
448
471
  * @returns {Promise<Handle>}
449
472
  */
450
- static join(invite: string, { parent, network, identity, store, spec, namespace, routes, timeout }?: StaticJoinOpts): Promise<Handle>;
473
+ static join(invite: string, { parent, network, identity, store, spec, namespace, writer, timeout }?: StaticJoinOpts): Promise<Handle>;
474
+ /**
475
+ * A handle opened with the keys a pairing reply delivered.
476
+ *
477
+ * @param {import('@cero-base/core/pairing').JoinResult} reply
478
+ * @param {Omit<HandleOpts, 'key' | 'encryptionKey' | 'epochs' | 'keyPair'>} opts
479
+ * @returns {Handle}
480
+ */
481
+ static fromReply(reply: import('@cero-base/core/pairing').JoinResult, opts: Omit<HandleOpts, 'key' | 'encryptionKey' | 'epochs' | 'keyPair'>): Handle;
451
482
  }
@@ -0,0 +1,63 @@
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>;