-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
shared_settings.ts
607 lines (472 loc) · 15.8 KB
/
shared_settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import { checkShareIsValid } from "../core-validators/addresses.ts";
import { Crypto } from "../crypto/crypto.ts";
import { AuthorKeypair } from "../crypto/crypto-types.ts";
import { ShareAddress } from "./doc-types.ts";
import { isErr, ValidationError } from "./errors.ts";
import { Replica } from "../replica/replica.ts";
import { Peer } from "../peer/peer.ts";
import { ConfigEs5 } from "../formats/format_es5.ts";
const EARTHSTAR_KEY = "earthstar";
const AUTHOR_KEY = "current_author";
const SHARES_KEY = "shares";
const SHARE_SECRETS_KEY = "share_secrets";
const SERVERS_KEY = "servers";
type SharedSettingsOpts = {
/** A namespace to restrict these settings to. */
namespace?: string;
/** Whether to use session storage for these settings. */
sessionOnly?: true;
};
/** Get and set values from a common pool of settings for Earthstar clients, such as an author, shares, share secrets, and servers.
*
* Uses the Storage API, so only clients on the same origin will share the same settings.
*/
export class SharedSettings {
private namespace: string | undefined;
private storage = localStorage;
constructor(opts?: SharedSettingsOpts) {
this.namespace = opts?.namespace;
if (opts?.sessionOnly) {
this.storage = sessionStorage;
}
// Deno and Node don't know about the storage event yet
// So this is just for cross-browser tab changes.
addEventListener("storage", (event) => {
const changedKey = (event as any).key;
switch (changedKey) {
case makeStorageKey(AUTHOR_KEY, this.namespace): {
this.fireAuthorEvent();
break;
}
case makeStorageKey(SHARES_KEY, this.namespace): {
this.fireSharesEvent();
break;
}
case makeStorageKey(SHARE_SECRETS_KEY, this.namespace): {
this.fireSecretsEvent();
break;
}
case makeStorageKey(SERVERS_KEY, this.namespace): {
this.fireServersEvent();
break;
}
}
});
}
// Author
/** The currently persisted author keypair. */
get author(): AuthorKeypair | null {
const key = makeStorageKey(AUTHOR_KEY, this.namespace);
const authorKeypair = getParsedValue(
this.storage,
key,
isParsedAuthorKeypair,
);
return authorKeypair || null;
}
set author(keypair: AuthorKeypair | null) {
const key = makeStorageKey(AUTHOR_KEY, this.namespace);
this.storage.setItem(key, JSON.stringify(keypair));
this.fireAuthorEvent();
}
// Shares
/** An array of shares stored by these settings. */
get shares(): ShareAddress[] {
const key = makeStorageKey(SHARES_KEY, this.namespace);
const shares = getParsedValue(this.storage, key, isParsedSharesArray);
return shares || [];
}
/** Add a share to the settings.
* @returns All stored shares after the addition, or a `ValidationError` if the address is invalid.
*/
addShare(address: ShareAddress) {
if (isErr(checkShareIsValid(address))) {
return new ValidationError("Not a valid share");
}
const key = makeStorageKey(SHARES_KEY, this.namespace);
const nextSharesSet = new Set([...this.shares, address]);
const nextShares = Array.from(nextSharesSet);
this.storage.setItem(key, JSON.stringify(nextShares));
this.fireSharesEvent();
return nextShares;
}
/** Removes a share from settings.
* @returns All stored shares after the removal, or a `ValidationError` if the share is not yet known.
*/
removeShare(addressToRemove: string) {
const shares = this.shares;
const indexOfShareToRemove = shares.findIndex((address) =>
address === addressToRemove
);
if (indexOfShareToRemove === -1) {
return new ValidationError("That share is not known yet");
}
shares.splice(indexOfShareToRemove, 1);
const key = makeStorageKey(SHARES_KEY, this.namespace);
this.storage.setItem(key, JSON.stringify(shares));
this.fireSharesEvent();
return shares;
}
// Share secrets
/** A record of known shares and their corresponding secret, if known. */
get shareSecrets() {
const key = makeStorageKey(SHARE_SECRETS_KEY, this.namespace);
const shares = getParsedValue(this.storage, key, isParsedSecretsDict);
return shares || {};
}
/** Add a secret for a share already known to the settings.
* @returns The next record of share secret pairs, or returns a `ValidationError` if the share is not known or if the secret is incorrect.
*/
async addSecret(shareAddress: ShareAddress, secret: string) {
const knownShare = this.shares.find((addr) => shareAddress === addr);
if (!knownShare) {
return new ValidationError("This share is not yet known.");
}
if (isErr(await Crypto.checkKeypairIsValid({ shareAddress, secret }))) {
return new ValidationError("Not the right secret for this share.");
}
const key = makeStorageKey(SHARE_SECRETS_KEY, this.namespace);
const nextSecrets = { ...this.shareSecrets, [shareAddress]: secret };
this.storage.setItem(key, JSON.stringify(nextSecrets));
this.fireSecretsEvent();
return nextSecrets;
}
/** Remove a secret from settings.
* @returns The next record of share secret pairs, or returns a `ValidationError` if the share is not known.
*/
removeSecret(shareAddress: ShareAddress) {
const secrets = this.shareSecrets;
const currentSecret = secrets[shareAddress];
if (!currentSecret) {
return new ValidationError("Unknown share");
}
const key = makeStorageKey(SHARE_SECRETS_KEY, this.namespace);
const nextSecrets = { ...secrets };
delete nextSecrets[shareAddress];
this.storage.setItem(key, JSON.stringify(nextSecrets));
this.fireSecretsEvent();
return nextSecrets;
}
// Servers
/** An array of server URLs stored by these settings. */
get servers(): string[] {
const key = makeStorageKey(SERVERS_KEY, this.namespace);
const servers = getParsedValue(this.storage, key, isParsedUrlArray);
return servers || [];
}
/** Add a server URL to be stored by settings.
* @returns The list of servers after the addition, or a `ValidationError` if the string is not a valid URL.
*/
addServer(address: string): string[] | ValidationError {
try {
const url = new URL(address);
const urlSet = new Set([...this.servers, url.toString()]);
const nextServers = Array.from(urlSet);
const key = makeStorageKey(SERVERS_KEY, this.namespace);
this.storage.setItem(key, JSON.stringify(nextServers));
this.fireServersEvent();
return nextServers;
} catch {
return new ValidationError("Not a valid URL.");
}
}
/** Remove a server URL from the settings' stored list of servers..
* @returns The list of servers after the removal, or a `ValidationError` if the string is not yet known.
*/
removeServer(addressToRemove: string) {
try {
const url = new URL(addressToRemove);
const servers = this.servers;
const indexOfShareToRemove = servers.findIndex((address) =>
address === url.toString()
);
if (indexOfShareToRemove === -1) {
return new ValidationError("That server is not known yet");
}
servers.splice(indexOfShareToRemove, 1);
const key = makeStorageKey(SERVERS_KEY, this.namespace);
this.storage.setItem(key, JSON.stringify(servers));
this.fireServersEvent();
return servers;
} catch {
return new ValidationError("Not a valid URL");
}
}
/** Delete all stored settings. */
clear() {
const authorKey = makeStorageKey(AUTHOR_KEY, this.namespace);
this.storage.setItem(authorKey, JSON.stringify(null));
const sharesKey = makeStorageKey(SHARES_KEY, this.namespace);
this.storage.setItem(sharesKey, JSON.stringify([]));
const secretsKey = makeStorageKey(SHARE_SECRETS_KEY, this.namespace);
this.storage.setItem(secretsKey, JSON.stringify({}));
const serversKey = makeStorageKey(SERVERS_KEY, this.namespace);
this.storage.setItem(serversKey, JSON.stringify([]));
this.fireAuthorEvent();
this.fireSharesEvent();
this.fireSecretsEvent();
this.fireServersEvent();
}
private authorChangedCbs = new Set<(keypair: AuthorKeypair | null) => void>();
/** Fires the given callback when the stored author changes. */
onAuthorChanged(cb: (keypair: AuthorKeypair | null) => void) {
this.authorChangedCbs.add(cb);
return () => {
this.authorChangedCbs.delete(cb);
};
}
private sharesChangedCbs = new Set<(shares: ShareAddress[]) => void>();
/** Fires the given callback when the stored list of shares changes. */
onSharesChanged(cb: (shares: ShareAddress[]) => void) {
this.sharesChangedCbs.add(cb);
return () => {
this.sharesChangedCbs.delete(cb);
};
}
private shareSecretsChangedCbs = new Set<
(secrets: Record<ShareAddress, string>) => void
>();
/** Fires the given callback when the stored record of share secrets changes. */
onShareSecretsChanged(cb: (secrets: Record<ShareAddress, string>) => void) {
this.shareSecretsChangedCbs.add(cb);
return () => {
this.shareSecretsChangedCbs.delete(cb);
};
}
private serversChangedCbs = new Set<(shares: string[]) => void>();
/** Fires the given callback when the stored list of server URLs changes. */
onServersChanged(cb: (shares: string[]) => void) {
this.serversChangedCbs.add(cb);
return () => {
this.serversChangedCbs.delete(cb);
};
}
/** Get a new `Peer` preconfigured with shares, secrets, and syncers derived from these settings.
*
* When settings are updated, the peer's replicas and syncers will be updated too.
*/
getPeer(
{ sync, onCreateReplica }: {
/** Whether to start syncing using the settings' servers. */
sync: "once" | "continuous" | false;
/** Used to create replicas when a new share is added to settings. */
onCreateReplica: (addr: ShareAddress, secret?: string) => Replica;
},
): {
/** A preconfigured Peer. */
peer: Peer;
/** Stop changes to SharedSettings from propagating to the Peer. */
unsubscribeFromSettings: () => void;
} {
const peer = new Peer();
// Get all shares
const shares = this.shares;
// Add ones for those we do
for (const share of shares) {
const replica = onCreateReplica(share, this.shareSecrets[share]);
peer.addReplica(replica);
}
// Listen for share events
const unsubSharesChanged = this.onSharesChanged(async (newShares) => {
const existingReplicas = peer.replicas();
const existingShares = peer.shares();
for (const replica of existingReplicas) {
if (!newShares.includes(replica.share)) {
peer.removeReplica(replica);
await replica.close(false);
}
}
for (const share of newShares) {
if (!existingShares.includes(share)) {
const replica = onCreateReplica(share, this.shareSecrets[share]);
peer.addReplica(replica);
}
}
});
// Secrets
// Listen for secret events
const unsubSecretsChanged = this.onShareSecretsChanged(
async (newSecrets) => {
// We know that we can't add a secret without adding the share first.
const existingShares = peer.shares();
const nextShares = Object.keys(newSecrets);
for (const share of existingShares) {
// If the secret was removed, re-add the share's replica without the secret.
if (!nextShares.includes(share)) {
const existingReplica = peer.getReplica(share);
if (existingReplica) {
peer.removeReplica(existingReplica);
existingReplica.close(false);
}
const replica = onCreateReplica(share);
peer.addReplica(replica);
}
}
for (const share of nextShares) {
// If the secret was added, remove the old replica and add the new one with a secret
// But only if the share doesn't have a secret yet.
const existingReplica = peer.getReplica(share);
if (
existingReplica?.formatsConfig["es.5"] &&
(existingReplica.formatsConfig["es.5"] as ConfigEs5)["shareSecret"]
) {
continue;
}
await existingReplica?.close(false);
peer.removeReplicaByShare(share);
const replica = onCreateReplica(share, newSecrets[share]);
peer.addReplica(replica);
}
},
);
// Servers
// Add syncers for each server
if (sync) {
for (const server of this.servers) {
peer.sync(server, sync === "continuous");
}
}
// Listen for server events
const unsubServersChanged = this.onServersChanged((newServers) => {
if (sync) {
// Remove syncers no longer in the new list
const syncers = peer.getSyncers();
for (const [_id, { description, syncer }] of syncers) {
if (!newServers.includes(description)) {
syncer.cancel();
}
}
// Add syncers for servers not in the list yet.
for (const newServer of newServers) {
peer.sync(newServer, sync === "continuous");
}
}
});
const unsubscribeFromSettings = () => {
// Unsub.
unsubSharesChanged();
unsubSecretsChanged();
unsubServersChanged();
};
return { peer, unsubscribeFromSettings };
}
private fireAuthorEvent() {
const author = this.author;
for (const cb of this.authorChangedCbs) {
cb(author);
}
}
private fireSharesEvent() {
const shares = this.shares;
for (const cb of this.sharesChangedCbs) {
cb(shares);
}
}
private fireSecretsEvent() {
const secrets = this.shareSecrets;
for (const cb of this.shareSecretsChangedCbs) {
cb(secrets);
}
}
private fireServersEvent() {
const servers = this.servers;
for (const cb of this.serversChangedCbs) {
cb(servers);
}
}
}
function makeStorageKey(key: string, namespace?: string) {
return `${EARTHSTAR_KEY}:${namespace ? `${namespace}:` : ""}${key}`;
}
function getParsedValue<T>(
storage: Storage,
key: string,
check: (parsed: unknown) => parsed is T,
): T | undefined {
const value = storage.getItem(key);
if (value === null) {
return undefined;
}
try {
const parsed = JSON.parse(value);
if (check(parsed)) {
return parsed;
} else {
return undefined;
}
} catch {
return undefined;
}
}
function isObject(t: unknown): t is Record<string, unknown> {
if (t === null || t === undefined) {
return false;
}
if (typeof t !== "object") {
return false;
}
return true;
}
function isParsedAuthorKeypair(t: unknown): t is AuthorKeypair {
if (!isObject(t)) {
return false;
}
if (Object.keys(t).length !== 2) {
return false;
}
if ("address" in t === false) {
return false;
}
if ("secret" in t === false) {
return false;
}
return true;
}
function isParsedSharesArray(t: unknown): t is ShareAddress[] {
if (!Array.isArray(t)) {
return false;
}
if (t.some((val) => typeof val !== "string")) {
return false;
}
if (t.some((val) => isErr(checkShareIsValid(val)))) {
return false;
}
return true;
}
function isParsedSecretsDict(t: unknown): t is Record<ShareAddress, string> {
if (!isObject(t)) {
return false;
}
for (const key in t) {
const secret = t[key];
if (typeof secret !== "string") {
return false;
}
if (
isErr(Crypto.checkKeypairIsValid({
shareAddress: key,
secret: secret,
}))
) {
return false;
}
}
return true;
}
function isParsedUrlArray(t: unknown): t is ShareAddress[] {
if (!Array.isArray(t)) {
return false;
}
if (t.some((val) => typeof val !== "string")) {
return false;
}
for (const val of t) {
try {
new URL(val);
} catch {
return false;
}
}
return true;
}