@classic-homes/offline-core 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cache/index.d.ts +17 -1
- package/dist/cache/index.js +6 -6
- package/dist/{chunk-XHMX3BYT.js → chunk-34PR6T4B.js} +231 -59
- package/dist/{chunk-IDRYBB6Z.js → chunk-4KL3GDYP.js} +204 -80
- package/dist/{chunk-ZDXEYCDZ.js → chunk-JPXEFKZO.js} +99 -28
- package/dist/{chunk-4QWZX3LM.js → chunk-MA4ZHPQS.js} +110 -27
- package/dist/{chunk-ZC7F2HDV.js → chunk-QS7N4PTV.js} +5 -2
- package/dist/{chunk-U27TKOYZ.js → chunk-R2IP2AA5.js} +2 -2
- package/dist/{chunk-IIG4QLLN.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 +11 -1
- package/dist/storage/index.js +2 -2
- package/dist/sync/index.d.ts +64 -7
- package/dist/sync/index.js +3 -3
- package/dist/{types-Ho1JiLQR.d.ts → types-CRMTQnjY.d.ts} +6 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEFAULT_EXPIRATIONS } from './chunk-
|
|
1
|
+
import { DEFAULT_EXPIRATIONS } from './chunk-QS7N4PTV.js';
|
|
2
2
|
|
|
3
3
|
// src/drafts/types.ts
|
|
4
4
|
var DRAFT_PREFIX = "draft:";
|
|
@@ -21,7 +21,7 @@ function parseDraftV2Key(key) {
|
|
|
21
21
|
return null;
|
|
22
22
|
}
|
|
23
23
|
const rest = key.slice(DRAFT_V2_PREFIX.length);
|
|
24
|
-
const colonIndex = rest.
|
|
24
|
+
const colonIndex = rest.lastIndexOf(":");
|
|
25
25
|
if (colonIndex === -1) {
|
|
26
26
|
return null;
|
|
27
27
|
}
|
|
@@ -40,17 +40,23 @@ function generateDraftId() {
|
|
|
40
40
|
return v.toString(16);
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
|
+
var sessionDeviceId;
|
|
43
44
|
function getOrCreateDeviceId() {
|
|
44
45
|
const DEVICE_ID_KEY = "classic-offline-device-id";
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
try {
|
|
47
|
+
if (typeof localStorage === "undefined") {
|
|
48
|
+
return generateDraftId();
|
|
49
|
+
}
|
|
50
|
+
let deviceId = localStorage.getItem(DEVICE_ID_KEY);
|
|
51
|
+
if (!deviceId) {
|
|
52
|
+
deviceId = generateDraftId();
|
|
53
|
+
localStorage.setItem(DEVICE_ID_KEY, deviceId);
|
|
54
|
+
}
|
|
55
|
+
return deviceId;
|
|
56
|
+
} catch {
|
|
57
|
+
sessionDeviceId ?? (sessionDeviceId = generateDraftId());
|
|
58
|
+
return sessionDeviceId;
|
|
52
59
|
}
|
|
53
|
-
return deviceId;
|
|
54
60
|
}
|
|
55
61
|
function isLegacyDraftKey(key) {
|
|
56
62
|
return key.startsWith(DRAFT_PREFIX) && !key.startsWith(DRAFT_V2_PREFIX);
|
|
@@ -60,6 +66,24 @@ function isDraftV2Key(key) {
|
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
// src/drafts/manager.ts
|
|
69
|
+
var draftLocks = /* @__PURE__ */ new Map();
|
|
70
|
+
async function withDraftLock(draftId, fn) {
|
|
71
|
+
const previous = draftLocks.get(draftId) ?? Promise.resolve();
|
|
72
|
+
const run = previous.then(fn, fn);
|
|
73
|
+
const tail = run.catch(() => void 0);
|
|
74
|
+
draftLocks.set(draftId, tail);
|
|
75
|
+
try {
|
|
76
|
+
return await run;
|
|
77
|
+
} finally {
|
|
78
|
+
if (draftLocks.get(draftId) === tail) {
|
|
79
|
+
draftLocks.delete(draftId);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function draftLifetime(metadata) {
|
|
84
|
+
const lifetime = metadata.expiresAt - metadata.updatedAt;
|
|
85
|
+
return lifetime > 0 ? lifetime : DEFAULT_EXPIRATIONS.DRAFT;
|
|
86
|
+
}
|
|
63
87
|
var DraftManagerService = class {
|
|
64
88
|
constructor(adapter) {
|
|
65
89
|
this.adapter = adapter;
|
|
@@ -198,33 +222,44 @@ var DraftManagerService = class {
|
|
|
198
222
|
// Update Operations
|
|
199
223
|
// ============================================
|
|
200
224
|
/**
|
|
201
|
-
* Update draft data
|
|
225
|
+
* Update draft data. This is a local edit: it bumps `version` and
|
|
226
|
+
* `updatedAt` and extends the draft's expiry by its original lifetime.
|
|
227
|
+
*
|
|
228
|
+
* Returns false if the draft does not exist (or has expired), or if
|
|
229
|
+
* `options.expectedVersion` no longer matches.
|
|
202
230
|
*/
|
|
203
|
-
async updateDraft(draftId, data, fileRefs) {
|
|
231
|
+
async updateDraft(draftId, data, fileRefs, options = {}) {
|
|
232
|
+
return withDraftLock(draftId, () => this.writeDraftData(draftId, data, fileRefs, options));
|
|
233
|
+
}
|
|
234
|
+
async writeDraftData(draftId, data, fileRefs, options) {
|
|
204
235
|
const draft = await this.getDraft(draftId);
|
|
205
236
|
if (!draft) {
|
|
206
237
|
return false;
|
|
207
238
|
}
|
|
239
|
+
if (options.expectedVersion !== void 0 && draft.metadata.version !== options.expectedVersion) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
208
242
|
const now = Date.now();
|
|
209
243
|
const updatedFileRefs = fileRefs ?? draft.fileRefs;
|
|
210
244
|
const fileCount = Object.values(updatedFileRefs).flat().length;
|
|
245
|
+
const lifetime = draftLifetime(draft.metadata);
|
|
211
246
|
const updatedDraft = {
|
|
212
247
|
metadata: {
|
|
213
248
|
...draft.metadata,
|
|
214
249
|
updatedAt: now,
|
|
250
|
+
expiresAt: now + lifetime,
|
|
215
251
|
version: draft.metadata.version + 1,
|
|
216
252
|
deviceId: this.deviceId,
|
|
217
253
|
fileCount,
|
|
218
254
|
// Mark as pending upload if was synced
|
|
219
255
|
syncStatus: draft.metadata.syncStatus === "synced" ? "pending-upload" : draft.metadata.syncStatus
|
|
220
256
|
},
|
|
221
|
-
data: { ...draft.data, ...data },
|
|
257
|
+
data: options.replace ? { ...data } : { ...draft.data, ...data },
|
|
222
258
|
fileRefs: updatedFileRefs
|
|
223
259
|
};
|
|
224
260
|
const key = getDraftV2Key(draft.metadata.formKey, draftId);
|
|
225
|
-
const ttl = updatedDraft.metadata.expiresAt - now;
|
|
226
261
|
await this.adapter.storeJSON(key, updatedDraft, {
|
|
227
|
-
ttl:
|
|
262
|
+
ttl: lifetime,
|
|
228
263
|
tags: [
|
|
229
264
|
"draft",
|
|
230
265
|
"draft-v2",
|
|
@@ -235,23 +270,43 @@ var DraftManagerService = class {
|
|
|
235
270
|
return true;
|
|
236
271
|
}
|
|
237
272
|
/**
|
|
238
|
-
* Update draft metadata only
|
|
273
|
+
* Update draft metadata only.
|
|
274
|
+
*
|
|
275
|
+
* Sync bookkeeping (`syncStatus`, `remoteId`, `lastSyncedAt`, `syncedVersion`)
|
|
276
|
+
* is not a local edit and leaves `version` and `updatedAt` alone. Renaming
|
|
277
|
+
* is: it bumps `version` and marks a synced draft `pending-upload`, unless
|
|
278
|
+
* the same update sets `syncStatus`. Setting `syncStatus: 'synced'` without
|
|
279
|
+
* a `syncedVersion` records the resulting version as synced.
|
|
280
|
+
*
|
|
281
|
+
* Returns false if the draft does not exist, or if
|
|
282
|
+
* `options.expectedVersion` no longer matches.
|
|
239
283
|
*/
|
|
240
|
-
async updateDraftMetadata(draftId, updates) {
|
|
284
|
+
async updateDraftMetadata(draftId, updates, options = {}) {
|
|
285
|
+
return withDraftLock(draftId, () => this.writeDraftMetadata(draftId, updates, options));
|
|
286
|
+
}
|
|
287
|
+
async writeDraftMetadata(draftId, updates, options) {
|
|
241
288
|
const draft = await this.getDraft(draftId);
|
|
242
289
|
if (!draft) {
|
|
243
290
|
return false;
|
|
244
291
|
}
|
|
292
|
+
if (options.expectedVersion !== void 0 && draft.metadata.version !== options.expectedVersion) {
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
245
295
|
const now = Date.now();
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
296
|
+
const renamed = updates.name !== void 0 && updates.name !== draft.metadata.name;
|
|
297
|
+
const metadata = { ...draft.metadata, ...updates };
|
|
298
|
+
if (renamed) {
|
|
299
|
+
metadata.version = draft.metadata.version + 1;
|
|
300
|
+
metadata.updatedAt = now;
|
|
301
|
+
metadata.expiresAt = now + draftLifetime(draft.metadata);
|
|
302
|
+
if (updates.syncStatus === void 0 && draft.metadata.syncStatus === "synced") {
|
|
303
|
+
metadata.syncStatus = "pending-upload";
|
|
253
304
|
}
|
|
254
|
-
}
|
|
305
|
+
}
|
|
306
|
+
if (updates.syncStatus === "synced" && updates.syncedVersion === void 0) {
|
|
307
|
+
metadata.syncedVersion = metadata.version;
|
|
308
|
+
}
|
|
309
|
+
const updatedDraft = { ...draft, metadata };
|
|
255
310
|
const key = getDraftV2Key(draft.metadata.formKey, draftId);
|
|
256
311
|
const ttl = updatedDraft.metadata.expiresAt - now;
|
|
257
312
|
await this.adapter.storeJSON(key, updatedDraft, {
|
|
@@ -269,18 +324,29 @@ var DraftManagerService = class {
|
|
|
269
324
|
// Delete Operations
|
|
270
325
|
// ============================================
|
|
271
326
|
/**
|
|
272
|
-
* Delete a draft
|
|
327
|
+
* Delete a draft.
|
|
328
|
+
*
|
|
329
|
+
* Returns false if the draft does not exist, or if
|
|
330
|
+
* `options.expectedVersion` no longer matches (it was edited meanwhile).
|
|
273
331
|
*/
|
|
274
|
-
async deleteDraft(draftId, deleteFiles = false) {
|
|
332
|
+
async deleteDraft(draftId, deleteFiles = false, options = {}) {
|
|
333
|
+
return withDraftLock(draftId, () => this.removeDraft(draftId, deleteFiles, options));
|
|
334
|
+
}
|
|
335
|
+
async removeDraft(draftId, deleteFiles, options) {
|
|
275
336
|
const draft = await this.getDraft(draftId);
|
|
276
337
|
if (!draft) {
|
|
277
338
|
return false;
|
|
278
339
|
}
|
|
340
|
+
if (options.expectedVersion !== void 0 && draft.metadata.version !== options.expectedVersion) {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
279
343
|
const key = getDraftV2Key(draft.metadata.formKey, draftId);
|
|
280
344
|
await this.adapter.deleteJSON(key);
|
|
281
345
|
if (deleteFiles) {
|
|
282
346
|
const fileIds = Object.values(draft.fileRefs).flat();
|
|
347
|
+
const shared = fileIds.length > 0 ? await this.referencedFileIds() : /* @__PURE__ */ new Set();
|
|
283
348
|
for (const fileId of fileIds) {
|
|
349
|
+
if (shared.has(fileId)) continue;
|
|
284
350
|
try {
|
|
285
351
|
await this.adapter.deleteBlob(fileId);
|
|
286
352
|
} catch {
|
|
@@ -289,6 +355,23 @@ var DraftManagerService = class {
|
|
|
289
355
|
}
|
|
290
356
|
return true;
|
|
291
357
|
}
|
|
358
|
+
/**
|
|
359
|
+
* File IDs referenced by any stored draft (multi-draft and legacy)
|
|
360
|
+
*/
|
|
361
|
+
async referencedFileIds() {
|
|
362
|
+
const ids = /* @__PURE__ */ new Set();
|
|
363
|
+
const keys = [
|
|
364
|
+
...await this.adapter.listJSON(DRAFT_V2_PREFIX),
|
|
365
|
+
...(await this.adapter.listJSON(DRAFT_PREFIX)).filter(isLegacyDraftKey)
|
|
366
|
+
];
|
|
367
|
+
for (const key of keys) {
|
|
368
|
+
const entry = await this.adapter.getJSON(key);
|
|
369
|
+
for (const id of Object.values(entry?.data?.fileRefs ?? {}).flat()) {
|
|
370
|
+
ids.add(id);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return ids;
|
|
374
|
+
}
|
|
292
375
|
/**
|
|
293
376
|
* Delete all drafts for a form key
|
|
294
377
|
*/
|
|
@@ -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,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, 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-JPXEFKZO.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-34PR6T4B.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>;
|
|
@@ -124,6 +130,10 @@ declare class OPFSAdapter implements StorageAdapter {
|
|
|
124
130
|
* Decode filename back to key
|
|
125
131
|
*/
|
|
126
132
|
private decodeKey;
|
|
133
|
+
/**
|
|
134
|
+
* Remove a blob file and its metadata file, ignoring missing ones
|
|
135
|
+
*/
|
|
136
|
+
private removeFiles;
|
|
127
137
|
storeBlob(key: string, blob: Blob, metadata?: BlobMetadata): Promise<void>;
|
|
128
138
|
getBlob(key: string): Promise<BlobWithMetadata | null>;
|
|
129
139
|
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';
|
package/dist/sync/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { g as StoredDraftV2 } from '../types-
|
|
1
|
+
import { g as StoredDraftV2 } from '../types-CRMTQnjY.js';
|
|
2
2
|
import { S as StorageAdapter } from '../types-DDhQm0hc.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -48,6 +48,12 @@ interface DraftPayload {
|
|
|
48
48
|
updatedAt: number;
|
|
49
49
|
/** Device ID */
|
|
50
50
|
deviceId: string;
|
|
51
|
+
/**
|
|
52
|
+
* Local draft ID. Stable across retries of the same draft, so a server can
|
|
53
|
+
* use it as an idempotency key on create: a retried create whose first
|
|
54
|
+
* response was lost should return the existing draft, not a duplicate.
|
|
55
|
+
*/
|
|
56
|
+
localId?: string;
|
|
51
57
|
}
|
|
52
58
|
/**
|
|
53
59
|
* Draft data returned from the server
|
|
@@ -118,7 +124,12 @@ interface SyncResponse {
|
|
|
118
124
|
*/
|
|
119
125
|
interface SyncAdapter {
|
|
120
126
|
/**
|
|
121
|
-
* Fetch all drafts for the current user
|
|
127
|
+
* Fetch all drafts for the current user.
|
|
128
|
+
*
|
|
129
|
+
* Must return the **complete** list, not a page or a partial result: the
|
|
130
|
+
* sync service treats a previously synced draft missing from this list as
|
|
131
|
+
* deleted on the server and deletes the local copy (unless it was edited
|
|
132
|
+
* since the sync started). Throw instead of returning a partial list.
|
|
122
133
|
*/
|
|
123
134
|
fetchDrafts(): Promise<RemoteDraft[]>;
|
|
124
135
|
/**
|
|
@@ -163,11 +174,24 @@ interface SyncConfig {
|
|
|
163
174
|
/** Callback for sync errors */
|
|
164
175
|
onSyncError?: (error: Error) => void;
|
|
165
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* A single draft that failed to sync. The rest of the sync still runs.
|
|
179
|
+
*/
|
|
180
|
+
interface SyncFailure {
|
|
181
|
+
/** What was being done */
|
|
182
|
+
operation: 'upload' | 'download' | 'delete';
|
|
183
|
+
/** Local draft ID (absent for a failed download of a new remote draft) */
|
|
184
|
+
draftId?: string;
|
|
185
|
+
/** Remote draft ID, if known */
|
|
186
|
+
remoteId?: string;
|
|
187
|
+
/** Error message */
|
|
188
|
+
error: string;
|
|
189
|
+
}
|
|
166
190
|
/**
|
|
167
191
|
* Result of a sync operation
|
|
168
192
|
*/
|
|
169
193
|
interface SyncResult {
|
|
170
|
-
/** Whether sync was successful */
|
|
194
|
+
/** Whether sync was successful (false if any draft failed; see `failures`) */
|
|
171
195
|
success: boolean;
|
|
172
196
|
/** Number of drafts uploaded */
|
|
173
197
|
uploaded: number;
|
|
@@ -181,6 +205,11 @@ interface SyncResult {
|
|
|
181
205
|
conflicts: DraftConflict[];
|
|
182
206
|
/** Error message if failed */
|
|
183
207
|
error?: string;
|
|
208
|
+
/**
|
|
209
|
+
* Drafts that failed while the rest synced. A failed upload is retried on
|
|
210
|
+
* later syncs with exponential backoff.
|
|
211
|
+
*/
|
|
212
|
+
failures?: SyncFailure[];
|
|
184
213
|
/** Timestamp of sync */
|
|
185
214
|
timestamp: number;
|
|
186
215
|
/** Duration in milliseconds */
|
|
@@ -503,6 +532,9 @@ declare class SyncService {
|
|
|
503
532
|
private conflicts;
|
|
504
533
|
private eventListeners;
|
|
505
534
|
private connectivityUnsubscribe;
|
|
535
|
+
private pendingDraftCount;
|
|
536
|
+
/** Upload backoff per local draft ID */
|
|
537
|
+
private uploadFailures;
|
|
506
538
|
private onSyncComplete?;
|
|
507
539
|
private onConflictDetected?;
|
|
508
540
|
private onOnlineStatusChange?;
|
|
@@ -525,9 +557,15 @@ declare class SyncService {
|
|
|
525
557
|
*/
|
|
526
558
|
get lastSyncAt(): number | null;
|
|
527
559
|
/**
|
|
528
|
-
*
|
|
560
|
+
* Number of local drafts waiting to be uploaded ('local-only' or
|
|
561
|
+
* 'pending-upload'), as of the last sync, `initialize()` or
|
|
562
|
+
* `refreshPendingCount()`
|
|
529
563
|
*/
|
|
530
564
|
get pendingCount(): number;
|
|
565
|
+
/**
|
|
566
|
+
* Recount the local drafts waiting to be uploaded
|
|
567
|
+
*/
|
|
568
|
+
refreshPendingCount(): Promise<number>;
|
|
531
569
|
/**
|
|
532
570
|
* Get current conflicts
|
|
533
571
|
*/
|
|
@@ -540,10 +578,12 @@ declare class SyncService {
|
|
|
540
578
|
* Sync all drafts
|
|
541
579
|
*/
|
|
542
580
|
syncAll(): Promise<SyncResult>;
|
|
581
|
+
private runSyncAll;
|
|
543
582
|
/**
|
|
544
583
|
* Sync a specific draft
|
|
545
584
|
*/
|
|
546
585
|
syncDraft(draftId: string): Promise<SyncResult>;
|
|
586
|
+
private runSyncDraft;
|
|
547
587
|
/**
|
|
548
588
|
* Force push a draft (overwrite remote)
|
|
549
589
|
*/
|
|
@@ -581,15 +621,32 @@ declare class SyncService {
|
|
|
581
621
|
*/
|
|
582
622
|
dispose(): void;
|
|
583
623
|
/**
|
|
584
|
-
* Perform the actual sync
|
|
624
|
+
* Perform the actual sync. One draft failing does not stop the rest: each
|
|
625
|
+
* failure is recorded in `failures` and the sync carries on.
|
|
585
626
|
*/
|
|
586
627
|
private performSync;
|
|
628
|
+
/**
|
|
629
|
+
* Whether a failing draft is still waiting out its retry delay
|
|
630
|
+
*/
|
|
631
|
+
private isBackedOff;
|
|
632
|
+
/**
|
|
633
|
+
* Record a failed upload and schedule its next attempt (exponential backoff)
|
|
634
|
+
*/
|
|
635
|
+
private recordUploadFailure;
|
|
587
636
|
/**
|
|
588
637
|
* Sync a single draft
|
|
589
638
|
*/
|
|
590
639
|
private syncSingleDraft;
|
|
591
640
|
/**
|
|
592
|
-
*
|
|
641
|
+
* Mark an uploaded draft as synced, but only if it was not edited while the
|
|
642
|
+
* upload was in flight (compare-and-set on `version`). An edited draft stays
|
|
643
|
+
* pending so the edit is uploaded next time; its remote ID is still
|
|
644
|
+
* recorded so that upload updates the remote copy instead of creating another.
|
|
645
|
+
*/
|
|
646
|
+
private markUploaded;
|
|
647
|
+
/**
|
|
648
|
+
* Download a remote draft. Into an existing local draft only if that draft
|
|
649
|
+
* is unchanged since it was read; returns false when a local edit won.
|
|
593
650
|
*/
|
|
594
651
|
private downloadRemoteDraft;
|
|
595
652
|
/**
|
|
@@ -618,4 +675,4 @@ declare class SyncService {
|
|
|
618
675
|
*/
|
|
619
676
|
declare function createSyncService(adapter: StorageAdapter, config: SyncConfig): SyncService;
|
|
620
677
|
|
|
621
|
-
export { type ConflictResolution, ConnectivityMonitor, type ConnectivityState, type DraftConflict, type DraftPayload, type PendingSyncOperation, type RemoteDraft, type SyncAdapter, type SyncConfig, SyncConflictError, type SyncEvent, type SyncEventListener, type SyncEventType, SyncQueue, type SyncQueueState, type SyncRequest, type SyncResponse, type SyncResult, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge };
|
|
678
|
+
export { type ConflictResolution, ConnectivityMonitor, type ConnectivityState, type DraftConflict, type DraftPayload, type PendingSyncOperation, type RemoteDraft, type SyncAdapter, type SyncConfig, SyncConflictError, type SyncEvent, type SyncEventListener, type SyncEventType, type SyncFailure, SyncQueue, type SyncQueueState, type SyncRequest, type SyncResponse, type SyncResult, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge };
|
package/dist/sync/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { ConnectivityMonitor, SyncConflictError, SyncQueue, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge } from '../chunk-
|
|
2
|
-
import '../chunk-
|
|
3
|
-
import '../chunk-
|
|
1
|
+
export { ConnectivityMonitor, SyncConflictError, SyncQueue, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge } from '../chunk-34PR6T4B.js';
|
|
2
|
+
import '../chunk-MA4ZHPQS.js';
|
|
3
|
+
import '../chunk-QS7N4PTV.js';
|
|
@@ -40,8 +40,13 @@ interface DraftMetadata {
|
|
|
40
40
|
syncStatus: DraftSyncStatus;
|
|
41
41
|
/** Server-side ID (null if never synced) */
|
|
42
42
|
remoteId: string | null;
|
|
43
|
-
/** Version number for conflict detection */
|
|
43
|
+
/** Version number for conflict detection. Bumped by local edits only, not by metadata-only updates */
|
|
44
44
|
version: number;
|
|
45
|
+
/**
|
|
46
|
+
* The `version` last confirmed in sync with the server. A draft whose
|
|
47
|
+
* `version` differs has local edits that have not been uploaded.
|
|
48
|
+
*/
|
|
49
|
+
syncedVersion?: number;
|
|
45
50
|
/** Timestamp of last successful sync */
|
|
46
51
|
lastSyncedAt: number | null;
|
|
47
52
|
/** Device ID where draft was created/modified */
|