selfstore 1.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.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +364 -0
  3. package/dist/advanced.d.ts +90 -0
  4. package/dist/advanced.js +41 -0
  5. package/dist/advanced.js.map +1 -0
  6. package/dist/backups.d.ts +182 -0
  7. package/dist/backups.js +360 -0
  8. package/dist/backups.js.map +1 -0
  9. package/dist/cache-De5_sALA.d.ts +129 -0
  10. package/dist/cache-lock-LZILZNLP.js +23 -0
  11. package/dist/cache-lock-LZILZNLP.js.map +1 -0
  12. package/dist/chunk-ATUPHVLG.js +441 -0
  13. package/dist/chunk-ATUPHVLG.js.map +1 -0
  14. package/dist/chunk-BJ3RUMGI.js +45 -0
  15. package/dist/chunk-BJ3RUMGI.js.map +1 -0
  16. package/dist/chunk-CUWB5EWF.js +732 -0
  17. package/dist/chunk-CUWB5EWF.js.map +1 -0
  18. package/dist/chunk-FDPAKHQS.js +230 -0
  19. package/dist/chunk-FDPAKHQS.js.map +1 -0
  20. package/dist/chunk-IBIEKK36.js +331 -0
  21. package/dist/chunk-IBIEKK36.js.map +1 -0
  22. package/dist/chunk-JB4JHM7V.js +474 -0
  23. package/dist/chunk-JB4JHM7V.js.map +1 -0
  24. package/dist/chunk-N4RPMX3Y.js +1908 -0
  25. package/dist/chunk-N4RPMX3Y.js.map +1 -0
  26. package/dist/chunk-OWLX6PNT.js +231 -0
  27. package/dist/chunk-OWLX6PNT.js.map +1 -0
  28. package/dist/chunk-QBCU7OOP.js +20 -0
  29. package/dist/chunk-QBCU7OOP.js.map +1 -0
  30. package/dist/chunk-SGISYMT5.js +46 -0
  31. package/dist/chunk-SGISYMT5.js.map +1 -0
  32. package/dist/chunk-VBFMMR7Q.js +29 -0
  33. package/dist/chunk-VBFMMR7Q.js.map +1 -0
  34. package/dist/drive-Bv94UM8b.d.ts +222 -0
  35. package/dist/flows.d.ts +306 -0
  36. package/dist/flows.js +153 -0
  37. package/dist/flows.js.map +1 -0
  38. package/dist/groups.d.ts +39 -0
  39. package/dist/groups.js +103 -0
  40. package/dist/groups.js.map +1 -0
  41. package/dist/households.d.ts +214 -0
  42. package/dist/households.js +410 -0
  43. package/dist/households.js.map +1 -0
  44. package/dist/index.d.ts +205 -0
  45. package/dist/index.js +249 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/kdf-worker.js +2395 -0
  48. package/dist/kdf-worker.js.map +1 -0
  49. package/dist/local-2py_vGGy.d.ts +47 -0
  50. package/dist/local-AM22RWRR.js +7 -0
  51. package/dist/local-AM22RWRR.js.map +1 -0
  52. package/dist/machine-ALMkT3mk.d.ts +17 -0
  53. package/dist/store-ChIGEOph.d.ts +577 -0
  54. package/dist/sync.d.ts +107 -0
  55. package/dist/sync.js +3 -0
  56. package/dist/sync.js.map +1 -0
  57. package/dist/widgets.d.ts +426 -0
  58. package/dist/widgets.js +2579 -0
  59. package/dist/widgets.js.map +1 -0
  60. package/llms.txt +405 -0
  61. package/package.json +99 -0
@@ -0,0 +1,182 @@
1
+ import { K as KV } from './cache-De5_sALA.js';
2
+ import { B as BackupTarget, L as LocalStore } from './store-ChIGEOph.js';
3
+ import { F as FlowStore } from './machine-ALMkT3mk.js';
4
+ import './sync.js';
5
+
6
+ /** What the destination reports about a listed file (one cheap metadata call). */
7
+ interface BackupFileInfo {
8
+ id: string;
9
+ name: string;
10
+ /** RFC 3339 last-write timestamp, when the destination reports one. */
11
+ modifiedTime: string | null;
12
+ }
13
+ /** The destination I/O the manager drives - the host wires its own adapter
14
+ * (e.g. over the Drive target ops). Every method may throw; the manager turns
15
+ * failures into stable error codes. */
16
+ interface BackupsHost {
17
+ /** The target kind the managed files live on (e.g. 'drive'): the manager
18
+ * only stamps an active file while the store is attached to this kind. */
19
+ kind: string;
20
+ /** The kv key the attached file id is persisted under (the same one the
21
+ * target's connect path reads first). */
22
+ activeIdKey: string;
23
+ /** List candidate backup files on the account. The manager applies the
24
+ * naming rules on what comes back; internal files simply do not match. */
25
+ list(): Promise<BackupFileInfo[]>;
26
+ /** A target over a FIXED file id - never re-resolved by name. The fixed id
27
+ * is what keeps two backups from silently merging mid-switch. */
28
+ open(fileId: string): BackupTarget;
29
+ /** Create an empty file under this name and return its id. Refuse a name
30
+ * the account already carries (a duplicate poisons find-by-name). */
31
+ create(fileName: string): Promise<{
32
+ fileId: string;
33
+ }>;
34
+ /** Delete a file for good. */
35
+ remove(fileId: string): Promise<void>;
36
+ /** Rename a file in place (metadata only: the id must not change). */
37
+ rename?(fileId: string, fileName: string): Promise<void>;
38
+ /** Find (or create) the PERSONAL file by its canonical name, ignoring any
39
+ * currently adopted id. Null when the user backs out of the session. */
40
+ findOrCreatePersonal(): Promise<{
41
+ fileId: string;
42
+ created: boolean;
43
+ } | null>;
44
+ /** Make sure a usable session exists before a remote call (re-consent when
45
+ * it lapsed). False = the user refused; the gesture aborts as 'cancelled'.
46
+ * Absent = sessions are the target's own business. */
47
+ ensureSession?(): Promise<boolean>;
48
+ /** Resolve the owner label of a file this account does not own, for the
49
+ * shared registry ("shared by X"). Best-effort. */
50
+ fileOwner?(fileId: string): Promise<{
51
+ email: string | null;
52
+ name: string | null;
53
+ } | null>;
54
+ /** File ids that must never enter the registry even though the store may
55
+ * attach them (e.g. a sharing engine's published copy file). */
56
+ excludedFileIds?(): Promise<(string | null | undefined)[]>;
57
+ }
58
+ /** How backup files are named on the destination. */
59
+ interface BackupsNaming {
60
+ /** The personal, unnamed file ("App.zip"). Its label reads as null. */
61
+ canonicalName: string;
62
+ /** label -> file name. Default: "Stem (label).ext" derived from
63
+ * canonicalName ("App.zip" + "family" -> "App (family).zip"). */
64
+ namedFileFor?(label: string): string;
65
+ /** file name -> parsed label; null = the personal file; undefined = not one
66
+ * of ours (dropped from listings). Default matches the default namedFileFor. */
67
+ parseLabel?(name: string): string | null | undefined;
68
+ }
69
+ /** The kv keys the manager persists under - override to keep a pre-existing
70
+ * install's records. */
71
+ interface BackupsKeys {
72
+ /** The known-backups registry. */
73
+ registry?: string;
74
+ /** fileId -> encrypted? map. */
75
+ encrypted?: string;
76
+ /** fileId -> shared? map. */
77
+ shared?: string;
78
+ /** Set while the ACTIVE file is not the personal one (another account's). */
79
+ joined?: string;
80
+ /** Cached owner label of that file. */
81
+ owner?: string;
82
+ }
83
+ /** Every backup this device has connected to: its own personal file and each
84
+ * OTHER account's file it was let into. Isolated snapshots, all of them. */
85
+ interface KnownBackups {
86
+ personalFileId: string | null;
87
+ shared: {
88
+ fileId: string;
89
+ ownerEmail: string | null;
90
+ ownerName: string | null;
91
+ }[];
92
+ }
93
+ /** One row of the "my backups" list: a file this account owns. */
94
+ interface BackupRow {
95
+ fileId: string;
96
+ name: string;
97
+ /** Parsed display label; null for the personal, unnamed file. */
98
+ label: string | null;
99
+ /** Last-write time, epoch ms (null when the destination did not say). */
100
+ modifiedAt: number | null;
101
+ /** true/false once known (opened, created or probed); null until then. */
102
+ encrypted: boolean | null;
103
+ /** true/false once learned (while this file was the active one); null
104
+ * until then. A share link existing is the shared state. */
105
+ shared: boolean | null;
106
+ }
107
+ /** The owner label of a joined (another account's) file. */
108
+ interface BackupOwner {
109
+ email: string | null;
110
+ name: string | null;
111
+ }
112
+ /** Stable failure codes; the host maps them to its own copy. */
113
+ type BackupsErrorCode = 'cancelled' | 'gone' | 'failed';
114
+ interface BackupsSnapshot {
115
+ registry: KnownBackups;
116
+ /** The file currently attached, when the store is on the managed kind. */
117
+ activeFileId: string | null;
118
+ /** The active file is not the personal one (someone else's). */
119
+ joined: boolean;
120
+ /** Who owns the joined file, when known. */
121
+ owner: BackupOwner | null;
122
+ /** The last gesture's failure, or null. Reset at each gesture start. */
123
+ lastError: BackupsErrorCode | null;
124
+ }
125
+ interface BackupsManager extends FlowStore<BackupsSnapshot> {
126
+ /** Boot: load registry + joined/owner from kv and stamp the active file.
127
+ * Kv-only, no destination call. */
128
+ hydrate(): Promise<void>;
129
+ /** Re-read the registry and restamp the active file. Kv-only, cheap. */
130
+ refresh(): Promise<void>;
131
+ /** After an attach the manager did not make (a connect widget, a join):
132
+ * record the now-active file in the registry and restamp joined/owner. */
133
+ markActive(fileId: string): Promise<void>;
134
+ /** Live list of this account's own files, per the naming rules, decorated
135
+ * with the learned memories. One destination listing call. */
136
+ list(): Promise<BackupRow[]>;
137
+ /** Learn a listed file's encrypted bit without opening it for real: one
138
+ * download + header read, remembered for good. Null when unreachable. */
139
+ probeEncryption(fileId: string): Promise<boolean | null>;
140
+ /** Record the ACTIVE file's live share state, keyed by file id, so its row
141
+ * stays truthful after a switch away. */
142
+ noteShared(fileId: string, shared: boolean): Promise<void>;
143
+ /** Open a known backup: an isolated snapshot replaces the local data.
144
+ * 'encrypted' = collect its passphrase and call again with it. A file the
145
+ * destination reports GONE is pruned from the registry (lastError 'gone'). */
146
+ openBackup(fileId: string, passphrase?: string | null): Promise<'ok' | 'encrypted' | 'failed'>;
147
+ /** Open (or create blank) the personal file. */
148
+ openPersonal(passphrase?: string | null): Promise<'ok' | 'encrypted' | 'failed'>;
149
+ /** Create a brand-new empty named backup and open it (starts blank - never
150
+ * seeded from the currently loaded file). */
151
+ createNamed(label: string): Promise<'ok' | 'failed'>;
152
+ /** Record a file in the SHARED registry under the sharer's label, without
153
+ * opening it. For a joined portfolio's dedicated wallet: the file may live
154
+ * on this very account, yet it presents as "shared by X", never as mine. */
155
+ registerShared(fileId: string, owner: BackupOwner): Promise<void>;
156
+ /** Create a joined portfolio's dedicated wallet: a file under the given
157
+ * name (outside the own-files naming on purpose), attached BLANK - never
158
+ * seeded from the previously loaded silo - and recorded in the shared
159
+ * registry under the sharer's label. The host stages the shared content
160
+ * afterwards (restore the projection, then save). */
161
+ createShared(fileName: string, owner: BackupOwner): Promise<'ok' | 'failed'>;
162
+ /** Rename a file in place (id unchanged, so renaming the ACTIVE file never
163
+ * detaches it). The host target refuses a name already carried. */
164
+ renameBackup(fileId: string, label: string): Promise<'ok' | 'failed'>;
165
+ /** Delete a file for good. Deleting the ACTIVE one detaches first (the app
166
+ * goes device-only, local data stays), so nothing writes mid-delete. */
167
+ deleteBackup(fileId: string): Promise<boolean>;
168
+ /** Drop a KNOWN shared (another account's) file from this device's
169
+ * registry. Forgetting only - the owner's file is never touched. */
170
+ forgetShared(fileId: string): Promise<void>;
171
+ /** The file name a label produces (the create form's preview line). */
172
+ fileNameFor(label: string): string;
173
+ }
174
+ declare function createBackupsManager(deps: {
175
+ store: LocalStore;
176
+ kv: KV;
177
+ host: BackupsHost;
178
+ naming: BackupsNaming;
179
+ keys?: BackupsKeys;
180
+ }): BackupsManager;
181
+
182
+ export { type BackupFileInfo, type BackupOwner, type BackupRow, type BackupsErrorCode, type BackupsHost, type BackupsKeys, type BackupsManager, type BackupsNaming, type BackupsSnapshot, type KnownBackups, createBackupsManager };
@@ -0,0 +1,360 @@
1
+ import { makeMachine } from './chunk-SGISYMT5.js';
2
+ import './chunk-VBFMMR7Q.js';
3
+
4
+ // src/backups/manager.ts
5
+ var DEFAULT_KEYS = {
6
+ registry: "selfstore:backups:registry:v1",
7
+ encrypted: "selfstore:backups:encrypted:v1",
8
+ shared: "selfstore:backups:shared:v1",
9
+ joined: "selfstore:backups:joined:v1",
10
+ owner: "selfstore:backups:owner:v1"
11
+ };
12
+ var escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
13
+ function defaultNaming(naming) {
14
+ const dot = naming.canonicalName.lastIndexOf(".");
15
+ const stem = dot > 0 ? naming.canonicalName.slice(0, dot) : naming.canonicalName;
16
+ const ext = dot > 0 ? naming.canonicalName.slice(dot) : "";
17
+ const named = new RegExp(`^${escapeRe(stem)} \\((.+)\\)${escapeRe(ext)}$`);
18
+ return {
19
+ namedFileFor: (label) => `${stem} (${label.trim()})${ext}`,
20
+ parseLabel: (name) => {
21
+ if (name === naming.canonicalName) return null;
22
+ const m = named.exec(name);
23
+ return m ? m[1] : void 0;
24
+ }
25
+ };
26
+ }
27
+ function createBackupsManager(deps) {
28
+ const { store, kv, host } = deps;
29
+ const KEYS = { ...DEFAULT_KEYS, ...deps.keys };
30
+ const namingDefaults = defaultNaming(deps.naming);
31
+ const naming = {
32
+ namedFileFor: deps.naming.namedFileFor ?? namingDefaults.namedFileFor,
33
+ parseLabel: deps.naming.parseLabel ?? namingDefaults.parseLabel
34
+ };
35
+ const m = makeMachine({
36
+ registry: { personalFileId: null, shared: [] },
37
+ activeFileId: null,
38
+ joined: false,
39
+ owner: null,
40
+ lastError: null
41
+ });
42
+ const fail = (code) => m.set({ lastError: code });
43
+ let ownIds = null;
44
+ let ownCanonicalId = null;
45
+ function learnOwn(files) {
46
+ const ids = /* @__PURE__ */ new Set();
47
+ let canonical = null;
48
+ for (const f of files) {
49
+ const label = naming.parseLabel(f.name);
50
+ if (label === void 0) continue;
51
+ ids.add(f.id);
52
+ if (label === null) canonical = f.id;
53
+ }
54
+ ownIds = ids;
55
+ ownCanonicalId = canonical;
56
+ }
57
+ async function ensureOwnKnown() {
58
+ if (ownIds !== null) return;
59
+ try {
60
+ learnOwn(await host.list());
61
+ } catch {
62
+ }
63
+ }
64
+ async function loadRegistry() {
65
+ const reg = await kv.get(KEYS.registry) ?? { personalFileId: null, shared: [] };
66
+ const excluded = await host.excludedFileIds?.() ?? [];
67
+ const bad = new Set(excluded.filter((x) => typeof x === "string"));
68
+ if (bad.size === 0) return reg;
69
+ const healed = {
70
+ personalFileId: reg.personalFileId && bad.has(reg.personalFileId) ? null : reg.personalFileId,
71
+ shared: reg.shared.filter((s) => !bad.has(s.fileId))
72
+ };
73
+ if (healed.personalFileId !== reg.personalFileId || healed.shared.length !== reg.shared.length) {
74
+ await kv.set(KEYS.registry, healed);
75
+ }
76
+ return healed;
77
+ }
78
+ async function saveRegistry(next) {
79
+ m.set({ registry: next });
80
+ await kv.set(KEYS.registry, next);
81
+ }
82
+ async function noteEncrypted(fileId, encrypted) {
83
+ const map = await kv.get(KEYS.encrypted) ?? {};
84
+ if (map[fileId] === encrypted) return;
85
+ map[fileId] = encrypted;
86
+ await kv.set(KEYS.encrypted, map);
87
+ }
88
+ async function registerActive() {
89
+ if (store.state.targetKind !== host.kind) {
90
+ m.set({ activeFileId: null });
91
+ return;
92
+ }
93
+ const fileId = await kv.get(host.activeIdKey) ?? null;
94
+ m.set({ activeFileId: fileId });
95
+ if (!fileId) return;
96
+ const excluded = await host.excludedFileIds?.() ?? [];
97
+ if (excluded.includes(fileId)) return;
98
+ const reg = await loadRegistry();
99
+ if (m.snapshot.joined) {
100
+ const owner = m.snapshot.owner;
101
+ const entry = { fileId, ownerEmail: owner?.email ?? null, ownerName: owner?.name ?? null };
102
+ await saveRegistry({
103
+ personalFileId: reg.personalFileId === fileId ? null : reg.personalFileId,
104
+ shared: [entry, ...reg.shared.filter((s) => s.fileId !== fileId)]
105
+ });
106
+ return;
107
+ }
108
+ const canonical = fileId === ownCanonicalId || reg.personalFileId === fileId || ownIds === null && reg.personalFileId === null;
109
+ if (canonical) {
110
+ await saveRegistry({
111
+ personalFileId: fileId,
112
+ shared: reg.shared.filter((s) => s.fileId !== fileId)
113
+ });
114
+ } else if (reg.shared.some((s) => s.fileId === fileId)) {
115
+ await saveRegistry({
116
+ personalFileId: reg.personalFileId,
117
+ shared: reg.shared.filter((s) => s.fileId !== fileId)
118
+ });
119
+ }
120
+ }
121
+ async function ensureSession() {
122
+ if (!host.ensureSession) return true;
123
+ return host.ensureSession();
124
+ }
125
+ async function markActive(fileId) {
126
+ const reg = await loadRegistry();
127
+ const knownShared = reg.shared.find((s) => s.fileId === fileId);
128
+ let mine = reg.personalFileId === fileId;
129
+ if (!mine && !knownShared) {
130
+ await ensureOwnKnown();
131
+ mine = ownIds === null ? reg.personalFileId === null : ownIds.has(fileId);
132
+ }
133
+ if (mine || !knownShared && ownIds?.has(fileId)) {
134
+ await kv.del(KEYS.joined);
135
+ await kv.del(KEYS.owner);
136
+ m.set({ joined: false, owner: null });
137
+ } else {
138
+ const owner = knownShared ? { email: knownShared.ownerEmail, name: knownShared.ownerName } : await host.fileOwner?.(fileId).catch(() => null) ?? null;
139
+ m.set({ joined: true, owner });
140
+ await kv.set(KEYS.joined, true);
141
+ if (owner) await kv.set(KEYS.owner, owner);
142
+ else await kv.del(KEYS.owner);
143
+ }
144
+ await registerActive();
145
+ }
146
+ async function attachFixed(fileId, passphrase, strategy) {
147
+ const target = host.open(fileId);
148
+ await store.attachTarget(target, {
149
+ password: passphrase,
150
+ strategy,
151
+ keepSession: store.state.targetKind === host.kind,
152
+ wipe: true
153
+ });
154
+ await kv.set(host.activeIdKey, fileId);
155
+ }
156
+ return {
157
+ get snapshot() {
158
+ return m.snapshot;
159
+ },
160
+ subscribe: m.subscribe,
161
+ fileNameFor(label) {
162
+ return naming.namedFileFor(label.trim());
163
+ },
164
+ async hydrate() {
165
+ const joined = await kv.get(KEYS.joined) === true;
166
+ const owner = await kv.get(KEYS.owner) ?? null;
167
+ m.set({ joined, owner, registry: await loadRegistry() });
168
+ await registerActive();
169
+ },
170
+ async refresh() {
171
+ m.set({ registry: await loadRegistry() });
172
+ await registerActive();
173
+ },
174
+ markActive,
175
+ async list() {
176
+ const files = await host.list();
177
+ learnOwn(files);
178
+ const enc = await kv.get(KEYS.encrypted) ?? {};
179
+ const shr = await kv.get(KEYS.shared) ?? {};
180
+ const rows = [];
181
+ for (const f of files) {
182
+ const label = naming.parseLabel(f.name);
183
+ if (label === void 0) continue;
184
+ rows.push({
185
+ fileId: f.id,
186
+ name: f.name,
187
+ label,
188
+ modifiedAt: f.modifiedTime ? Date.parse(f.modifiedTime) || null : null,
189
+ encrypted: enc[f.id] ?? null,
190
+ shared: shr[f.id] ?? null
191
+ });
192
+ }
193
+ return rows;
194
+ },
195
+ async probeEncryption(fileId) {
196
+ try {
197
+ const info = await store.inspectTarget(host.open(fileId));
198
+ if (!info.hasBackup) return null;
199
+ await noteEncrypted(fileId, info.encrypted);
200
+ return info.encrypted;
201
+ } catch {
202
+ return null;
203
+ }
204
+ },
205
+ async noteShared(fileId, shared) {
206
+ const map = await kv.get(KEYS.shared) ?? {};
207
+ if (map[fileId] === shared) return;
208
+ map[fileId] = shared;
209
+ await kv.set(KEYS.shared, map);
210
+ },
211
+ async openBackup(fileId, passphrase = null) {
212
+ m.set({ lastError: null });
213
+ try {
214
+ if (!await ensureSession()) {
215
+ fail("cancelled");
216
+ return "failed";
217
+ }
218
+ const info = await store.inspectTarget(host.open(fileId));
219
+ if (!info.hasBackup) {
220
+ const reg = await loadRegistry();
221
+ await saveRegistry({
222
+ personalFileId: reg.personalFileId === fileId ? null : reg.personalFileId,
223
+ shared: reg.shared.filter((s) => s.fileId !== fileId)
224
+ });
225
+ fail("gone");
226
+ return "failed";
227
+ }
228
+ await noteEncrypted(fileId, info.encrypted);
229
+ if (info.encrypted && !passphrase) return "encrypted";
230
+ await attachFixed(fileId, passphrase, "replace-local");
231
+ await markActive(fileId);
232
+ return store.state.locked ? "encrypted" : "ok";
233
+ } catch {
234
+ fail("failed");
235
+ return "failed";
236
+ }
237
+ },
238
+ async openPersonal(passphrase = null) {
239
+ m.set({ lastError: null });
240
+ try {
241
+ const own = await host.findOrCreatePersonal();
242
+ if (!own) {
243
+ fail("cancelled");
244
+ return "failed";
245
+ }
246
+ ownCanonicalId = own.fileId;
247
+ ownIds?.add(own.fileId);
248
+ const reg = await loadRegistry();
249
+ await saveRegistry({
250
+ personalFileId: own.fileId,
251
+ shared: reg.shared.filter((s) => s.fileId !== own.fileId)
252
+ });
253
+ if (!own.created) return await this.openBackup(own.fileId, passphrase);
254
+ await attachFixed(own.fileId, null, "replace-remote");
255
+ await markActive(own.fileId);
256
+ return "ok";
257
+ } catch {
258
+ fail("failed");
259
+ return "failed";
260
+ }
261
+ },
262
+ async createNamed(label) {
263
+ m.set({ lastError: null });
264
+ const clean = label.trim();
265
+ if (!clean) {
266
+ fail("failed");
267
+ return "failed";
268
+ }
269
+ try {
270
+ if (!await ensureSession()) {
271
+ fail("cancelled");
272
+ return "failed";
273
+ }
274
+ const { fileId } = await host.create(naming.namedFileFor(clean));
275
+ ownIds?.add(fileId);
276
+ await attachFixed(fileId, null, "replace-remote");
277
+ await noteEncrypted(fileId, false);
278
+ await markActive(fileId);
279
+ return "ok";
280
+ } catch {
281
+ fail("failed");
282
+ return "failed";
283
+ }
284
+ },
285
+ async registerShared(fileId, owner) {
286
+ const reg = await loadRegistry();
287
+ await saveRegistry({
288
+ personalFileId: reg.personalFileId === fileId ? null : reg.personalFileId,
289
+ shared: [
290
+ { fileId, ownerEmail: owner.email, ownerName: owner.name },
291
+ ...reg.shared.filter((s) => s.fileId !== fileId)
292
+ ]
293
+ });
294
+ },
295
+ async createShared(fileName, owner) {
296
+ m.set({ lastError: null });
297
+ try {
298
+ if (!await ensureSession()) {
299
+ fail("cancelled");
300
+ return "failed";
301
+ }
302
+ const { fileId } = await host.create(fileName);
303
+ await this.registerShared(fileId, owner);
304
+ await attachFixed(fileId, null, "replace-remote");
305
+ await noteEncrypted(fileId, false);
306
+ await markActive(fileId);
307
+ return "ok";
308
+ } catch {
309
+ fail("failed");
310
+ return "failed";
311
+ }
312
+ },
313
+ async renameBackup(fileId, label) {
314
+ m.set({ lastError: null });
315
+ const clean = label.trim();
316
+ if (!clean || !host.rename) {
317
+ fail("failed");
318
+ return "failed";
319
+ }
320
+ try {
321
+ await host.rename(fileId, naming.namedFileFor(clean));
322
+ return "ok";
323
+ } catch {
324
+ fail("failed");
325
+ return "failed";
326
+ }
327
+ },
328
+ async deleteBackup(fileId) {
329
+ m.set({ lastError: null });
330
+ try {
331
+ if (fileId === m.snapshot.activeFileId) {
332
+ m.set({ activeFileId: null });
333
+ await store.detachTarget({ keepSession: true });
334
+ await kv.del(host.activeIdKey);
335
+ }
336
+ await host.remove(fileId);
337
+ const reg = await loadRegistry();
338
+ await saveRegistry({
339
+ personalFileId: reg.personalFileId === fileId ? null : reg.personalFileId,
340
+ shared: reg.shared.filter((s) => s.fileId !== fileId)
341
+ });
342
+ return true;
343
+ } catch {
344
+ fail("failed");
345
+ return false;
346
+ }
347
+ },
348
+ async forgetShared(fileId) {
349
+ const reg = await loadRegistry();
350
+ await saveRegistry({
351
+ personalFileId: reg.personalFileId,
352
+ shared: reg.shared.filter((s) => s.fileId !== fileId)
353
+ });
354
+ }
355
+ };
356
+ }
357
+
358
+ export { createBackupsManager };
359
+ //# sourceMappingURL=backups.js.map
360
+ //# sourceMappingURL=backups.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/backups/manager.ts"],"names":[],"mappings":";;;;AAiMA,IAAM,YAAA,GAAsC;AAAA,EAC3C,QAAA,EAAU,+BAAA;AAAA,EACV,SAAA,EAAW,gCAAA;AAAA,EACX,MAAA,EAAQ,6BAAA;AAAA,EACR,MAAA,EAAQ,6BAAA;AAAA,EACR,KAAA,EAAO;AACR,CAAA;AAEA,IAAM,WAAW,CAAC,CAAA,KAAsB,CAAA,CAAE,OAAA,CAAQ,uBAAuB,MAAM,CAAA;AAI/E,SAAS,cAAc,MAAA,EAAqF;AAC3G,EAAA,MAAM,GAAA,GAAM,MAAA,CAAO,aAAA,CAAc,WAAA,CAAY,GAAG,CAAA;AAChD,EAAA,MAAM,IAAA,GAAO,MAAM,CAAA,GAAI,MAAA,CAAO,cAAc,KAAA,CAAM,CAAA,EAAG,GAAG,CAAA,GAAI,MAAA,CAAO,aAAA;AACnE,EAAA,MAAM,MAAM,GAAA,GAAM,CAAA,GAAI,OAAO,aAAA,CAAc,KAAA,CAAM,GAAG,CAAA,GAAI,EAAA;AACxD,EAAA,MAAM,KAAA,GAAQ,IAAI,MAAA,CAAO,CAAA,CAAA,EAAI,QAAA,CAAS,IAAI,CAAC,CAAA,WAAA,EAAc,QAAA,CAAS,GAAG,CAAC,CAAA,CAAA,CAAG,CAAA;AACzE,EAAA,OAAO;AAAA,IACN,YAAA,EAAc,CAAC,KAAA,KAAkB,CAAA,EAAG,IAAI,KAAK,KAAA,CAAM,IAAA,EAAM,CAAA,CAAA,EAAI,GAAG,CAAA,CAAA;AAAA,IAChE,UAAA,EAAY,CAAC,IAAA,KA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,GAAM,MAAM,YAAA,EAAa;AAC/B,QAAA,MAAM,YAAA,CAAa;AAAA,UAClB,cAAA,EAAgB,GAAA,CAAI,cAAA,KAAmB,MAAA,GAAS,OAAO,GAAA,CAAI,cAAA;AAAA,UAC3D,MAAA,EAAQ,IAAI,MAAA,CAAO,MAAA,CAAO,CAAC,CAAA,KAAM,CAAA,CAAE,WAAW,MAAM;AAAA,SACpD,CAAA;AACD,QAAA,OAAO,IAAA;AAAA,MACR,CAAA,CAAA,MAAQ;AACP,QAAA,IAAA,CAAK,QAAQ,CAAA;AACb,QAAA,OAAO,KAAA;AAAA,MACR;AAAA,IACD,CAAA;AAAA,IAEA,MAAM,aAAa,MAAA,EAAuB;AACzC,MAAA,MAAM,GAAA,GAAM,MAAM,YAAA,EAAa;AAC/B,MAAA,MAAM,YAAA,CAAa;AAAA,QAClB,gBAAgB,GAAA,CAAI,cAAA;AAAA,QACpB,MAAA,EAAQ,IAAI,MAAA,CAAO,MAAA,CAAO,CAAC,CAAA,KAAM,CAAA,CAAE,WAAW,MAAM;AAAA,OACpD,CAAA;AAAA,IACF;AAAA,GACD;AACD","file":"backups.js","sourcesContent":["// Multi-backup manager: several backup files on one destination account, one\n// attached at a time, each an isolated snapshot - opening one replaces the\n// local data, never merges, never touches the other files.\n//\n// It owns the bookkeeping a backup panel needs: the self-healing registry of\n// files this device has connected to; the per-file memories a listing cannot\n// provide (encrypted? shared? - those bits live inside the files, learned as\n// they are opened or probed and remembered in the kv); and the open / create\n// / rename / delete gestures with their isolation invariants (fixed-id\n// targets, active-id committed only after a successful attach, wipe on every\n// switch so no sync-meta crosses files).\n//\n// Injectable and headless: a LocalStore + a kv + a BackupsHost port for the\n// destination I/O. Errors surface as stable codes, never prose; hosts wrap\n// subscribe in their own reactivity.\n\nimport type { KV } from '../persistence/cache';\nimport type { LocalStore } from '../persistence/store';\nimport type { BackupTarget } from '../persistence/target';\nimport { makeMachine, type FlowStore } from '../flows/machine';\n\n// --- Ports --------------------------------------------------------------------\n\n/** What the destination reports about a listed file (one cheap metadata call). */\nexport interface BackupFileInfo {\n\tid: string;\n\tname: string;\n\t/** RFC 3339 last-write timestamp, when the destination reports one. */\n\tmodifiedTime: string | null;\n}\n\n/** The destination I/O the manager drives - the host wires its own adapter\n * (e.g. over the Drive target ops). Every method may throw; the manager turns\n * failures into stable error codes. */\nexport interface BackupsHost {\n\t/** The target kind the managed files live on (e.g. 'drive'): the manager\n\t * only stamps an active file while the store is attached to this kind. */\n\tkind: string;\n\t/** The kv key the attached file id is persisted under (the same one the\n\t * target's connect path reads first). */\n\tactiveIdKey: string;\n\t/** List candidate backup files on the account. The manager applies the\n\t * naming rules on what comes back; internal files simply do not match. */\n\tlist(): Promise<BackupFileInfo[]>;\n\t/** A target over a FIXED file id - never re-resolved by name. The fixed id\n\t * is what keeps two backups from silently merging mid-switch. */\n\topen(fileId: string): BackupTarget;\n\t/** Create an empty file under this name and return its id. Refuse a name\n\t * the account already carries (a duplicate poisons find-by-name). */\n\tcreate(fileName: string): Promise<{ fileId: string }>;\n\t/** Delete a file for good. */\n\tremove(fileId: string): Promise<void>;\n\t/** Rename a file in place (metadata only: the id must not change). */\n\trename?(fileId: string, fileName: string): Promise<void>;\n\t/** Find (or create) the PERSONAL file by its canonical name, ignoring any\n\t * currently adopted id. Null when the user backs out of the session. */\n\tfindOrCreatePersonal(): Promise<{ fileId: string; created: boolean } | null>;\n\t/** Make sure a usable session exists before a remote call (re-consent when\n\t * it lapsed). False = the user refused; the gesture aborts as 'cancelled'.\n\t * Absent = sessions are the target's own business. */\n\tensureSession?(): Promise<boolean>;\n\t/** Resolve the owner label of a file this account does not own, for the\n\t * shared registry (\"shared by X\"). Best-effort. */\n\tfileOwner?(fileId: string): Promise<{ email: string | null; name: string | null } | null>;\n\t/** File ids that must never enter the registry even though the store may\n\t * attach them (e.g. a sharing engine's published copy file). */\n\texcludedFileIds?(): Promise<(string | null | undefined)[]>;\n}\n\n/** How backup files are named on the destination. */\nexport interface BackupsNaming {\n\t/** The personal, unnamed file (\"App.zip\"). Its label reads as null. */\n\tcanonicalName: string;\n\t/** label -> file name. Default: \"Stem (label).ext\" derived from\n\t * canonicalName (\"App.zip\" + \"family\" -> \"App (family).zip\"). */\n\tnamedFileFor?(label: string): string;\n\t/** file name -> parsed label; null = the personal file; undefined = not one\n\t * of ours (dropped from listings). Default matches the default namedFileFor. */\n\tparseLabel?(name: string): string | null | undefined;\n}\n\n/** The kv keys the manager persists under - override to keep a pre-existing\n * install's records. */\nexport interface BackupsKeys {\n\t/** The known-backups registry. */\n\tregistry?: string;\n\t/** fileId -> encrypted? map. */\n\tencrypted?: string;\n\t/** fileId -> shared? map. */\n\tshared?: string;\n\t/** Set while the ACTIVE file is not the personal one (another account's). */\n\tjoined?: string;\n\t/** Cached owner label of that file. */\n\towner?: string;\n}\n\n// --- Shapes -------------------------------------------------------------------\n\n/** Every backup this device has connected to: its own personal file and each\n * OTHER account's file it was let into. Isolated snapshots, all of them. */\nexport interface KnownBackups {\n\tpersonalFileId: string | null;\n\tshared: { fileId: string; ownerEmail: string | null; ownerName: string | null }[];\n}\n\n/** One row of the \"my backups\" list: a file this account owns. */\nexport interface BackupRow {\n\tfileId: string;\n\tname: string;\n\t/** Parsed display label; null for the personal, unnamed file. */\n\tlabel: string | null;\n\t/** Last-write time, epoch ms (null when the destination did not say). */\n\tmodifiedAt: number | null;\n\t/** true/false once known (opened, created or probed); null until then. */\n\tencrypted: boolean | null;\n\t/** true/false once learned (while this file was the active one); null\n\t * until then. A share link existing is the shared state. */\n\tshared: boolean | null;\n}\n\n/** The owner label of a joined (another account's) file. */\nexport interface BackupOwner {\n\temail: string | null;\n\tname: string | null;\n}\n\n/** Stable failure codes; the host maps them to its own copy. */\nexport type BackupsErrorCode = 'cancelled' | 'gone' | 'failed';\n\nexport interface BackupsSnapshot {\n\tregistry: KnownBackups;\n\t/** The file currently attached, when the store is on the managed kind. */\n\tactiveFileId: string | null;\n\t/** The active file is not the personal one (someone else's). */\n\tjoined: boolean;\n\t/** Who owns the joined file, when known. */\n\towner: BackupOwner | null;\n\t/** The last gesture's failure, or null. Reset at each gesture start. */\n\tlastError: BackupsErrorCode | null;\n}\n\nexport interface BackupsManager extends FlowStore<BackupsSnapshot> {\n\t/** Boot: load registry + joined/owner from kv and stamp the active file.\n\t * Kv-only, no destination call. */\n\thydrate(): Promise<void>;\n\t/** Re-read the registry and restamp the active file. Kv-only, cheap. */\n\trefresh(): Promise<void>;\n\t/** After an attach the manager did not make (a connect widget, a join):\n\t * record the now-active file in the registry and restamp joined/owner. */\n\tmarkActive(fileId: string): Promise<void>;\n\t/** Live list of this account's own files, per the naming rules, decorated\n\t * with the learned memories. One destination listing call. */\n\tlist(): Promise<BackupRow[]>;\n\t/** Learn a listed file's encrypted bit without opening it for real: one\n\t * download + header read, remembered for good. Null when unreachable. */\n\tprobeEncryption(fileId: string): Promise<boolean | null>;\n\t/** Record the ACTIVE file's live share state, keyed by file id, so its row\n\t * stays truthful after a switch away. */\n\tnoteShared(fileId: string, shared: boolean): Promise<void>;\n\t/** Open a known backup: an isolated snapshot replaces the local data.\n\t * 'encrypted' = collect its passphrase and call again with it. A file the\n\t * destination reports GONE is pruned from the registry (lastError 'gone'). */\n\topenBackup(fileId: string, passphrase?: string | null): Promise<'ok' | 'encrypted' | 'failed'>;\n\t/** Open (or create blank) the personal file. */\n\topenPersonal(passphrase?: string | null): Promise<'ok' | 'encrypted' | 'failed'>;\n\t/** Create a brand-new empty named backup and open it (starts blank - never\n\t * seeded from the currently loaded file). */\n\tcreateNamed(label: string): Promise<'ok' | 'failed'>;\n\t/** Record a file in the SHARED registry under the sharer's label, without\n\t * opening it. For a joined portfolio's dedicated wallet: the file may live\n\t * on this very account, yet it presents as \"shared by X\", never as mine. */\n\tregisterShared(fileId: string, owner: BackupOwner): Promise<void>;\n\t/** Create a joined portfolio's dedicated wallet: a file under the given\n\t * name (outside the own-files naming on purpose), attached BLANK - never\n\t * seeded from the previously loaded silo - and recorded in the shared\n\t * registry under the sharer's label. The host stages the shared content\n\t * afterwards (restore the projection, then save). */\n\tcreateShared(fileName: string, owner: BackupOwner): Promise<'ok' | 'failed'>;\n\t/** Rename a file in place (id unchanged, so renaming the ACTIVE file never\n\t * detaches it). The host target refuses a name already carried. */\n\trenameBackup(fileId: string, label: string): Promise<'ok' | 'failed'>;\n\t/** Delete a file for good. Deleting the ACTIVE one detaches first (the app\n\t * goes device-only, local data stays), so nothing writes mid-delete. */\n\tdeleteBackup(fileId: string): Promise<boolean>;\n\t/** Drop a KNOWN shared (another account's) file from this device's\n\t * registry. Forgetting only - the owner's file is never touched. */\n\tforgetShared(fileId: string): Promise<void>;\n\t/** The file name a label produces (the create form's preview line). */\n\tfileNameFor(label: string): string;\n}\n\n// --- Defaults -----------------------------------------------------------------\n\nconst DEFAULT_KEYS: Required<BackupsKeys> = {\n\tregistry: 'selfstore:backups:registry:v1',\n\tencrypted: 'selfstore:backups:encrypted:v1',\n\tshared: 'selfstore:backups:shared:v1',\n\tjoined: 'selfstore:backups:joined:v1',\n\towner: 'selfstore:backups:owner:v1'\n};\n\nconst escapeRe = (s: string): string => s.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n/** The default naming scheme: \"App.zip\" -> personal; \"App (label).zip\" ->\n * named. Derived from the canonical name's stem and extension. */\nfunction defaultNaming(naming: BackupsNaming): Required<Pick<BackupsNaming, 'namedFileFor' | 'parseLabel'>> {\n\tconst dot = naming.canonicalName.lastIndexOf('.');\n\tconst stem = dot > 0 ? naming.canonicalName.slice(0, dot) : naming.canonicalName;\n\tconst ext = dot > 0 ? naming.canonicalName.slice(dot) : '';\n\tconst named = new RegExp(`^${escapeRe(stem)} \\\\((.+)\\\\)${escapeRe(ext)}$`);\n\treturn {\n\t\tnamedFileFor: (label: string) => `${stem} (${label.trim()})${ext}`,\n\t\tparseLabel: (name: string) => {\n\t\t\tif (name === naming.canonicalName) return null;\n\t\t\tconst m = named.exec(name);\n\t\t\treturn m ? m[1] : undefined;\n\t\t}\n\t};\n}\n\n// --- Factory ------------------------------------------------------------------\n\nexport function createBackupsManager(deps: {\n\tstore: LocalStore;\n\tkv: KV;\n\thost: BackupsHost;\n\tnaming: BackupsNaming;\n\tkeys?: BackupsKeys;\n}): BackupsManager {\n\tconst { store, kv, host } = deps;\n\tconst KEYS = { ...DEFAULT_KEYS, ...deps.keys };\n\tconst namingDefaults = defaultNaming(deps.naming);\n\tconst naming = {\n\t\tnamedFileFor: deps.naming.namedFileFor ?? namingDefaults.namedFileFor,\n\t\tparseLabel: deps.naming.parseLabel ?? namingDefaults.parseLabel\n\t};\n\n\tconst m = makeMachine<BackupsSnapshot>({\n\t\tregistry: { personalFileId: null, shared: [] },\n\t\tactiveFileId: null,\n\t\tjoined: false,\n\t\towner: null,\n\t\tlastError: null\n\t});\n\n\tconst fail = (code: BackupsErrorCode): void => m.set({ lastError: code });\n\n\t// Which file ids the destination lists as OURS, and which one carries the\n\t// canonical (personal) name. Filled by list(), refreshed lazily where a\n\t// truthful own/foreign call is needed. Null until first learned.\n\tlet ownIds: Set<string> | null = null;\n\tlet ownCanonicalId: string | null = null;\n\n\tfunction learnOwn(files: BackupFileInfo[]): void {\n\t\tconst ids = new Set<string>();\n\t\tlet canonical: string | null = null;\n\t\tfor (const f of files) {\n\t\t\tconst label = naming.parseLabel(f.name);\n\t\t\tif (label === undefined) continue;\n\t\t\tids.add(f.id);\n\t\t\tif (label === null) canonical = f.id;\n\t\t}\n\t\townIds = ids;\n\t\townCanonicalId = canonical;\n\t}\n\n\tasync function ensureOwnKnown(): Promise<void> {\n\t\tif (ownIds !== null) return;\n\t\ttry {\n\t\t\tlearnOwn(await host.list());\n\t\t} catch {\n\t\t\t// Offline: leave unknown - markActive falls back on the registry alone.\n\t\t}\n\t}\n\n\t/** Load the registry, self-healing: excluded ids (a sharing engine's copy\n\t * file) must never sit in it - recorded as \"personal\" they would mislabel\n\t * the chooser and let a later open treat a shared file as one's own. */\n\tasync function loadRegistry(): Promise<KnownBackups> {\n\t\tconst reg = (await kv.get<KnownBackups>(KEYS.registry)) ?? { personalFileId: null, shared: [] };\n\t\tconst excluded = (await host.excludedFileIds?.()) ?? [];\n\t\tconst bad = new Set(excluded.filter((x): x is string => typeof x === 'string'));\n\t\tif (bad.size === 0) return reg;\n\t\tconst healed: KnownBackups = {\n\t\t\tpersonalFileId: reg.personalFileId && bad.has(reg.personalFileId) ? null : reg.personalFileId,\n\t\t\tshared: reg.shared.filter((s) => !bad.has(s.fileId))\n\t\t};\n\t\tif (healed.personalFileId !== reg.personalFileId || healed.shared.length !== reg.shared.length) {\n\t\t\tawait kv.set(KEYS.registry, healed);\n\t\t}\n\t\treturn healed;\n\t}\n\n\tasync function saveRegistry(next: KnownBackups): Promise<void> {\n\t\tm.set({ registry: next });\n\t\tawait kv.set(KEYS.registry, next);\n\t}\n\n\tasync function noteEncrypted(fileId: string, encrypted: boolean): Promise<void> {\n\t\tconst map = (await kv.get<Record<string, boolean>>(KEYS.encrypted)) ?? {};\n\t\tif (map[fileId] === encrypted) return;\n\t\tmap[fileId] = encrypted;\n\t\tawait kv.set(KEYS.encrypted, map);\n\t}\n\n\t/** Stamp the currently attached file (from the target's own persisted id)\n\t * into the snapshot and the registry. Kv-only. */\n\tasync function registerActive(): Promise<void> {\n\t\tif (store.state.targetKind !== host.kind) {\n\t\t\tm.set({ activeFileId: null });\n\t\t\treturn;\n\t\t}\n\t\tconst fileId = (await kv.get<string>(host.activeIdKey)) ?? null;\n\t\tm.set({ activeFileId: fileId });\n\t\tif (!fileId) return;\n\t\tconst excluded = (await host.excludedFileIds?.()) ?? [];\n\t\tif (excluded.includes(fileId)) return;\n\t\tconst reg = await loadRegistry();\n\t\tif (m.snapshot.joined) {\n\t\t\tconst owner = m.snapshot.owner;\n\t\t\tconst entry = { fileId, ownerEmail: owner?.email ?? null, ownerName: owner?.name ?? null };\n\t\t\tawait saveRegistry({\n\t\t\t\tpersonalFileId: reg.personalFileId === fileId ? null : reg.personalFileId,\n\t\t\t\tshared: [entry, ...reg.shared.filter((s) => s.fileId !== fileId)]\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\t// Not joined. The personal slot belongs to the CANONICAL file alone: an\n\t\t// own NAMED backup lists live and must not clobber it (the slot is the\n\t\t// escape back to the personal file).\n\t\tconst canonical =\n\t\t\tfileId === ownCanonicalId || reg.personalFileId === fileId || (ownIds === null && reg.personalFileId === null);\n\t\tif (canonical) {\n\t\t\tawait saveRegistry({\n\t\t\t\tpersonalFileId: fileId,\n\t\t\t\tshared: reg.shared.filter((s) => s.fileId !== fileId)\n\t\t\t});\n\t\t} else if (reg.shared.some((s) => s.fileId === fileId)) {\n\t\t\tawait saveRegistry({\n\t\t\t\tpersonalFileId: reg.personalFileId,\n\t\t\t\tshared: reg.shared.filter((s) => s.fileId !== fileId)\n\t\t\t});\n\t\t}\n\t}\n\n\t/** Session guard shared by the remote gestures: absent hook = no guard. */\n\tasync function ensureSession(): Promise<boolean> {\n\t\tif (!host.ensureSession) return true;\n\t\treturn host.ensureSession();\n\t}\n\n\tasync function markActive(fileId: string): Promise<void> {\n\t\tconst reg = await loadRegistry();\n\t\tconst knownShared = reg.shared.find((s) => s.fileId === fileId);\n\t\tlet mine = reg.personalFileId === fileId;\n\t\tif (!mine && !knownShared) {\n\t\t\t// Unknown file: ask the destination whose it is (one listing call,\n\t\t\t// cached). An own named backup must never read as joined - labeled\n\t\t\t// \"shared by me\", it would sit in the wrong section for good.\n\t\t\tawait ensureOwnKnown();\n\t\t\tmine = ownIds === null ? reg.personalFileId === null : ownIds.has(fileId);\n\t\t}\n\t\tif (mine || (!knownShared && ownIds?.has(fileId))) {\n\t\t\tawait kv.del(KEYS.joined);\n\t\t\tawait kv.del(KEYS.owner);\n\t\t\tm.set({ joined: false, owner: null });\n\t\t} else {\n\t\t\tconst owner: BackupOwner | null = knownShared\n\t\t\t\t? { email: knownShared.ownerEmail, name: knownShared.ownerName }\n\t\t\t\t: ((await host.fileOwner?.(fileId).catch(() => null)) ?? null);\n\t\t\tm.set({ joined: true, owner });\n\t\t\tawait kv.set(KEYS.joined, true);\n\t\t\tif (owner) await kv.set(KEYS.owner, owner);\n\t\t\telse await kv.del(KEYS.owner);\n\t\t}\n\t\tawait registerActive();\n\t}\n\n\t/** The one attach path every open shares: FIXED-id target, and the\n\t * remembered id committed only after the attach succeeds - committed up\n\t * front, the OUTGOING target (which resolves its file lazily from that\n\t * same key) would flush the old file's data into the one being opened:\n\t * two \"isolated\" backups silently merged. `wipe` keeps sync-meta from\n\t * crossing files (cross-file tombstones erase the other side). */\n\tasync function attachFixed(\n\t\tfileId: string,\n\t\tpassphrase: string | null,\n\t\tstrategy: 'replace-local' | 'replace-remote'\n\t): Promise<void> {\n\t\tconst target = host.open(fileId);\n\t\tawait store.attachTarget(target, {\n\t\t\tpassword: passphrase,\n\t\t\tstrategy,\n\t\t\tkeepSession: store.state.targetKind === host.kind,\n\t\t\twipe: true\n\t\t});\n\t\tawait kv.set(host.activeIdKey, fileId);\n\t}\n\n\treturn {\n\t\tget snapshot() {\n\t\t\treturn m.snapshot;\n\t\t},\n\t\tsubscribe: m.subscribe,\n\n\t\tfileNameFor(label: string): string {\n\t\t\treturn naming.namedFileFor(label.trim());\n\t\t},\n\n\t\tasync hydrate(): Promise<void> {\n\t\t\tconst joined = (await kv.get(KEYS.joined)) === true;\n\t\t\tconst owner = (await kv.get<BackupOwner>(KEYS.owner)) ?? null;\n\t\t\tm.set({ joined, owner, registry: await loadRegistry() });\n\t\t\tawait registerActive();\n\t\t},\n\n\t\tasync refresh(): Promise<void> {\n\t\t\tm.set({ registry: await loadRegistry() });\n\t\t\tawait registerActive();\n\t\t},\n\n\t\tmarkActive,\n\n\t\tasync list(): Promise<BackupRow[]> {\n\t\t\tconst files = await host.list();\n\t\t\tlearnOwn(files);\n\t\t\tconst enc = (await kv.get<Record<string, boolean>>(KEYS.encrypted)) ?? {};\n\t\t\tconst shr = (await kv.get<Record<string, boolean>>(KEYS.shared)) ?? {};\n\t\t\tconst rows: BackupRow[] = [];\n\t\t\tfor (const f of files) {\n\t\t\t\tconst label = naming.parseLabel(f.name);\n\t\t\t\tif (label === undefined) continue; // not one of ours (a copy, a bulletin, ...)\n\t\t\t\trows.push({\n\t\t\t\t\tfileId: f.id,\n\t\t\t\t\tname: f.name,\n\t\t\t\t\tlabel,\n\t\t\t\t\tmodifiedAt: f.modifiedTime ? Date.parse(f.modifiedTime) || null : null,\n\t\t\t\t\tencrypted: enc[f.id] ?? null,\n\t\t\t\t\tshared: shr[f.id] ?? null\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn rows;\n\t\t},\n\n\t\tasync probeEncryption(fileId: string): Promise<boolean | null> {\n\t\t\ttry {\n\t\t\t\tconst info = await store.inspectTarget(host.open(fileId));\n\t\t\t\tif (!info.hasBackup) return null;\n\t\t\t\tawait noteEncrypted(fileId, info.encrypted);\n\t\t\t\treturn info.encrypted;\n\t\t\t} catch {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t},\n\n\t\tasync noteShared(fileId: string, shared: boolean): Promise<void> {\n\t\t\tconst map = (await kv.get<Record<string, boolean>>(KEYS.shared)) ?? {};\n\t\t\tif (map[fileId] === shared) return;\n\t\t\tmap[fileId] = shared;\n\t\t\tawait kv.set(KEYS.shared, map);\n\t\t},\n\n\t\tasync openBackup(fileId, passphrase = null): Promise<'ok' | 'encrypted' | 'failed'> {\n\t\t\tm.set({ lastError: null });\n\t\t\ttry {\n\t\t\t\tif (!(await ensureSession())) {\n\t\t\t\t\tfail('cancelled');\n\t\t\t\t\treturn 'failed';\n\t\t\t\t}\n\t\t\t\tconst info = await store.inspectTarget(host.open(fileId));\n\t\t\t\tif (!info.hasBackup) {\n\t\t\t\t\t// Dead entry (deleted, or unshared from this account): prune it so\n\t\t\t\t\t// its row stops re-failing forever. A transient failure throws\n\t\t\t\t\t// instead and never prunes - dropping a good record on a blip\n\t\t\t\t\t// would be far worse than one more failed tap.\n\t\t\t\t\tconst reg = await loadRegistry();\n\t\t\t\t\tawait saveRegistry({\n\t\t\t\t\t\tpersonalFileId: reg.personalFileId === fileId ? null : reg.personalFileId,\n\t\t\t\t\t\tshared: reg.shared.filter((s) => s.fileId !== fileId)\n\t\t\t\t\t});\n\t\t\t\t\tfail('gone');\n\t\t\t\t\treturn 'failed';\n\t\t\t\t}\n\t\t\t\tawait noteEncrypted(fileId, info.encrypted); // the listing cannot tell\n\t\t\t\tif (info.encrypted && !passphrase) return 'encrypted';\n\t\t\t\tawait attachFixed(fileId, passphrase, 'replace-local');\n\t\t\t\tawait markActive(fileId);\n\t\t\t\treturn store.state.locked ? 'encrypted' : 'ok';\n\t\t\t} catch {\n\t\t\t\tfail('failed');\n\t\t\t\treturn 'failed';\n\t\t\t}\n\t\t},\n\n\t\tasync openPersonal(passphrase = null): Promise<'ok' | 'encrypted' | 'failed'> {\n\t\t\tm.set({ lastError: null });\n\t\t\ttry {\n\t\t\t\tconst own = await host.findOrCreatePersonal();\n\t\t\t\tif (!own) {\n\t\t\t\t\tfail('cancelled');\n\t\t\t\t\treturn 'failed';\n\t\t\t\t}\n\t\t\t\townCanonicalId = own.fileId;\n\t\t\t\townIds?.add(own.fileId);\n\t\t\t\tconst reg = await loadRegistry();\n\t\t\t\tawait saveRegistry({\n\t\t\t\t\tpersonalFileId: own.fileId,\n\t\t\t\t\tshared: reg.shared.filter((s) => s.fileId !== own.fileId)\n\t\t\t\t});\n\t\t\t\tif (!own.created) return await this.openBackup(own.fileId, passphrase);\n\t\t\t\tawait attachFixed(own.fileId, null, 'replace-remote');\n\t\t\t\tawait markActive(own.fileId);\n\t\t\t\treturn 'ok';\n\t\t\t} catch {\n\t\t\t\tfail('failed');\n\t\t\t\treturn 'failed';\n\t\t\t}\n\t\t},\n\n\t\tasync createNamed(label): Promise<'ok' | 'failed'> {\n\t\t\tm.set({ lastError: null });\n\t\t\tconst clean = label.trim();\n\t\t\tif (!clean) {\n\t\t\t\tfail('failed');\n\t\t\t\treturn 'failed';\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tif (!(await ensureSession())) {\n\t\t\t\t\tfail('cancelled');\n\t\t\t\t\treturn 'failed';\n\t\t\t\t}\n\t\t\t\tconst { fileId } = await host.create(naming.namedFileFor(clean));\n\t\t\t\townIds?.add(fileId);\n\t\t\t\tawait attachFixed(fileId, null, 'replace-remote');\n\t\t\t\tawait noteEncrypted(fileId, false); // a fresh backup starts unprotected\n\t\t\t\tawait markActive(fileId);\n\t\t\t\treturn 'ok';\n\t\t\t} catch {\n\t\t\t\tfail('failed');\n\t\t\t\treturn 'failed';\n\t\t\t}\n\t\t},\n\n\t\tasync registerShared(fileId, owner): Promise<void> {\n\t\t\tconst reg = await loadRegistry();\n\t\t\tawait saveRegistry({\n\t\t\t\tpersonalFileId: reg.personalFileId === fileId ? null : reg.personalFileId,\n\t\t\t\tshared: [\n\t\t\t\t\t{ fileId, ownerEmail: owner.email, ownerName: owner.name },\n\t\t\t\t\t...reg.shared.filter((s) => s.fileId !== fileId)\n\t\t\t\t]\n\t\t\t});\n\t\t},\n\n\t\tasync createShared(fileName, owner): Promise<'ok' | 'failed'> {\n\t\t\tm.set({ lastError: null });\n\t\t\ttry {\n\t\t\t\tif (!(await ensureSession())) {\n\t\t\t\t\tfail('cancelled');\n\t\t\t\t\treturn 'failed';\n\t\t\t\t}\n\t\t\t\tconst { fileId } = await host.create(fileName);\n\t\t\t\t// Registered as shared BEFORE the attach: markActive must find it in\n\t\t\t\t// the shared registry, or it would resolve this account as the owner\n\t\t\t\t// and file the joined wallet under \"mine\".\n\t\t\t\tawait this.registerShared(fileId, owner);\n\t\t\t\tawait attachFixed(fileId, null, 'replace-remote');\n\t\t\t\tawait noteEncrypted(fileId, false); // a member wallet is never protected\n\t\t\t\tawait markActive(fileId);\n\t\t\t\treturn 'ok';\n\t\t\t} catch {\n\t\t\t\tfail('failed');\n\t\t\t\treturn 'failed';\n\t\t\t}\n\t\t},\n\n\t\tasync renameBackup(fileId, label): Promise<'ok' | 'failed'> {\n\t\t\tm.set({ lastError: null });\n\t\t\tconst clean = label.trim();\n\t\t\tif (!clean || !host.rename) {\n\t\t\t\tfail('failed');\n\t\t\t\treturn 'failed';\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait host.rename(fileId, naming.namedFileFor(clean));\n\t\t\t\treturn 'ok';\n\t\t\t} catch {\n\t\t\t\tfail('failed');\n\t\t\t\treturn 'failed';\n\t\t\t}\n\t\t},\n\n\t\tasync deleteBackup(fileId): Promise<boolean> {\n\t\t\tm.set({ lastError: null });\n\t\t\ttry {\n\t\t\t\tif (fileId === m.snapshot.activeFileId) {\n\t\t\t\t\t// Detach first: the app goes device-only (local data stays) so\n\t\t\t\t\t// nothing writes to the file as it is removed. keepSession: a\n\t\t\t\t\t// default detach runs the target's disconnect, which forgets the\n\t\t\t\t\t// destination credentials - the remove below still needs them,\n\t\t\t\t\t// and the user deleted a FILE, not their account connection.\n\t\t\t\t\tm.set({ activeFileId: null });\n\t\t\t\t\tawait store.detachTarget({ keepSession: true });\n\t\t\t\t\t// The remembered file id would normally be cleared by that\n\t\t\t\t\t// disconnect; do it here so nothing dangles on a deleted file.\n\t\t\t\t\tawait kv.del(host.activeIdKey);\n\t\t\t\t}\n\t\t\t\tawait host.remove(fileId);\n\t\t\t\tconst reg = await loadRegistry();\n\t\t\t\tawait saveRegistry({\n\t\t\t\t\tpersonalFileId: reg.personalFileId === fileId ? null : reg.personalFileId,\n\t\t\t\t\tshared: reg.shared.filter((s) => s.fileId !== fileId)\n\t\t\t\t});\n\t\t\t\treturn true;\n\t\t\t} catch {\n\t\t\t\tfail('failed');\n\t\t\t\treturn false;\n\t\t\t}\n\t\t},\n\n\t\tasync forgetShared(fileId): Promise<void> {\n\t\t\tconst reg = await loadRegistry();\n\t\t\tawait saveRegistry({\n\t\t\t\tpersonalFileId: reg.personalFileId,\n\t\t\t\tshared: reg.shared.filter((s) => s.fileId !== fileId)\n\t\t\t});\n\t\t}\n\t};\n}\n"]}