@tik-choco/mistvault 0.0.0-stage → 0.2.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/LICENSE +373 -0
- package/README.ja.md +126 -0
- package/README.md +183 -2
- package/README.zh.md +126 -0
- package/dist/appState.d.ts +24 -0
- package/dist/appState.js +20 -0
- package/dist/code.d.ts +7 -0
- package/dist/code.js +49 -0
- package/dist/controller.d.ts +55 -0
- package/dist/controller.js +74 -0
- package/dist/core/client.d.ts +112 -0
- package/dist/core/client.js +536 -0
- package/dist/core/crypto.d.ts +24 -0
- package/dist/core/crypto.js +185 -0
- package/dist/core/head.d.ts +54 -0
- package/dist/core/head.js +123 -0
- package/dist/core/parse.d.ts +1 -0
- package/dist/core/parse.js +1 -0
- package/dist/core/protocol.d.ts +74 -0
- package/dist/core/protocol.js +93 -0
- package/dist/i18n.d.ts +132 -0
- package/dist/i18n.js +96 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +12 -0
- package/dist/parts.d.ts +42 -0
- package/dist/parts.js +267 -0
- package/dist/preact/RoomControls.d.ts +13 -0
- package/dist/preact/RoomControls.js +12 -0
- package/dist/preact/ScopeVaultPanel.d.ts +6 -0
- package/dist/preact/ScopeVaultPanel.js +20 -0
- package/dist/preact/VaultPanel.d.ts +12 -0
- package/dist/preact/VaultPanel.js +111 -0
- package/dist/preact/index.d.ts +5 -0
- package/dist/preact/index.js +3 -0
- package/dist/preact/ui.css +131 -0
- package/dist/preact/useVault.d.ts +6 -0
- package/dist/preact/useVault.js +13 -0
- package/dist/scopeKey.d.ts +1 -0
- package/dist/scopeKey.js +49 -0
- package/dist/scopeManager.d.ts +14 -0
- package/dist/scopeManager.js +110 -0
- package/dist/scopeVault.d.ts +39 -0
- package/dist/scopeVault.js +212 -0
- package/dist/session.d.ts +44 -0
- package/dist/session.js +736 -0
- package/dist/settings.d.ts +21 -0
- package/dist/settings.js +59 -0
- package/dist/stored.d.ts +4 -0
- package/dist/stored.js +27 -0
- package/dist/transport.d.ts +20 -0
- package/dist/transport.js +117 -0
- package/package.json +52 -4
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
.mv-panel {
|
|
2
|
+
color: var(--mv-fg, inherit);
|
|
3
|
+
background: var(--mv-bg, transparent);
|
|
4
|
+
font: inherit;
|
|
5
|
+
line-height: 1.5;
|
|
6
|
+
max-width: 44rem;
|
|
7
|
+
min-width: 0;
|
|
8
|
+
box-sizing: border-box;
|
|
9
|
+
}
|
|
10
|
+
.mv-panel *, .mv-panel *::before, .mv-panel *::after { box-sizing: border-box; }
|
|
11
|
+
.mv-panel h2 { font-size: 1.3em; line-height: 1.25; margin: 0 0 1rem; }
|
|
12
|
+
.mv-panel h3 { font-size: 1em; line-height: 1.3; margin: 0; }
|
|
13
|
+
.mv-panel p { margin: 0; overflow-wrap: anywhere; }
|
|
14
|
+
.mv-panel .mv-muted { color: var(--mv-muted, #6d7772); font-size: .9em; }
|
|
15
|
+
.mv-panel label { display: block; font-weight: 600; }
|
|
16
|
+
.mv-panel input:not([type=checkbox]), .mv-panel select {
|
|
17
|
+
display: block;
|
|
18
|
+
min-width: 0;
|
|
19
|
+
width: 100%;
|
|
20
|
+
padding: .65rem .7rem;
|
|
21
|
+
border: 1px solid var(--mv-border, rgba(127, 127, 127, .35));
|
|
22
|
+
border-radius: .5rem;
|
|
23
|
+
color: inherit;
|
|
24
|
+
background: var(--mv-bg, transparent);
|
|
25
|
+
font: inherit;
|
|
26
|
+
font-weight: 400;
|
|
27
|
+
}
|
|
28
|
+
.mv-panel input::placeholder { color: var(--mv-muted, #6d7772); opacity: 1; }
|
|
29
|
+
.mv-panel button {
|
|
30
|
+
min-height: 2.75rem;
|
|
31
|
+
padding: .6rem .8rem;
|
|
32
|
+
border: 1px solid var(--mv-border, rgba(127, 127, 127, .35));
|
|
33
|
+
border-radius: .5rem;
|
|
34
|
+
color: inherit;
|
|
35
|
+
background: var(--mv-bg, transparent);
|
|
36
|
+
font: inherit;
|
|
37
|
+
font-weight: 600;
|
|
38
|
+
line-height: 1.3;
|
|
39
|
+
cursor: pointer;
|
|
40
|
+
overflow-wrap: anywhere;
|
|
41
|
+
}
|
|
42
|
+
.mv-panel button:hover:not(:disabled) { border-color: var(--mv-accent, #315fbd); }
|
|
43
|
+
.mv-panel button.mv-primary { color: var(--mv-bg, #fff); background: var(--mv-accent, #315fbd); border-color: var(--mv-accent, #315fbd); }
|
|
44
|
+
.mv-panel button:disabled { opacity: .5; cursor: default; }
|
|
45
|
+
.mv-panel :focus-visible { outline: 2px solid var(--mv-accent, #315fbd); outline-offset: 3px; }
|
|
46
|
+
.mv-panel .mv-welcome > p { margin-bottom: 1.25rem; }
|
|
47
|
+
.mv-panel .mv-stack { display: grid; gap: .55rem; }
|
|
48
|
+
.mv-panel .mv-actions { display: flex; flex-wrap: wrap; gap: .5rem; }
|
|
49
|
+
.mv-panel .mv-actions button { flex: 1 1 7rem; }
|
|
50
|
+
.mv-panel .mv-status-card, .mv-panel .mv-confirm {
|
|
51
|
+
padding: 1rem;
|
|
52
|
+
border: 1px solid var(--mv-border, rgba(127, 127, 127, .35));
|
|
53
|
+
border-radius: .75rem;
|
|
54
|
+
background: color-mix(in srgb, var(--mv-accent, #315fbd) 7%, transparent);
|
|
55
|
+
}
|
|
56
|
+
.mv-panel .mv-status-text { display: grid; gap: .15rem; margin-bottom: .85rem; }
|
|
57
|
+
.mv-panel .mv-status-text strong::before {
|
|
58
|
+
display: inline-block;
|
|
59
|
+
width: .55rem;
|
|
60
|
+
height: .55rem;
|
|
61
|
+
margin-inline-end: .5rem;
|
|
62
|
+
border-radius: 50%;
|
|
63
|
+
background: var(--mv-accent, #315fbd);
|
|
64
|
+
content: '';
|
|
65
|
+
vertical-align: .05em;
|
|
66
|
+
}
|
|
67
|
+
.mv-panel .mv-status-text[data-state=off] strong::before { background: var(--mv-muted, #6d7772); }
|
|
68
|
+
.mv-panel .mv-status-text[data-state=error] strong::before { background: var(--mv-error, #b33b3b); }
|
|
69
|
+
.mv-panel .mv-switch {
|
|
70
|
+
display: flex;
|
|
71
|
+
align-items: center;
|
|
72
|
+
justify-content: space-between;
|
|
73
|
+
gap: 1rem;
|
|
74
|
+
padding: 1.1rem 0;
|
|
75
|
+
cursor: pointer;
|
|
76
|
+
}
|
|
77
|
+
.mv-panel .mv-switch span { min-width: 0; overflow-wrap: anywhere; }
|
|
78
|
+
.mv-panel .mv-switch input {
|
|
79
|
+
appearance: none;
|
|
80
|
+
flex: 0 0 auto;
|
|
81
|
+
width: 2.6rem;
|
|
82
|
+
height: 1.5rem;
|
|
83
|
+
margin: 0;
|
|
84
|
+
border: 1px solid var(--mv-border, rgba(127, 127, 127, .55));
|
|
85
|
+
border-radius: 1rem;
|
|
86
|
+
background: var(--mv-border, #9ca9a1);
|
|
87
|
+
cursor: pointer;
|
|
88
|
+
}
|
|
89
|
+
.mv-panel .mv-switch input::before {
|
|
90
|
+
display: block;
|
|
91
|
+
width: 1.1rem;
|
|
92
|
+
height: 1.1rem;
|
|
93
|
+
margin: .13rem;
|
|
94
|
+
border-radius: 50%;
|
|
95
|
+
background: var(--mv-bg, #fff);
|
|
96
|
+
content: '';
|
|
97
|
+
transition: transform .15s ease;
|
|
98
|
+
}
|
|
99
|
+
.mv-panel .mv-switch input:checked { border-color: var(--mv-accent, #315fbd); background: var(--mv-accent, #315fbd); }
|
|
100
|
+
.mv-panel .mv-switch input:checked::before { transform: translateX(1.1rem); }
|
|
101
|
+
.mv-panel .mv-room { padding-bottom: 1.1rem; }
|
|
102
|
+
.mv-panel .mv-room-row { display: flex; align-items: end; gap: .5rem; }
|
|
103
|
+
.mv-panel .mv-room-row label { flex: 1 1 auto; min-width: 0; }
|
|
104
|
+
.mv-panel .mv-room-row input { margin-top: .4rem; }
|
|
105
|
+
.mv-panel .mv-room-row button { flex: 0 0 auto; }
|
|
106
|
+
.mv-panel .mv-hint { margin-top: .4rem; }
|
|
107
|
+
.mv-panel .mv-disclosure { min-width: 0; border-top: 1px solid var(--mv-border, rgba(127, 127, 127, .35)); }
|
|
108
|
+
.mv-panel .mv-disclosure:last-of-type { border-bottom: 1px solid var(--mv-border, rgba(127, 127, 127, .35)); }
|
|
109
|
+
.mv-panel .mv-disclosure summary { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .9rem 0; font-weight: 600; cursor: pointer; list-style: none; }
|
|
110
|
+
.mv-panel .mv-disclosure summary::-webkit-details-marker { display: none; }
|
|
111
|
+
.mv-panel .mv-disclosure summary::after { color: var(--mv-muted, #6d7772); content: '+'; font-size: 1.3em; font-weight: 400; line-height: 1; }
|
|
112
|
+
.mv-panel .mv-disclosure[open] summary::after { content: '−'; }
|
|
113
|
+
.mv-panel .mv-disclosure-body { display: grid; gap: .65rem; padding: 0 0 1rem; }
|
|
114
|
+
.mv-panel .mv-disclosure-body label, .mv-panel .mv-import label { display: grid; gap: .4rem; }
|
|
115
|
+
.mv-panel .mv-disclosure-body > button { justify-self: start; }
|
|
116
|
+
.mv-panel .mv-import { display: grid; gap: .55rem; margin-top: .8rem; padding-top: .9rem; border-top: 1px solid var(--mv-border, rgba(127, 127, 127, .35)); }
|
|
117
|
+
.mv-panel .mv-confirm { margin-bottom: 1.25rem; }
|
|
118
|
+
.mv-panel .mv-confirm p { margin: .55rem 0 .9rem; }
|
|
119
|
+
.mv-panel .mv-parts, .mv-panel .mv-notice { margin-top: .9rem; }
|
|
120
|
+
.mv-panel .mv-error { color: var(--mv-error, #b33b3b); font-size: .9em; }
|
|
121
|
+
@media (prefers-color-scheme: dark) {
|
|
122
|
+
.mv-panel { color-scheme: dark; }
|
|
123
|
+
.mv-panel .mv-muted, .mv-panel input::placeholder, .mv-panel .mv-disclosure summary::after { color: var(--mv-muted, #afbdb5); }
|
|
124
|
+
.mv-panel .mv-error { color: var(--mv-error, #ff9999); }
|
|
125
|
+
.mv-panel .mv-status-text[data-state=error] strong::before { background: var(--mv-error, #ff9999); }
|
|
126
|
+
}
|
|
127
|
+
@media (prefers-reduced-motion: reduce) { .mv-panel .mv-switch input::before { transition: none; } }
|
|
128
|
+
@media (forced-colors: active) {
|
|
129
|
+
.mv-panel .mv-switch input { appearance: auto; }
|
|
130
|
+
.mv-panel .mv-switch input::before { content: none; }
|
|
131
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { VaultController, VaultView } from '../controller.js';
|
|
2
|
+
export declare function useVault(controller: VaultController): VaultView;
|
|
3
|
+
export declare function useVaultView<V>(controller: {
|
|
4
|
+
getView(): V;
|
|
5
|
+
subscribe(cb: (view: V) => void): () => void;
|
|
6
|
+
}): V;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useLayoutEffect, useState } from 'preact/hooks';
|
|
2
|
+
export function useVault(controller) {
|
|
3
|
+
return useVaultView(controller);
|
|
4
|
+
}
|
|
5
|
+
export function useVaultView(controller) {
|
|
6
|
+
const [view, setView] = useState(() => controller.getView());
|
|
7
|
+
useLayoutEffect(() => {
|
|
8
|
+
const off = controller.subscribe(setView);
|
|
9
|
+
setView(controller.getView());
|
|
10
|
+
return off;
|
|
11
|
+
}, [controller]);
|
|
12
|
+
return view;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function scopeStateKey(app: string, roomId: string): string;
|
package/dist/scopeKey.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { utf8 } from './core/crypto.js';
|
|
2
|
+
// The config API is synchronous, so it must locate persisted revisions before its first call.
|
|
3
|
+
// Only the storage address uses this SHA-256 implementation. Vault keys use Web Crypto.
|
|
4
|
+
const constants = [
|
|
5
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
6
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
7
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
8
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
9
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
10
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
11
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
12
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
13
|
+
];
|
|
14
|
+
const rotate = (word, bits) => word >>> bits | word << (32 - bits);
|
|
15
|
+
export function scopeStateKey(app, roomId) {
|
|
16
|
+
const bytes = utf8(app + '\n' + roomId);
|
|
17
|
+
const padded = new Uint8Array(Math.ceil((bytes.length + 9) / 64) * 64);
|
|
18
|
+
padded.set(bytes);
|
|
19
|
+
padded[bytes.length] = 0x80;
|
|
20
|
+
const data = new DataView(padded.buffer);
|
|
21
|
+
data.setBigUint64(padded.length - 8, BigInt(bytes.length) * 8n);
|
|
22
|
+
const hash = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]);
|
|
23
|
+
const words = new Uint32Array(64);
|
|
24
|
+
for (let offset = 0; offset < padded.length; offset += 64) {
|
|
25
|
+
for (let i = 0; i < 16; i++)
|
|
26
|
+
words[i] = data.getUint32(offset + i * 4);
|
|
27
|
+
for (let i = 16; i < 64; i++) {
|
|
28
|
+
const x = words[i - 15], y = words[i - 2];
|
|
29
|
+
words[i] = words[i - 16] + (rotate(x, 7) ^ rotate(x, 18) ^ x >>> 3) + words[i - 7] + (rotate(y, 17) ^ rotate(y, 19) ^ y >>> 10);
|
|
30
|
+
}
|
|
31
|
+
let [a, b, c, d, e, f, g, h] = hash;
|
|
32
|
+
for (let i = 0; i < 64; i++) {
|
|
33
|
+
const first = h + (rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25)) + ((e & f) ^ (~e & g)) + constants[i] + words[i];
|
|
34
|
+
const second = (rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22)) + ((a & b) ^ (a & c) ^ (b & c));
|
|
35
|
+
h = g;
|
|
36
|
+
g = f;
|
|
37
|
+
f = e;
|
|
38
|
+
e = (d + first) >>> 0;
|
|
39
|
+
d = c;
|
|
40
|
+
c = b;
|
|
41
|
+
b = a;
|
|
42
|
+
a = (first + second) >>> 0;
|
|
43
|
+
}
|
|
44
|
+
for (const [i, value] of [a, b, c, d, e, f, g, h].entries())
|
|
45
|
+
hash[i] += value;
|
|
46
|
+
}
|
|
47
|
+
const prefix = Array.from(hash.subarray(0, 4), word => word.toString(16).padStart(8, '0')).join('');
|
|
48
|
+
return 'tc-vault:scope:' + app + ':' + prefix + ':v1';
|
|
49
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EscrowTransport } from './core/client.js';
|
|
2
|
+
import type { ScopeVault, ScopeVaultOptions } from './scopeVault.js';
|
|
3
|
+
export type ManagedScope<S> = Pick<ScopeVaultOptions<S>, 'roomId' | 'adapter' | 'isWriter' | 'defaultConfig'>;
|
|
4
|
+
export interface ScopeVaultManagerOptions extends Pick<ScopeVaultOptions<unknown>, 'app' | 'nodeId' | 'storage' | 'now' | 'partGcGraceMs'> {
|
|
5
|
+
transportFor(room: string): EscrowTransport;
|
|
6
|
+
}
|
|
7
|
+
export interface ScopeVaultManager<S = unknown> {
|
|
8
|
+
sync(scopes: readonly ManagedScope<S>[]): void;
|
|
9
|
+
get(roomId: string): ScopeVault | undefined;
|
|
10
|
+
rooms(): string[];
|
|
11
|
+
subscribe(cb: () => void): () => void;
|
|
12
|
+
dispose(): void;
|
|
13
|
+
}
|
|
14
|
+
export declare function createScopeVaultManager<S = unknown>(o: ScopeVaultManagerOptions): ScopeVaultManager<S>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { openScopeVault } from './scopeVault.js';
|
|
2
|
+
function roomPool(transportFor) {
|
|
3
|
+
const pool = new Map();
|
|
4
|
+
return (id, hasProvider) => {
|
|
5
|
+
const room = pool.get(id) ?? { source: transportFor(id), leases: 0, members: new Set() };
|
|
6
|
+
pool.set(id, room);
|
|
7
|
+
room.leases++;
|
|
8
|
+
let lastJoin;
|
|
9
|
+
let probedJoin;
|
|
10
|
+
const { source } = room;
|
|
11
|
+
function leave() {
|
|
12
|
+
if (room.members.delete(hasProvider) && !room.members.size && room.joined) {
|
|
13
|
+
room.joined = undefined;
|
|
14
|
+
source.leave(id);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
transport: {
|
|
19
|
+
join() {
|
|
20
|
+
room.members.add(hasProvider);
|
|
21
|
+
if (room.joined && room.joined === lastJoin) {
|
|
22
|
+
// Probe once if a co-tenant still sees a provider. Its view may also
|
|
23
|
+
// be stale, so a second unsuccessful discovery forces a fresh join.
|
|
24
|
+
if (probedJoin !== room.joined && [...room.members].some(connected => connected())) {
|
|
25
|
+
probedJoin = room.joined;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
room.joined = undefined;
|
|
29
|
+
source.leave(id);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
room.joined ??= source.join(id);
|
|
33
|
+
lastJoin = room.joined;
|
|
34
|
+
return lastJoin;
|
|
35
|
+
},
|
|
36
|
+
leave,
|
|
37
|
+
send: (_, to, bytes) => source.send(id, to, bytes),
|
|
38
|
+
onMessage: (_, cb) => source.onMessage(id, cb),
|
|
39
|
+
onPeer: (_, cb) => source.onPeer(id, cb),
|
|
40
|
+
storageAdd: (name, bytes) => source.storageAdd(name, bytes),
|
|
41
|
+
storageGet: cid => source.storageGet(cid),
|
|
42
|
+
},
|
|
43
|
+
providerFound() { probedJoin = undefined; },
|
|
44
|
+
release() { leave(); if (--room.leases === 0)
|
|
45
|
+
pool.delete(id); },
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export function createScopeVaultManager(o) {
|
|
50
|
+
const acquire = roomPool(room => o.transportFor(room));
|
|
51
|
+
const listeners = new Set();
|
|
52
|
+
const entries = new Map();
|
|
53
|
+
let disposed = false;
|
|
54
|
+
function emit() {
|
|
55
|
+
for (const cb of listeners) {
|
|
56
|
+
try {
|
|
57
|
+
cb();
|
|
58
|
+
}
|
|
59
|
+
catch { /* Isolate subscribers. */ }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function remove(id) {
|
|
63
|
+
const entry = entries.get(id);
|
|
64
|
+
entries.delete(id);
|
|
65
|
+
entry.off();
|
|
66
|
+
entry.vault.dispose();
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
sync(scopes) {
|
|
70
|
+
if (disposed)
|
|
71
|
+
return;
|
|
72
|
+
const wanted = new Map(scopes.map(scope => [scope.roomId, scope]));
|
|
73
|
+
let changed = false;
|
|
74
|
+
for (const [id, entry] of entries) {
|
|
75
|
+
if (wanted.get(id)?.adapter !== entry.scope.adapter) {
|
|
76
|
+
remove(id);
|
|
77
|
+
changed = true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
for (const [id, scope] of wanted) {
|
|
81
|
+
const existing = entries.get(id);
|
|
82
|
+
if (existing) {
|
|
83
|
+
existing.scope = scope;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
// The closing vault still needs its own election during its final save.
|
|
87
|
+
const vault = openScopeVault({ ...o, ...scope, isWriter: () => entry.scope.isWriter?.() ?? true }, acquire);
|
|
88
|
+
const entry = { scope, vault, room: vault.getView().room, off: vault.subscribe(view => {
|
|
89
|
+
if (view.room !== entry.room) {
|
|
90
|
+
entry.room = view.room;
|
|
91
|
+
emit();
|
|
92
|
+
}
|
|
93
|
+
}) };
|
|
94
|
+
entries.set(id, entry);
|
|
95
|
+
changed = true;
|
|
96
|
+
}
|
|
97
|
+
if (changed)
|
|
98
|
+
emit();
|
|
99
|
+
},
|
|
100
|
+
get: id => entries.get(id)?.vault,
|
|
101
|
+
rooms: () => [...new Set(Array.from(entries.values(), entry => entry.room).filter(Boolean))].sort(),
|
|
102
|
+
subscribe(cb) { listeners.add(cb); return () => { listeners.delete(cb); }; },
|
|
103
|
+
dispose() {
|
|
104
|
+
disposed = true;
|
|
105
|
+
for (const id of entries.keys())
|
|
106
|
+
remove(id);
|
|
107
|
+
listeners.clear();
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { VaultOptions, VaultView } from './controller.js';
|
|
2
|
+
import type { VaultMode } from './settings.js';
|
|
3
|
+
import type { AcquireTransport } from './session.js';
|
|
4
|
+
export interface ScopeConfig {
|
|
5
|
+
mode: VaultMode;
|
|
6
|
+
room: string;
|
|
7
|
+
rev: number;
|
|
8
|
+
by: string;
|
|
9
|
+
}
|
|
10
|
+
type Setting = Pick<ScopeConfig, 'mode' | 'room'>;
|
|
11
|
+
export interface ScopeVaultOptions<S> extends VaultOptions<S> {
|
|
12
|
+
app: string;
|
|
13
|
+
roomId: string;
|
|
14
|
+
nodeId: string;
|
|
15
|
+
isWriter?: () => boolean;
|
|
16
|
+
defaultConfig?: () => Setting | Promise<Setting>;
|
|
17
|
+
}
|
|
18
|
+
export interface ScopeVaultView extends Omit<VaultView, 'hasCode' | 'appEnabled'> {
|
|
19
|
+
config: ScopeConfig;
|
|
20
|
+
providerSeen: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface ScopeVault {
|
|
23
|
+
getView(): ScopeVaultView;
|
|
24
|
+
subscribe(cb: (view: ScopeVaultView) => void): () => void;
|
|
25
|
+
configure(mode: VaultMode, room?: string): boolean;
|
|
26
|
+
materializeDefault(): Promise<boolean>;
|
|
27
|
+
receiveConfig(cfg: unknown, from: string): boolean;
|
|
28
|
+
onConfig(cb: (cfg: ScopeConfig) => void): () => void;
|
|
29
|
+
changed(): void;
|
|
30
|
+
saveNow(): Promise<void>;
|
|
31
|
+
restoreNow(): Promise<void>;
|
|
32
|
+
dispose(): void;
|
|
33
|
+
}
|
|
34
|
+
export declare function defaultScopeConfig(storage?: Storage | undefined): Promise<Setting>;
|
|
35
|
+
export declare function scopeInviteParam(cfg: Setting): string;
|
|
36
|
+
export declare function parseScopeInviteParam(v: unknown): Setting | null;
|
|
37
|
+
export declare function createScopeVault<S>(o: ScopeVaultOptions<S>): ScopeVault;
|
|
38
|
+
export declare function openScopeVault<S>(o: Omit<ScopeVaultOptions<S>, 'transport'>, acquire: AcquireTransport): ScopeVault;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { deriveAutoRoom } from './core/crypto.js';
|
|
2
|
+
import { isRecord, safeInt } from './core/head.js';
|
|
3
|
+
import { defaultAppState, parseAppState } from './appState.js';
|
|
4
|
+
import { browserStorage, loadSettings, validVaultRoom } from './settings.js';
|
|
5
|
+
import { createVaultSession } from './session.js';
|
|
6
|
+
import { scopeStateKey } from './scopeKey.js';
|
|
7
|
+
import { read, write, notifyStored, subscribeStored } from './stored.js';
|
|
8
|
+
const validNode = (v) => typeof v === 'string' && v.length > 0 && v.length <= 128;
|
|
9
|
+
const auto = () => ({ mode: 'auto', room: '' });
|
|
10
|
+
// A local default has no author. Only authored configs are valid on the wire.
|
|
11
|
+
const localConfig = ({ mode, room }) => ({ mode, room, rev: 0, by: '' });
|
|
12
|
+
const validSetting = (v) => isRecord(v) && typeof v.room === 'string' &&
|
|
13
|
+
(v.mode === 'custom' ? validVaultRoom(v.room) : (v.mode === 'off' || v.mode === 'auto') && v.room === '');
|
|
14
|
+
const newer = (a, b) => a.rev > b.rev || a.rev === b.rev && a.by > b.by;
|
|
15
|
+
const CFG_STEP = 1000;
|
|
16
|
+
const peerRevisionLimit = Number.MAX_SAFE_INTEGER - CFG_STEP;
|
|
17
|
+
// Reopening a scope can replace its adapter while the old apply still owns the store.
|
|
18
|
+
const scopeWorkQueues = new Map();
|
|
19
|
+
function parseConfig(value, roomId) {
|
|
20
|
+
if (!isRecord(value))
|
|
21
|
+
return;
|
|
22
|
+
const { rev, by } = value;
|
|
23
|
+
if (!validNode(by) || !safeInt(rev, 0) || !validSetting(value) || value.room === roomId)
|
|
24
|
+
return;
|
|
25
|
+
return { mode: value.mode, room: value.room, rev, by };
|
|
26
|
+
}
|
|
27
|
+
function personalRoomSetting(storage) {
|
|
28
|
+
const personal = loadSettings(storage);
|
|
29
|
+
if (!personal.code || personal.mode === 'off')
|
|
30
|
+
return auto();
|
|
31
|
+
if (personal.mode === 'custom')
|
|
32
|
+
return { mode: 'custom', room: personal.room };
|
|
33
|
+
return deriveAutoRoom('tc-vault', personal.code).then(room => ({ mode: 'custom', room }));
|
|
34
|
+
}
|
|
35
|
+
export async function defaultScopeConfig(storage = browserStorage()) {
|
|
36
|
+
return personalRoomSetting(storage);
|
|
37
|
+
}
|
|
38
|
+
// Always explicit: unlike tc-linkai, a joiner's own default may differ (personal vault room), so 'auto' is spelled out.
|
|
39
|
+
// Custom rooms carry a 'c:' prefix so a room literally named 'auto' or 'off' stays unambiguous.
|
|
40
|
+
export function scopeInviteParam(cfg) {
|
|
41
|
+
return cfg.mode === 'auto' ? 'auto' : cfg.mode === 'off' ? 'off' : 'c:' + cfg.room;
|
|
42
|
+
}
|
|
43
|
+
export function parseScopeInviteParam(v) {
|
|
44
|
+
if (v === 'auto' || v === 'off')
|
|
45
|
+
return { mode: v, room: '' };
|
|
46
|
+
if (v === '')
|
|
47
|
+
return { mode: 'off', room: '' };
|
|
48
|
+
if (typeof v === 'string' && v.startsWith('c:'))
|
|
49
|
+
return validVaultRoom(v.slice(2)) ? { mode: 'custom', room: v.slice(2) } : null;
|
|
50
|
+
return typeof v === 'string' && validVaultRoom(v) ? { mode: 'custom', room: v } : null;
|
|
51
|
+
}
|
|
52
|
+
export function createScopeVault(o) {
|
|
53
|
+
return openScopeVault(o, () => ({ transport: typeof o.transport === 'function' ? o.transport() : o.transport }));
|
|
54
|
+
}
|
|
55
|
+
export function openScopeVault(o, acquire) {
|
|
56
|
+
const { app, roomId, nodeId } = o, storage = o.storage ?? browserStorage();
|
|
57
|
+
if (!app || !roomId || !validNode(nodeId))
|
|
58
|
+
throw new Error('Scope app, room ID and node ID are required');
|
|
59
|
+
const key = scopeStateKey(app, roomId);
|
|
60
|
+
const load = () => {
|
|
61
|
+
const raw = read(key, storage);
|
|
62
|
+
let cfg;
|
|
63
|
+
if (isRecord(raw)) {
|
|
64
|
+
if (raw.sharedConfig !== false) {
|
|
65
|
+
cfg = parseConfig(raw.cfg, roomId);
|
|
66
|
+
// Older clients persisted unchecked peer clocks. Keep the chosen room and
|
|
67
|
+
// author, but let a saturated legacy clock start again at the first revision.
|
|
68
|
+
if (cfg && raw.boundedClock !== true && cfg.rev >= peerRevisionLimit - CFG_STEP)
|
|
69
|
+
cfg.rev = 0;
|
|
70
|
+
}
|
|
71
|
+
else if (validSetting(raw.cfg) && raw.cfg.room !== roomId)
|
|
72
|
+
cfg = localConfig(raw.cfg);
|
|
73
|
+
}
|
|
74
|
+
return { state: parseAppState(raw), cfg,
|
|
75
|
+
providerSeen: isRecord(raw) && raw.providerSeen === true };
|
|
76
|
+
};
|
|
77
|
+
const stored = load();
|
|
78
|
+
let cfg = stored.cfg ?? localConfig(auto());
|
|
79
|
+
let providerSeen = stored.providerSeen, initialized = false, disposed = false;
|
|
80
|
+
const configListeners = new Set();
|
|
81
|
+
let ready;
|
|
82
|
+
const initialization = new Promise(resolve => { ready = resolve; });
|
|
83
|
+
const core = createVaultSession({
|
|
84
|
+
...o, acquire, autoRoomApp: app, lockName: key, workQueues: scopeWorkQueues,
|
|
85
|
+
isWriter: o.isWriter ?? (() => true),
|
|
86
|
+
loadState: () => load().state,
|
|
87
|
+
saveState(state) {
|
|
88
|
+
const latest = load();
|
|
89
|
+
if (latest.cfg?.by && newer(latest.cfg, cfg))
|
|
90
|
+
return;
|
|
91
|
+
providerSeen ||= latest.providerSeen;
|
|
92
|
+
write(key, { ...state, ...(initialized && { cfg, sharedConfig: !!cfg.by, boundedClock: true }), providerSeen }, storage);
|
|
93
|
+
notifyStored(storage, key);
|
|
94
|
+
},
|
|
95
|
+
waitForProvider: () => cfg.mode === 'custom' || providerSeen,
|
|
96
|
+
providerFound() { if (!providerSeen) {
|
|
97
|
+
providerSeen = true;
|
|
98
|
+
core.persist();
|
|
99
|
+
} },
|
|
100
|
+
});
|
|
101
|
+
function start() {
|
|
102
|
+
initialized = true;
|
|
103
|
+
core.start({ ...cfg, code: roomId });
|
|
104
|
+
ready();
|
|
105
|
+
}
|
|
106
|
+
function announce() {
|
|
107
|
+
for (const cb of configListeners) {
|
|
108
|
+
try {
|
|
109
|
+
cb({ ...cfg });
|
|
110
|
+
}
|
|
111
|
+
catch { /* Isolate subscribers. */ }
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function adopt(next, fromStorage = false) {
|
|
115
|
+
const switched = cfg.mode !== next.mode || cfg.room !== next.room;
|
|
116
|
+
cfg = next;
|
|
117
|
+
if (switched)
|
|
118
|
+
core.resetState(fromStorage ? load().state : { ...defaultAppState(), dirty: true });
|
|
119
|
+
if (switched || !initialized)
|
|
120
|
+
start();
|
|
121
|
+
if (!fromStorage)
|
|
122
|
+
core.persist();
|
|
123
|
+
announce();
|
|
124
|
+
}
|
|
125
|
+
function readShared() {
|
|
126
|
+
if (disposed || core.writing)
|
|
127
|
+
return;
|
|
128
|
+
const next = load();
|
|
129
|
+
providerSeen ||= next.providerSeen;
|
|
130
|
+
if (next.cfg?.by && newer(next.cfg, cfg))
|
|
131
|
+
adopt(next.cfg, true);
|
|
132
|
+
core.sharedStateChanged(next.state);
|
|
133
|
+
}
|
|
134
|
+
const offStorage = subscribeStored(readShared, storage, globalThis.window, k => k === key || k === null);
|
|
135
|
+
function initialize(setting) {
|
|
136
|
+
if (core.closed || initialized)
|
|
137
|
+
return;
|
|
138
|
+
const initial = validSetting(setting) && setting.room !== roomId ? setting : auto();
|
|
139
|
+
cfg = localConfig(initial);
|
|
140
|
+
start();
|
|
141
|
+
core.persist();
|
|
142
|
+
}
|
|
143
|
+
if (stored.cfg)
|
|
144
|
+
start();
|
|
145
|
+
else {
|
|
146
|
+
try {
|
|
147
|
+
const initial = o.defaultConfig ? o.defaultConfig() : personalRoomSetting(storage);
|
|
148
|
+
if (validSetting(initial))
|
|
149
|
+
initialize(initial);
|
|
150
|
+
else
|
|
151
|
+
void Promise.resolve(initial).then(initialize, () => initialize(auto()));
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
initialize(auto());
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function getView() {
|
|
158
|
+
const { hasCode, appEnabled, ...view } = core.getView();
|
|
159
|
+
return { ...view, ...(!initialized && { mode: cfg.mode, status: 'searching' }), config: { ...cfg }, providerSeen };
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
getView, subscribe: cb => core.subscribe(() => cb(getView())),
|
|
163
|
+
configure(mode, room = '') {
|
|
164
|
+
if (disposed)
|
|
165
|
+
return false;
|
|
166
|
+
readShared();
|
|
167
|
+
const next = { mode, room: mode === 'custom' ? room : '', rev: cfg.rev + 1, by: nodeId };
|
|
168
|
+
if (!parseConfig(next, roomId) || cfg.by && next.mode === cfg.mode && next.room === cfg.room)
|
|
169
|
+
return false;
|
|
170
|
+
adopt(next);
|
|
171
|
+
return true;
|
|
172
|
+
},
|
|
173
|
+
async materializeDefault() {
|
|
174
|
+
if (!disposed && !initialized)
|
|
175
|
+
await initialization;
|
|
176
|
+
if (disposed || cfg.by)
|
|
177
|
+
return false;
|
|
178
|
+
readShared();
|
|
179
|
+
if (disposed || cfg.by)
|
|
180
|
+
return false;
|
|
181
|
+
adopt({ ...cfg, rev: 1, by: nodeId });
|
|
182
|
+
return true;
|
|
183
|
+
},
|
|
184
|
+
receiveConfig(value, from) {
|
|
185
|
+
if (disposed || !validNode(from))
|
|
186
|
+
return false;
|
|
187
|
+
readShared();
|
|
188
|
+
const next = parseConfig(value, roomId);
|
|
189
|
+
if (!next || !newer(next, cfg))
|
|
190
|
+
return false;
|
|
191
|
+
// Bound each peer advance and reserve safe integer revisions for local edits.
|
|
192
|
+
if ((cfg.by && next.rev - cfg.rev > CFG_STEP) || next.rev >= peerRevisionLimit)
|
|
193
|
+
return false;
|
|
194
|
+
adopt(next);
|
|
195
|
+
return true;
|
|
196
|
+
},
|
|
197
|
+
onConfig(cb) { configListeners.add(cb); return () => { configListeners.delete(cb); }; },
|
|
198
|
+
changed: core.changed,
|
|
199
|
+
async saveNow() { if (!disposed && !initialized)
|
|
200
|
+
await initialization; await core.saveNow(); },
|
|
201
|
+
async restoreNow() { if (!disposed && !initialized)
|
|
202
|
+
await initialization; await core.restoreNow(); },
|
|
203
|
+
dispose() {
|
|
204
|
+
if (disposed)
|
|
205
|
+
return;
|
|
206
|
+
disposed = true;
|
|
207
|
+
offStorage();
|
|
208
|
+
configListeners.clear();
|
|
209
|
+
void core.dispose(initialization).then(ready, ready);
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { EscrowTransport } from './core/client.js';
|
|
2
|
+
import type { VaultSettings, VaultAppState } from './settings.js';
|
|
3
|
+
import type { VaultAdapter, VaultView } from './controller.js';
|
|
4
|
+
type Destination = Pick<VaultSettings, 'mode' | 'room' | 'code'>;
|
|
5
|
+
export interface TransportLease {
|
|
6
|
+
transport: EscrowTransport;
|
|
7
|
+
release?(): void;
|
|
8
|
+
providerFound?(): void;
|
|
9
|
+
}
|
|
10
|
+
export type AcquireTransport = (room: string, hasProvider: () => boolean) => TransportLease;
|
|
11
|
+
interface SessionOptions<S> {
|
|
12
|
+
app: string;
|
|
13
|
+
adapter: VaultAdapter<S>;
|
|
14
|
+
autoRoomApp: string;
|
|
15
|
+
lockName: string;
|
|
16
|
+
acquire: AcquireTransport;
|
|
17
|
+
workQueues?: Map<string, Promise<void>>;
|
|
18
|
+
loadState(): VaultAppState;
|
|
19
|
+
saveState(state: VaultAppState): void;
|
|
20
|
+
isWriter?(): boolean;
|
|
21
|
+
waitForProvider?(): boolean;
|
|
22
|
+
providerFound?(): void;
|
|
23
|
+
now?: () => number;
|
|
24
|
+
partGcGraceMs?: number;
|
|
25
|
+
}
|
|
26
|
+
export declare function createVaultSession<S>(o: SessionOptions<S>): {
|
|
27
|
+
start: (destination: Destination) => void;
|
|
28
|
+
getView: () => VaultView;
|
|
29
|
+
changed: () => void;
|
|
30
|
+
persist: (clear?: {
|
|
31
|
+
version: number;
|
|
32
|
+
token?: string;
|
|
33
|
+
}) => void;
|
|
34
|
+
sharedStateChanged: (app: VaultAppState) => void;
|
|
35
|
+
readonly state: VaultAppState;
|
|
36
|
+
resetState(next: VaultAppState): void;
|
|
37
|
+
readonly writing: boolean;
|
|
38
|
+
readonly closed: boolean;
|
|
39
|
+
subscribe(cb: (view: VaultView) => void): () => void;
|
|
40
|
+
saveNow: () => Promise<void>;
|
|
41
|
+
restoreNow: () => Promise<void>;
|
|
42
|
+
dispose(configReady?: Promise<void>): Promise<void>;
|
|
43
|
+
};
|
|
44
|
+
export {};
|