@abraca/dabra 2.89.0 → 2.91.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/package.json +5 -11
- package/src/AbracadabraBaseProvider.ts +0 -963
- package/src/AbracadabraClient.ts +0 -1960
- package/src/AbracadabraProvider.ts +0 -915
- package/src/AbracadabraWS.ts +0 -695
- package/src/BackgroundSyncManager.ts +0 -856
- package/src/BackgroundSyncPersistence.ts +0 -140
- package/src/ChatClient.ts +0 -314
- package/src/CloseEvents.ts +0 -49
- package/src/ContentManager.ts +0 -246
- package/src/CoverReconcile.ts +0 -168
- package/src/CryptoIdentityKeystore.ts +0 -754
- package/src/DeviceRegistrationService.ts +0 -257
- package/src/DocConverters.ts +0 -1859
- package/src/DocKeyManager.ts +0 -163
- package/src/DocTypes.ts +0 -628
- package/src/DocUtils.ts +0 -251
- package/src/DocumentCache.ts +0 -210
- package/src/DocumentManager.ts +0 -410
- package/src/E2EAbracadabraProvider.ts +0 -426
- package/src/E2EOfflineStore.ts +0 -55
- package/src/EncryptedChatClient.ts +0 -173
- package/src/EncryptedY.ts +0 -145
- package/src/EventEmitter.ts +0 -59
- package/src/FileBlobStore.ts +0 -569
- package/src/IdentityDoc.ts +0 -711
- package/src/IncomingMessage.ts +0 -63
- package/src/MessageReceiver.ts +0 -151
- package/src/MessageSender.ts +0 -22
- package/src/MetaManager.ts +0 -194
- package/src/MnemonicKeyDerivation.ts +0 -171
- package/src/NodeConnect.ts +0 -63
- package/src/NodeIdentity.ts +0 -378
- package/src/NotificationsClient.ts +0 -207
- package/src/OfflineStore.ts +0 -294
- package/src/OutgoingMessage.ts +0 -25
- package/src/OutgoingMessages/AuthenticationMessage.ts +0 -23
- package/src/OutgoingMessages/AwarenessMessage.ts +0 -41
- package/src/OutgoingMessages/CloseMessage.ts +0 -17
- package/src/OutgoingMessages/QueryAwarenessMessage.ts +0 -17
- package/src/OutgoingMessages/StatelessMessage.ts +0 -18
- package/src/OutgoingMessages/SubdocMessage.ts +0 -35
- package/src/OutgoingMessages/SyncStepOneMessage.ts +0 -25
- package/src/OutgoingMessages/SyncStepTwoMessage.ts +0 -25
- package/src/OutgoingMessages/UpdateMessage.ts +0 -20
- package/src/QueryClient.ts +0 -209
- package/src/RpcClient.ts +0 -659
- package/src/SchemaTypes.ts +0 -179
- package/src/SearchIndex.ts +0 -250
- package/src/ServerRegistry.ts +0 -220
- package/src/ServerReset.ts +0 -222
- package/src/TokenManager.ts +0 -337
- package/src/TreeManager.ts +0 -894
- package/src/TreeTimestamps.ts +0 -101
- package/src/auth.ts +0 -62
- package/src/awarenessStatesToArray.ts +0 -10
- package/src/index.ts +0 -219
- package/src/messageRecord.ts +0 -121
- package/src/sync/BroadcastChannelSync.ts +0 -235
- package/src/types.ts +0 -790
- package/src/webrtc/AbracadabraWebRTC.ts +0 -683
- package/src/webrtc/DataChannelRouter.ts +0 -179
- package/src/webrtc/DevicePairingChannel.ts +0 -496
- package/src/webrtc/E2EEChannel.ts +0 -195
- package/src/webrtc/FileTransferChannel.ts +0 -367
- package/src/webrtc/ManualSignaling.ts +0 -204
- package/src/webrtc/PeerConnection.ts +0 -133
- package/src/webrtc/SignalingSocket.ts +0 -378
- package/src/webrtc/YjsDataChannel.ts +0 -203
- package/src/webrtc/index.ts +0 -26
- package/src/webrtc/types.ts +0 -177
package/src/DocUtils.ts
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared utilities for the DocumentManager ORM layer.
|
|
3
|
-
*
|
|
4
|
-
* These functions were previously duplicated across `mcp/src/utils.ts`,
|
|
5
|
-
* `mcp/src/server.ts`, `cli/src/connection.ts`, and `mcp/src/tools/tree.ts`.
|
|
6
|
-
*/
|
|
7
|
-
import * as Y from "yjs";
|
|
8
|
-
import type { PageMeta, TreeEntry } from "./DocTypes.ts";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Wait for a provider's `synced` event with a timeout.
|
|
12
|
-
* Resolves immediately if the provider is already synced.
|
|
13
|
-
*/
|
|
14
|
-
export function waitForSync(
|
|
15
|
-
provider: {
|
|
16
|
-
isSynced?: boolean;
|
|
17
|
-
on(event: string, cb: () => void): void;
|
|
18
|
-
off(event: string, cb: () => void): void;
|
|
19
|
-
},
|
|
20
|
-
timeoutMs = 15000,
|
|
21
|
-
): Promise<void> {
|
|
22
|
-
// Already synced — resolve immediately.
|
|
23
|
-
if (provider.isSynced) return Promise.resolve();
|
|
24
|
-
|
|
25
|
-
return new Promise<void>((resolve, reject) => {
|
|
26
|
-
const timer = setTimeout(() => {
|
|
27
|
-
provider.off("synced", handler);
|
|
28
|
-
reject(new Error(`Sync timed out after ${timeoutMs}ms`));
|
|
29
|
-
}, timeoutMs);
|
|
30
|
-
|
|
31
|
-
function handler() {
|
|
32
|
-
clearTimeout(timer);
|
|
33
|
-
// Detach so a one-shot `waitForSync` never leaves a dangling
|
|
34
|
-
// `synced` listener on a long-lived provider.
|
|
35
|
-
provider.off("synced", handler);
|
|
36
|
-
resolve();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
provider.on("synced", handler);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Wrap a promise with a timeout.
|
|
45
|
-
*/
|
|
46
|
-
export function withTimeout<T>(
|
|
47
|
-
promise: Promise<T>,
|
|
48
|
-
timeoutMs: number,
|
|
49
|
-
message?: string,
|
|
50
|
-
): Promise<T> {
|
|
51
|
-
return new Promise<T>((resolve, reject) => {
|
|
52
|
-
const timer = setTimeout(
|
|
53
|
-
() =>
|
|
54
|
-
reject(
|
|
55
|
-
new Error(
|
|
56
|
-
message ?? `Operation timed out after ${timeoutMs}ms`,
|
|
57
|
-
),
|
|
58
|
-
),
|
|
59
|
-
timeoutMs,
|
|
60
|
-
);
|
|
61
|
-
promise.then(
|
|
62
|
-
(val) => {
|
|
63
|
-
clearTimeout(timer);
|
|
64
|
-
resolve(val);
|
|
65
|
-
},
|
|
66
|
-
(err) => {
|
|
67
|
-
clearTimeout(timer);
|
|
68
|
-
reject(err);
|
|
69
|
-
},
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Normalize a document ID so the hub/root doc ID is treated as the tree root
|
|
76
|
-
* (null). This lets callers pass the hub doc_id from list_spaces as
|
|
77
|
-
* parentId/rootId and get the expected root-level results instead of an empty
|
|
78
|
-
* set.
|
|
79
|
-
*/
|
|
80
|
-
export function normalizeRootId(
|
|
81
|
-
id: string | null | undefined,
|
|
82
|
-
rootDocId: string | null,
|
|
83
|
-
): string | null {
|
|
84
|
-
if (id == null) return null;
|
|
85
|
-
return id === rootDocId ? null : id;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Safely read a tree map value, converting Y.Map to plain object if needed.
|
|
90
|
-
*/
|
|
91
|
-
export function toPlain(val: unknown): unknown {
|
|
92
|
-
return val instanceof Y.Map ? val.toJSON() : val;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Read every entry of a `doc-tree` Y.Map into a flat {@link TreeEntry}[].
|
|
97
|
-
*
|
|
98
|
-
* Skips non-entry keys (e.g. `":updatedAt"` timestamps), and — when `selfId`
|
|
99
|
-
* is given — the space root's own self-descriptor entry (keyed by the
|
|
100
|
-
* connected root doc id, `parentId:null`, `order:-1`) so the space doesn't
|
|
101
|
-
* render as a phantom first child of itself. Entry values are nested
|
|
102
|
-
* `Y.Map`s, so each is resolved via {@link toPlain} (raw field access on a
|
|
103
|
-
* Y.Map returns undefined).
|
|
104
|
-
*
|
|
105
|
-
* This is the single shared implementation; `@abraca/mcp`, `@abraca/cli`, and
|
|
106
|
-
* `@abraca/resend` previously each carried a near-identical copy.
|
|
107
|
-
*/
|
|
108
|
-
export function readEntries(
|
|
109
|
-
treeMap: Y.Map<unknown>,
|
|
110
|
-
selfId?: string | null,
|
|
111
|
-
): TreeEntry[] {
|
|
112
|
-
const entries: TreeEntry[] = [];
|
|
113
|
-
treeMap.forEach((raw, id) => {
|
|
114
|
-
if (selfId && id === selfId) return;
|
|
115
|
-
const value = toPlain(raw);
|
|
116
|
-
if (typeof value !== "object" || value === null) return;
|
|
117
|
-
const v = value as Record<string, unknown>;
|
|
118
|
-
entries.push({
|
|
119
|
-
id,
|
|
120
|
-
label: typeof v.label === "string" && v.label ? v.label : "Untitled",
|
|
121
|
-
parentId: (v.parentId as string | null) ?? null,
|
|
122
|
-
order: typeof v.order === "number" ? v.order : 0,
|
|
123
|
-
type: typeof v.type === "string" ? v.type : undefined,
|
|
124
|
-
meta: v.meta as PageMeta | undefined,
|
|
125
|
-
createdAt: typeof v.createdAt === "number" ? v.createdAt : undefined,
|
|
126
|
-
updatedAt: typeof v.updatedAt === "number" ? v.updatedAt : undefined,
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
return entries;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Build a tree/trash entry as a nested `Y.Map`. Use for a brand-new or
|
|
134
|
-
* re-created key (create / duplicate / restore) where no concurrent
|
|
135
|
-
* writer exists, so a whole-value write is safe. `undefined` fields are
|
|
136
|
-
* omitted; `null` is kept (a real value, e.g. top-level `parentId`).
|
|
137
|
-
*/
|
|
138
|
-
export function makeEntryMap(
|
|
139
|
-
fields: Record<string, unknown>,
|
|
140
|
-
): Y.Map<unknown> {
|
|
141
|
-
const m = new Y.Map<unknown>();
|
|
142
|
-
for (const [k, v] of Object.entries(fields)) {
|
|
143
|
-
if (v !== undefined) m.set(k, v);
|
|
144
|
-
}
|
|
145
|
-
return m;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* A label is a "placeholder" — i.e. carries no real user title — when it is
|
|
150
|
-
* empty/whitespace, null/undefined, or the literal `"Untitled"` sentinel
|
|
151
|
-
* (case-insensitive, so `"untitled"` from a `labelToFilename` round-trip is
|
|
152
|
-
* caught too). The whole tree-label corruption class boils down to a
|
|
153
|
-
* placeholder being allowed to overwrite a real title; `patchEntry` refuses
|
|
154
|
-
* exactly that. Mirrors cou-sh's `isEmptyTreeLabel`.
|
|
155
|
-
*/
|
|
156
|
-
export function isPlaceholderLabel(label: unknown): boolean {
|
|
157
|
-
if (typeof label !== "string") return label == null;
|
|
158
|
-
const t = label.trim();
|
|
159
|
-
return t === "" || t.toLowerCase() === "untitled";
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Patch an EXISTING entry's fields per-key on its nested `Y.Map`, so a
|
|
164
|
-
* concurrent edit to a *different* field by a peer is preserved instead
|
|
165
|
-
* of being clobbered by a whole-entry write — the whole-entry-LWW fix
|
|
166
|
-
* (audit ⑦), the mirror of the Rust provider's `with_entry_mut`.
|
|
167
|
-
*
|
|
168
|
-
* - nested `Y.Map` entry → set/delete only the touched keys in place;
|
|
169
|
-
* - legacy opaque (plain-object) entry → migrated once to a `Y.Map`;
|
|
170
|
-
* - missing entry → created from the patch (lenient; matches the prior
|
|
171
|
-
* call-site behaviour of spreading `undefined`).
|
|
172
|
-
*
|
|
173
|
-
* A patch value of `undefined` deletes the key; `null` is written.
|
|
174
|
-
* Self-transacting: it batches its writes in one `Y.Doc` transaction
|
|
175
|
-
* (a safe reentrant no-op join when already inside one), so callers
|
|
176
|
-
* don't need to pass or own a transaction.
|
|
177
|
-
*
|
|
178
|
-
* ── NO-DESTROY LABEL INVARIANT ──────────────────────────────────────────
|
|
179
|
-
* A `label` patch that is a placeholder (empty/whitespace/"Untitled") is
|
|
180
|
-
* DROPPED when the entry already holds a real (non-placeholder) label —
|
|
181
|
-
* regardless of which consumer (cou-sh title-sync, fs-sync rename
|
|
182
|
-
* detection, MCP, table renderers, a stale snapshot) tried it. This is the
|
|
183
|
-
* source-of-truth guard against the "card title silently becomes Untitled
|
|
184
|
-
* / files renamed to untitled.md" corruption: a placeholder must never win
|
|
185
|
-
* over a real title. Creating a brand-new entry with an empty label (e.g.
|
|
186
|
-
* a fresh kanban card) is still allowed — the guard only fires when a real
|
|
187
|
-
* label already exists. Pass `{ allowLabelClear: true }` to override (the
|
|
188
|
-
* single legitimate "user explicitly cleared it" path).
|
|
189
|
-
*/
|
|
190
|
-
export function patchEntry(
|
|
191
|
-
treeMap: Y.Map<unknown>,
|
|
192
|
-
id: string,
|
|
193
|
-
patch: Record<string, unknown>,
|
|
194
|
-
removeKeys: string[] = [],
|
|
195
|
-
opts: { allowLabelClear?: boolean } = {},
|
|
196
|
-
): void {
|
|
197
|
-
const apply = (): void => {
|
|
198
|
-
const raw = treeMap.get(id);
|
|
199
|
-
|
|
200
|
-
// NO-DESTROY label guard: strip a placeholder `label` from the patch
|
|
201
|
-
// when a real label already exists on the entry. The existing-label
|
|
202
|
-
// read is DUCK-TYPED (not `instanceof Y.Map`): under a host that
|
|
203
|
-
// resolves a second physical yjs (cou-sh's local file-link), a
|
|
204
|
-
// foreign Y.Map fails `instanceof` and would read `undefined` —
|
|
205
|
-
// disabling the guard in the very environment that needs it. `.get`
|
|
206
|
-
// / `.toJSON` work regardless of which yjs constructed the map.
|
|
207
|
-
let effectivePatch = patch;
|
|
208
|
-
if (
|
|
209
|
-
!opts.allowLabelClear &&
|
|
210
|
-
Object.hasOwn(patch, "label") &&
|
|
211
|
-
isPlaceholderLabel(patch.label)
|
|
212
|
-
) {
|
|
213
|
-
let existingLabel: unknown;
|
|
214
|
-
if (raw != null && typeof (raw as { get?: unknown }).get === "function") {
|
|
215
|
-
existingLabel = (raw as Y.Map<unknown>).get("label");
|
|
216
|
-
} else if (
|
|
217
|
-
raw != null &&
|
|
218
|
-
typeof (raw as { toJSON?: unknown }).toJSON === "function"
|
|
219
|
-
) {
|
|
220
|
-
existingLabel = ((raw as { toJSON: () => Record<string, unknown> }).toJSON())
|
|
221
|
-
?.label;
|
|
222
|
-
} else if (raw != null && typeof raw === "object") {
|
|
223
|
-
existingLabel = (raw as Record<string, unknown>).label;
|
|
224
|
-
}
|
|
225
|
-
if (!isPlaceholderLabel(existingLabel)) {
|
|
226
|
-
const { label: _dropped, ...rest } = patch;
|
|
227
|
-
effectivePatch = rest;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (raw instanceof Y.Map) {
|
|
232
|
-
for (const [k, v] of Object.entries(effectivePatch)) {
|
|
233
|
-
if (v === undefined) raw.delete(k);
|
|
234
|
-
else raw.set(k, v);
|
|
235
|
-
}
|
|
236
|
-
for (const k of removeKeys) raw.delete(k);
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
const base =
|
|
240
|
-
raw == null ? {} : (toPlain(raw) as Record<string, unknown>);
|
|
241
|
-
const merged: Record<string, unknown> = { ...base, ...effectivePatch };
|
|
242
|
-
for (const [k, v] of Object.entries(effectivePatch)) {
|
|
243
|
-
if (v === undefined) delete merged[k];
|
|
244
|
-
}
|
|
245
|
-
for (const k of removeKeys) delete merged[k];
|
|
246
|
-
treeMap.set(id, makeEntryMap(merged));
|
|
247
|
-
};
|
|
248
|
-
const doc = treeMap.doc;
|
|
249
|
-
if (doc) doc.transact(apply);
|
|
250
|
-
else apply();
|
|
251
|
-
}
|
package/src/DocumentCache.ts
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* IndexedDB-backed cache for REST API metadata (documents, children, profiles,
|
|
3
|
-
* permissions, uploads). Provides offline-first read access to data fetched
|
|
4
|
-
* from the server.
|
|
5
|
-
*
|
|
6
|
-
* Each entry carries a `cachedAt` timestamp; reads return null once the TTL
|
|
7
|
-
* has elapsed. The cache is scoped by server origin so the same app can work
|
|
8
|
-
* against multiple backends without cross-contamination.
|
|
9
|
-
*
|
|
10
|
-
* Designed to be passed to AbracadabraClientConfig.cache so the client
|
|
11
|
-
* automatically checks the cache before hitting the network.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type {
|
|
15
|
-
DocumentMeta,
|
|
16
|
-
UserProfile,
|
|
17
|
-
PermissionEntry,
|
|
18
|
-
UploadInfo,
|
|
19
|
-
} from "./types.ts";
|
|
20
|
-
|
|
21
|
-
const DB_VERSION = 1;
|
|
22
|
-
const DEFAULT_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
|
23
|
-
|
|
24
|
-
function idbAvailable(): boolean {
|
|
25
|
-
return typeof globalThis !== "undefined" && "indexedDB" in globalThis;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function openDb(origin: string): Promise<IDBDatabase> {
|
|
29
|
-
return new Promise((resolve, reject) => {
|
|
30
|
-
const req = globalThis.indexedDB.open(
|
|
31
|
-
`abracadabra:meta-cache:${origin}`,
|
|
32
|
-
DB_VERSION,
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
req.onupgradeneeded = (event) => {
|
|
36
|
-
const db = (event.target as IDBOpenDBRequest).result;
|
|
37
|
-
for (const name of ["doc_meta", "children", "user_profile", "permissions", "uploads"]) {
|
|
38
|
-
if (!db.objectStoreNames.contains(name)) {
|
|
39
|
-
db.createObjectStore(name);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
req.onsuccess = () => resolve(req.result);
|
|
45
|
-
req.onerror = () => reject(req.error);
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function txPromise<T>(store: IDBObjectStore, request: IDBRequest<T>): Promise<T> {
|
|
50
|
-
return new Promise((resolve, reject) => {
|
|
51
|
-
request.onsuccess = () => resolve(request.result);
|
|
52
|
-
request.onerror = () => reject(request.error);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface DocumentCacheOptions {
|
|
57
|
-
/** How long cached entries remain valid. Default: 5 minutes. */
|
|
58
|
-
ttlMs?: number;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export class DocumentCache {
|
|
62
|
-
private readonly origin: string;
|
|
63
|
-
private readonly ttlMs: number;
|
|
64
|
-
private dbPromise: Promise<IDBDatabase | null> | null = null;
|
|
65
|
-
private db: IDBDatabase | null = null;
|
|
66
|
-
|
|
67
|
-
constructor(serverOrigin: string, opts?: DocumentCacheOptions) {
|
|
68
|
-
this.origin = serverOrigin;
|
|
69
|
-
this.ttlMs = opts?.ttlMs ?? DEFAULT_TTL_MS;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
private getDb(): Promise<IDBDatabase | null> {
|
|
73
|
-
if (!idbAvailable()) return Promise.resolve(null);
|
|
74
|
-
if (!this.dbPromise) {
|
|
75
|
-
this.dbPromise = openDb(this.origin)
|
|
76
|
-
.catch(() => null)
|
|
77
|
-
.then((db) => {
|
|
78
|
-
this.db = db;
|
|
79
|
-
return db;
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
return this.dbPromise;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private isExpired(cachedAt: number): boolean {
|
|
86
|
-
return Date.now() - cachedAt > this.ttlMs;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// ── Generic TTL helpers ───────────────────────────────────────────────────
|
|
90
|
-
|
|
91
|
-
private async getWithTtl<T>(
|
|
92
|
-
storeName: string,
|
|
93
|
-
key: string,
|
|
94
|
-
): Promise<T | null> {
|
|
95
|
-
const db = await this.getDb();
|
|
96
|
-
if (!db) return null;
|
|
97
|
-
const tx = db.transaction(storeName, "readonly");
|
|
98
|
-
const result = await txPromise<{ value: T; cachedAt: number } | undefined>(
|
|
99
|
-
tx.objectStore(storeName),
|
|
100
|
-
tx.objectStore(storeName).get(key),
|
|
101
|
-
);
|
|
102
|
-
if (!result) return null;
|
|
103
|
-
if (this.isExpired(result.cachedAt)) {
|
|
104
|
-
// Stale — evict asynchronously and signal miss
|
|
105
|
-
this.deleteKey(storeName, key).catch(() => null);
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
return result.value;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private async setWithTtl<T>(storeName: string, key: string, value: T): Promise<void> {
|
|
112
|
-
const db = await this.getDb();
|
|
113
|
-
if (!db) return;
|
|
114
|
-
const entry = { value, cachedAt: Date.now() };
|
|
115
|
-
const tx = db.transaction(storeName, "readwrite");
|
|
116
|
-
await txPromise(
|
|
117
|
-
tx.objectStore(storeName),
|
|
118
|
-
tx.objectStore(storeName).put(entry, key),
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private async deleteKey(storeName: string, key: string): Promise<void> {
|
|
123
|
-
const db = await this.getDb();
|
|
124
|
-
if (!db) return;
|
|
125
|
-
const tx = db.transaction(storeName, "readwrite");
|
|
126
|
-
await txPromise(tx.objectStore(storeName), tx.objectStore(storeName).delete(key));
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// ── Document metadata ─────────────────────────────────────────────────────
|
|
130
|
-
|
|
131
|
-
async getDoc(docId: string): Promise<DocumentMeta | null> {
|
|
132
|
-
return this.getWithTtl<DocumentMeta>("doc_meta", docId);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async setDoc(meta: DocumentMeta): Promise<void> {
|
|
136
|
-
return this.setWithTtl("doc_meta", meta.id, meta);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async invalidateDoc(docId: string): Promise<void> {
|
|
140
|
-
await this.deleteKey("doc_meta", docId).catch(() => null);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// ── Children list ─────────────────────────────────────────────────────────
|
|
144
|
-
|
|
145
|
-
async getChildren(parentId: string): Promise<string[] | null> {
|
|
146
|
-
return this.getWithTtl<string[]>("children", parentId);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
async setChildren(parentId: string, items: string[]): Promise<void> {
|
|
150
|
-
return this.setWithTtl("children", parentId, items);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async invalidateChildren(parentId: string): Promise<void> {
|
|
154
|
-
await this.deleteKey("children", parentId).catch(() => null);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// ── User profile ──────────────────────────────────────────────────────────
|
|
158
|
-
|
|
159
|
-
async getProfile(userId: string): Promise<UserProfile | null> {
|
|
160
|
-
return this.getWithTtl<UserProfile>("user_profile", userId);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
async setProfile(profile: UserProfile): Promise<void> {
|
|
164
|
-
return this.setWithTtl("user_profile", profile.id, profile);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** Get the cached profile for the currently authenticated user. */
|
|
168
|
-
async getCurrentProfile(): Promise<UserProfile | null> {
|
|
169
|
-
return this.getWithTtl<UserProfile>("user_profile", "__current__");
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/** Cache a profile both by its ID and as the current user. */
|
|
173
|
-
async setCurrentProfile(profile: UserProfile): Promise<void> {
|
|
174
|
-
await Promise.all([
|
|
175
|
-
this.setWithTtl("user_profile", profile.id, profile),
|
|
176
|
-
this.setWithTtl("user_profile", "__current__", profile),
|
|
177
|
-
]);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// ── Permissions ───────────────────────────────────────────────────────────
|
|
181
|
-
|
|
182
|
-
async getPermissions(docId: string): Promise<PermissionEntry[] | null> {
|
|
183
|
-
return this.getWithTtl<PermissionEntry[]>("permissions", docId);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
async setPermissions(docId: string, items: PermissionEntry[]): Promise<void> {
|
|
187
|
-
return this.setWithTtl("permissions", docId, items);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// ── Uploads list ──────────────────────────────────────────────────────────
|
|
191
|
-
|
|
192
|
-
async getUploads(docId: string): Promise<UploadInfo[] | null> {
|
|
193
|
-
return this.getWithTtl<UploadInfo[]>("uploads", docId);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
async setUploads(docId: string, items: UploadInfo[]): Promise<void> {
|
|
197
|
-
return this.setWithTtl("uploads", docId, items);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async invalidateUploads(docId: string): Promise<void> {
|
|
201
|
-
await this.deleteKey("uploads", docId).catch(() => null);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// ── Lifecycle ─────────────────────────────────────────────────────────────
|
|
205
|
-
|
|
206
|
-
destroy(): void {
|
|
207
|
-
this.db?.close();
|
|
208
|
-
this.db = null;
|
|
209
|
-
}
|
|
210
|
-
}
|