@cero-base/cero 2.12.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.
- package/package.json +6 -5
- package/src/build/index.js +296 -57
- package/src/build/internal.js +80 -5
- package/src/build/schemas.js +118 -25
- package/src/extensions/profile-sync.js +17 -1
- package/src/handle/index.js +1217 -189
- package/src/index.js +68 -35
- package/src/lib/constants.js +4 -4
- package/src/lib/operators.js +53 -14
- package/src/lib/peek.js +14 -8
- package/src/lib/reserved.js +61 -0
- package/src/lib/spec.js +7 -1
- package/src/lib/swap.js +59 -6
- package/src/local/index.js +6 -2
- package/src/push/index.js +26 -13
- package/src/rpc/client.js +24 -10
- package/src/rpc/push.js +9 -3
- package/src/rpc/server.js +132 -51
- package/types/build/internal.d.ts +57 -0
- package/types/build/schemas.d.ts +58 -5
- package/types/extensions/index.d.ts +2 -0
- package/types/extensions/profile-sync.d.ts +1 -1
- package/types/handle/index.d.ts +121 -15
- package/types/index.d.ts +9 -6
- package/types/lib/constants.d.ts +2 -2
- package/types/lib/operators.d.ts +16 -6
- package/types/lib/peek.d.ts +7 -4
- package/types/lib/reserved.d.ts +1 -0
- package/types/lib/spec.d.ts +35 -2
- package/types/lib/swap.d.ts +28 -3
- package/types/push/index.d.ts +4 -12
- package/types/rpc/client.d.ts +34 -18
- package/types/rpc/server.d.ts +24 -3
- package/src/handle/join.js +0 -139
- package/types/handle/join.d.ts +0 -63
package/types/lib/spec.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export type RefInfo = {
|
|
|
15
15
|
*/
|
|
16
16
|
internal?: boolean;
|
|
17
17
|
/**
|
|
18
|
-
* Computed on the device, never stored: status,
|
|
18
|
+
* Computed on the device, never stored: status, nearby, topics.
|
|
19
19
|
*/
|
|
20
20
|
live?: boolean;
|
|
21
21
|
/**
|
|
@@ -35,6 +35,10 @@ export type RefInfo = {
|
|
|
35
35
|
* Whether a write to this ref pushes a notification.
|
|
36
36
|
*/
|
|
37
37
|
push?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* The contract that brought it, 0 for one the spec had when its history began.
|
|
40
|
+
*/
|
|
41
|
+
since?: number;
|
|
38
42
|
};
|
|
39
43
|
export type SpecMeta = {
|
|
40
44
|
ns?: string;
|
|
@@ -43,6 +47,29 @@ export type SpecMeta = {
|
|
|
43
47
|
*/
|
|
44
48
|
type?: string;
|
|
45
49
|
version?: number;
|
|
50
|
+
/**
|
|
51
|
+
* What a write means to Cero, raised by a release that changes it.
|
|
52
|
+
*/
|
|
53
|
+
rules?: number;
|
|
54
|
+
/**
|
|
55
|
+
* The contract the spec's history begins at.
|
|
56
|
+
*/
|
|
57
|
+
start?: number;
|
|
58
|
+
/**
|
|
59
|
+
* The contract this build asks databases to use.
|
|
60
|
+
*/
|
|
61
|
+
target?: number;
|
|
62
|
+
/**
|
|
63
|
+
* The contract that brought each route, and each field of each stored type, by full name.
|
|
64
|
+
*/
|
|
65
|
+
since?: {
|
|
66
|
+
routes: Record<string, number>;
|
|
67
|
+
types: Record<string, Record<string, number>>;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* The schema version to encode with as of each contract.
|
|
71
|
+
*/
|
|
72
|
+
encoding?: Record<string, number>;
|
|
46
73
|
refs: Record<string, RefInfo>;
|
|
47
74
|
local?: {
|
|
48
75
|
refs: Record<string, RefInfo>;
|
|
@@ -69,17 +96,23 @@ export type Row = import('@cero-base/core/database').Row;
|
|
|
69
96
|
* @property {string} [schema]
|
|
70
97
|
* @property {string} [type] The handle type a `handle` ref opens.
|
|
71
98
|
* @property {boolean} [internal] A builtin: members, devices, files and the like.
|
|
72
|
-
* @property {boolean} [live] Computed on the device, never stored: status,
|
|
99
|
+
* @property {boolean} [live] Computed on the device, never stored: status, nearby, topics.
|
|
73
100
|
* @property {string[]} [fields] The declared fields, in schema order.
|
|
74
101
|
* @property {Record<string, string>} [required] The required fields and their types.
|
|
75
102
|
* @property {string[]} [files] The fields holding file ids.
|
|
76
103
|
* @property {string} [verb]
|
|
77
104
|
* @property {boolean} [push] Whether a write to this ref pushes a notification.
|
|
105
|
+
* @property {number} [since] The contract that brought it, 0 for one the spec had when its history began.
|
|
78
106
|
*
|
|
79
107
|
* @typedef {object} SpecMeta
|
|
80
108
|
* @property {string} [ns]
|
|
81
109
|
* @property {string} [type] The handle type, on a handle's spec.
|
|
82
110
|
* @property {number} [version]
|
|
111
|
+
* @property {number} [rules] What a write means to Cero, raised by a release that changes it.
|
|
112
|
+
* @property {number} [start] The contract the spec's history begins at.
|
|
113
|
+
* @property {number} [target] The contract this build asks databases to use.
|
|
114
|
+
* @property {{ routes: Record<string, number>, types: Record<string, Record<string, number>> }} [since] The contract that brought each route, and each field of each stored type, by full name.
|
|
115
|
+
* @property {Record<string, number>} [encoding] The schema version to encode with as of each contract.
|
|
83
116
|
* @property {Record<string, RefInfo>} refs
|
|
84
117
|
* @property {{ refs: Record<string, RefInfo> }} [local]
|
|
85
118
|
* @property {Record<string, SpecMeta>} [handles]
|
package/types/lib/swap.d.ts
CHANGED
|
@@ -15,12 +15,30 @@ export declare const nextOf: (dir: string) => string;
|
|
|
15
15
|
export declare function current(dir: string): Promise<string>;
|
|
16
16
|
/**
|
|
17
17
|
* A swap killed part way, finished or undone before anything opens `dir`: past its first rename the incoming
|
|
18
|
-
* identity had opened, so it takes main's place; without it the old one comes back.
|
|
19
|
-
* in `main.next` goes, unless a swap is running there.
|
|
18
|
+
* identity had opened, so it takes main's place; without it the old one comes back.
|
|
20
19
|
*
|
|
21
20
|
* @param {string} dir
|
|
22
21
|
*/
|
|
23
22
|
export declare function settle(dir: string): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* The lock on `dir`, a file beside the stores, held from before a store closes or opens until the one in `main` is
|
|
25
|
+
* open: a store's own lock goes with its close and moves with its directory. Another holder is waited for, a try
|
|
26
|
+
* every few ms; a kill frees it.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} dir
|
|
29
|
+
* @param {number} [timeout] The longest wait in ms, 0 for no limit.
|
|
30
|
+
* @returns {Promise<{ close: () => Promise<void> }>} Held until closed.
|
|
31
|
+
*/
|
|
32
|
+
export declare function lock(dir: string, timeout?: number): Promise<{
|
|
33
|
+
close: () => Promise<void>;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* What a killed swap left in `main.next` goes, unless a swap is running there. Only once `main` is open: a swap in
|
|
37
|
+
* another process keeps main open while the incoming identity opens.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} dir
|
|
40
|
+
*/
|
|
41
|
+
export declare function sweep(dir: string): Promise<void>;
|
|
24
42
|
/**
|
|
25
43
|
* Run `fn` with `dir/main.next` set aside for it, emptied before and after.
|
|
26
44
|
*
|
|
@@ -31,7 +49,14 @@ export declare function settle(dir: string): Promise<void>;
|
|
|
31
49
|
*/
|
|
32
50
|
export declare function staging<T>(dir: string, fn: () => Promise<T>): Promise<T>;
|
|
33
51
|
/**
|
|
34
|
-
* The identity opened and closed in `main.next
|
|
52
|
+
* The identity opened and closed in `main.next`, all of it on disk: a rename that follows never points at data
|
|
53
|
+
* still in a cache.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} dir
|
|
56
|
+
*/
|
|
57
|
+
export declare function flush(dir: string): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* The identity opened, closed and flushed in `main.next` takes main's place. Both must be closed.
|
|
35
60
|
*
|
|
36
61
|
* @param {string} dir
|
|
37
62
|
*/
|
package/types/push/index.d.ts
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A push verified against the
|
|
3
|
-
* rode along and this device holds the handle's keys.
|
|
2
|
+
* A push verified against the handle its topic names, read from that handle's `topics` row alone,
|
|
3
|
+
* with the row it wrote when the block rode along and this device holds the handle's keys.
|
|
4
4
|
*
|
|
5
5
|
* @param {import('hypercore-storage')} storage
|
|
6
6
|
* @param {import('../lib/spec.js').Spec} spec
|
|
7
7
|
* @param {Uint8Array} payload
|
|
8
|
-
* @param {
|
|
8
|
+
* @param {import('@cero-base/core/storage').Storage} store The root's local store, which keeps the `topics` rows.
|
|
9
9
|
* @returns {Promise<{ id: string, type: string, name: string | null, newer: boolean, ref: string | null, row: object | null } | null>}
|
|
10
10
|
*/
|
|
11
|
-
export declare function read(storage: import('hypercore-storage'), spec: import('../lib/spec.js').Spec, payload: Uint8Array,
|
|
12
|
-
id: string;
|
|
13
|
-
handle: string;
|
|
14
|
-
type: string;
|
|
15
|
-
key: Uint8Array;
|
|
16
|
-
name: string | null;
|
|
17
|
-
encryptionKey?: Uint8Array | null;
|
|
18
|
-
epochs?: Uint8Array | null;
|
|
19
|
-
}>): Promise<{
|
|
11
|
+
export declare function read(storage: import('hypercore-storage'), spec: import('../lib/spec.js').Spec, payload: Uint8Array, store: import('@cero-base/core/storage').Storage): Promise<{
|
|
20
12
|
id: string;
|
|
21
13
|
type: string;
|
|
22
14
|
name: string | null;
|
package/types/rpc/client.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ declare const remote: {
|
|
|
11
11
|
del(ref: verbs.Ref, id?: string): Promise<void>;
|
|
12
12
|
call(ref: verbs.Ref, d?: verbs.Row): Promise<void>;
|
|
13
13
|
get(ref: verbs.Ref, q?: string | Record<string, unknown>): Promise<verbs.SingleResult | verbs.ListResult>;
|
|
14
|
+
supports(ref: verbs.Ref, field?: string): Promise<boolean>;
|
|
14
15
|
watch(ref: verbs.Ref, query?: string | (Record<string, unknown> & {
|
|
15
16
|
changes?: boolean;
|
|
16
17
|
}), opts?: {
|
|
@@ -65,7 +66,7 @@ declare const remote: {
|
|
|
65
66
|
topic?: string;
|
|
66
67
|
}): Promise<void>;
|
|
67
68
|
};
|
|
68
|
-
export declare const put: typeof verbs.put, set: typeof verbs.set, get: typeof verbs.get, del: typeof verbs.del, watch: typeof verbs.watch, call: typeof verbs.call, open: typeof verbs.open, invite: typeof verbs.invite, revoke: typeof verbs.revoke, rotate: typeof verbs.rotate, accept: typeof verbs.accept, deny: typeof verbs.deny, leave: typeof verbs.leave, removed: typeof verbs.removed, close: typeof verbs.close, cancel: typeof verbs.cancel, suspend: typeof verbs.suspend, resume: typeof verbs.resume, activate: typeof verbs.activate, deactivate: typeof verbs.deactivate, phrase: typeof verbs.phrase, nearby: typeof verbs.nearby;
|
|
69
|
+
export declare const put: typeof verbs.put, set: typeof verbs.set, get: typeof verbs.get, del: typeof verbs.del, watch: typeof verbs.watch, call: typeof verbs.call, supports: typeof verbs.supports, open: typeof verbs.open, invite: typeof verbs.invite, revoke: typeof verbs.revoke, rotate: typeof verbs.rotate, accept: typeof verbs.accept, deny: typeof verbs.deny, leave: typeof verbs.leave, removed: typeof verbs.removed, close: typeof verbs.close, cancel: typeof verbs.cancel, suspend: typeof verbs.suspend, resume: typeof verbs.resume, activate: typeof verbs.activate, deactivate: typeof verbs.deactivate, phrase: typeof verbs.phrase, nearby: typeof verbs.nearby;
|
|
69
70
|
export { t, schema };
|
|
70
71
|
export type BaseRPCClient = import('@cero-base/core/rpc').RPCClient;
|
|
71
72
|
export type RefInfo = import('../lib/spec.js').RefInfo;
|
|
@@ -79,9 +80,31 @@ export type HandleStub = {
|
|
|
79
80
|
name: string | null;
|
|
80
81
|
};
|
|
81
82
|
export type Context = (Client | Handle) & Record<string, Ref>;
|
|
83
|
+
export type ClientOpts = {
|
|
84
|
+
/**
|
|
85
|
+
* Where the worker's background errors go, the console without one.
|
|
86
|
+
*/
|
|
87
|
+
onerror?: (err: Error) => void;
|
|
88
|
+
/**
|
|
89
|
+
* The platform's messaging service, to follow the handles' push topics.
|
|
90
|
+
*/
|
|
91
|
+
push?: import('./push.js').Messaging;
|
|
92
|
+
/**
|
|
93
|
+
* On a worker whose store is new, it recovers the identity of this phrase, as `cero(dir, spec, { phrase })`.
|
|
94
|
+
*/
|
|
95
|
+
phrase?: string;
|
|
96
|
+
/**
|
|
97
|
+
* On a worker whose store is new, it links to the identity of this device invite, as `cero(dir, spec, { invite })`.
|
|
98
|
+
*/
|
|
99
|
+
invite?: string;
|
|
100
|
+
/**
|
|
101
|
+
* With `invite`: the request's id as it is sent.
|
|
102
|
+
*/
|
|
103
|
+
onrequest?: (id: string) => void;
|
|
104
|
+
};
|
|
82
105
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
106
|
+
* Switch the app to the identity of a recovery phrase, as `cero.restore` does in the worker: the
|
|
107
|
+
* client is then that identity.
|
|
85
108
|
*
|
|
86
109
|
* @param {Client} me
|
|
87
110
|
* @param {string} phrase
|
|
@@ -126,6 +149,8 @@ declare class LocalRefs {
|
|
|
126
149
|
* across the wire.
|
|
127
150
|
*/
|
|
128
151
|
export declare class Client extends RPCClient {
|
|
152
|
+
/** @private */
|
|
153
|
+
_how;
|
|
129
154
|
/** @private */
|
|
130
155
|
_push;
|
|
131
156
|
/** @private */
|
|
@@ -147,12 +172,9 @@ export declare class Client extends RPCClient {
|
|
|
147
172
|
/**
|
|
148
173
|
* @param {import('streamx').Duplex} ipc Framed IPC stream (must be writable).
|
|
149
174
|
* @param {Spec} spec Compiled cero spec (schema + rpc + handles).
|
|
150
|
-
* @param {
|
|
175
|
+
* @param {ClientOpts} [opts]
|
|
151
176
|
*/
|
|
152
|
-
constructor(ipc: import('streamx').Duplex, spec: Spec, { onerror, push }?:
|
|
153
|
-
onerror?: (err: Error) => void;
|
|
154
|
-
push?: import('./push.js').Messaging;
|
|
155
|
-
});
|
|
177
|
+
constructor(ipc: import('streamx').Duplex, spec: Spec, { onerror, push, phrase, invite, onrequest }?: ClientOpts);
|
|
156
178
|
/** @private */
|
|
157
179
|
private _pumpErrors;
|
|
158
180
|
/** @private */
|
|
@@ -217,26 +239,20 @@ declare class Handle {
|
|
|
217
239
|
*
|
|
218
240
|
* @param {import('streamx').Duplex} ipc
|
|
219
241
|
* @param {Spec} spec
|
|
220
|
-
* @param {
|
|
242
|
+
* @param {ClientOpts} [opts]
|
|
221
243
|
* @returns {Promise<Client>}
|
|
222
244
|
*/
|
|
223
|
-
export declare function connect(ipc: import('streamx').Duplex, spec: Spec, opts?:
|
|
224
|
-
onerror?: (err: Error) => void;
|
|
225
|
-
push?: import('./push.js').Messaging;
|
|
226
|
-
}): Promise<Client>;
|
|
245
|
+
export declare function connect(ipc: import('streamx').Duplex, spec: Spec, opts?: ClientOpts): Promise<Client>;
|
|
227
246
|
/**
|
|
228
247
|
* Symmetric client entry. Mirrors the main `cero`, but `cero(ipc, spec)` connects to a
|
|
229
248
|
* server (via `connect`) instead of opening a local store.
|
|
230
249
|
*
|
|
231
250
|
* @param {import('streamx').Duplex} ipc Framed IPC duplex stream.
|
|
232
251
|
* @param {Spec} spec Built cero spec.
|
|
233
|
-
* @param {
|
|
252
|
+
* @param {ClientOpts} [opts]
|
|
234
253
|
* @returns {Promise<Context>}
|
|
235
254
|
*/
|
|
236
|
-
declare function start(ipc: import('streamx').Duplex, spec: Spec, opts?:
|
|
237
|
-
onerror?: (err: Error) => void;
|
|
238
|
-
push?: import('./push.js').Messaging;
|
|
239
|
-
}): Promise<Context>;
|
|
255
|
+
declare function start(ipc: import('streamx').Duplex, spec: Spec, opts?: ClientOpts): Promise<Context>;
|
|
240
256
|
/** @type {typeof start & typeof remote & { connect: typeof connect, restore: typeof restore, link: typeof link, t: typeof t, schema: typeof schema }} */
|
|
241
257
|
export declare const cero: typeof start & typeof remote & {
|
|
242
258
|
connect: typeof connect;
|
package/types/rpc/server.d.ts
CHANGED
|
@@ -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:
|
|
72
|
-
* root via `cero()` as soon as it opens, so the network is up while the UI still loads
|
|
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.
|
package/src/handle/join.js
DELETED
|
@@ -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
|
-
}
|
package/types/handle/join.d.ts
DELETED
|
@@ -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>;
|