@keepkit/core 0.28.9 → 0.28.10

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/dist/schema.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { g as KeepSchema, c as KeepItem } from './types-GTCMFt5T.js';
1
+ import { X as KeepSchema, K as KeepItem } from './types-DjXvE9Zb.js';
2
2
 
3
3
  declare class KeepSchemaValidationError extends Error {
4
4
  readonly cause?: unknown;
package/dist/storage.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { a as StorageAdapter, R as RemoteSyncDriver, d as SyncQueueAdapter, x as KeepConflictResolver, S as SyncScope, e as KeepSyncQueueOperation, p as SyncCapableStorageAdapter, K as KeepCollectionDefinition, b as KeepCollectionMembership, M as KeepSyncState, c as KeepItem, L as KeepSyncResolution } from './types-GTCMFt5T.js';
2
- import { K as KeepScope } from './scope-1bxGd68Y.js';
3
- export { S as ScopedStorageAdapter, a as ScopedSyncQueueAdapter, c as createScopedStorageAdapter } from './scope-1bxGd68Y.js';
1
+ import { S as StorageAdapter, R as RemoteSyncDriver, ad as SyncQueueAdapter, L as KeepConflictResolver, k as SyncScope, a6 as KeepSyncQueueOperation, l as SyncCapableStorageAdapter, a2 as KeepStorageRecovery, n as KeepCollectionDefinition, d as KeepCollectionMembership, a8 as KeepSyncState, K as KeepItem, a7 as KeepSyncResolution, e as KeepScope, F as FallbackRecoveryJournal } from './types-DjXvE9Zb.js';
2
+ export { p as FallbackRecoveryJournalState, q as FallbackRecoveryJournalV2, r as FallbackRecoveryOperation, s as FallbackRecoverySnapshot, T as KeepRecoveryInspection, U as KeepRecoveryResult, V as KeepRecoveryRow, W as KeepRecoveryState, aa as LocalStorageRecoveryJournal, ab as ScopedStorageAdapter, ac as ScopedSyncQueueAdapter, ae as createScopedStorageAdapter } from './types-DjXvE9Zb.js';
4
3
 
5
4
  type LocalStorageSyncQueueOptions = {
6
5
  key?: string;
@@ -75,6 +74,7 @@ declare class FallbackSyncQueueAdapter<TMeta = Record<string, unknown>> implemen
75
74
  /** A local-first adapter that persists remote operations until they are acknowledged. */
76
75
  declare class SyncStorageAdapter<TMeta = Record<string, unknown>> implements SyncCapableStorageAdapter<TMeta> {
77
76
  readonly storageKey?: string;
77
+ readonly recovery?: KeepStorageRecovery<TMeta>;
78
78
  readonly getCollections?: () => Promise<KeepCollectionDefinition[]>;
79
79
  readonly setCollection?: (collection: KeepCollectionDefinition) => Promise<void>;
80
80
  readonly removeCollection?: (id: string, scope?: SyncScope) => Promise<void>;
@@ -173,8 +173,11 @@ type StorageAdapterFactoryOptions<TMeta = Record<string, unknown>> = {
173
173
  storageKey?: string;
174
174
  };
175
175
  type FallbackStorageAdapterOptions<TMeta = Record<string, unknown>> = {
176
- primary: StorageAdapter<TMeta>;
176
+ primary?: StorageAdapter<TMeta>;
177
+ scope?: KeepScope;
177
178
  fallback: StorageAdapter<TMeta>;
179
+ /** Persist fallback-only operations and replay them before returning to the primary. */
180
+ recoveryJournal?: FallbackRecoveryJournal<TMeta>;
178
181
  /** Decide which primary adapter failures should activate the fallback. */
179
182
  shouldFallback?: (error: unknown) => boolean;
180
183
  onFallback?: (error: unknown) => void;
@@ -202,6 +205,8 @@ declare class FallbackStorageAdapter<TMeta = Record<string, unknown>> implements
202
205
  private readonly onFallback?;
203
206
  private readonly migrateFallbackOnEmpty;
204
207
  private readonly mirrorWrites;
208
+ readonly recovery?: KeepStorageRecovery<TMeta>;
209
+ private readonly journalController;
205
210
  private active;
206
211
  private readonly listeners;
207
212
  constructor(options: FallbackStorageAdapterOptions<TMeta>);
@@ -209,17 +214,24 @@ declare class FallbackStorageAdapter<TMeta = Record<string, unknown>> implements
209
214
  getAll(): Promise<KeepItem<TMeta>[]>;
210
215
  set(item: KeepItem<TMeta>): Promise<void>;
211
216
  setMany(items: KeepItem<TMeta>[]): Promise<void>;
212
- remove(id: string, scope?: KeepScope): Promise<void>;
213
- removeMany(ids: string[], scope?: KeepScope): Promise<void>;
217
+ remove(id: string, scope?: KeepScope | undefined): Promise<void>;
218
+ removeMany(ids: string[], scope?: KeepScope | undefined): Promise<void>;
214
219
  clear(): Promise<void>;
215
220
  merge(localItems: KeepItem<TMeta>[]): Promise<KeepItem<TMeta>[]>;
216
221
  subscribe(listener: () => void): () => void;
217
222
  private readAll;
218
223
  private activateFallback;
224
+ private reportFallbackError;
225
+ private notifyListeners;
219
226
  private executeWrite;
220
227
  private execute;
221
228
  }
222
- type BrowserStorageAdapterOptions = {
229
+ type RecoveryJournalFactoryContext = {
230
+ storageKey: string;
231
+ lockKey: string;
232
+ scope?: KeepScope;
233
+ };
234
+ type BrowserStorageAdapterOptions<TMeta = Record<string, unknown>> = {
223
235
  key?: string;
224
236
  databaseName?: string;
225
237
  storeName?: string;
@@ -227,9 +239,10 @@ type BrowserStorageAdapterOptions = {
227
239
  indexedDB?: IDBFactory;
228
240
  storage?: Storage;
229
241
  scope?: KeepScope;
242
+ createRecoveryJournal?: (context: RecoveryJournalFactoryContext) => FallbackRecoveryJournal<TMeta>;
230
243
  };
231
- /** IndexedDB-first browser storage with localStorage fallback. */
232
- declare function createBrowserStorageAdapter<TMeta = Record<string, unknown>>(options?: BrowserStorageAdapterOptions): StorageAdapter<TMeta>;
244
+ /** IndexedDB-first browser storage with a scoped durable recovery checkpoint. */
245
+ declare function createBrowserStorageAdapter<TMeta = Record<string, unknown>>(options?: BrowserStorageAdapterOptions<TMeta>): StorageAdapter<TMeta>;
233
246
  /** Adapt sync or async persistence functions to the StorageAdapter contract. */
234
247
  declare function createStorageAdapter<TMeta = Record<string, unknown>>(options: StorageAdapterFactoryOptions<TMeta>): StorageAdapter<TMeta>;
235
248
 
@@ -288,4 +301,4 @@ declare class IndexedDBAdapter<TMeta = Record<string, unknown>> implements Stora
288
301
  private open;
289
302
  }
290
303
 
291
- export { type BrowserStorageAdapterOptions, DEFAULT_INDEXEDDB_DATABASE, DEFAULT_INDEXEDDB_STORE, DEFAULT_STORAGE_KEY, DEFAULT_SYNC_QUEUE_DATABASE, DEFAULT_SYNC_QUEUE_KEY, DEFAULT_SYNC_QUEUE_STORE, FallbackStorageAdapter, type FallbackStorageAdapterOptions, FallbackSyncQueueAdapter, type FallbackSyncQueueAdapterOptions, IndexedDBAdapter, type IndexedDBAdapterOptions, IndexedDBSyncQueueAdapter, type IndexedDBSyncQueueOptions, KeepScope, LocalStorageAdapter, type LocalStorageAdapterOptions, LocalStorageSyncQueueAdapter, type LocalStorageSyncQueueOptions, type StorageAdapterFactoryOptions, SyncStorageAdapter, type SyncStorageAdapterOptions, createBrowserStorageAdapter, createStorageAdapter };
304
+ export { type BrowserStorageAdapterOptions, DEFAULT_INDEXEDDB_DATABASE, DEFAULT_INDEXEDDB_STORE, DEFAULT_STORAGE_KEY, DEFAULT_SYNC_QUEUE_DATABASE, DEFAULT_SYNC_QUEUE_KEY, DEFAULT_SYNC_QUEUE_STORE, FallbackRecoveryJournal, FallbackStorageAdapter, type FallbackStorageAdapterOptions, FallbackSyncQueueAdapter, type FallbackSyncQueueAdapterOptions, IndexedDBAdapter, type IndexedDBAdapterOptions, IndexedDBSyncQueueAdapter, type IndexedDBSyncQueueOptions, KeepScope, KeepStorageRecovery, LocalStorageAdapter, type LocalStorageAdapterOptions, LocalStorageSyncQueueAdapter, type LocalStorageSyncQueueOptions, type RecoveryJournalFactoryContext, type StorageAdapterFactoryOptions, SyncStorageAdapter, type SyncStorageAdapterOptions, createBrowserStorageAdapter, createStorageAdapter };
package/dist/storage.js CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  IndexedDBAdapter,
11
11
  IndexedDBSyncQueueAdapter,
12
12
  LocalStorageAdapter,
13
+ LocalStorageRecoveryJournal,
13
14
  LocalStorageSyncQueueAdapter,
14
15
  ScopedStorageAdapter,
15
16
  ScopedSyncQueueAdapter,
@@ -17,7 +18,8 @@ import {
17
18
  createBrowserStorageAdapter,
18
19
  createScopedStorageAdapter,
19
20
  createStorageAdapter
20
- } from "./chunk-SLH4DFYQ.js";
21
+ } from "./chunk-SS22UTBH.js";
22
+ import "./chunk-5W4QSJHV.js";
21
23
  export {
22
24
  DEFAULT_INDEXEDDB_DATABASE,
23
25
  DEFAULT_INDEXEDDB_STORE,
@@ -30,6 +32,7 @@ export {
30
32
  IndexedDBAdapter,
31
33
  IndexedDBSyncQueueAdapter,
32
34
  LocalStorageAdapter,
35
+ LocalStorageRecoveryJournal,
33
36
  LocalStorageSyncQueueAdapter,
34
37
  ScopedStorageAdapter,
35
38
  ScopedSyncQueueAdapter,
@@ -1,4 +1,4 @@
1
- import { c as KeepItem, f as KeepItemStatus, a as StorageAdapter, g as KeepSchema, h as KeepInvalidItemPolicy, S as SyncScope, K as KeepCollectionDefinition, b as KeepCollectionMembership, i as KeepChangeContext, j as KeepUndoState } from './types-GTCMFt5T.js';
1
+ import { K as KeepItem, a as KeepItemStatus, S as StorageAdapter, b as KeepChangeContext, c as KeepUndoState } from './types-DjXvE9Zb.js';
2
2
 
3
3
  type KeepNavigationState<TMeta = Record<string, unknown>> = {
4
4
  items: KeepItem<TMeta>[];
@@ -164,77 +164,6 @@ declare function decodeKeepListQuery(input: string | URL | URLSearchParams, opti
164
164
  declare function serializeKeepListQuery<TMeta = Record<string, unknown>>(query?: KeepListQuery<TMeta>, options?: Pick<KeepUrlSyncOptions, "params">): string;
165
165
  declare function mergeKeepListQueryFromUrl<TMeta = Record<string, unknown>>(query: KeepListQuery<TMeta>, input: string | URL | URLSearchParams, options?: Pick<KeepUrlSyncOptions, "params">): KeepListQuery<TMeta>;
166
166
 
167
- declare const KEEP_BACKUP_FORMAT = "keepkit";
168
- declare const KEEP_BACKUP_VERSION = 2;
169
- type KeepBackupV1<TMeta = Record<string, unknown>> = {
170
- format: typeof KEEP_BACKUP_FORMAT;
171
- version: 1;
172
- exportedAt: number;
173
- items: KeepItem<TMeta>[];
174
- };
175
- type KeepBackupV2<TMeta = Record<string, unknown>> = {
176
- format: typeof KEEP_BACKUP_FORMAT;
177
- version: typeof KEEP_BACKUP_VERSION;
178
- exportedAt: number;
179
- items: KeepItem<TMeta>[];
180
- collections: KeepCollectionDefinition[];
181
- memberships: KeepCollectionMembership[];
182
- /** Scopes represented by the records in this backup. Omitted by early v2 backups. */
183
- scopes?: SyncScope[];
184
- };
185
- type KeepBackup<TMeta = Record<string, unknown>> = KeepBackupV1<TMeta> | KeepBackupV2<TMeta>;
186
- type ImportItemsOptions<TMeta = unknown> = {
187
- mode?: "replace" | "merge";
188
- schema?: KeepSchema<TMeta>;
189
- invalidItemPolicy?: KeepInvalidItemPolicy;
190
- onInvalidItem?: (error: unknown, item: KeepItem<unknown>) => void;
191
- /** Resolves a same-ID collection name collision when merging. Defaults to the backup name. */
192
- collectionNameConflict?: "backup" | "existing";
193
- };
194
- type KeepBackupDataType = "items" | "collections" | "memberships";
195
- type KeepBackupAppliedCounts = {
196
- items: number;
197
- collections: number;
198
- memberships: number;
199
- };
200
- type ImportItemsResult<TMeta = Record<string, unknown>> = {
201
- mode: "replace" | "merge";
202
- imported: number;
203
- failed: number;
204
- total: number;
205
- items: KeepItem<TMeta>[];
206
- applied: KeepBackupAppliedCounts;
207
- missingCollectionIds: string[];
208
- includedData: KeepBackupDataType[];
209
- scopes: SyncScope[];
210
- };
211
- declare class KeepBackupParseError extends Error {
212
- readonly cause?: unknown;
213
- constructor(message: string, options?: {
214
- cause?: unknown;
215
- });
216
- }
217
- declare class KeepBackupImportError extends Error {
218
- readonly mode: "replace" | "merge";
219
- readonly imported: number;
220
- readonly failed: number;
221
- readonly applied: KeepBackupAppliedCounts;
222
- readonly failedStage: "items" | "collections" | "memberships" | "clear" | "validation";
223
- readonly cause?: unknown;
224
- constructor(message: string, options: {
225
- mode: "replace" | "merge";
226
- imported: number;
227
- failed: number;
228
- applied?: KeepBackupAppliedCounts;
229
- failedStage?: "items" | "collections" | "memberships" | "clear" | "validation";
230
- cause?: unknown;
231
- });
232
- }
233
- /** Serialize all adapter data into a versioned JSON backup. */
234
- declare function exportItems<TMeta>(adapter: StorageAdapter<TMeta>): Promise<string>;
235
- /** Validate and restore a backup, either replacing or merging existing data. */
236
- declare function importItems<TMeta>(adapter: StorageAdapter<TMeta>, data: string | KeepBackup<TMeta>, options?: ImportItemsOptions<TMeta>): Promise<ImportItemsResult<TMeta>>;
237
-
238
167
  type KeepCollectionMeta = {
239
168
  name: string;
240
169
  };
@@ -290,4 +219,4 @@ declare class KeepStore<TMeta = Record<string, unknown>> {
290
219
  setState(next: Partial<KeepStoreState<TMeta>>): void;
291
220
  }
292
221
 
293
- export { type KeepUrlSyncOptions as A, createInboxQuery as B, createRediscoveryQuery as C, DEFAULT_KEEP_URL_PARAMS as D, decodeKeepListQuery as E, encodeKeepListQuery as F, exportItems as G, getKeepNavigationState as H, type ImportItemsOptions as I, getTagCounts as J, type KeepListQuery as K, importItems as L, isKeepItemMetadataStale as M, mergeKeepListQueryFromUrl as N, moveKeepItem as O, orderKeepItems as P, type QueryKeepItemsResult as Q, type RevalidateKeepItemsOptions as R, queryKeepItems as S, reconcileKeepItems as T, reorderKeepItems as U, revalidateKeepItems as V, serializeKeepListQuery as W, type ImportItemsResult as a, KEEP_BACKUP_FORMAT as b, KEEP_BACKUP_VERSION as c, type KeepAutoRevalidationOptions as d, type KeepBackup as e, type KeepBackupAppliedCounts as f, type KeepBackupDataType as g, KeepBackupImportError as h, KeepBackupParseError as i, type KeepBackupV1 as j, type KeepBackupV2 as k, type KeepCollectionMeta as l, type KeepItemMetadataRefresher as m, type KeepItemResolver as n, type KeepItemRevalidationRecord as o, type KeepItemRevalidationResult as p, type KeepItemRevalidationSummary as q, type KeepItemRevalidator as r, type KeepNavigationState as s, type KeepRediscoveryOptions as t, type KeepRediscoveryStrategy as u, KeepStore as v, type KeepStoreActions as w, type KeepStoreState as x, type KeepUrlParamNames as y, type KeepUrlState as z };
222
+ export { orderKeepItems as A, queryKeepItems as B, reconcileKeepItems as C, DEFAULT_KEEP_URL_PARAMS as D, reorderKeepItems as E, revalidateKeepItems as F, serializeKeepListQuery as G, type KeepListQuery as K, type QueryKeepItemsResult as Q, type RevalidateKeepItemsOptions as R, type KeepAutoRevalidationOptions as a, type KeepCollectionMeta as b, type KeepItemMetadataRefresher as c, type KeepItemResolver as d, type KeepItemRevalidationRecord as e, type KeepItemRevalidationResult as f, type KeepItemRevalidationSummary as g, type KeepItemRevalidator as h, type KeepNavigationState as i, type KeepRediscoveryOptions as j, type KeepRediscoveryStrategy as k, KeepStore as l, type KeepStoreActions as m, type KeepStoreState as n, type KeepUrlParamNames as o, type KeepUrlState as p, type KeepUrlSyncOptions as q, createInboxQuery as r, createRediscoveryQuery as s, decodeKeepListQuery as t, encodeKeepListQuery as u, getKeepNavigationState as v, getTagCounts as w, isKeepItemMetadataStale as x, mergeKeepListQueryFromUrl as y, moveKeepItem as z };
@@ -1,3 +1,234 @@
1
+ declare const KEEP_BACKUP_FORMAT = "keepkit";
2
+ declare const KEEP_BACKUP_VERSION = 2;
3
+ type KeepBackupV1<TMeta = Record<string, unknown>> = {
4
+ format: typeof KEEP_BACKUP_FORMAT;
5
+ version: 1;
6
+ exportedAt: number;
7
+ items: KeepItem<TMeta>[];
8
+ };
9
+ type KeepBackupV2<TMeta = Record<string, unknown>> = {
10
+ format: typeof KEEP_BACKUP_FORMAT;
11
+ version: typeof KEEP_BACKUP_VERSION;
12
+ exportedAt: number;
13
+ items: KeepItem<TMeta>[];
14
+ collections: KeepCollectionDefinition[];
15
+ memberships: KeepCollectionMembership[];
16
+ /** Scopes represented by the records in this backup. Omitted by early v2 backups. */
17
+ scopes?: SyncScope[];
18
+ };
19
+ type KeepBackup<TMeta = Record<string, unknown>> = KeepBackupV1<TMeta> | KeepBackupV2<TMeta>;
20
+ type ImportItemsOptions<TMeta = unknown> = {
21
+ mode?: "replace" | "merge";
22
+ schema?: KeepSchema<TMeta>;
23
+ invalidItemPolicy?: KeepInvalidItemPolicy;
24
+ onInvalidItem?: (error: unknown, item: KeepItem<unknown>) => void;
25
+ /** Resolves a same-ID collection name collision when merging. Defaults to the backup name. */
26
+ collectionNameConflict?: "backup" | "existing";
27
+ };
28
+ type KeepBackupDataType = "items" | "collections" | "memberships";
29
+ type KeepBackupAppliedCounts = {
30
+ items: number;
31
+ collections: number;
32
+ memberships: number;
33
+ };
34
+ type ImportItemsResult<TMeta = Record<string, unknown>> = {
35
+ mode: "replace" | "merge";
36
+ imported: number;
37
+ failed: number;
38
+ total: number;
39
+ items: KeepItem<TMeta>[];
40
+ applied: KeepBackupAppliedCounts;
41
+ missingCollectionIds: string[];
42
+ includedData: KeepBackupDataType[];
43
+ scopes: SyncScope[];
44
+ };
45
+ declare class KeepBackupParseError extends Error {
46
+ readonly cause?: unknown;
47
+ constructor(message: string, options?: {
48
+ cause?: unknown;
49
+ });
50
+ }
51
+ declare class KeepBackupImportError extends Error {
52
+ readonly mode: "replace" | "merge";
53
+ readonly imported: number;
54
+ readonly failed: number;
55
+ readonly applied: KeepBackupAppliedCounts;
56
+ readonly failedStage: "items" | "collections" | "memberships" | "clear" | "validation";
57
+ readonly cause?: unknown;
58
+ constructor(message: string, options: {
59
+ mode: "replace" | "merge";
60
+ imported: number;
61
+ failed: number;
62
+ applied?: KeepBackupAppliedCounts;
63
+ failedStage?: "items" | "collections" | "memberships" | "clear" | "validation";
64
+ cause?: unknown;
65
+ });
66
+ }
67
+ /** Serialize all adapter data into a versioned JSON backup. */
68
+ declare function exportItems<TMeta>(adapter: StorageAdapter<TMeta>): Promise<string>;
69
+ /** Validate and restore a backup, either replacing or merging existing data. */
70
+ declare function importItems<TMeta>(adapter: StorageAdapter<TMeta>, data: string | KeepBackup<TMeta>, options?: ImportItemsOptions<TMeta>): Promise<ImportItemsResult<TMeta>>;
71
+
72
+ type KeepScope = SyncScope;
73
+ /** Return a stable, human-readable namespace for browser storage and queues. */
74
+ declare function getKeepScopeKey(scope?: KeepScope): string;
75
+ declare function isSameKeepScope(left: KeepScope | undefined, right: KeepScope | undefined): boolean;
76
+ /**
77
+ * Adds an isolated user/tenant view over any StorageAdapter. Writes preserve
78
+ * records belonging to other scopes, which makes account switching safe even
79
+ * when applications share one physical localStorage key.
80
+ */
81
+ declare class ScopedStorageAdapter<TMeta = Record<string, unknown>> implements StorageAdapter<TMeta> {
82
+ readonly scope?: KeepScope;
83
+ readonly storageKey?: string;
84
+ readonly recovery?: KeepStorageRecovery<TMeta>;
85
+ readonly getCollections?: () => Promise<KeepCollectionDefinition[]>;
86
+ readonly setCollection?: (collection: KeepCollectionDefinition) => Promise<void>;
87
+ readonly removeCollection?: (id: string, scope?: SyncScope) => Promise<void>;
88
+ readonly getCollectionMemberships?: () => Promise<KeepCollectionMembership[]>;
89
+ readonly setCollectionMembership?: (membership: KeepCollectionMembership) => Promise<void>;
90
+ readonly removeCollectionMembership?: (collectionId: string, itemId: string, scope?: SyncScope) => Promise<void>;
91
+ private readonly base;
92
+ constructor(base: StorageAdapter<TMeta>, scope?: KeepScope);
93
+ getAll(): Promise<KeepItem<TMeta>[]>;
94
+ set(item: KeepItem<TMeta>): Promise<void>;
95
+ setMany(items: KeepItem<TMeta>[]): Promise<void>;
96
+ remove(id: string): Promise<void>;
97
+ removeMany(ids: string[]): Promise<void>;
98
+ clear(): Promise<void>;
99
+ merge(items: KeepItem<TMeta>[]): Promise<KeepItem<TMeta>[]>;
100
+ subscribe(listener: () => void): () => void;
101
+ private assertWritableScope;
102
+ private removeMembershipsForItems;
103
+ private clearMembershipsForScope;
104
+ }
105
+ declare function createScopedStorageAdapter<TMeta = Record<string, unknown>>(base: StorageAdapter<TMeta>, scope?: KeepScope): ScopedStorageAdapter<TMeta>;
106
+ /** Scope a durable synchronization queue so a user switch cannot flush another user's operations. */
107
+ declare class ScopedSyncQueueAdapter<TMeta = Record<string, unknown>> implements SyncQueueAdapter<TMeta> {
108
+ private readonly base;
109
+ private readonly scope?;
110
+ constructor(base: SyncQueueAdapter<TMeta>, scope?: KeepScope);
111
+ getAll(): Promise<KeepSyncQueueOperation<TMeta>[]>;
112
+ setMany(operations: KeepSyncQueueOperation<TMeta>[]): Promise<void>;
113
+ remove(operationIds: string[]): Promise<void>;
114
+ clear(): Promise<void>;
115
+ }
116
+
117
+ type FallbackRecoveryOperation<TMeta = Record<string, unknown>> = {
118
+ type: "setItems";
119
+ items: KeepItem<TMeta>[];
120
+ } | {
121
+ type: "removeItems";
122
+ ids: string[];
123
+ scope?: KeepScope;
124
+ } | {
125
+ type: "clearItems";
126
+ } | {
127
+ type: "setCollection";
128
+ collection: KeepCollectionDefinition;
129
+ } | {
130
+ type: "removeCollection";
131
+ id: string;
132
+ scope?: KeepScope;
133
+ } | {
134
+ type: "setMembership";
135
+ membership: KeepCollectionMembership;
136
+ } | {
137
+ type: "removeMembership";
138
+ collectionId: string;
139
+ itemId: string;
140
+ scope?: KeepScope;
141
+ };
142
+ type FallbackRecoverySnapshot<TMeta = Record<string, unknown>> = {
143
+ items: KeepItem<TMeta>[];
144
+ collections: KeepCollectionDefinition[];
145
+ memberships: KeepCollectionMembership[];
146
+ capabilities: {
147
+ collections: boolean;
148
+ memberships: boolean;
149
+ };
150
+ capturedAt: number;
151
+ };
152
+ type FallbackRecoveryJournalV2<TMeta = Record<string, unknown>> = {
153
+ version: 2;
154
+ operations: FallbackRecoveryOperation<TMeta>[];
155
+ snapshot?: FallbackRecoverySnapshot<TMeta>;
156
+ archives?: FallbackRecoverySnapshot<TMeta>[];
157
+ backups?: KeepBackupV2<TMeta>[];
158
+ /** An established primary history must never import old fallback deletions again. */
159
+ initialized?: boolean;
160
+ };
161
+ type FallbackRecoveryJournalState<TMeta = Record<string, unknown>> = {
162
+ version: 1;
163
+ operations: FallbackRecoveryOperation<TMeta>[];
164
+ } | FallbackRecoveryJournalV2<TMeta>;
165
+ interface FallbackRecoveryJournal<TMeta = Record<string, unknown>> {
166
+ read(): Promise<FallbackRecoveryJournalState<TMeta> | undefined>;
167
+ write(state: FallbackRecoveryJournalState<TMeta>): Promise<void>;
168
+ /** Use one backend-wide lock across every scope's journal and adapter operations. */
169
+ runExclusive<T>(operation: () => Promise<T>): Promise<T>;
170
+ subscribe?(listener: () => void): () => void;
171
+ }
172
+ type KeepRecoveryState = {
173
+ source: "primary" | "fallback";
174
+ pendingOperations: number;
175
+ snapshotStale: boolean;
176
+ capturedAt?: number;
177
+ issue?: unknown;
178
+ repairSupported: boolean;
179
+ };
180
+ type KeepRecoveryResult = {
181
+ status: "applied" | "pending";
182
+ pendingOperations: number;
183
+ };
184
+ type KeepRecoveryRow<TMeta = Record<string, unknown>> = {
185
+ key: string;
186
+ kind: "item" | "collection" | "membership";
187
+ id: string;
188
+ candidates: Array<{
189
+ source: string;
190
+ value: KeepItem<TMeta> | KeepCollectionDefinition | KeepCollectionMembership;
191
+ }>;
192
+ conflict: boolean;
193
+ selectedSource?: string;
194
+ };
195
+ type KeepRecoveryInspection<TMeta = Record<string, unknown>> = {
196
+ token: string;
197
+ sources: Array<{
198
+ id: string;
199
+ label: string;
200
+ snapshot: FallbackRecoverySnapshot<TMeta>;
201
+ }>;
202
+ rows: KeepRecoveryRow<TMeta>[];
203
+ issues: string[];
204
+ operations: FallbackRecoveryOperation<TMeta>[];
205
+ backups: KeepBackupV2<TMeta>[];
206
+ retainedBackups?: KeepBackupV2<TMeta>[];
207
+ };
208
+ interface KeepStorageRecovery<TMeta = Record<string, unknown>> {
209
+ getState(): KeepRecoveryState;
210
+ subscribe(listener: () => void): () => void;
211
+ inspect?(options?: {
212
+ selections?: Record<string, string>;
213
+ }): Promise<KeepRecoveryInspection<TMeta>>;
214
+ apply?(options: {
215
+ token: string;
216
+ selections: Record<string, string>;
217
+ }): Promise<KeepRecoveryResult>;
218
+ retry?(): Promise<KeepRecoveryResult>;
219
+ }
220
+ /** A sidecar journal; storage and lock keys deliberately have separate scopes. */
221
+ declare class LocalStorageRecoveryJournal<TMeta = Record<string, unknown>> implements FallbackRecoveryJournal<TMeta> {
222
+ private readonly key;
223
+ private readonly storage;
224
+ private readonly lockKey;
225
+ constructor(key: string, storage: Storage | undefined, lockKey?: string);
226
+ read(): Promise<FallbackRecoveryJournalState<TMeta> | undefined>;
227
+ write(state: FallbackRecoveryJournalState<TMeta>): Promise<void>;
228
+ runExclusive<T>(operation: () => Promise<T>): Promise<T>;
229
+ subscribe(listener: () => void): () => void;
230
+ }
231
+
1
232
  type KeepItemStatus = "available" | "expired" | "removed" | "deleted" | "private" | "unknown";
2
233
  type SyncScope = {
3
234
  userId?: string;
@@ -64,6 +295,8 @@ type KeepItemInput<TMeta = Record<string, unknown>> = {
64
295
  interface StorageAdapter<TMeta = Record<string, unknown>> {
65
296
  /** Active user/tenant scope when this adapter is scoped. */
66
297
  readonly scope?: SyncScope;
298
+ /** Optional local fallback status and explicitly invoked repair tools. */
299
+ readonly recovery?: KeepStorageRecovery<TMeta>;
67
300
  getAll(): Promise<KeepItem<TMeta>[]>;
68
301
  /** Optional local collection metadata; independent of saved items and item backups. */
69
302
  getCollections?(): Promise<KeepCollectionDefinition[]>;
@@ -293,9 +526,18 @@ declare class KeepStorageParseError extends KeepStorageError {
293
526
  cause?: unknown;
294
527
  });
295
528
  }
529
+ declare class KeepStorageSerializationError extends KeepStorageError {
530
+ constructor(options: {
531
+ operation: KeepStorageOperation;
532
+ storageKey?: string;
533
+ cause?: unknown;
534
+ });
535
+ }
296
536
  type KeepErrorContext = {
297
537
  action: KeepAction;
298
538
  id?: string;
539
+ /** The mutation was persisted; only a subsequent notification failed. */
540
+ phase?: "notification";
299
541
  };
300
542
  type KeepErrorHandler = (error: unknown, context: KeepErrorContext) => void;
301
543
  type KeepEventHandlers<TMeta = Record<string, unknown>> = {
@@ -315,4 +557,4 @@ type KeepUndoState = {
315
557
  };
316
558
  declare function normalizeKeepTags(tags?: string[]): string[] | undefined;
317
559
 
318
- export { type KeepEventHandlers as A, type KeepItemInput as B, type KeepSchemaParseResult as C, KeepStorageAccessError as D, KeepStorageError as E, type KeepStorageOperation as F, KeepStorageParseError as G, KeepStorageQuotaError as H, type KeepSyncAuthStatus as I, type KeepSyncConflict as J, type KeepCollectionDefinition as K, type KeepSyncResolution as L, type KeepSyncState as M, type KeepSyncStatus as N, isKeepSyncAuthError as O, normalizeKeepTags as P, type RemoteSyncDriver as R, type SyncScope as S, type StorageAdapter as a, type KeepCollectionMembership as b, type KeepItem as c, type SyncQueueAdapter as d, type KeepSyncQueueOperation as e, type KeepItemStatus as f, type KeepSchema as g, type KeepInvalidItemPolicy as h, type KeepChangeContext as i, type KeepUndoState as j, type KeepPluginContext as k, type KeepPlugin as l, type SyncOperation as m, type KeepCollectionSyncOperation as n, type KeepMembershipSyncOperation as o, type SyncCapableStorageAdapter as p, type RemoteSyncResult as q, KeepSyncAuthError as r, type KeepAction as s, type KeepChangePhase as t, type KeepCollectionSyncConflict as u, type KeepCollectionSyncResult as v, type KeepConflictContext as w, type KeepConflictResolver as x, type KeepErrorContext as y, type KeepErrorHandler as z };
560
+ export { type KeepStorageOperation as $, KeepBackupImportError as A, KeepBackupParseError as B, type KeepBackupV1 as C, type KeepBackupV2 as D, type KeepChangePhase as E, type FallbackRecoveryJournal as F, type KeepCollectionSyncConflict as G, type KeepCollectionSyncResult as H, type ImportItemsOptions as I, type KeepConflictContext as J, type KeepItem as K, type KeepConflictResolver as L, type KeepErrorContext as M, type KeepErrorHandler as N, type KeepEventHandlers as O, type KeepInvalidItemPolicy as P, type KeepItemInput as Q, type RemoteSyncDriver as R, type StorageAdapter as S, type KeepRecoveryInspection as T, type KeepRecoveryResult as U, type KeepRecoveryRow as V, type KeepRecoveryState as W, type KeepSchema as X, type KeepSchemaParseResult as Y, KeepStorageAccessError as Z, KeepStorageError as _, type KeepItemStatus as a, KeepStorageParseError as a0, KeepStorageQuotaError as a1, type KeepStorageRecovery as a2, KeepStorageSerializationError as a3, type KeepSyncAuthStatus as a4, type KeepSyncConflict as a5, type KeepSyncQueueOperation as a6, type KeepSyncResolution as a7, type KeepSyncState as a8, type KeepSyncStatus as a9, LocalStorageRecoveryJournal as aa, ScopedStorageAdapter as ab, ScopedSyncQueueAdapter as ac, type SyncQueueAdapter as ad, createScopedStorageAdapter as ae, exportItems as af, getKeepScopeKey as ag, importItems as ah, isKeepSyncAuthError as ai, isSameKeepScope as aj, normalizeKeepTags as ak, type KeepChangeContext as b, type KeepUndoState as c, type KeepCollectionMembership as d, type KeepScope as e, type KeepPluginContext as f, type KeepPlugin as g, type SyncOperation as h, type KeepCollectionSyncOperation as i, type KeepMembershipSyncOperation as j, type SyncScope as k, type SyncCapableStorageAdapter as l, type RemoteSyncResult as m, type KeepCollectionDefinition as n, KeepSyncAuthError as o, type FallbackRecoveryJournalState as p, type FallbackRecoveryJournalV2 as q, type FallbackRecoveryOperation as r, type FallbackRecoverySnapshot as s, type ImportItemsResult as t, KEEP_BACKUP_FORMAT as u, KEEP_BACKUP_VERSION as v, type KeepAction as w, type KeepBackup as x, type KeepBackupAppliedCounts as y, type KeepBackupDataType as z };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keepkit/core",
3
- "version": "0.28.9",
3
+ "version": "0.28.10",
4
4
  "description": "A flexible save-and-collect toolkit for React apps.",
5
5
  "type": "module",
6
6
  "license": "MIT",