@tik-choco/mistvault 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/LICENSE +373 -0
  2. package/README.ja.md +164 -0
  3. package/README.md +221 -2
  4. package/README.zh.md +164 -0
  5. package/dist/appState.d.ts +24 -0
  6. package/dist/appState.js +20 -0
  7. package/dist/code.d.ts +7 -0
  8. package/dist/code.js +49 -0
  9. package/dist/controller.d.ts +55 -0
  10. package/dist/controller.js +74 -0
  11. package/dist/core/client.d.ts +112 -0
  12. package/dist/core/client.js +536 -0
  13. package/dist/core/crypto.d.ts +24 -0
  14. package/dist/core/crypto.js +185 -0
  15. package/dist/core/head.d.ts +54 -0
  16. package/dist/core/head.js +123 -0
  17. package/dist/core/parse.d.ts +1 -0
  18. package/dist/core/parse.js +1 -0
  19. package/dist/core/protocol.d.ts +74 -0
  20. package/dist/core/protocol.js +93 -0
  21. package/dist/elements/PanelElement.d.ts +30 -0
  22. package/dist/elements/PanelElement.js +56 -0
  23. package/dist/elements/RoomControls.d.ts +21 -0
  24. package/dist/elements/RoomControls.js +27 -0
  25. package/dist/elements/ScopeVaultPanelElement.d.ts +10 -0
  26. package/dist/elements/ScopeVaultPanelElement.js +27 -0
  27. package/dist/elements/VaultPanelElement.d.ts +39 -0
  28. package/dist/elements/VaultPanelElement.js +191 -0
  29. package/dist/elements/dom.d.ts +14 -0
  30. package/dist/elements/dom.js +58 -0
  31. package/dist/elements/index.d.ts +15 -0
  32. package/dist/elements/index.js +12 -0
  33. package/dist/i18n.d.ts +132 -0
  34. package/dist/i18n.js +96 -0
  35. package/dist/index.d.ts +13 -0
  36. package/dist/index.js +12 -0
  37. package/dist/parts.d.ts +42 -0
  38. package/dist/parts.js +267 -0
  39. package/dist/preact/ScopeVaultPanel.d.ts +10 -0
  40. package/dist/preact/ScopeVaultPanel.js +8 -0
  41. package/dist/preact/VaultPanel.d.ts +16 -0
  42. package/dist/preact/VaultPanel.js +14 -0
  43. package/dist/preact/index.d.ts +5 -0
  44. package/dist/preact/index.js +3 -0
  45. package/dist/preact/ui.css +132 -0
  46. package/dist/preact/useVault.d.ts +6 -0
  47. package/dist/preact/useVault.js +13 -0
  48. package/dist/scopeKey.d.ts +1 -0
  49. package/dist/scopeKey.js +49 -0
  50. package/dist/scopeManager.d.ts +14 -0
  51. package/dist/scopeManager.js +110 -0
  52. package/dist/scopeVault.d.ts +39 -0
  53. package/dist/scopeVault.js +212 -0
  54. package/dist/session.d.ts +44 -0
  55. package/dist/session.js +736 -0
  56. package/dist/settings.d.ts +21 -0
  57. package/dist/settings.js +59 -0
  58. package/dist/stored.d.ts +4 -0
  59. package/dist/stored.js +27 -0
  60. package/dist/transport.d.ts +20 -0
  61. package/dist/transport.js +117 -0
  62. package/package.json +57 -4
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ export * from './core/crypto.js';
2
+ export * from './core/head.js';
3
+ export * from './core/protocol.js';
4
+ export * from './core/client.js';
5
+ export * from './code.js';
6
+ export * from './settings.js';
7
+ export * from './transport.js';
8
+ export * from './controller.js';
9
+ export { createScopeVault, defaultScopeConfig, scopeInviteParam, parseScopeInviteParam } from './scopeVault.js';
10
+ export * from './scopeManager.js';
11
+ export * from './parts.js';
12
+ export * from './i18n.js';
@@ -0,0 +1,42 @@
1
+ import type { EscrowHead } from './core/head.js';
2
+ import type { EscrowClient } from './core/client.js';
3
+ import type { VaultAppState } from './settings.js';
4
+ export interface VaultParts<S> {
5
+ list(state: S): {
6
+ id: string;
7
+ size: number;
8
+ }[];
9
+ read(id: string): Promise<Uint8Array | null>;
10
+ has(id: string): Promise<boolean>;
11
+ write(id: string, bytes: Uint8Array): Promise<void>;
12
+ maxBytes?: number;
13
+ }
14
+ export declare const DEFAULT_MAX_PART_BYTES: number;
15
+ export declare const DEFAULT_PART_GC_GRACE_MS: number;
16
+ export declare const PART_SCHEMA = "tc-vault/part/1";
17
+ /** Runs at most two attachment operations at a time. */
18
+ export declare function mapParts<T>(items: readonly T[], work: (item: T) => Promise<void>): Promise<void>;
19
+ export declare function listedParts<S>(adapter: VaultParts<S>, state: S): {
20
+ id: string;
21
+ size: number;
22
+ }[];
23
+ export interface PartContext<S> {
24
+ adapter: VaultParts<S>;
25
+ app: string;
26
+ code: string;
27
+ client: EscrowClient;
28
+ state: VaultAppState;
29
+ active(): boolean;
30
+ persist(): void;
31
+ collecting?: Set<string>;
32
+ canUpload?(): boolean;
33
+ canCollect?(): boolean;
34
+ now?(): number;
35
+ partGcGraceMs?: number;
36
+ }
37
+ /** Record references only from an acknowledged or observed current main snapshot. */
38
+ export declare function observePartReferences<S>(ctx: PartContext<S>, snapshot: S, main: EscrowHead): boolean;
39
+ export declare function uploadParts<S>(ctx: PartContext<S>, snapshot: S): Promise<void>;
40
+ export declare function fetchParts<S>(ctx: PartContext<S>, snapshot: S): Promise<number>;
41
+ /** Validate the exact part generation, then re-check the main head immediately before deletion. */
42
+ export declare function collectParts<S>(ctx: PartContext<S>, snapshot: S, main: EscrowHead): Promise<void>;
package/dist/parts.js ADDED
@@ -0,0 +1,267 @@
1
+ import { derivePartKeys, PART_ID_RE, sha256 } from './core/crypto.js';
2
+ import { DEFAULT_MAX_BLOB, headHash, verifyHead } from './core/head.js';
3
+ import { EscrowRequestError } from './core/client.js';
4
+ export const DEFAULT_MAX_PART_BYTES = DEFAULT_MAX_BLOB - 64 * 1024;
5
+ export const DEFAULT_PART_GC_GRACE_MS = 24 * 60 * 60 * 1000;
6
+ export const PART_SCHEMA = 'tc-vault/part/1';
7
+ /** Runs at most two attachment operations at a time. */
8
+ export async function mapParts(items, work) {
9
+ let index = 0;
10
+ const run = async () => { while (index < items.length)
11
+ await work(items[index++]); };
12
+ const outcomes = await Promise.allSettled([run(), run()]);
13
+ const failed = outcomes.find(result => result.status === 'rejected');
14
+ if (failed?.status === 'rejected')
15
+ throw failed.reason;
16
+ }
17
+ export function listedParts(adapter, state) {
18
+ const parts = adapter.list(state), ids = new Set();
19
+ for (const p of parts) {
20
+ if (typeof p.id !== 'string' || !PART_ID_RE.test(p.id) || !Number.isSafeInteger(p.size) || p.size < 0 || ids.has(p.id))
21
+ throw new Error('Invalid vault part list');
22
+ ids.add(p.id);
23
+ }
24
+ return parts;
25
+ }
26
+ /** Record references only from an acknowledged or observed current main snapshot. */
27
+ export function observePartReferences(ctx, snapshot, main) {
28
+ if (!ctx.active() || main.seq < (ctx.state.partsObservedSeq ?? 0))
29
+ return false;
30
+ const keep = new Set(listedParts(ctx.adapter, snapshot).map(p => p.id)), now = (ctx.now ?? Date.now)();
31
+ let changed = ctx.state.partsObservedSeq !== main.seq;
32
+ ctx.state.partsObservedSeq = main.seq;
33
+ for (const inventory of [ctx.state.parts, ctx.state.pendingDeletes]) {
34
+ for (const [id, entry] of Object.entries(inventory ?? {})) {
35
+ if (keep.has(id)) {
36
+ if (entry.unreferencedSince !== undefined) {
37
+ const { unreferencedSince: _, ...referenced } = entry;
38
+ inventory[id] = referenced;
39
+ changed = true;
40
+ }
41
+ }
42
+ else if (entry.unreferencedSince === undefined) {
43
+ inventory[id] = { ...entry, unreferencedSince: now };
44
+ changed = true;
45
+ }
46
+ }
47
+ }
48
+ if (changed)
49
+ ctx.persist();
50
+ return true;
51
+ }
52
+ function invalidatePart(ctx, id) {
53
+ if (ctx.state.parts[id]) {
54
+ delete ctx.state.parts[id];
55
+ ctx.persist();
56
+ }
57
+ }
58
+ export async function uploadParts(ctx, snapshot) {
59
+ await uploadListedParts(ctx, listedParts(ctx.adapter, snapshot));
60
+ }
61
+ async function uploadListedParts(ctx, parts, recordOmissions = true) {
62
+ const { adapter, client, app, code, state } = ctx;
63
+ const omitted = new Set();
64
+ await mapParts(parts, async ({ id, size }) => {
65
+ if (!ctx.active() || ctx.canUpload?.() === false)
66
+ return;
67
+ const max = adapter.maxBytes ?? DEFAULT_MAX_PART_BYTES;
68
+ if (size > max) {
69
+ omitted.add(id);
70
+ return;
71
+ }
72
+ const bytes = await adapter.read(id);
73
+ if (!ctx.active())
74
+ return;
75
+ if (!bytes || bytes.byteLength > max) {
76
+ omitted.add(id);
77
+ return;
78
+ }
79
+ const hash = await sha256(bytes);
80
+ if (!ctx.active())
81
+ return;
82
+ const keys = await derivePartKeys(app, code, id);
83
+ for (let attempt = 0; attempt < 3 && ctx.active(); attempt++) {
84
+ const head = await client.getHead(keys.slot);
85
+ if (!ctx.active())
86
+ return;
87
+ if (head && !await verifyHead(head, { slot: keys.slot, writer: keys.writer }))
88
+ throw new Error('Invalid part head');
89
+ if (!head)
90
+ invalidatePart(ctx, id);
91
+ const tracked = state.parts[id];
92
+ if (head && tracked?.hash === hash && tracked.seq === head.seq && !ctx.collecting?.has(id)) {
93
+ const generation = await headHash(head);
94
+ if (tracked.headHash === generation)
95
+ return;
96
+ if (!tracked.headHash) {
97
+ const latest = await client.fetchLatestWithKeys(keys);
98
+ if (!ctx.active())
99
+ return;
100
+ if (latest && await headHash(latest.head) === generation && await sha256(latest.plaintext) === hash) {
101
+ state.parts = { ...state.parts, [id]: { ...tracked, headHash: generation } };
102
+ ctx.persist();
103
+ return;
104
+ }
105
+ }
106
+ }
107
+ const parent = head ? await headHash(head) : null;
108
+ if (!ctx.active() || ctx.canUpload?.() === false)
109
+ return;
110
+ const result = await client.saveWithKeys(keys, bytes, { schema: PART_SCHEMA, comp: 'none',
111
+ seq: (head?.seq ?? 0) + 1, parent });
112
+ if (!ctx.active())
113
+ return;
114
+ if (result.type === 'ack') {
115
+ state.parts = { ...state.parts, [id]: { seq: result.seq, hash, headHash: result.hash } };
116
+ delete state.pendingDeletes?.[id];
117
+ ctx.persist();
118
+ return;
119
+ }
120
+ if (result.type === 'error') {
121
+ if (result.code === 'deleted')
122
+ invalidatePart(ctx, id);
123
+ if (result.code === 'quota' || result.code === 'too_large') {
124
+ omitted.add(id);
125
+ return;
126
+ }
127
+ // Deleted slots retain their sequence and parent on supporting providers.
128
+ if (result.code === 'deleted' && result.head) {
129
+ const deletedParent = await headHash(result.head);
130
+ if (!ctx.active() || ctx.canUpload?.() === false)
131
+ return;
132
+ const retry = await client.saveWithKeys(keys, bytes, { schema: PART_SCHEMA, comp: 'none', seq: result.head.seq + 1, parent: deletedParent });
133
+ if (!ctx.active())
134
+ return;
135
+ if (retry.type === 'error' && retry.code === 'not_writer')
136
+ throw new EscrowRequestError(retry.code, retry.message);
137
+ if (retry.type === 'ack') {
138
+ state.parts = { ...state.parts, [id]: { seq: retry.seq, hash, headHash: retry.hash } };
139
+ delete state.pendingDeletes?.[id];
140
+ ctx.persist();
141
+ return;
142
+ }
143
+ }
144
+ throw new EscrowRequestError(result.code, result.code + ': ' + result.message);
145
+ }
146
+ }
147
+ if (ctx.active())
148
+ throw new Error('Part conflict retry limit reached');
149
+ });
150
+ if (ctx.active() && ctx.canUpload?.() !== false && recordOmissions) {
151
+ state.omitted = [...omitted].sort();
152
+ ctx.persist();
153
+ }
154
+ }
155
+ export async function fetchParts(ctx, snapshot) {
156
+ let pending = 0;
157
+ await mapParts(listedParts(ctx.adapter, snapshot), async (part) => {
158
+ const { id } = part;
159
+ if (!ctx.active())
160
+ return;
161
+ const keys = await derivePartKeys(ctx.app, ctx.code, id);
162
+ if (!ctx.active())
163
+ return;
164
+ try {
165
+ const head = await ctx.client.getHead(keys.slot);
166
+ if (!ctx.active())
167
+ return;
168
+ if (head && !await verifyHead(head, { slot: keys.slot, writer: keys.writer }))
169
+ throw new Error('Invalid part head');
170
+ if (!head) {
171
+ invalidatePart(ctx, id);
172
+ // Local bytes can repair a deletion even when has() still reports a local copy.
173
+ await uploadListedParts(ctx, [part], false);
174
+ if (ctx.active() && !ctx.state.parts[id])
175
+ pending++;
176
+ return;
177
+ }
178
+ if (await ctx.adapter.has(id))
179
+ return;
180
+ const latest = await ctx.client.fetchLatestWithKeys(keys);
181
+ if (!ctx.active())
182
+ return;
183
+ if (!latest) {
184
+ invalidatePart(ctx, id);
185
+ await uploadListedParts(ctx, [part], false);
186
+ if (ctx.active() && !ctx.state.parts[id])
187
+ pending++;
188
+ return;
189
+ }
190
+ if (latest.head.schema !== PART_SCHEMA)
191
+ throw new Error('Unsupported part schema');
192
+ await ctx.adapter.write(id, latest.plaintext);
193
+ const hash = await sha256(latest.plaintext);
194
+ const generation = await headHash(latest.head);
195
+ if (ctx.active()) {
196
+ ctx.state.parts = { ...ctx.state.parts, [id]: { seq: latest.head.seq, hash, headHash: generation } };
197
+ ctx.persist();
198
+ }
199
+ }
200
+ catch {
201
+ // Keep unavailable bytes pending; later restores retry without writing a placeholder.
202
+ if (ctx.active())
203
+ pending++;
204
+ }
205
+ });
206
+ return pending;
207
+ }
208
+ /** Validate the exact part generation, then re-check the main head immediately before deletion. */
209
+ export async function collectParts(ctx, snapshot, main) {
210
+ const keep = new Set(listedParts(ctx.adapter, snapshot).map(p => p.id)), expected = await headHash(main);
211
+ const observed = await ctx.client.getHead(main.slot);
212
+ if (!ctx.active() || !observed || await headHash(observed) !== expected || ctx.canCollect && !ctx.canCollect())
213
+ return;
214
+ if (!observePartReferences(ctx, snapshot, main))
215
+ return;
216
+ for (const id of new Set([...Object.keys(ctx.state.parts), ...Object.keys(ctx.state.pendingDeletes ?? {})])) {
217
+ if (!ctx.active() || keep.has(id))
218
+ continue;
219
+ if (ctx.canCollect && !ctx.canCollect())
220
+ return;
221
+ const entry = ctx.state.parts[id] ?? ctx.state.pendingDeletes?.[id];
222
+ if (!entry)
223
+ continue;
224
+ if (entry.unreferencedSince === undefined ||
225
+ (ctx.now ?? Date.now)() - entry.unreferencedSince < (ctx.partGcGraceMs ?? DEFAULT_PART_GC_GRACE_MS))
226
+ continue;
227
+ ctx.collecting?.add(id);
228
+ try {
229
+ const keys = await derivePartKeys(ctx.app, ctx.code, id), head = await ctx.client.getHead(keys.slot);
230
+ if (!ctx.active() || ctx.canCollect && !ctx.canCollect())
231
+ return;
232
+ if (head) {
233
+ if (head.seq !== entry.seq || !await verifyHead(head, { slot: keys.slot, writer: keys.writer }))
234
+ continue;
235
+ if (entry.headHash) {
236
+ if (await headHash(head) !== entry.headHash)
237
+ continue;
238
+ }
239
+ else {
240
+ // Older inventories recorded only a plaintext hash.
241
+ const latest = await ctx.client.fetchLatestWithKeys(keys);
242
+ if (!latest || await headHash(latest.head) !== await headHash(head) || await sha256(latest.plaintext) !== entry.hash)
243
+ continue;
244
+ }
245
+ }
246
+ const current = await ctx.client.getHead(main.slot);
247
+ // Only the acknowledged, parsed snapshot is trusted to describe references.
248
+ // A different head defers collection until its references have been observed.
249
+ if (!ctx.active() || !current || await headHash(current) !== expected ||
250
+ ctx.canCollect && !ctx.canCollect() || (ctx.state.parts[id] ?? ctx.state.pendingDeletes?.[id]) !== entry)
251
+ return;
252
+ ctx.state.pendingDeletes = { ...ctx.state.pendingDeletes, [id]: entry };
253
+ delete ctx.state.parts[id];
254
+ ctx.persist();
255
+ if (!head || (await ctx.client.deleteWithKeys(keys, head)).type === 'deleted') {
256
+ if (ctx.active() && ctx.state.pendingDeletes?.[id] === entry) {
257
+ delete ctx.state.pendingDeletes[id];
258
+ ctx.persist();
259
+ }
260
+ }
261
+ }
262
+ catch { /* Retry uncertain deletions without trusting the uploaded inventory. */ }
263
+ finally {
264
+ ctx.collecting?.delete(id);
265
+ }
266
+ }
267
+ }
@@ -0,0 +1,10 @@
1
+ import type { ScopeVault } from '../scopeVault.js';
2
+ import type { ScopeVaultPanelElement } from '../elements/index.js';
3
+ import '../elements/index.js';
4
+ export interface ScopeVaultPanelProps {
5
+ vault: ScopeVault;
6
+ locale?: string;
7
+ }
8
+ export declare function ScopeVaultPanel({ vault, locale }: ScopeVaultPanelProps): import("preact").VNode<import("preact").Attributes & {
9
+ ref: import("preact").RefObject<ScopeVaultPanelElement>;
10
+ }>;
@@ -0,0 +1,8 @@
1
+ import { h } from 'preact';
2
+ import { useLayoutEffect, useRef } from 'preact/hooks';
3
+ import '../elements/index.js';
4
+ export function ScopeVaultPanel({ vault, locale }) {
5
+ const ref = useRef(null);
6
+ useLayoutEffect(() => { Object.assign(ref.current, { locale, vault }); });
7
+ return h('mistvault-scope-panel', { ref });
8
+ }
@@ -0,0 +1,16 @@
1
+ import type { VaultController } from '../controller.js';
2
+ import type { VaultPanelElement } from '../elements/index.js';
3
+ import '../elements/index.js';
4
+ export interface VaultPanelProps {
5
+ controller: VaultController;
6
+ locale?: string;
7
+ appName?: string;
8
+ extraRooms?: string[];
9
+ /** A device-link code consumed by the application before the panel was mounted. */
10
+ pendingCode?: string | null;
11
+ /** Called after a supplied pending code is imported or dismissed. Clear pendingCode in the caller. */
12
+ onPendingHandled?: (result: 'imported' | 'cancelled') => void;
13
+ }
14
+ export declare function VaultPanel({ controller, locale, appName, pendingCode, onPendingHandled, extraRooms }: VaultPanelProps): import("preact").VNode<import("preact").Attributes & {
15
+ ref: import("preact").RefObject<VaultPanelElement>;
16
+ }>;
@@ -0,0 +1,14 @@
1
+ import { h } from 'preact';
2
+ import { useLayoutEffect, useRef } from 'preact/hooks';
3
+ import '../elements/index.js';
4
+ export function VaultPanel({ controller, locale, appName, pendingCode, onPendingHandled, extraRooms }) {
5
+ const ref = useRef(null);
6
+ useLayoutEffect(() => {
7
+ const panel = ref.current;
8
+ const handled = (e) => onPendingHandled?.(e.detail);
9
+ panel.addEventListener('mistvault-pending-handled', handled);
10
+ Object.assign(panel, { locale, appName, pendingCode, extraRooms, controller });
11
+ return () => panel.removeEventListener('mistvault-pending-handled', handled);
12
+ });
13
+ return h('mistvault-panel', { ref });
14
+ }
@@ -0,0 +1,5 @@
1
+ export { VaultPanel } from './VaultPanel.js';
2
+ export type { VaultPanelProps } from './VaultPanel.js';
3
+ export { useVault } from './useVault.js';
4
+ export { ScopeVaultPanel } from './ScopeVaultPanel.js';
5
+ export type { ScopeVaultPanelProps } from './ScopeVaultPanel.js';
@@ -0,0 +1,3 @@
1
+ export { VaultPanel } from './VaultPanel.js';
2
+ export { useVault } from './useVault.js';
3
+ export { ScopeVaultPanel } from './ScopeVaultPanel.js';
@@ -0,0 +1,132 @@
1
+ mistvault-panel, mistvault-scope-panel { display: block; }
2
+ .mv-panel {
3
+ color: var(--mv-fg, var(--mist-fg, inherit));
4
+ background: var(--mv-bg, var(--mist-bg, transparent));
5
+ font: inherit;
6
+ line-height: 1.5;
7
+ max-width: 44rem;
8
+ min-width: 0;
9
+ box-sizing: border-box;
10
+ }
11
+ .mv-panel *, .mv-panel *::before, .mv-panel *::after { box-sizing: border-box; }
12
+ .mv-panel h2 { font-size: 1.3em; line-height: 1.25; margin: 0 0 1rem; }
13
+ .mv-panel h3 { font-size: 1em; line-height: 1.3; margin: 0; }
14
+ .mv-panel p { margin: 0; overflow-wrap: anywhere; }
15
+ .mv-panel .mv-muted { color: var(--mv-muted, var(--mist-muted, #6d7772)); font-size: .9em; }
16
+ .mv-panel label { display: block; font-weight: 600; }
17
+ .mv-panel input:not([type=checkbox]), .mv-panel select {
18
+ display: block;
19
+ min-width: 0;
20
+ width: 100%;
21
+ padding: .65rem .7rem;
22
+ border: 1px solid var(--mv-border, var(--mist-border, rgba(127, 127, 127, .35)));
23
+ border-radius: .5rem;
24
+ color: inherit;
25
+ background: var(--mv-bg, var(--mist-bg, transparent));
26
+ font: inherit;
27
+ font-weight: 400;
28
+ }
29
+ .mv-panel input::placeholder { color: var(--mv-muted, var(--mist-muted, #6d7772)); opacity: 1; }
30
+ .mv-panel button {
31
+ min-height: 2.75rem;
32
+ padding: .6rem .8rem;
33
+ border: 1px solid var(--mv-border, var(--mist-border, rgba(127, 127, 127, .35)));
34
+ border-radius: .5rem;
35
+ color: inherit;
36
+ background: var(--mv-bg, var(--mist-bg, transparent));
37
+ font: inherit;
38
+ font-weight: 600;
39
+ line-height: 1.3;
40
+ cursor: pointer;
41
+ overflow-wrap: anywhere;
42
+ }
43
+ .mv-panel button:hover:not(:disabled) { border-color: var(--mv-accent, var(--mist-accent, #315fbd)); }
44
+ .mv-panel button.mv-primary { color: var(--mv-bg, var(--mist-bg, #fff)); background: var(--mv-accent, var(--mist-accent, #315fbd)); border-color: var(--mv-accent, var(--mist-accent, #315fbd)); }
45
+ .mv-panel button:disabled { opacity: .5; cursor: default; }
46
+ .mv-panel :focus-visible { outline: 2px solid var(--mv-accent, var(--mist-accent, #315fbd)); outline-offset: 3px; }
47
+ .mv-panel .mv-welcome > p { margin-bottom: 1.25rem; }
48
+ .mv-panel .mv-stack { display: grid; gap: .55rem; }
49
+ .mv-panel .mv-actions { display: flex; flex-wrap: wrap; gap: .5rem; }
50
+ .mv-panel .mv-actions button { flex: 1 1 7rem; }
51
+ .mv-panel .mv-status-card, .mv-panel .mv-confirm {
52
+ padding: 1rem;
53
+ border: 1px solid var(--mv-border, var(--mist-border, rgba(127, 127, 127, .35)));
54
+ border-radius: .75rem;
55
+ background: color-mix(in srgb, var(--mv-accent, var(--mist-accent, #315fbd)) 7%, transparent);
56
+ }
57
+ .mv-panel .mv-status-text { display: grid; gap: .15rem; margin-bottom: .85rem; }
58
+ .mv-panel .mv-status-text strong::before {
59
+ display: inline-block;
60
+ width: .55rem;
61
+ height: .55rem;
62
+ margin-inline-end: .5rem;
63
+ border-radius: 50%;
64
+ background: var(--mv-accent, var(--mist-accent, #315fbd));
65
+ content: '';
66
+ vertical-align: .05em;
67
+ }
68
+ .mv-panel .mv-status-text[data-state=off] strong::before { background: var(--mv-muted, var(--mist-muted, #6d7772)); }
69
+ .mv-panel .mv-status-text[data-state=error] strong::before { background: var(--mv-error, var(--mist-error, #b33b3b)); }
70
+ .mv-panel .mv-switch {
71
+ display: flex;
72
+ align-items: center;
73
+ justify-content: space-between;
74
+ gap: 1rem;
75
+ padding: 1.1rem 0;
76
+ cursor: pointer;
77
+ }
78
+ .mv-panel .mv-switch span { min-width: 0; overflow-wrap: anywhere; }
79
+ .mv-panel .mv-switch input {
80
+ appearance: none;
81
+ flex: 0 0 auto;
82
+ width: 2.6rem;
83
+ height: 1.5rem;
84
+ margin: 0;
85
+ border: 1px solid var(--mv-border, var(--mist-border, rgba(127, 127, 127, .55)));
86
+ border-radius: 1rem;
87
+ background: var(--mv-border, var(--mist-border, #9ca9a1));
88
+ cursor: pointer;
89
+ }
90
+ .mv-panel .mv-switch input::before {
91
+ display: block;
92
+ width: 1.1rem;
93
+ height: 1.1rem;
94
+ margin: .13rem;
95
+ border-radius: 50%;
96
+ background: var(--mv-bg, var(--mist-bg, #fff));
97
+ content: '';
98
+ transition: transform .15s ease;
99
+ }
100
+ .mv-panel .mv-switch input:checked { border-color: var(--mv-accent, var(--mist-accent, #315fbd)); background: var(--mv-accent, var(--mist-accent, #315fbd)); }
101
+ .mv-panel .mv-switch input:checked::before { transform: translateX(1.1rem); }
102
+ .mv-panel .mv-room { padding-bottom: 1.1rem; }
103
+ .mv-panel .mv-room-row { display: flex; align-items: end; gap: .5rem; }
104
+ .mv-panel .mv-room-row label { flex: 1 1 auto; min-width: 0; }
105
+ .mv-panel .mv-room-row input { margin-top: .4rem; }
106
+ .mv-panel .mv-room-row button { flex: 0 0 auto; }
107
+ .mv-panel .mv-hint { margin-top: .4rem; }
108
+ .mv-panel .mv-disclosure { min-width: 0; border-top: 1px solid var(--mv-border, var(--mist-border, rgba(127, 127, 127, .35))); }
109
+ .mv-panel .mv-disclosure:last-of-type { border-bottom: 1px solid var(--mv-border, var(--mist-border, rgba(127, 127, 127, .35))); }
110
+ .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; }
111
+ .mv-panel .mv-disclosure summary::-webkit-details-marker { display: none; }
112
+ .mv-panel .mv-disclosure summary::after { color: var(--mv-muted, var(--mist-muted, #6d7772)); content: '+'; font-size: 1.3em; font-weight: 400; line-height: 1; }
113
+ .mv-panel .mv-disclosure[open] summary::after { content: '−'; }
114
+ .mv-panel .mv-disclosure-body { display: grid; gap: .65rem; padding: 0 0 1rem; }
115
+ .mv-panel .mv-disclosure-body label, .mv-panel .mv-import label { display: grid; gap: .4rem; }
116
+ .mv-panel .mv-disclosure-body > button { justify-self: start; }
117
+ .mv-panel .mv-import { display: grid; gap: .55rem; margin-top: .8rem; padding-top: .9rem; border-top: 1px solid var(--mv-border, var(--mist-border, rgba(127, 127, 127, .35))); }
118
+ .mv-panel .mv-confirm { margin-bottom: 1.25rem; }
119
+ .mv-panel .mv-confirm p { margin: .55rem 0 .9rem; }
120
+ .mv-panel .mv-parts, .mv-panel .mv-notice { margin-top: .9rem; }
121
+ .mv-panel .mv-error { color: var(--mv-error, var(--mist-error, #b33b3b)); font-size: .9em; }
122
+ @media (prefers-color-scheme: dark) {
123
+ .mv-panel { color-scheme: dark; }
124
+ .mv-panel .mv-muted, .mv-panel input::placeholder, .mv-panel .mv-disclosure summary::after { color: var(--mv-muted, var(--mist-muted, #afbdb5)); }
125
+ .mv-panel .mv-error { color: var(--mv-error, var(--mist-error, #ff9999)); }
126
+ .mv-panel .mv-status-text[data-state=error] strong::before { background: var(--mv-error, var(--mist-error, #ff9999)); }
127
+ }
128
+ @media (prefers-reduced-motion: reduce) { .mv-panel .mv-switch input::before { transition: none; } }
129
+ @media (forced-colors: active) {
130
+ .mv-panel .mv-switch input { appearance: auto; }
131
+ .mv-panel .mv-switch input::before { content: none; }
132
+ }
@@ -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;
@@ -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>;