@classic-homes/offline-core 0.1.5 → 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.
- package/README.md +2 -2
- package/dist/cache/index.d.ts +42 -1
- package/dist/cache/index.js +6 -6
- package/dist/{chunk-J2ZYJDJX.js → chunk-4KL3GDYP.js} +94 -44
- package/dist/{chunk-WWKATW37.js → chunk-MA4ZHPQS.js} +95 -18
- package/dist/{chunk-DIXYWY34.js → chunk-NCAGT2LL.js} +361 -73
- package/dist/{chunk-ZC7F2HDV.js → chunk-QS7N4PTV.js} +5 -2
- package/dist/{chunk-U27TKOYZ.js → chunk-R2IP2AA5.js} +2 -2
- package/dist/{chunk-PEQVHD6Q.js → chunk-SLDAYKRY.js} +191 -34
- package/dist/{chunk-EWMN3Y7N.js → chunk-UL7MZBSF.js} +2 -2
- package/dist/drafts/index.d.ts +46 -9
- package/dist/drafts/index.js +3 -3
- package/dist/files/index.js +2 -2
- package/dist/index.d.ts +7 -4
- package/dist/index.js +7 -7
- package/dist/storage/index.d.ts +7 -1
- package/dist/storage/index.js +2 -2
- package/dist/sync/index.d.ts +109 -14
- package/dist/sync/index.js +3 -3
- package/dist/{types-Ho1JiLQR.d.ts → types-CRMTQnjY.d.ts} +6 -1
- package/package.json +1 -1
|
@@ -91,8 +91,11 @@ function isExpired(expiresAt) {
|
|
|
91
91
|
var DEFAULT_EXPIRATIONS = {
|
|
92
92
|
/** Form draft expiration: 7 days */
|
|
93
93
|
DRAFT: 7 * 24 * 60 * 60 * 1e3,
|
|
94
|
-
/**
|
|
95
|
-
|
|
94
|
+
/**
|
|
95
|
+
* File attachment expiration: 7 days, the same as drafts, so a restored
|
|
96
|
+
* draft still has its attachments
|
|
97
|
+
*/
|
|
98
|
+
FILE: 7 * 24 * 60 * 60 * 1e3,
|
|
96
99
|
/** Cache entry expiration: 1 hour */
|
|
97
100
|
CACHE: 60 * 60 * 1e3
|
|
98
101
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEFAULT_EXPIRATIONS } from './chunk-
|
|
1
|
+
import { DEFAULT_EXPIRATIONS } from './chunk-QS7N4PTV.js';
|
|
2
2
|
|
|
3
3
|
// src/files/types.ts
|
|
4
4
|
function storedFileToBlobMetadata(file) {
|
|
@@ -58,7 +58,7 @@ var FileStorageService = class {
|
|
|
58
58
|
if (constraints.maxFiles !== void 0) {
|
|
59
59
|
const totalCount = existingCount + files.length;
|
|
60
60
|
if (totalCount > constraints.maxFiles) {
|
|
61
|
-
const excess = files.slice(constraints.maxFiles - existingCount);
|
|
61
|
+
const excess = files.slice(Math.max(0, constraints.maxFiles - existingCount));
|
|
62
62
|
for (const file of excess) {
|
|
63
63
|
errors.push({
|
|
64
64
|
file,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createFileStorageService } from './chunk-
|
|
3
|
-
import { createDraftStorageService } from './chunk-
|
|
4
|
-
import { createDraftManagerService } from './chunk-
|
|
5
|
-
import { DEFAULT_EXPIRATIONS, CleanupScheduler } from './chunk-
|
|
1
|
+
import { OfflineStorageError, createAdapter, detectAvailableAdapters } from './chunk-4KL3GDYP.js';
|
|
2
|
+
import { createFileStorageService } from './chunk-R2IP2AA5.js';
|
|
3
|
+
import { createDraftStorageService } from './chunk-UL7MZBSF.js';
|
|
4
|
+
import { createDraftManagerService } from './chunk-MA4ZHPQS.js';
|
|
5
|
+
import { DEFAULT_EXPIRATIONS, CleanupScheduler } from './chunk-QS7N4PTV.js';
|
|
6
6
|
|
|
7
7
|
// src/cache/types.ts
|
|
8
8
|
var CACHE_PREFIX = "cache:";
|
|
@@ -21,6 +21,13 @@ var DEFAULT_IN_FLIGHT_TIMEOUT = 3e4;
|
|
|
21
21
|
var DEFAULT_MAX_IN_FLIGHT = 100;
|
|
22
22
|
var DEFAULT_MAX_ENTRIES = 1e3;
|
|
23
23
|
var DEFAULT_MAX_SIZE_BYTES = 50 * 1024 * 1024;
|
|
24
|
+
function estimateSize(entry) {
|
|
25
|
+
try {
|
|
26
|
+
return JSON.stringify(entry)?.length ?? 0;
|
|
27
|
+
} catch {
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
24
31
|
var CacheService = class {
|
|
25
32
|
constructor(adapter, options) {
|
|
26
33
|
this.adapter = adapter;
|
|
@@ -31,8 +38,17 @@ var CacheService = class {
|
|
|
31
38
|
};
|
|
32
39
|
// Track in-flight fetches to avoid duplicate requests
|
|
33
40
|
this.inFlightFetches = /* @__PURE__ */ new Map();
|
|
41
|
+
// Writes that have not stored their value yet, and the generation that
|
|
42
|
+
// invalidateAll() bumps. Invalidation cancels the tickets it covers.
|
|
43
|
+
this.pendingWrites = /* @__PURE__ */ new Set();
|
|
44
|
+
this.generation = 0;
|
|
34
45
|
// Track access order for LRU eviction (key -> last access timestamp)
|
|
35
46
|
this.accessOrder = /* @__PURE__ */ new Map();
|
|
47
|
+
// Approximate size of each of this cache's entries (key -> bytes), loaded
|
|
48
|
+
// lazily from storage. Size limits count only these, never other data on
|
|
49
|
+
// the origin (files, drafts, other caches) that storage.estimate() reports.
|
|
50
|
+
this.entrySizes = null;
|
|
51
|
+
this.entrySizesLoading = null;
|
|
36
52
|
this.inFlightTimeout = options?.inFlightTimeout ?? DEFAULT_IN_FLIGHT_TIMEOUT;
|
|
37
53
|
this.maxInFlight = options?.maxInFlight ?? DEFAULT_MAX_IN_FLIGHT;
|
|
38
54
|
this.maxEntries = options?.maxEntries ?? DEFAULT_MAX_ENTRIES;
|
|
@@ -90,6 +106,48 @@ var CacheService = class {
|
|
|
90
106
|
* Set a cached value
|
|
91
107
|
*/
|
|
92
108
|
async set(key, value, options = {}) {
|
|
109
|
+
const ticket = this.openTicket(key, options.tags);
|
|
110
|
+
try {
|
|
111
|
+
await this.write(key, value, options, ticket);
|
|
112
|
+
} finally {
|
|
113
|
+
this.pendingWrites.delete(ticket);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Register a pending write for `key` (see {@link WriteTicket}).
|
|
118
|
+
*/
|
|
119
|
+
openTicket(key, tags) {
|
|
120
|
+
const ticket = {
|
|
121
|
+
key,
|
|
122
|
+
tags: tags ? [...tags] : [],
|
|
123
|
+
generation: this.generation,
|
|
124
|
+
cancelled: false
|
|
125
|
+
};
|
|
126
|
+
this.pendingWrites.add(ticket);
|
|
127
|
+
return ticket;
|
|
128
|
+
}
|
|
129
|
+
isLive(ticket) {
|
|
130
|
+
return !ticket.cancelled && ticket.generation === this.generation;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Cancel the pending writes `matches` selects, and forget their in-flight
|
|
134
|
+
* fetches so later reads start their own.
|
|
135
|
+
*/
|
|
136
|
+
cancelPending(matches) {
|
|
137
|
+
for (const ticket of this.pendingWrites) {
|
|
138
|
+
if (matches(ticket)) ticket.cancelled = true;
|
|
139
|
+
}
|
|
140
|
+
for (const [key, entry] of this.inFlightFetches) {
|
|
141
|
+
if (entry.ticket.cancelled) this.inFlightFetches.delete(key);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Store a value, unless `ticket` is cancelled before the write is issued.
|
|
146
|
+
* A cancellation that lands while the write is in progress deletes the
|
|
147
|
+
* entry again: invalidated data must not survive the invalidation.
|
|
148
|
+
*/
|
|
149
|
+
async write(key, value, options, ticket) {
|
|
150
|
+
if (!this.isLive(ticket)) return;
|
|
93
151
|
const storageKey = getCacheKey(key);
|
|
94
152
|
const now = Date.now();
|
|
95
153
|
const ttl = options.ttl ?? DEFAULT_EXPIRATIONS.CACHE;
|
|
@@ -99,17 +157,44 @@ var CacheService = class {
|
|
|
99
157
|
expiresAt: ttl > 0 ? now + ttl : null,
|
|
100
158
|
tags: options.tags
|
|
101
159
|
};
|
|
102
|
-
|
|
160
|
+
const size = estimateSize(entry);
|
|
161
|
+
await this.enforceLimit(key, size);
|
|
162
|
+
if (!this.isLive(ticket)) return;
|
|
163
|
+
const staleTime = options.staleTime ?? ttl;
|
|
103
164
|
await this.adapter.storeJSON(storageKey, entry, {
|
|
104
|
-
tags: options.tags ? [...options.tags, "cache"] : ["cache"]
|
|
165
|
+
tags: options.tags ? [...options.tags, "cache"] : ["cache"],
|
|
166
|
+
...ttl > 0 && { ttl: ttl + Math.max(0, staleTime) }
|
|
105
167
|
});
|
|
168
|
+
if (!this.isLive(ticket)) {
|
|
169
|
+
await this.adapter.deleteJSON(storageKey);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
106
172
|
this.accessOrder.set(key, now);
|
|
173
|
+
this.entrySizes?.set(key, size);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Sizes of this cache's entries, read from storage the first time
|
|
177
|
+
*/
|
|
178
|
+
async getEntrySizes() {
|
|
179
|
+
if (this.entrySizes) return this.entrySizes;
|
|
180
|
+
this.entrySizesLoading ?? (this.entrySizesLoading = (async () => {
|
|
181
|
+
const sizes = /* @__PURE__ */ new Map();
|
|
182
|
+
for (const key of await this.keys()) {
|
|
183
|
+
const entry = await this.adapter.getJSON(getCacheKey(key));
|
|
184
|
+
if (entry) sizes.set(key, estimateSize(entry.data));
|
|
185
|
+
}
|
|
186
|
+
this.entrySizes = sizes;
|
|
187
|
+
this.entrySizesLoading = null;
|
|
188
|
+
return sizes;
|
|
189
|
+
})());
|
|
190
|
+
return this.entrySizesLoading;
|
|
107
191
|
}
|
|
108
192
|
/**
|
|
109
193
|
* Enforce cache size limits using LRU eviction
|
|
110
194
|
* @param newKey - Key being added (skip count check if it already exists)
|
|
195
|
+
* @param newSize - Approximate size of the entry being written
|
|
111
196
|
*/
|
|
112
|
-
async enforceLimit(newKey) {
|
|
197
|
+
async enforceLimit(newKey, newSize = 0) {
|
|
113
198
|
const keys = await this.keys();
|
|
114
199
|
const isNewEntry = newKey ? !keys.includes(newKey) : true;
|
|
115
200
|
if (isNewEntry && keys.length >= this.maxEntries) {
|
|
@@ -117,26 +202,44 @@ var CacheService = class {
|
|
|
117
202
|
await this.evictLRU(keys, evictCount);
|
|
118
203
|
}
|
|
119
204
|
try {
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
205
|
+
const sizes = await this.getEntrySizes();
|
|
206
|
+
const current = await this.keys();
|
|
207
|
+
const live = new Set(current);
|
|
208
|
+
for (const key of sizes.keys()) {
|
|
209
|
+
if (!live.has(key)) sizes.delete(key);
|
|
210
|
+
}
|
|
211
|
+
const candidates = current.filter((key) => key !== newKey);
|
|
212
|
+
let total = newSize;
|
|
213
|
+
for (const key of candidates) total += sizes.get(key) ?? 0;
|
|
214
|
+
if (total <= this.maxSizeBytes) return;
|
|
215
|
+
const sorted = this.sortByAccess(candidates);
|
|
216
|
+
const toEvict = [];
|
|
217
|
+
for (const key of sorted) {
|
|
218
|
+
if (total <= this.maxSizeBytes) break;
|
|
219
|
+
total -= sizes.get(key) ?? 0;
|
|
220
|
+
toEvict.push(key);
|
|
124
221
|
}
|
|
222
|
+
await this.evictLRU(toEvict, toEvict.length);
|
|
125
223
|
} catch {
|
|
126
224
|
}
|
|
127
225
|
}
|
|
128
226
|
/**
|
|
129
|
-
*
|
|
227
|
+
* Sort keys by access time, oldest first
|
|
130
228
|
*/
|
|
131
|
-
|
|
132
|
-
|
|
229
|
+
sortByAccess(keys) {
|
|
230
|
+
return [...keys].sort((a, b) => {
|
|
133
231
|
const accessA = this.accessOrder.get(a) ?? 0;
|
|
134
232
|
const accessB = this.accessOrder.get(b) ?? 0;
|
|
135
233
|
return accessA - accessB;
|
|
136
234
|
});
|
|
137
|
-
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Evict least recently used entries
|
|
238
|
+
*/
|
|
239
|
+
async evictLRU(keys, count) {
|
|
240
|
+
const toEvict = this.sortByAccess(keys).slice(0, count);
|
|
138
241
|
for (const key of toEvict) {
|
|
139
|
-
await this.
|
|
242
|
+
await this.removeEntry(key);
|
|
140
243
|
this.accessOrder.delete(key);
|
|
141
244
|
}
|
|
142
245
|
}
|
|
@@ -144,6 +247,14 @@ var CacheService = class {
|
|
|
144
247
|
* Get cached value or fetch and cache it
|
|
145
248
|
*/
|
|
146
249
|
async getOrSet(key, fetcher, options = {}) {
|
|
250
|
+
const ticket = this.openTicket(key, options.tags);
|
|
251
|
+
try {
|
|
252
|
+
return await this.getOrFetch(key, fetcher, options, ticket);
|
|
253
|
+
} finally {
|
|
254
|
+
this.pendingWrites.delete(ticket);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
async getOrFetch(key, fetcher, options, ticket) {
|
|
147
258
|
const {
|
|
148
259
|
ttl = DEFAULT_EXPIRATIONS.CACHE,
|
|
149
260
|
tags,
|
|
@@ -164,20 +275,27 @@ var CacheService = class {
|
|
|
164
275
|
const now = Date.now();
|
|
165
276
|
const age = now - entry.createdAt;
|
|
166
277
|
if (age < (entry.expiresAt !== null ? entry.expiresAt - entry.createdAt : 0) + staleThreshold) {
|
|
167
|
-
void this.revalidateInBackground(key, fetcher, { ttl, tags });
|
|
278
|
+
void this.revalidateInBackground(key, fetcher, { ttl, tags, staleTime }, ticket);
|
|
168
279
|
return cached.value;
|
|
169
280
|
}
|
|
170
281
|
}
|
|
171
282
|
}
|
|
172
|
-
return this.fetchAndCache(
|
|
283
|
+
return this.fetchAndCache(
|
|
284
|
+
key,
|
|
285
|
+
fetcher,
|
|
286
|
+
{ ttl, tags, staleTime },
|
|
287
|
+
ticket,
|
|
288
|
+
onFetching,
|
|
289
|
+
onFetchComplete
|
|
290
|
+
);
|
|
173
291
|
}
|
|
174
292
|
/**
|
|
175
293
|
* Fetch and cache a value (with deduplication)
|
|
176
294
|
*/
|
|
177
|
-
async fetchAndCache(key, fetcher, options, onFetching, onFetchComplete) {
|
|
295
|
+
async fetchAndCache(key, fetcher, options, caller, onFetching, onFetchComplete) {
|
|
178
296
|
this.cleanupStaleInFlight();
|
|
179
297
|
const existing = this.inFlightFetches.get(key);
|
|
180
|
-
if (existing) {
|
|
298
|
+
if (existing && this.isLive(existing.ticket) && this.isLive(caller)) {
|
|
181
299
|
return existing.promise;
|
|
182
300
|
}
|
|
183
301
|
if (this.inFlightFetches.size >= this.maxInFlight) {
|
|
@@ -187,30 +305,35 @@ var CacheService = class {
|
|
|
187
305
|
}
|
|
188
306
|
}
|
|
189
307
|
onFetching?.();
|
|
308
|
+
const ticket = this.openTicket(key, options.tags);
|
|
309
|
+
ticket.generation = caller.generation;
|
|
310
|
+
ticket.cancelled = caller.cancelled;
|
|
311
|
+
let entry;
|
|
190
312
|
const fetchPromise = (async () => {
|
|
191
313
|
try {
|
|
192
314
|
const value = await fetcher();
|
|
193
|
-
await this.
|
|
315
|
+
await this.write(key, value, options, ticket);
|
|
194
316
|
onFetchComplete?.();
|
|
195
317
|
return value;
|
|
196
318
|
} catch (error) {
|
|
197
319
|
onFetchComplete?.(error);
|
|
198
320
|
throw error;
|
|
199
321
|
} finally {
|
|
200
|
-
this.
|
|
322
|
+
this.pendingWrites.delete(ticket);
|
|
323
|
+
if (this.inFlightFetches.get(key) === entry) this.inFlightFetches.delete(key);
|
|
201
324
|
}
|
|
202
325
|
})();
|
|
203
|
-
this.
|
|
204
|
-
promise: fetchPromise,
|
|
205
|
-
|
|
206
|
-
}
|
|
326
|
+
if (this.isLive(ticket)) {
|
|
327
|
+
entry = { promise: fetchPromise, startedAt: Date.now(), ticket };
|
|
328
|
+
this.inFlightFetches.set(key, entry);
|
|
329
|
+
}
|
|
207
330
|
return fetchPromise;
|
|
208
331
|
}
|
|
209
332
|
/**
|
|
210
333
|
* Revalidate cache in background
|
|
211
334
|
*/
|
|
212
|
-
async revalidateInBackground(key, fetcher, options) {
|
|
213
|
-
this.fetchAndCache(key, fetcher, options).catch((error) => {
|
|
335
|
+
async revalidateInBackground(key, fetcher, options, caller) {
|
|
336
|
+
this.fetchAndCache(key, fetcher, options, caller).catch((error) => {
|
|
214
337
|
console.warn(`Background revalidation failed for ${key}:`, error);
|
|
215
338
|
});
|
|
216
339
|
}
|
|
@@ -219,15 +342,27 @@ var CacheService = class {
|
|
|
219
342
|
*/
|
|
220
343
|
async invalidate(key) {
|
|
221
344
|
this.cleanupStaleInFlight();
|
|
345
|
+
this.cancelPending((ticket) => ticket.key === key);
|
|
346
|
+
await this.removeEntry(key);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Delete one entry from storage (eviction and invalidate). Unlike
|
|
350
|
+
* invalidate() it leaves pending writes alone: eviction makes room, it
|
|
351
|
+
* does not declare the data stale.
|
|
352
|
+
*/
|
|
353
|
+
async removeEntry(key) {
|
|
222
354
|
const storageKey = getCacheKey(key);
|
|
223
355
|
await this.adapter.deleteJSON(storageKey);
|
|
224
356
|
this.accessOrder.delete(key);
|
|
357
|
+
this.entrySizes?.delete(key);
|
|
225
358
|
}
|
|
226
359
|
/**
|
|
227
360
|
* Invalidate all entries with a specific tag
|
|
228
361
|
*/
|
|
229
362
|
async invalidateByTag(tag) {
|
|
230
363
|
this.cleanupStaleInFlight();
|
|
364
|
+
this.cancelPending((ticket) => ticket.tags.includes(tag) || tag === "cache");
|
|
365
|
+
this.entrySizes = null;
|
|
231
366
|
return this.adapter.deleteJSONByTag(tag);
|
|
232
367
|
}
|
|
233
368
|
/**
|
|
@@ -235,8 +370,11 @@ var CacheService = class {
|
|
|
235
370
|
*/
|
|
236
371
|
async invalidateAll() {
|
|
237
372
|
this.cleanupStaleInFlight();
|
|
373
|
+
this.generation += 1;
|
|
374
|
+
this.cancelPending(() => true);
|
|
238
375
|
const count = await this.adapter.deleteJSONByTag("cache");
|
|
239
376
|
this.accessOrder.clear();
|
|
377
|
+
this.entrySizes = null;
|
|
240
378
|
return count;
|
|
241
379
|
}
|
|
242
380
|
/**
|
|
@@ -328,13 +466,32 @@ async function initOfflineStorage(options) {
|
|
|
328
466
|
initPromise = null;
|
|
329
467
|
}
|
|
330
468
|
}
|
|
469
|
+
async function createInitializedAdapter(preferredAdapters, dbName) {
|
|
470
|
+
let remaining = [...preferredAdapters];
|
|
471
|
+
for (; ; ) {
|
|
472
|
+
const { adapter, adapterName } = await createAdapter({
|
|
473
|
+
preferredAdapters: remaining,
|
|
474
|
+
dbName
|
|
475
|
+
});
|
|
476
|
+
try {
|
|
477
|
+
await adapter.initialize();
|
|
478
|
+
return { adapter, adapterName };
|
|
479
|
+
} catch (error) {
|
|
480
|
+
if (adapterName === "memory") {
|
|
481
|
+
throw error;
|
|
482
|
+
}
|
|
483
|
+
console.warn(`[OfflineStorage] ${adapterName} adapter failed to initialize:`, error);
|
|
484
|
+
await adapter.dispose().catch(() => void 0);
|
|
485
|
+
remaining = remaining.slice(remaining.indexOf(adapterName) + 1);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
331
489
|
async function doInitialize(options) {
|
|
332
490
|
const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
333
|
-
const { adapter, adapterName } = await
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
await adapter.initialize();
|
|
491
|
+
const { adapter, adapterName } = await createInitializedAdapter(
|
|
492
|
+
mergedOptions.preferredAdapters,
|
|
493
|
+
mergedOptions.dbName
|
|
494
|
+
);
|
|
338
495
|
const files = createFileStorageService(adapter);
|
|
339
496
|
const drafts = createDraftStorageService(adapter);
|
|
340
497
|
const draftManager = createDraftManagerService(adapter);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getDraftKey, DRAFT_PREFIX, getFormKeyFromDraftKey } from './chunk-
|
|
2
|
-
import { DEFAULT_EXPIRATIONS } from './chunk-
|
|
1
|
+
import { getDraftKey, DRAFT_PREFIX, getFormKeyFromDraftKey } from './chunk-MA4ZHPQS.js';
|
|
2
|
+
import { DEFAULT_EXPIRATIONS } from './chunk-QS7N4PTV.js';
|
|
3
3
|
|
|
4
4
|
// src/drafts/service.ts
|
|
5
5
|
var DraftStorageService = class {
|
package/dist/drafts/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as SaveDraftOptions, d as LoadDraftOptions, e as LoadDraftResult, C as CreateDraftOptions, b as DraftMetadata, g as StoredDraftV2, L as ListDraftsOptions, c as DraftSyncStatus } from '../types-
|
|
2
|
-
export { j as CURRENT_DRAFT_PREFIX, D as DRAFT_PREFIX, a as DRAFT_V2_PREFIX, f as StoredDraft, h as generateDraftId, k as getCurrentDraftKey, l as getDraftKey, m as getDraftV2Key, n as getFormKeyFromDraftKey, i as getOrCreateDeviceId, o as isDraftV2Key, p as isLegacyDraftKey, q as parseDraftV2Key } from '../types-
|
|
1
|
+
import { S as SaveDraftOptions, d as LoadDraftOptions, e as LoadDraftResult, C as CreateDraftOptions, b as DraftMetadata, g as StoredDraftV2, L as ListDraftsOptions, c as DraftSyncStatus } from '../types-CRMTQnjY.js';
|
|
2
|
+
export { j as CURRENT_DRAFT_PREFIX, D as DRAFT_PREFIX, a as DRAFT_V2_PREFIX, f as StoredDraft, h as generateDraftId, k as getCurrentDraftKey, l as getDraftKey, m as getDraftV2Key, n as getFormKeyFromDraftKey, i as getOrCreateDeviceId, o as isDraftV2Key, p as isLegacyDraftKey, q as parseDraftV2Key } from '../types-CRMTQnjY.js';
|
|
3
3
|
import { S as StorageAdapter } from '../types-DDhQm0hc.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -91,6 +91,20 @@ declare function createDraftStorageService(adapter: StorageAdapter): DraftStorag
|
|
|
91
91
|
* Supports legacy draft migration and backward compatibility.
|
|
92
92
|
*/
|
|
93
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Options for a compare-and-set write
|
|
96
|
+
*/
|
|
97
|
+
interface DraftWriteOptions {
|
|
98
|
+
/** Only write if the stored draft still has this `version` */
|
|
99
|
+
expectedVersion?: number;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Options for updating draft data
|
|
103
|
+
*/
|
|
104
|
+
interface UpdateDraftOptions extends DraftWriteOptions {
|
|
105
|
+
/** Replace the stored data instead of merging into it (default: false) */
|
|
106
|
+
replace?: boolean;
|
|
107
|
+
}
|
|
94
108
|
/**
|
|
95
109
|
* Draft manager service for multi-draft support
|
|
96
110
|
*/
|
|
@@ -127,17 +141,40 @@ declare class DraftManagerService {
|
|
|
127
141
|
*/
|
|
128
142
|
getDraftCount(formKey?: string): Promise<number>;
|
|
129
143
|
/**
|
|
130
|
-
* Update draft data
|
|
144
|
+
* Update draft data. This is a local edit: it bumps `version` and
|
|
145
|
+
* `updatedAt` and extends the draft's expiry by its original lifetime.
|
|
146
|
+
*
|
|
147
|
+
* Returns false if the draft does not exist (or has expired), or if
|
|
148
|
+
* `options.expectedVersion` no longer matches.
|
|
149
|
+
*/
|
|
150
|
+
updateDraft<T = Record<string, unknown>>(draftId: string, data: Partial<T>, fileRefs?: Record<string, string[]>, options?: UpdateDraftOptions): Promise<boolean>;
|
|
151
|
+
private writeDraftData;
|
|
152
|
+
/**
|
|
153
|
+
* Update draft metadata only.
|
|
154
|
+
*
|
|
155
|
+
* Sync bookkeeping (`syncStatus`, `remoteId`, `lastSyncedAt`, `syncedVersion`)
|
|
156
|
+
* is not a local edit and leaves `version` and `updatedAt` alone. Renaming
|
|
157
|
+
* is: it bumps `version` and marks a synced draft `pending-upload`, unless
|
|
158
|
+
* the same update sets `syncStatus`. Setting `syncStatus: 'synced'` without
|
|
159
|
+
* a `syncedVersion` records the resulting version as synced.
|
|
160
|
+
*
|
|
161
|
+
* Returns false if the draft does not exist, or if
|
|
162
|
+
* `options.expectedVersion` no longer matches.
|
|
131
163
|
*/
|
|
132
|
-
|
|
164
|
+
updateDraftMetadata(draftId: string, updates: Partial<Pick<DraftMetadata, 'name' | 'syncStatus' | 'remoteId' | 'lastSyncedAt' | 'syncedVersion'>>, options?: DraftWriteOptions): Promise<boolean>;
|
|
165
|
+
private writeDraftMetadata;
|
|
133
166
|
/**
|
|
134
|
-
*
|
|
167
|
+
* Delete a draft.
|
|
168
|
+
*
|
|
169
|
+
* Returns false if the draft does not exist, or if
|
|
170
|
+
* `options.expectedVersion` no longer matches (it was edited meanwhile).
|
|
135
171
|
*/
|
|
136
|
-
|
|
172
|
+
deleteDraft(draftId: string, deleteFiles?: boolean, options?: DraftWriteOptions): Promise<boolean>;
|
|
173
|
+
private removeDraft;
|
|
137
174
|
/**
|
|
138
|
-
*
|
|
175
|
+
* File IDs referenced by any stored draft (multi-draft and legacy)
|
|
139
176
|
*/
|
|
140
|
-
|
|
177
|
+
private referencedFileIds;
|
|
141
178
|
/**
|
|
142
179
|
* Delete all drafts for a form key
|
|
143
180
|
*/
|
|
@@ -200,4 +237,4 @@ declare class DraftManagerService {
|
|
|
200
237
|
*/
|
|
201
238
|
declare function createDraftManagerService(adapter: StorageAdapter): DraftManagerService;
|
|
202
239
|
|
|
203
|
-
export { CreateDraftOptions, DraftManagerService, DraftMetadata, DraftStorageService, DraftSyncStatus, type FileExistsValidator, ListDraftsOptions, LoadDraftOptions, LoadDraftResult, SaveDraftOptions, StoredDraftV2, createDraftManagerService, createDraftStorageService };
|
|
240
|
+
export { CreateDraftOptions, DraftManagerService, DraftMetadata, DraftStorageService, DraftSyncStatus, type DraftWriteOptions, type FileExistsValidator, ListDraftsOptions, LoadDraftOptions, LoadDraftResult, SaveDraftOptions, StoredDraftV2, type UpdateDraftOptions, createDraftManagerService, createDraftStorageService };
|
package/dist/drafts/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { DraftStorageService, createDraftStorageService } from '../chunk-
|
|
2
|
-
export { CURRENT_DRAFT_PREFIX, DRAFT_PREFIX, DRAFT_V2_PREFIX, DraftManagerService, createDraftManagerService, generateDraftId, getCurrentDraftKey, getDraftKey, getDraftV2Key, getFormKeyFromDraftKey, getOrCreateDeviceId, isDraftV2Key, isLegacyDraftKey, parseDraftV2Key } from '../chunk-
|
|
3
|
-
import '../chunk-
|
|
1
|
+
export { DraftStorageService, createDraftStorageService } from '../chunk-UL7MZBSF.js';
|
|
2
|
+
export { CURRENT_DRAFT_PREFIX, DRAFT_PREFIX, DRAFT_V2_PREFIX, DraftManagerService, createDraftManagerService, generateDraftId, getCurrentDraftKey, getDraftKey, getDraftV2Key, getFormKeyFromDraftKey, getOrCreateDeviceId, isDraftV2Key, isLegacyDraftKey, parseDraftV2Key } from '../chunk-MA4ZHPQS.js';
|
|
3
|
+
import '../chunk-QS7N4PTV.js';
|
package/dist/files/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { FileStorageService, blobMetadataToStoredFile, createFileStorageService, storedFileToBlobMetadata } from '../chunk-
|
|
2
|
-
import '../chunk-
|
|
1
|
+
export { FileStorageService, blobMetadataToStoredFile, createFileStorageService, storedFileToBlobMetadata } from '../chunk-R2IP2AA5.js';
|
|
2
|
+
import '../chunk-QS7N4PTV.js';
|
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,12 @@ export { B as BlobMetadata, c as BlobWithMetadata, C as CleanupResult, J as JSON
|
|
|
3
3
|
import { FileStorageService } from './files/index.js';
|
|
4
4
|
export { DeleteFilesResult, FileConstraints, FileValidationError, GetFilesOptions, PersistedFile, StoreFileOptions, StoredFile, createFileStorageService } from './files/index.js';
|
|
5
5
|
import { DraftStorageService, DraftManagerService } from './drafts/index.js';
|
|
6
|
-
export { createDraftManagerService, createDraftStorageService } from './drafts/index.js';
|
|
6
|
+
export { DraftWriteOptions, UpdateDraftOptions, createDraftManagerService, createDraftStorageService } from './drafts/index.js';
|
|
7
7
|
import { CacheService } from './cache/index.js';
|
|
8
8
|
export { CACHE_PREFIX, CacheEntry, CacheGetResult, CacheOptions, CacheStats, GetOrSetOptions, createCacheService, invalidateAllCache, invalidateCacheByTag } from './cache/index.js';
|
|
9
9
|
export { CreateAdapterOptions, CreateAdapterResult, createAdapter, detectAvailableAdapters } from './storage/index.js';
|
|
10
|
-
export { C as CreateDraftOptions, D as DRAFT_PREFIX, a as DRAFT_V2_PREFIX, b as DraftMetadata, c as DraftSyncStatus, L as ListDraftsOptions, d as LoadDraftOptions, e as LoadDraftResult, S as SaveDraftOptions, f as StoredDraft, g as StoredDraftV2, h as generateDraftId, i as getOrCreateDeviceId } from './types-
|
|
11
|
-
export { ConflictResolution, ConnectivityMonitor, ConnectivityState, DraftConflict, DraftPayload, PendingSyncOperation, RemoteDraft, SyncAdapter, SyncConfig, SyncEvent, SyncEventListener, SyncEventType, SyncQueue, SyncQueueState, SyncRequest, SyncResponse, SyncResult, SyncService, applyResolution, compareDrafts, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict } from './sync/index.js';
|
|
10
|
+
export { C as CreateDraftOptions, D as DRAFT_PREFIX, a as DRAFT_V2_PREFIX, b as DraftMetadata, c as DraftSyncStatus, L as ListDraftsOptions, d as LoadDraftOptions, e as LoadDraftResult, S as SaveDraftOptions, f as StoredDraft, g as StoredDraftV2, h as generateDraftId, i as getOrCreateDeviceId } from './types-CRMTQnjY.js';
|
|
11
|
+
export { ConflictResolution, ConnectivityMonitor, ConnectivityState, DraftConflict, DraftPayload, PendingSyncOperation, RemoteDraft, SyncAdapter, SyncAdapterCallOptions, SyncConfig, SyncEvent, SyncEventListener, SyncEventType, SyncFailure, SyncQueue, SyncQueueState, SyncRequest, SyncResponse, SyncResult, SyncService, applyResolution, compareDrafts, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict } from './sync/index.js';
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Error types for the offline storage package
|
|
@@ -121,7 +121,10 @@ declare function isExpired(expiresAt: number | null | undefined): boolean;
|
|
|
121
121
|
declare const DEFAULT_EXPIRATIONS: {
|
|
122
122
|
/** Form draft expiration: 7 days */
|
|
123
123
|
readonly DRAFT: number;
|
|
124
|
-
/**
|
|
124
|
+
/**
|
|
125
|
+
* File attachment expiration: 7 days, the same as drafts, so a restored
|
|
126
|
+
* draft still has its attachments
|
|
127
|
+
*/
|
|
125
128
|
readonly FILE: number;
|
|
126
129
|
/** Cache entry expiration: 1 hour */
|
|
127
130
|
readonly CACHE: number;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { CACHE_PREFIX, CacheService, createCacheService, detectStorageAdapter, getOfflineStorage, getOfflineStorageOrNull, initOfflineStorage, invalidateAllCache, invalidateCacheByTag, isOfflineStorageInitialized, resetOfflineStorage } from './chunk-
|
|
2
|
-
export { OfflineStorageError, createAdapter, detectAvailableAdapters, detectBrowser, isBrowser, isSSR, isWorker, supportsIndexedDB, supportsLocalStorage, supportsOPFS } from './chunk-
|
|
3
|
-
export { FileStorageService, createFileStorageService } from './chunk-
|
|
4
|
-
export { DraftStorageService, createDraftStorageService } from './chunk-
|
|
5
|
-
export { ConnectivityMonitor, SyncQueue, SyncService, applyResolution, compareDrafts, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict } from './chunk-
|
|
6
|
-
export { DRAFT_PREFIX, DRAFT_V2_PREFIX, DraftManagerService, createDraftManagerService, generateDraftId, getOrCreateDeviceId } from './chunk-
|
|
7
|
-
export { CleanupScheduler, DEFAULT_EXPIRATIONS, calculateExpiration, isExpired } from './chunk-
|
|
1
|
+
export { CACHE_PREFIX, CacheService, createCacheService, detectStorageAdapter, getOfflineStorage, getOfflineStorageOrNull, initOfflineStorage, invalidateAllCache, invalidateCacheByTag, isOfflineStorageInitialized, resetOfflineStorage } from './chunk-SLDAYKRY.js';
|
|
2
|
+
export { OfflineStorageError, createAdapter, detectAvailableAdapters, detectBrowser, isBrowser, isSSR, isWorker, supportsIndexedDB, supportsLocalStorage, supportsOPFS } from './chunk-4KL3GDYP.js';
|
|
3
|
+
export { FileStorageService, createFileStorageService } from './chunk-R2IP2AA5.js';
|
|
4
|
+
export { DraftStorageService, createDraftStorageService } from './chunk-UL7MZBSF.js';
|
|
5
|
+
export { ConnectivityMonitor, SyncQueue, SyncService, applyResolution, compareDrafts, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict } from './chunk-NCAGT2LL.js';
|
|
6
|
+
export { DRAFT_PREFIX, DRAFT_V2_PREFIX, DraftManagerService, createDraftManagerService, generateDraftId, getOrCreateDeviceId } from './chunk-MA4ZHPQS.js';
|
|
7
|
+
export { CleanupScheduler, DEFAULT_EXPIRATIONS, calculateExpiration, isExpired } from './chunk-QS7N4PTV.js';
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -53,12 +53,18 @@ declare function createMemoryAdapter(options?: StorageAdapterOptions): MemoryAda
|
|
|
53
53
|
declare class IndexedDBAdapter implements StorageAdapter {
|
|
54
54
|
readonly name: "indexeddb";
|
|
55
55
|
private db;
|
|
56
|
+
private reopening;
|
|
57
|
+
private initialized;
|
|
56
58
|
private readonly dbName;
|
|
57
59
|
constructor(options?: StorageAdapterOptions);
|
|
58
60
|
/**
|
|
59
|
-
* Get the database,
|
|
61
|
+
* Get the database, reopening it if the connection was closed
|
|
60
62
|
*/
|
|
61
63
|
private getDB;
|
|
64
|
+
/**
|
|
65
|
+
* Open the database and keep the connection only while it is usable
|
|
66
|
+
*/
|
|
67
|
+
private open;
|
|
62
68
|
storeBlob(key: string, blob: Blob, metadata?: BlobMetadata): Promise<void>;
|
|
63
69
|
getBlob(key: string): Promise<BlobWithMetadata | null>;
|
|
64
70
|
deleteBlob(key: string): Promise<void>;
|
package/dist/storage/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { IndexedDBAdapter, MemoryAdapter, OPFSAdapter, createAdapter, createIndexedDBAdapter, createMemoryAdapter, createOPFSAdapter, detectAvailableAdapters } from '../chunk-
|
|
2
|
-
import '../chunk-
|
|
1
|
+
export { IndexedDBAdapter, MemoryAdapter, OPFSAdapter, createAdapter, createIndexedDBAdapter, createMemoryAdapter, createOPFSAdapter, detectAvailableAdapters } from '../chunk-4KL3GDYP.js';
|
|
2
|
+
import '../chunk-QS7N4PTV.js';
|