@serfab/cadre-client 0.0.0-stage → 2.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/ADOPTING.md +220 -0
- package/README.md +129 -3
- package/dist/address.d.ts +45 -0
- package/dist/address.js +109 -0
- package/dist/address.js.map +1 -0
- package/dist/app-link/index.d.ts +16 -0
- package/dist/app-link/index.js +17 -0
- package/dist/app-link/index.js.map +1 -0
- package/dist/app-link/join-response.d.ts +31 -0
- package/dist/app-link/join-response.js +87 -0
- package/dist/app-link/join-response.js.map +1 -0
- package/dist/app-link/known-apps.d.ts +32 -0
- package/dist/app-link/known-apps.js +51 -0
- package/dist/app-link/known-apps.js.map +1 -0
- package/dist/app-link/link-request-store.d.ts +50 -0
- package/dist/app-link/link-request-store.js +101 -0
- package/dist/app-link/link-request-store.js.map +1 -0
- package/dist/app-link/links.d.ts +47 -0
- package/dist/app-link/links.js +76 -0
- package/dist/app-link/links.js.map +1 -0
- package/dist/app-link/recommend-links.d.ts +30 -0
- package/dist/app-link/recommend-links.js +33 -0
- package/dist/app-link/recommend-links.js.map +1 -0
- package/dist/app-link/recommendation-store.d.ts +54 -0
- package/dist/app-link/recommendation-store.js +99 -0
- package/dist/app-link/recommendation-store.js.map +1 -0
- package/dist/bootstrap.d.ts +106 -0
- package/dist/bootstrap.js +106 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/inbox.d.ts +10 -0
- package/dist/inbox.js +28 -0
- package/dist/inbox.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/invitation-text.d.ts +81 -0
- package/dist/invitation-text.js +80 -0
- package/dist/invitation-text.js.map +1 -0
- package/dist/lifecycle/background-runner.d.ts +94 -0
- package/dist/lifecycle/background-runner.js +278 -0
- package/dist/lifecycle/background-runner.js.map +1 -0
- package/dist/lifecycle/index.d.ts +9 -0
- package/dist/lifecycle/index.js +10 -0
- package/dist/lifecycle/index.js.map +1 -0
- package/dist/loader.d.ts +47 -0
- package/dist/loader.js +93 -0
- package/dist/loader.js.map +1 -0
- package/dist/model.d.ts +284 -0
- package/dist/model.js +22 -0
- package/dist/model.js.map +1 -0
- package/dist/node-code-text.d.ts +10 -0
- package/dist/node-code-text.js +30 -0
- package/dist/node-code-text.js.map +1 -0
- package/dist/result.d.ts +79 -0
- package/dist/result.js +84 -0
- package/dist/result.js.map +1 -0
- package/dist/sereus/client.d.ts +49 -0
- package/dist/sereus/client.js +420 -0
- package/dist/sereus/client.js.map +1 -0
- package/dist/sereus/edge-host.d.ts +22 -0
- package/dist/sereus/edge-host.js +61 -0
- package/dist/sereus/edge-host.js.map +1 -0
- package/dist/sereus/host.d.ts +57 -0
- package/dist/sereus/host.js +2 -0
- package/dist/sereus/host.js.map +1 -0
- package/dist/sereus/index.d.ts +8 -0
- package/dist/sereus/index.js +9 -0
- package/dist/sereus/index.js.map +1 -0
- package/dist/sereus/invitations.d.ts +16 -0
- package/dist/sereus/invitations.js +196 -0
- package/dist/sereus/invitations.js.map +1 -0
- package/dist/sereus/node-code.d.ts +52 -0
- package/dist/sereus/node-code.js +51 -0
- package/dist/sereus/node-code.js.map +1 -0
- package/dist/store.d.ts +27 -0
- package/dist/store.js +51 -0
- package/dist/store.js.map +1 -0
- package/dist/stores.d.ts +37 -0
- package/dist/stores.js +45 -0
- package/dist/stores.js.map +1 -0
- package/examples/README.md +20 -0
- package/examples/nativescript/cadrons-page.xml +24 -0
- package/examples/nativescript/cadrons-view-model.ts +68 -0
- package/examples/react-native/CadronList.tsx +204 -0
- package/examples/react-native/InvitationPanel.tsx +162 -0
- package/examples/react-native/useStore.ts +11 -0
- package/examples/svelte/CadronList.svelte +86 -0
- package/examples/svelte/InvitationPanel.svelte +79 -0
- package/package.json +65 -3
- package/src/address.ts +125 -0
- package/src/app-link/index.ts +45 -0
- package/src/app-link/join-response.ts +109 -0
- package/src/app-link/known-apps.ts +70 -0
- package/src/app-link/link-request-store.ts +129 -0
- package/src/app-link/links.ts +90 -0
- package/src/app-link/recommend-links.ts +49 -0
- package/src/app-link/recommendation-store.ts +132 -0
- package/src/bootstrap.ts +184 -0
- package/src/globals.d.ts +26 -0
- package/src/inbox.ts +36 -0
- package/src/index.ts +15 -0
- package/src/invitation-text.ts +156 -0
- package/src/lifecycle/background-runner.ts +363 -0
- package/src/lifecycle/index.ts +17 -0
- package/src/loader.ts +103 -0
- package/src/model.ts +299 -0
- package/src/node-code-text.ts +25 -0
- package/src/result.ts +131 -0
- package/src/sereus/client.ts +454 -0
- package/src/sereus/edge-host.ts +81 -0
- package/src/sereus/host.ts +56 -0
- package/src/sereus/index.ts +8 -0
- package/src/sereus/invitations.ts +206 -0
- package/src/sereus/node-code.ts +80 -0
- package/src/store.ts +61 -0
- package/src/stores.ts +59 -0
package/src/model.ts
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cadre as an app sees it: this party's own machines, the owner key that binds them,
|
|
3
|
+
* and whether anybody can reach this device to answer an invitation.
|
|
4
|
+
*
|
|
5
|
+
* Machines join the way sereus 1.14 has them join: a node waiting to be claimed shows a
|
|
6
|
+
* node code (`sereus-join:1.…`, as a QR code, a link or text); the owner's device reads
|
|
7
|
+
* it, the person approves, and the device claims the node. A code carries the node's
|
|
8
|
+
* one-time claim secret: it is never logged, and only the peer id appears in logs.
|
|
9
|
+
*
|
|
10
|
+
* These are interfaces. `@serfab/cadre-client/sereus` implements them over a running
|
|
11
|
+
* cadre-core node; an app's mock implements them over sample data.
|
|
12
|
+
*/
|
|
13
|
+
import type { CadreResult } from './result.js';
|
|
14
|
+
import type { BootstrapPlan, CadronPreferences } from './bootstrap.js';
|
|
15
|
+
import type { InvitationText } from './invitation-text.js';
|
|
16
|
+
|
|
17
|
+
export type CadreMachine = {
|
|
18
|
+
peerId: string;
|
|
19
|
+
/** This device. */
|
|
20
|
+
thisDevice: boolean;
|
|
21
|
+
/** Where the cadre last recorded it as reachable; may be empty. */
|
|
22
|
+
addresses: string[];
|
|
23
|
+
/** The machine says it stays on (a server, a cadre-host node); null until it has said, or for this device. */
|
|
24
|
+
alwaysOn: boolean | null;
|
|
25
|
+
/** When the machine last re-published its own record (epoch ms): roughly when it was last heard from. Null when unknown. */
|
|
26
|
+
lastSeenAt: number | null;
|
|
27
|
+
/**
|
|
28
|
+
* This device holds a control-network connection to it right now (any kind, relayed included).
|
|
29
|
+
* Reliable for an always-on cadron, which every machine dials; two phones are rarely connected
|
|
30
|
+
* to each other even while both run, so for a phone read `lastSeenAt` too (`cadronPresence`).
|
|
31
|
+
* For this device: its own node is running.
|
|
32
|
+
*/
|
|
33
|
+
connected: boolean;
|
|
34
|
+
/** The person's own name for it (`CadronPreferences.names`), or null. */
|
|
35
|
+
name?: string | null;
|
|
36
|
+
/**
|
|
37
|
+
* It passes this device's membership check (an owner vouched for it, or it redeemed a valid
|
|
38
|
+
* invitation); false for a row that does not, null before its record has reached this device.
|
|
39
|
+
* Show an untrusted cadron as such, not like the others.
|
|
40
|
+
*/
|
|
41
|
+
trusted?: boolean | null;
|
|
42
|
+
/** Its identity key (base64url), or null when not known. */
|
|
43
|
+
publicKey?: string | null;
|
|
44
|
+
/** Named first in invitations (`CadronPreferences.preferred`). */
|
|
45
|
+
preferred?: boolean;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A cadron: one machine of a person's cadre (a phone, a home server, a rented node). The term
|
|
50
|
+
* this package introduces; sereus's own APIs say machine, node or peer.
|
|
51
|
+
*/
|
|
52
|
+
export type Cadron = CadreMachine;
|
|
53
|
+
|
|
54
|
+
export type CadreStatus = {
|
|
55
|
+
running: boolean;
|
|
56
|
+
partyId: string | null;
|
|
57
|
+
peerId: string | null;
|
|
58
|
+
/** This device is an enrolled owner of its cadre, so it can claim and remove machines. */
|
|
59
|
+
ownerEnrolled: boolean;
|
|
60
|
+
/** First characters of the owner key: the fingerprint a claimed node shows as its owner. */
|
|
61
|
+
ownerFingerprint: string | null;
|
|
62
|
+
/** This device first, then the others. */
|
|
63
|
+
machines: CadreMachine[];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** Where this device can reach a node from: `anywhere` (a public address or name) or `home-network`. */
|
|
67
|
+
export type NodeReach = 'anywhere' | 'home-network';
|
|
68
|
+
|
|
69
|
+
/** What a node code says, shown for approval before a claim. Never includes the secret. */
|
|
70
|
+
export type NodeCodePreview = {
|
|
71
|
+
peerId: string;
|
|
72
|
+
addresses: string[];
|
|
73
|
+
reach: NodeReach;
|
|
74
|
+
/** The cadre it would join: this device's network id, for the approval prompt. Null before the node starts. */
|
|
75
|
+
partyId: string | null;
|
|
76
|
+
/** This device's owner key fingerprint, which the node shows as its owner once it joins. Null when none is enrolled. */
|
|
77
|
+
ownerFingerprint: string | null;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** What removing a cadron would do, for the confirmation (`CadreModel.planRemoval`). */
|
|
81
|
+
export type RemovalPlan = {
|
|
82
|
+
/** Why it would be refused, or null: `not-an-owner` (no owner key here), `self` (this device). */
|
|
83
|
+
refusal: 'not-an-owner' | 'self' | null;
|
|
84
|
+
/** No live record names it here: already removed. */
|
|
85
|
+
alreadyRemoved: boolean;
|
|
86
|
+
/** Invitations it could come back through, which the removal withdraws. */
|
|
87
|
+
invitations: number;
|
|
88
|
+
/**
|
|
89
|
+
* Each closed strand the party holds a key for, where that key is replaced so the removed cadron
|
|
90
|
+
* reads nothing new there: `here` (by this removal), `later` (by a machine that runs the strand),
|
|
91
|
+
* `another-party` (the strand's host must re-invite the party).
|
|
92
|
+
*/
|
|
93
|
+
closedStrands: Array<{ strandId: string; replaced: 'here' | 'later' | 'another-party' }>;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** What a removal did (`CadreModel.removeMachine`). */
|
|
97
|
+
export type RemovalOutcome = {
|
|
98
|
+
/** Invitations withdrawn, which the removed cadron could have come back through. */
|
|
99
|
+
withdrawnInvitations: number;
|
|
100
|
+
/** Per closed strand: `rotated` (done), `pending` (finishes on a machine that runs it), `needs-another-party`. */
|
|
101
|
+
closedStrands: Array<{ strandId: string; outcome: 'rotated' | 'pending' | 'needs-another-party' }>;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/** What connecting to a node by its address did (`CadreModel.connectNode`). */
|
|
105
|
+
export type ConnectNodeOutcome = {
|
|
106
|
+
peerId: string;
|
|
107
|
+
/** The node took the seed over the network. */
|
|
108
|
+
delivered: boolean;
|
|
109
|
+
/** Why it did not (the node's refusal, or no answer), when not delivered. */
|
|
110
|
+
reason?: string;
|
|
111
|
+
/**
|
|
112
|
+
* When not delivered: the seed to hand the node another way (`cadre start --seed …`). The node is
|
|
113
|
+
* already authorized either way. Not a secret, but there is no reason to log it.
|
|
114
|
+
*/
|
|
115
|
+
seed?: string;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export interface CadreModel {
|
|
119
|
+
status(): Promise<CadreResult<CadreStatus>>;
|
|
120
|
+
/** Read scanned or pasted text. Fails `invalid-node-code` with the problem as `reason`. */
|
|
121
|
+
inspectNodeCode(text: string): Promise<CadreResult<NodeCodePreview>>;
|
|
122
|
+
/**
|
|
123
|
+
* Claim the node the code names, after the person approved (a link can be sent by
|
|
124
|
+
* anyone, so a code never claims on its own). Can take two minutes when no address
|
|
125
|
+
* answers. Fails `invalid-node-code`, `node-unreachable`, `claim-refused` (`retryable`
|
|
126
|
+
* says whether the same code can still work) or `claim-failed`.
|
|
127
|
+
*/
|
|
128
|
+
claimNode(text: string): Promise<CadreResult<void>>;
|
|
129
|
+
/**
|
|
130
|
+
* Add a node by its address (`/…/p2p/<peerId>`), for a node started to listen for seeds: authorize
|
|
131
|
+
* it (an owner-signed record), then deliver the seed. When the node does not take it over the
|
|
132
|
+
* network, the outcome carries the seed to deliver another way. Fails `invalid-address`,
|
|
133
|
+
* `unavailable`, or `not-supported` with no owner key here.
|
|
134
|
+
*/
|
|
135
|
+
connectNode(address: string): Promise<CadreResult<ConnectNodeOutcome>>;
|
|
136
|
+
/**
|
|
137
|
+
* A seed for a node not created yet (a provider-hosted node made from it), as text. It carries the
|
|
138
|
+
* cadre's addresses but does NOT authorize anything: once the node exists, add it with
|
|
139
|
+
* `connectNode` or a claim, or its traffic is refused.
|
|
140
|
+
*/
|
|
141
|
+
createSeed(): Promise<CadreResult<string>>;
|
|
142
|
+
/** What removing a cadron would do: for the confirmation ("this also closes 2 invitations"). */
|
|
143
|
+
planRemoval(peerId: string): Promise<CadreResult<RemovalPlan>>;
|
|
144
|
+
/**
|
|
145
|
+
* Take a cadron out of the cadre completely: its record, the invitations it could come back
|
|
146
|
+
* through, and the party's key in each closed strand (so it reads nothing new there). Fails
|
|
147
|
+
* `removal-refused` with `reason` `not-an-owner` or `self`.
|
|
148
|
+
*/
|
|
149
|
+
removeMachine(peerId: string): Promise<CadreResult<RemovalOutcome>>;
|
|
150
|
+
/**
|
|
151
|
+
* The owner key's private half, for an offline backup. It is the secret that controls
|
|
152
|
+
* the cadre: only behind an explicit, confirmed action, and never logged.
|
|
153
|
+
*/
|
|
154
|
+
exportOwnerKey(): Promise<CadreResult<string>>;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Who answers an invitation from this device:
|
|
159
|
+
* - `this-device`: it has an address of its own (a relay reservation, on a phone);
|
|
160
|
+
* - `own-machine`: one of the party's always-on machines has a public address, and an
|
|
161
|
+
* invitation names it, so a relay is not needed.
|
|
162
|
+
*/
|
|
163
|
+
export type ReachedVia = 'this-device' | 'own-machine';
|
|
164
|
+
|
|
165
|
+
export type Reachability = {
|
|
166
|
+
/** Can anybody answer an invitation from this device right now? */
|
|
167
|
+
reachable: boolean;
|
|
168
|
+
/** How, when reachable; null when not. */
|
|
169
|
+
via: ReachedVia | null;
|
|
170
|
+
/**
|
|
171
|
+
* Not reachable, but a relay is chosen and still connecting. Must not be shown as
|
|
172
|
+
* "nowhere to be reached": the user has just done what that screen asked of them.
|
|
173
|
+
*/
|
|
174
|
+
relayPending: boolean;
|
|
175
|
+
/** The relays this device is configured with. */
|
|
176
|
+
relays: string[];
|
|
177
|
+
/**
|
|
178
|
+
* The relay reservation, read live: a configured relay is not a working one. `null`
|
|
179
|
+
* when there is no node (a mock, or before start).
|
|
180
|
+
*/
|
|
181
|
+
relayStatus: 'none' | 'dialing' | 'reserved' | 'retrying' | 'error' | (string & {}) | null;
|
|
182
|
+
/**
|
|
183
|
+
* Why the relays last set could not be applied (the node could not be rebuilt with them), or
|
|
184
|
+
* null. `relays.set` returns at once; this is where its failure shows.
|
|
185
|
+
*/
|
|
186
|
+
relayError?: string | null;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export interface ReachabilityModel {
|
|
190
|
+
get(): Promise<Reachability>;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface RelaysModel {
|
|
194
|
+
get(): Promise<string[]>;
|
|
195
|
+
/**
|
|
196
|
+
* Replace the relays. Applying them restarts the node, so this returns at once and the
|
|
197
|
+
* node reconnects behind it; watch reachability for the reservation.
|
|
198
|
+
*/
|
|
199
|
+
set(addrs: string[]): Promise<CadreResult<void>>;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Whether a new invitation can be handed out with this device about to go quiet:
|
|
204
|
+
* - `ready`: another cadron holds the invitation's strand (one of its commits is held
|
|
205
|
+
* elsewhere), so it can answer a join without this device;
|
|
206
|
+
* - `waiting`: not yet (a new strand's copy reaches a cadron a second or two after the
|
|
207
|
+
* invitation is made);
|
|
208
|
+
* - `no-cadron`: no other cadron serves the party's strands; only this device answers.
|
|
209
|
+
*/
|
|
210
|
+
export type InvitationReadiness = 'ready' | 'waiting' | 'no-cadron';
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* What goes into an invitation, and whether it is ready to hand out. The app mints the
|
|
214
|
+
* invitation (cadre-core's `createOpenInvitation`), passes it through `withBootstrap` before
|
|
215
|
+
* encoding it, and shows `readiness` and `describe` on its invitation screen.
|
|
216
|
+
*/
|
|
217
|
+
export interface InvitationsModel {
|
|
218
|
+
preferences(): Promise<CadronPreferences>;
|
|
219
|
+
/** Merge and keep; returns what is kept (made whole and in range). */
|
|
220
|
+
setPreferences(changes: Partial<CadronPreferences>): Promise<CadreResult<CadronPreferences>>;
|
|
221
|
+
/** The addresses an invitation made now would name, and what to warn about. */
|
|
222
|
+
bootstrapPlan(): Promise<CadreResult<BootstrapPlan>>;
|
|
223
|
+
/**
|
|
224
|
+
* The invitation with its bootstrap list replaced by the plan's. Never fails: on any problem,
|
|
225
|
+
* or an empty plan, it returns the invitation as it came.
|
|
226
|
+
*/
|
|
227
|
+
withBootstrap<T extends { bootstrap: string[] }>(invitation: T): Promise<T>;
|
|
228
|
+
/**
|
|
229
|
+
* `newStrand`: the invitation founded this strand just now. Its copy reaches a cadron only once
|
|
230
|
+
* a commit of it is held there (the signal waited for). An established strand counts as ready
|
|
231
|
+
* when another cadron serves it.
|
|
232
|
+
*/
|
|
233
|
+
readiness(strandId: string, opts?: { newStrand?: boolean }): InvitationReadiness;
|
|
234
|
+
/**
|
|
235
|
+
* An invitation in words, from its encoded form. `link`: the text actually shared (the URL the
|
|
236
|
+
* QR code carries), for its length; the token's length otherwise. Its addresses grouped by
|
|
237
|
+
* machine, how each machine is (as the cadre screen shows it), and the other addresses it
|
|
238
|
+
* could name now, for a screen that lets the person change the list (`rebootstrap`).
|
|
239
|
+
*/
|
|
240
|
+
describe(token: string, link?: string): Promise<CadreResult<InvitationText>>;
|
|
241
|
+
/**
|
|
242
|
+
* The encoded invitation with its addresses replaced by `addrs`, in that order: the person's
|
|
243
|
+
* own choice for this one invitation. The token and its terms are the same, so a link already
|
|
244
|
+
* handed out still works, with the addresses it had. Fails `invalid-address` for an address
|
|
245
|
+
* that names no peer, or an empty list.
|
|
246
|
+
*/
|
|
247
|
+
rebootstrap(token: string, addrs: readonly string[]): Promise<CadreResult<string>>;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** What an invitation screen needs. */
|
|
251
|
+
export interface InvitationsView {
|
|
252
|
+
invitations: InvitationsModel;
|
|
253
|
+
changes: { invitations(onChange: () => void): () => void };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** What a cadre screen needs: its operations, and a signal to re-read on. An app's whole model can be passed if it has both. */
|
|
257
|
+
export interface CadreView {
|
|
258
|
+
cadre: CadreModel;
|
|
259
|
+
changes: { cadre(onChange: () => void): () => void };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** What a reachability display needs. */
|
|
263
|
+
export interface ReachabilityView {
|
|
264
|
+
reachability: ReachabilityModel;
|
|
265
|
+
changes: { reachability(onChange: () => void): () => void };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* How recently a cadron was alive, for a status dot:
|
|
270
|
+
* - `connected`: this device holds a connection to it now (`CadreMachine.connected`);
|
|
271
|
+
* - `recent`: not connected, but it published its record within `RECENT_RECORD_MS`, so it is
|
|
272
|
+
* running somewhere (a phone usually reads this way);
|
|
273
|
+
* - `quiet`: heard from, longer ago;
|
|
274
|
+
* - `unknown`: never heard from (its record has not reached this device).
|
|
275
|
+
*/
|
|
276
|
+
export type CadronPresence = 'connected' | 'recent' | 'quiet' | 'unknown';
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* A running machine re-publishes its record every 7.5 minutes, and a record older than 15 is
|
|
280
|
+
* stale: cadre-core's `DEFAULT_PEER_RECORD_HEARTBEAT_MS` and `DEFAULT_PEER_RECORD_MAX_AGE_MS`
|
|
281
|
+
* (repeated here; this entry point loads no cadre-core).
|
|
282
|
+
*/
|
|
283
|
+
export const RECENT_RECORD_MS = 15 * 60 * 1000;
|
|
284
|
+
|
|
285
|
+
export function cadronPresence(machine: Pick<CadreMachine, 'connected' | 'lastSeenAt'>, now: number = Date.now()): CadronPresence {
|
|
286
|
+
if (machine.connected) return 'connected';
|
|
287
|
+
if (machine.lastSeenAt === null) return 'unknown';
|
|
288
|
+
return now - machine.lastSeenAt < RECENT_RECORD_MS ? 'recent' : 'quiet';
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** First 8 characters of an owner key: the fingerprint a claimed node shows as its owner. */
|
|
292
|
+
export function ownerFingerprint(publicKeyB64: string): string {
|
|
293
|
+
return publicKeyB64.slice(0, 8);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** `…` + the last 8 characters: enough to match the peer id a node prints. */
|
|
297
|
+
export function shortPeerId(peerId: string): string {
|
|
298
|
+
return peerId.length > 12 ? `…${peerId.slice(-8)}` : peerId;
|
|
299
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node codes as text: the `sereus-join:` prefix a cadre node's code starts with, and
|
|
3
|
+
* finding one inside a link. Plain string handling, so any screen can recognise a node
|
|
4
|
+
* code (a scanner deciding where a scanned code goes) without loading the stack. Reading
|
|
5
|
+
* one is `readNodeCode` in `@serfab/cadre-client/sereus`.
|
|
6
|
+
*/
|
|
7
|
+
export const NODE_CODE_SCHEME = 'sereus-join:';
|
|
8
|
+
|
|
9
|
+
export function isNodeCode(text: string): boolean {
|
|
10
|
+
return text.trim().startsWith(NODE_CODE_SCHEME);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The node code inside a link (or the text itself, if it is one), else null. */
|
|
14
|
+
export function nodeCodeFromUrl(url: string | null | undefined): string | null {
|
|
15
|
+
if (!url) return null;
|
|
16
|
+
const trimmed = url.trim();
|
|
17
|
+
if (isNodeCode(trimmed)) return trimmed;
|
|
18
|
+
let decoded = trimmed;
|
|
19
|
+
try { decoded = decodeURIComponent(trimmed); } catch { /* keep as is */ }
|
|
20
|
+
const at = decoded.indexOf(NODE_CODE_SCHEME);
|
|
21
|
+
if (at <= 0) return null;
|
|
22
|
+
// base64url after the prefix, so it ends at the first character that cannot be part of it.
|
|
23
|
+
const match = /^sereus-join:[A-Za-z0-9._~-]+/.exec(decoded.slice(at));
|
|
24
|
+
return match ? match[0] : null;
|
|
25
|
+
}
|
package/src/result.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Results and errors. Operations return `CadreResult<T>` and do not throw for failures a
|
|
3
|
+
* screen must handle; a thrown error is a defect.
|
|
4
|
+
*
|
|
5
|
+
* A `CadreError` carries a stable `kind` a UI renders through its own string catalog
|
|
6
|
+
* (`cadre.errors.<kind>`, or `.<kind>.<reason>`), and `retryable`, which says whether
|
|
7
|
+
* trying again can help. `message` is English text for logs, and the words to show when a
|
|
8
|
+
* catalog has none. The shape is a subset of an app's own error type: an app whose error
|
|
9
|
+
* kinds include these can pass a `CadreResult` along as its own.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type CadreErrorKind =
|
|
13
|
+
/** Nobody can answer an invitation from this device: no address, and no machine of the party's that answers for it. */
|
|
14
|
+
| 'unreachable'
|
|
15
|
+
/** The cadre could not be read or written right now (a peer, the control network, a timeout). */
|
|
16
|
+
| 'unavailable'
|
|
17
|
+
/**
|
|
18
|
+
* A node code that cannot be used; `reason`: `not-node-code`, `newer-version`, `damaged`, or (a
|
|
19
|
+
* good code for a machine already known) `this-device`, `already-a-cadron`.
|
|
20
|
+
*/
|
|
21
|
+
| 'invalid-node-code'
|
|
22
|
+
/** Claiming a node: no address answered. `reason`: the node's reach, `anywhere` or `home-network`. */
|
|
23
|
+
| 'node-unreachable'
|
|
24
|
+
/** Claiming a node: it answered and said no. `reason`: its refusal code, when it sent one. */
|
|
25
|
+
| 'claim-refused'
|
|
26
|
+
/** Claiming a node: reached, then the exchange or a local step failed. Safe to repeat. `reason` `busy`: a claim is already running. */
|
|
27
|
+
| 'claim-failed'
|
|
28
|
+
/** Removing a cadron was refused. `reason`: `not-an-owner` (this device holds no owner key) or `self` (it is this device). */
|
|
29
|
+
| 'removal-refused'
|
|
30
|
+
/** An address that cannot be used: unparsable, or not ending in `/p2p/<peerId>`. */
|
|
31
|
+
| 'invalid-address'
|
|
32
|
+
/** This device cannot do that (no owner key held here, say). */
|
|
33
|
+
| 'not-supported'
|
|
34
|
+
/** Anything else. */
|
|
35
|
+
| 'unexpected';
|
|
36
|
+
|
|
37
|
+
const KINDS: Record<CadreErrorKind, true> = {
|
|
38
|
+
'unreachable': true, 'unavailable': true, 'invalid-node-code': true, 'node-unreachable': true,
|
|
39
|
+
'claim-refused': true, 'claim-failed': true, 'removal-refused': true, 'invalid-address': true, 'not-supported': true,
|
|
40
|
+
'unexpected': true,
|
|
41
|
+
};
|
|
42
|
+
/** Every kind, for catalogs and tools that must cover them all. */
|
|
43
|
+
export const CADRE_ERROR_KINDS = Object.keys(KINDS) as readonly CadreErrorKind[];
|
|
44
|
+
|
|
45
|
+
export interface CadreError {
|
|
46
|
+
kind: CadreErrorKind;
|
|
47
|
+
retryable: boolean;
|
|
48
|
+
message: string;
|
|
49
|
+
/** A finer, stable code within the kind (a claim refusal, a node's reach), for the UI to choose words. */
|
|
50
|
+
reason?: string;
|
|
51
|
+
/** The underlying error, for logs. */
|
|
52
|
+
cause?: unknown;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type CadreResult<T> = { ok: true; value: T } | { ok: false; error: CadreError };
|
|
56
|
+
|
|
57
|
+
const RETRYABLE: ReadonlySet<CadreErrorKind> = new Set<CadreErrorKind>(['unreachable', 'unavailable', 'node-unreachable', 'claim-failed']);
|
|
58
|
+
|
|
59
|
+
export function cadreOk<T>(value: T): CadreResult<T>;
|
|
60
|
+
export function cadreOk(): CadreResult<void>;
|
|
61
|
+
export function cadreOk<T>(value?: T): CadreResult<T | undefined> {
|
|
62
|
+
return { ok: true, value };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function cadreFail<T = never>(
|
|
66
|
+
kind: CadreErrorKind, message: string, opts: { retryable?: boolean; reason?: string; cause?: unknown } = {},
|
|
67
|
+
): CadreResult<T> {
|
|
68
|
+
return {
|
|
69
|
+
ok: false,
|
|
70
|
+
error: { kind, message, retryable: opts.retryable ?? RETRYABLE.has(kind), ...(opts.reason ? { reason: opts.reason } : {}), cause: opts.cause },
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Thrown inside the client to leave an operation with a known kind; `cadreAttempt` turns it into a failed result. */
|
|
75
|
+
export class CadreFailure extends Error {
|
|
76
|
+
readonly cadreError: CadreError;
|
|
77
|
+
constructor(kind: CadreErrorKind, message: string, opts: { retryable?: boolean; reason?: string; cause?: unknown } = {}) {
|
|
78
|
+
super(message);
|
|
79
|
+
this.name = 'CadreFailure';
|
|
80
|
+
const failed = cadreFail(kind, message, opts);
|
|
81
|
+
this.cadreError = (failed as { ok: false; error: CadreError }).error;
|
|
82
|
+
Object.setPrototypeOf(this, CadreFailure.prototype);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const UNAVAILABLE = /timed out|super-majority|not reachable|no peers|abort/i;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A caught value as text. An Error's message unchanged; anything else (a React Native WebSocket
|
|
90
|
+
* failure rejects with its error event, libp2p sometimes with a plain object) by its `message`,
|
|
91
|
+
* `reason` or `type` field, so it never reads `[object Object]`. cadre-core's `describeThrown` does
|
|
92
|
+
* the same; this entry point loads no cadre-core.
|
|
93
|
+
*/
|
|
94
|
+
export function describeThrown(value: unknown): string {
|
|
95
|
+
if (value instanceof Error) return value.message;
|
|
96
|
+
if (typeof value === 'object' && value !== null) {
|
|
97
|
+
const fields = value as { message?: unknown; reason?: unknown; type?: unknown };
|
|
98
|
+
for (const field of [fields.message, fields.reason, fields.type]) {
|
|
99
|
+
if (typeof field === 'string' && field) return field;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
return JSON.stringify(value);
|
|
103
|
+
} catch {
|
|
104
|
+
return Object.prototype.toString.call(value);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return `${value as string}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Run one operation: its value, or its failure as a result. Never throws. */
|
|
111
|
+
export async function cadreAttempt<T>(operation: () => Promise<T>): Promise<CadreResult<T>> {
|
|
112
|
+
try {
|
|
113
|
+
return { ok: true, value: await operation() };
|
|
114
|
+
} catch (err) {
|
|
115
|
+
if (err instanceof CadreFailure) return { ok: false, error: err.cadreError };
|
|
116
|
+
const message = describeThrown(err);
|
|
117
|
+
const name = err instanceof Error ? err.name : '';
|
|
118
|
+
return cadreFail(UNAVAILABLE.test(`${name} ${message}`) ? 'unavailable' : 'unexpected', message, { cause: err });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Reject with a clear error if `p` has not settled within `ms`. `p` is left to settle. */
|
|
123
|
+
export function withTimeout<T>(p: Promise<T>, ms: number, label: string): Promise<T> {
|
|
124
|
+
return new Promise<T>((resolve, reject) => {
|
|
125
|
+
const timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);
|
|
126
|
+
p.then(
|
|
127
|
+
value => { clearTimeout(timer); resolve(value); },
|
|
128
|
+
err => { clearTimeout(timer); reject(err); },
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
}
|