@keepkit/core 0.2.0 → 0.4.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 CHANGED
@@ -27,6 +27,8 @@ const storage = new LocalStorageAdapter({ key: "my-app:items" });
27
27
 
28
28
  `exportItems(adapter)` / `importItems(adapter, json, { mode: "replace" | "merge" })` でversion付きJSONバックアップを扱えます。結果には `imported` / `failed` 件数が含まれます。
29
29
 
30
+ `KeepButton` の標準ARIAラベルは `savedAriaLabel` / `unsavedAriaLabel` または `getAriaLabel` で多言語化できます。`useKeepItem(id).refreshMetadata` と `isKeepItemMetadataStale` はメタデータの再取得に使えます。`useKeepList().revalidate` または `useKeepContext().revalidateItems` は削除・非公開・期限切れの対象を検出します。検出した状態を整理する場合は `removeStatuses` を明示してください。
31
+
30
32
  大規模アプリでは `SyncStorageAdapter` を使ってローカル保存とリモート同期を分離できます。`IndexedDBSyncQueueAdapter`(既定)または `LocalStorageSyncQueueAdapter` に操作を永続化し、オンライン復帰時に `flushSync()` で再送します。`KeepProvider` の `syncState` で `pending` / `syncing` / `conflict` / `error` を取得できます。
31
33
 
32
34
  ```tsx
@@ -81,6 +83,8 @@ Use `useKeepItem(id, payload)` for saving, toggling, removing, and updating note
81
83
 
82
84
  Use `exportItems(adapter)` and `importItems(adapter, json, { mode: "replace" | "merge" })` for versioned JSON backups. Results include imported and failed counts.
83
85
 
86
+ Localize the default `KeepButton` ARIA label with `savedAriaLabel` / `unsavedAriaLabel` or `getAriaLabel`. `useKeepItem(id).refreshMetadata` and `isKeepItemMetadataStale` fetch current metadata, while `useKeepList().revalidate` or `useKeepContext().revalidateItems` detects deleted, private, and expired targets. Pass `removeStatuses` explicitly when detected records should be cleaned up.
87
+
84
88
  For larger applications, wrap a local adapter with `SyncStorageAdapter` to persist a durable offline queue and flush it with `flushSync()` when connectivity returns. Use `syncState` from `KeepProvider` for `pending`, `syncing`, `conflict`, and `error` feedback. `KeepProvider` also accepts a Zod-like `parse`, `safeParse`, or Standard Schema validator through `schema`; invalid stored records can be rejected or dropped with `invalidItemPolicy`.
85
89
 
86
90
  `useKeepList` supports `savedBetween`, tokenized `search` with `and` / `or` modes, `filterFn`, and `tagCounts`. Use `createKeepInvalidationPlugin` to connect TanStack Query, SWR, or another cache without adding framework dependencies to core.
@@ -46,6 +46,66 @@ function toTimestamp(value) {
46
46
  return value instanceof Date ? value.getTime() : value;
47
47
  }
48
48
 
49
+ // src/revalidation.ts
50
+ function isKeepItemMetadataStale(item, maxAgeMs, now = Date.now) {
51
+ return item.metaUpdatedAt === void 0 || now() - item.metaUpdatedAt >= maxAgeMs;
52
+ }
53
+ async function revalidateKeepItems(source, revalidator, options = {}) {
54
+ const removeStatuses = new Set(options.removeStatuses ?? []);
55
+ const now = options.now ?? Date.now;
56
+ const items = [];
57
+ const updatedItems = [];
58
+ const removedIds = [];
59
+ const results = [];
60
+ for (const item of source) {
61
+ const rawResult = await revalidator(item);
62
+ const result = typeof rawResult === "string" ? { status: rawResult } : rawResult;
63
+ if (result.status === "available") {
64
+ const timestamp = now();
65
+ const updated = result.meta === void 0 ? item : { ...item, meta: result.meta, metaUpdatedAt: timestamp, updatedAt: timestamp };
66
+ const didUpdate = updated !== item;
67
+ items.push(updated);
68
+ if (didUpdate) updatedItems.push(updated);
69
+ results.push({ item: updated, status: "available", updated: didUpdate });
70
+ continue;
71
+ }
72
+ const shouldRemove = removeStatuses.has(result.status);
73
+ if (shouldRemove) removedIds.push(item.id);
74
+ else items.push(item);
75
+ results.push({ item, status: result.status, reason: result.reason, updated: false });
76
+ }
77
+ return {
78
+ items,
79
+ checked: source.length,
80
+ updated: updatedItems.length,
81
+ removed: removedIds.length,
82
+ updatedItems,
83
+ removedIds,
84
+ results
85
+ };
86
+ }
87
+ async function reconcileKeepItems(storage, revalidator, options = {}) {
88
+ const source = await storage.getAll();
89
+ const summary = await revalidateKeepItems(source, revalidator, options);
90
+ if (summary.updatedItems.length > 0) await persistItems(storage, summary.updatedItems);
91
+ if (summary.removedIds.length > 0) await removeItems(storage, summary.removedIds);
92
+ return summary;
93
+ }
94
+ async function persistItems(storage, items) {
95
+ if (storage.setMany) {
96
+ await storage.setMany(items);
97
+ return;
98
+ }
99
+ for (const item of items) await storage.set(item);
100
+ }
101
+ async function removeItems(storage, ids) {
102
+ if (storage.removeMany) {
103
+ await storage.removeMany(ids);
104
+ return;
105
+ }
106
+ for (const id of ids) await storage.remove(id);
107
+ }
108
+
49
109
  // src/store.ts
50
110
  var KeepStore = class {
51
111
  constructor(initialState) {
@@ -74,6 +134,9 @@ var KeepStore = class {
74
134
  export {
75
135
  queryKeepItems,
76
136
  getTagCounts,
137
+ isKeepItemMetadataStale,
138
+ revalidateKeepItems,
139
+ reconcileKeepItems,
77
140
  KeepStore
78
141
  };
79
- //# sourceMappingURL=chunk-PLT2WJVM.js.map
142
+ //# sourceMappingURL=chunk-L4KP6M5R.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/query.ts","../src/revalidation.ts","../src/store.ts"],"sourcesContent":["import type { KeepItem } from \"./types\";\n\nexport type KeepListOptions<TMeta = Record<string, unknown>> = {\n targetType?: string;\n tag?: string;\n tags?: string[];\n sort?: {\n by: \"savedAt\" | \"updatedAt\";\n direction?: \"asc\" | \"desc\";\n };\n searchQuery?: string;\n search?: {\n query: string;\n mode?: \"and\" | \"or\";\n tokenize?: boolean;\n fields?: Array<\"note\" | \"meta\" | \"tags\">;\n };\n sortBy?: \"savedAt\" | \"updatedAt\";\n order?: \"asc\" | \"desc\";\n limit?: number;\n offset?: number;\n filter?: (item: KeepItem<TMeta>) => boolean;\n filterFn?: (item: KeepItem<TMeta>) => boolean;\n savedBetween?: readonly [Date | number, Date | number];\n};\n\nexport type QueryKeepItemsResult<TMeta = Record<string, unknown>> = {\n items: KeepItem<TMeta>[];\n totalCount: number;\n tagCounts: Record<string, number>;\n};\n\n/** Apply the same filtering and pagination rules as useKeepList without React. */\nexport function queryKeepItems<TMeta = Record<string, unknown>>(\n source: KeepItem<TMeta>[],\n options: KeepListOptions<TMeta> = {},\n): QueryKeepItemsResult<TMeta> {\n const filtered = source.filter((item) => {\n const [from, to] = options.savedBetween ?? [];\n const savedAt = item.savedAt;\n const lowerBound = from === undefined ? undefined : toTimestamp(from);\n const upperBound = to === undefined ? undefined : toTimestamp(to);\n return (\n (options.targetType === undefined || item.targetType === options.targetType) &&\n (options.tag === undefined || item.tags?.includes(options.tag) === true) &&\n (options.tags === undefined || options.tags.every((tag) => item.tags?.includes(tag))) &&\n (lowerBound === undefined || savedAt >= lowerBound) &&\n (upperBound === undefined || savedAt <= upperBound) &&\n matchesSearch(item, options.searchQuery, options.search) &&\n (options.filter?.(item) ?? true) &&\n (options.filterFn?.(item) ?? true)\n );\n });\n const tagCounts = getTagCounts(filtered);\n const sortBy = options.sortBy ?? options.sort?.by;\n const direction = (options.order ?? options.sort?.direction) === \"asc\" ? 1 : -1;\n const sorted = sortBy ? [...filtered].sort((a, b) => (a[sortBy] - b[sortBy]) * direction) : filtered;\n const offset = Math.max(0, options.offset ?? 0);\n const items =\n options.limit === undefined ? sorted.slice(offset) : sorted.slice(offset, offset + Math.max(0, options.limit));\n return { items, totalCount: sorted.length, tagCounts };\n}\n\nexport function getTagCounts<TMeta = Record<string, unknown>>(items: KeepItem<TMeta>[]): Record<string, number> {\n const counts: Record<string, number> = {};\n for (const item of items) {\n for (const tag of item.tags ?? []) counts[tag] = (counts[tag] ?? 0) + 1;\n }\n return Object.fromEntries(Object.entries(counts).sort(([a], [b]) => a.localeCompare(b)));\n}\n\nfunction matchesSearch<TMeta>(\n item: KeepItem<TMeta>,\n searchQuery?: string,\n search?: KeepListOptions<TMeta>[\"search\"],\n): boolean {\n const query = search?.query ?? searchQuery;\n if (!query?.trim()) return true;\n const fields = search?.fields ?? [\"note\", \"meta\", \"tags\"];\n const values = fields.map((field) => {\n if (field === \"note\") return item.note ?? \"\";\n if (field === \"tags\") return (item.tags ?? []).join(\" \");\n try {\n return JSON.stringify(item.meta) ?? \"\";\n } catch {\n return String(item.meta);\n }\n });\n const text = values.join(\" \").toLocaleLowerCase();\n if (!search) return text.includes(query.trim().toLocaleLowerCase());\n const normalized = query.trim().toLocaleLowerCase();\n const needles = search.tokenize === false ? [normalized] : normalized.split(/\\s+/).filter(Boolean);\n const matches = needles.map((needle) => text.includes(needle));\n return search.mode === \"or\" ? matches.some(Boolean) : matches.every(Boolean);\n}\n\nfunction toTimestamp(value: Date | number): number {\n return value instanceof Date ? value.getTime() : value;\n}\n","import type { KeepItem, StorageAdapter } from \"./types\";\n\nexport type KeepItemStatus = \"available\" | \"deleted\" | \"private\" | \"expired\" | \"unknown\";\n\nexport type KeepItemRevalidationResult<TMeta = Record<string, unknown>> =\n | { status: \"available\"; meta?: TMeta }\n | { status: Exclude<KeepItemStatus, \"available\">; reason?: string };\n\nexport type KeepItemRevalidator<TMeta = Record<string, unknown>> = (\n item: KeepItem<TMeta>,\n) =>\n | KeepItemRevalidationResult<TMeta>\n | KeepItemRevalidationResult<TMeta>[\"status\"]\n | Promise<KeepItemRevalidationResult<TMeta> | KeepItemRevalidationResult<TMeta>[\"status\"]>;\n\nexport type KeepItemMetadataRefresher<TMeta = Record<string, unknown>> = (\n item: KeepItem<TMeta>,\n) => TMeta | Promise<TMeta>;\n\n/** Return whether source metadata should be fetched again based on its age. */\nexport function isKeepItemMetadataStale<TMeta>(\n item: KeepItem<TMeta>,\n maxAgeMs: number,\n now: () => number = Date.now,\n): boolean {\n return item.metaUpdatedAt === undefined || now() - item.metaUpdatedAt >= maxAgeMs;\n}\n\nexport type KeepItemRevalidationRecord<TMeta = Record<string, unknown>> = {\n item: KeepItem<TMeta>;\n status: KeepItemStatus;\n reason?: string;\n updated: boolean;\n};\n\nexport type RevalidateKeepItemsOptions = {\n /** Statuses that should be removed after they are detected. Detection is the default. */\n removeStatuses?: Array<Exclude<KeepItemStatus, \"available\">>;\n now?: () => number;\n};\n\nexport type KeepItemRevalidationSummary<TMeta = Record<string, unknown>> = {\n items: KeepItem<TMeta>[];\n checked: number;\n updated: number;\n removed: number;\n updatedItems: KeepItem<TMeta>[];\n removedIds: string[];\n results: KeepItemRevalidationRecord<TMeta>[];\n};\n\n/** Revalidate saved items without coupling the checker to a network client. */\nexport async function revalidateKeepItems<TMeta = Record<string, unknown>>(\n source: KeepItem<TMeta>[],\n revalidator: KeepItemRevalidator<TMeta>,\n options: RevalidateKeepItemsOptions = {},\n): Promise<KeepItemRevalidationSummary<TMeta>> {\n const removeStatuses = new Set(options.removeStatuses ?? []);\n const now = options.now ?? Date.now;\n const items: KeepItem<TMeta>[] = [];\n const updatedItems: KeepItem<TMeta>[] = [];\n const removedIds: string[] = [];\n const results: KeepItemRevalidationRecord<TMeta>[] = [];\n\n for (const item of source) {\n const rawResult = await revalidator(item);\n const result: KeepItemRevalidationResult<TMeta> = typeof rawResult === \"string\" ? { status: rawResult } : rawResult;\n if (result.status === \"available\") {\n const timestamp = now();\n const updated =\n result.meta === undefined\n ? item\n : { ...item, meta: result.meta, metaUpdatedAt: timestamp, updatedAt: timestamp };\n const didUpdate = updated !== item;\n items.push(updated);\n if (didUpdate) updatedItems.push(updated);\n results.push({ item: updated, status: \"available\", updated: didUpdate });\n continue;\n }\n\n const shouldRemove = removeStatuses.has(result.status);\n if (shouldRemove) removedIds.push(item.id);\n else items.push(item);\n results.push({ item, status: result.status, reason: result.reason, updated: false });\n }\n\n return {\n items,\n checked: source.length,\n updated: updatedItems.length,\n removed: removedIds.length,\n updatedItems,\n removedIds,\n results,\n };\n}\n\n/** Revalidate and persist saved items for framework-neutral applications. */\nexport async function reconcileKeepItems<TMeta = Record<string, unknown>>(\n storage: StorageAdapter<TMeta>,\n revalidator: KeepItemRevalidator<TMeta>,\n options: RevalidateKeepItemsOptions = {},\n): Promise<KeepItemRevalidationSummary<TMeta>> {\n const source = await storage.getAll();\n const summary = await revalidateKeepItems(source, revalidator, options);\n if (summary.updatedItems.length > 0) await persistItems(storage, summary.updatedItems);\n if (summary.removedIds.length > 0) await removeItems(storage, summary.removedIds);\n return summary;\n}\n\nasync function persistItems<TMeta>(storage: StorageAdapter<TMeta>, items: KeepItem<TMeta>[]): Promise<void> {\n if (storage.setMany) {\n await storage.setMany(items);\n return;\n }\n for (const item of items) await storage.set(item);\n}\n\nasync function removeItems<TMeta>(storage: StorageAdapter<TMeta>, ids: string[]): Promise<void> {\n if (storage.removeMany) {\n await storage.removeMany(ids);\n return;\n }\n for (const id of ids) await storage.remove(id);\n}\n","import type {\n KeepItemMetadataRefresher,\n KeepItemRevalidationSummary,\n KeepItemRevalidator,\n RevalidateKeepItemsOptions,\n} from \"./revalidation\";\nimport type { KeepChangeContext, KeepItem } from \"./types\";\n\nexport type KeepStoreState<TMeta = Record<string, unknown>> = {\n items: KeepItem<TMeta>[];\n isLoading: boolean;\n isHydrated: boolean;\n isMutating: boolean;\n error: unknown | null;\n lastChange?: KeepChangeContext<TMeta>;\n};\n\nexport type KeepStoreActions<TMeta = Record<string, unknown>> = {\n saveItem: (item: KeepItem<TMeta>) => Promise<void>;\n updateNote: (id: string, note?: string) => Promise<void>;\n updateTags: (id: string, tags?: string[]) => Promise<void>;\n updateTagsBatch: (ids: string[], tags?: string[]) => Promise<void>;\n addTagsBatch: (ids: string[], tags: string[]) => Promise<void>;\n removeTagsBatch: (ids: string[], tags: string[]) => Promise<void>;\n removeItem: (id: string) => Promise<void>;\n removeItems: (ids: string[]) => Promise<void>;\n clear: () => Promise<void>;\n refresh: () => Promise<void>;\n refreshItemMetadata: (id: string, refresh: KeepItemMetadataRefresher<TMeta>) => Promise<void>;\n revalidateItems: (\n revalidator: KeepItemRevalidator<TMeta>,\n options?: RevalidateKeepItemsOptions,\n ) => Promise<KeepItemRevalidationSummary<TMeta>>;\n};\n\nexport class KeepStore<TMeta = Record<string, unknown>> {\n private state: KeepStoreState<TMeta>;\n private readonly listeners = new Set<() => void>();\n\n constructor(initialState: KeepStoreState<TMeta>) {\n this.state = initialState;\n }\n\n getSnapshot = (): KeepStoreState<TMeta> => this.state;\n\n subscribe = (listener: () => void): (() => void) => {\n this.listeners.add(listener);\n return () => this.listeners.delete(listener);\n };\n\n setState(next: Partial<KeepStoreState<TMeta>>): void {\n let changed = false;\n for (const key of Object.keys(next) as Array<keyof KeepStoreState<TMeta>>) {\n if (!Object.is(this.state[key], next[key])) {\n changed = true;\n break;\n }\n }\n if (!changed) return;\n this.state = { ...this.state, ...next };\n for (const listener of this.listeners) listener();\n }\n}\n"],"mappings":";AAiCO,SAAS,eACd,QACA,UAAkC,CAAC,GACN;AAC7B,QAAM,WAAW,OAAO,OAAO,CAAC,SAAS;AACvC,UAAM,CAAC,MAAM,EAAE,IAAI,QAAQ,gBAAgB,CAAC;AAC5C,UAAM,UAAU,KAAK;AACrB,UAAM,aAAa,SAAS,SAAY,SAAY,YAAY,IAAI;AACpE,UAAM,aAAa,OAAO,SAAY,SAAY,YAAY,EAAE;AAChE,YACG,QAAQ,eAAe,UAAa,KAAK,eAAe,QAAQ,gBAChE,QAAQ,QAAQ,UAAa,KAAK,MAAM,SAAS,QAAQ,GAAG,MAAM,UAClE,QAAQ,SAAS,UAAa,QAAQ,KAAK,MAAM,CAAC,QAAQ,KAAK,MAAM,SAAS,GAAG,CAAC,OAClF,eAAe,UAAa,WAAW,gBACvC,eAAe,UAAa,WAAW,eACxC,cAAc,MAAM,QAAQ,aAAa,QAAQ,MAAM,MACtD,QAAQ,SAAS,IAAI,KAAK,UAC1B,QAAQ,WAAW,IAAI,KAAK;AAAA,EAEjC,CAAC;AACD,QAAM,YAAY,aAAa,QAAQ;AACvC,QAAM,SAAS,QAAQ,UAAU,QAAQ,MAAM;AAC/C,QAAM,aAAa,QAAQ,SAAS,QAAQ,MAAM,eAAe,QAAQ,IAAI;AAC7E,QAAM,SAAS,SAAS,CAAC,GAAG,QAAQ,EAAE,KAAK,CAAC,GAAG,OAAO,EAAE,MAAM,IAAI,EAAE,MAAM,KAAK,SAAS,IAAI;AAC5F,QAAM,SAAS,KAAK,IAAI,GAA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ACV;AAAA,MACF;AAAA,IACF;AACA,QAAI,CAAC,QAAS;AACd,SAAK,QAAQ,EAAE,GAAG,KAAK,OAAO,GAAG,KAAK;AACtC,eAAW,YAAY,KAAK,UAAW,UAAS;AAAA,EAClD;AACF;","names":[]}
@@ -904,7 +904,7 @@ var IndexedDBAdapter = class {
904
904
  };
905
905
  function isKeepItemArray(value) {
906
906
  return Array.isArray(value) && value.every(
907
- (item) => isRecord2(item) && typeof item.id === "string" && typeof item.savedAt === "number" && Number.isFinite(item.savedAt) && typeof item.updatedAt === "number" && Number.isFinite(item.updatedAt) && "meta" in item && (item.targetType === void 0 || typeof item.targetType === "string") && (item.note === void 0 || typeof item.note === "string") && (item.schemaVersion === void 0 || typeof item.schemaVersion === "number" && Number.isFinite(item.schemaVersion)) && (item.revision === void 0 || typeof item.revision === "string") && (item.tags === void 0 || Array.isArray(item.tags) && item.tags.every((tag) => typeof tag === "string"))
907
+ (item) => isRecord2(item) && typeof item.id === "string" && typeof item.savedAt === "number" && Number.isFinite(item.savedAt) && typeof item.updatedAt === "number" && Number.isFinite(item.updatedAt) && "meta" in item && (item.targetType === void 0 || typeof item.targetType === "string") && (item.note === void 0 || typeof item.note === "string") && (item.schemaVersion === void 0 || typeof item.schemaVersion === "number" && Number.isFinite(item.schemaVersion)) && (item.revision === void 0 || typeof item.revision === "string") && (item.metaUpdatedAt === void 0 || typeof item.metaUpdatedAt === "number" && Number.isFinite(item.metaUpdatedAt)) && (item.tags === void 0 || Array.isArray(item.tags) && item.tags.every((tag) => typeof tag === "string"))
908
908
  );
909
909
  }
910
910
  function isRecord2(value) {
@@ -965,4 +965,4 @@ export {
965
965
  LocalStorageAdapter,
966
966
  IndexedDBAdapter
967
967
  };
968
- //# sourceMappingURL=chunk-C3SOQCVW.js.map
968
+ //# sourceMappingURL=chunk-X4UVKTBK.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/types.ts","../src/storage/sync.ts","../src/storage/index.ts"],"sourcesContent":["export type KeepItem<TMeta = Record<string, unknown>> = {\n id: string;\n savedAt: number;\n updatedAt: number;\n meta: TMeta;\n targetType?: string;\n note?: string;\n tags?: string[];\n schemaVersion?: number;\n /** Optional server-provided revision used by synchronizing adapters. */\n revision?: string;\n /** Timestamp for the last successful refresh of source metadata. */\n metaUpdatedAt?: number;\n};\n\nexport type KeepItemInput<TMeta = Record<string, unknown>> = Omit<KeepItem<TMeta>, \"id\" | \"savedAt\" | \"updatedAt\">;\n\nexport interface StorageAdapter<TMeta = Record<string, unknown>> {\n getAll(): Promise<KeepItem<TMeta>[]>;\n set(item: KeepItem<TMeta>): Promise<void>;\n setMany?(items: KeepItem<TMeta>[]): Promise<void>;\n remove(id: string): Promise<void>;\n removeMany?(ids: string[]): Promise<void>;\n clear(): Promise<void>;\n merge?(localItems: KeepItem<TMeta>[]): Promise<KeepItem<TMeta>[]>;\n subscribe?(listener: () => void): () => void;\n readonly storageKey?: string;\n}\n\nexport type KeepAction =\n | \"refresh\"\n | \"save\"\n | \"updateNote\"\n | \"updateTags\"\n | \"updateTagsBatch\"\n | \"revalidate\"\n | \"remove\"\n | \"removeBatch\"\n | \"clear\";\n\nexport type KeepChangePhase = \"local\" | \"synced\";\n\nexport type KeepChangeContext<TMeta = Record<string, unknown>> = {\n action: KeepAction;\n id?: string;\n item?: KeepItem<TMeta>;\n items?: KeepItem<TMeta>[];\n phase: KeepChangePhase;\n};\n\nexport type KeepPluginContext<TMeta = Record<string, unknown>> = {\n action: KeepAction;\n id?: string;\n item?: KeepItem<TMeta>;\n items?: KeepItem<TMeta>[];\n};\n\nexport type KeepPlugin<TMeta = Record<string, unknown>> = {\n name?: string;\n before?: (context: KeepPluginContext<TMeta>) => void | Promise<void>;\n after?: (context: KeepPluginContext<TMeta>) => void | Promise<void>;\n onError?: (error: unknown, context: KeepErrorContext) => void;\n};\n\nexport type KeepSchemaParseResult<T> = { success: true; data: T } | { success: false; error?: unknown };\n\nexport type KeepSchema<T> =\n | { parse: (value: unknown) => T | Promise<T> }\n | { safeParse: (value: unknown) => KeepSchemaParseResult<T> | Promise<KeepSchemaParseResult<T>> }\n | {\n \"~standard\": {\n validate: (\n value: unknown,\n ) => { value?: T; issues?: readonly unknown[] } | Promise<{ value?: T; issues?: readonly unknown[] }>;\n };\n };\n\nexport type KeepInvalidItemPolicy = \"error\" | \"drop\";\n\nexport type KeepSyncStatus = \"idle\" | \"pending\" | \"syncing\" | \"synced\" | \"conflict\" | \"error\";\n\nexport type KeepSyncState = {\n status: KeepSyncStatus;\n pendingCount: number;\n conflictIds: string[];\n lastSyncedAt?: number;\n error?: unknown;\n};\n\nexport type SyncOperation<TMeta = Record<string, unknown>> = {\n operationId: string;\n type: \"upsert\" | \"remove\";\n id: string;\n item?: KeepItem<TMeta>;\n createdAt: number;\n baseRevision?: string;\n};\n\nexport type RemoteSyncResult<TMeta = Record<string, unknown>> =\n | { type: \"synced\"; item?: KeepItem<TMeta>; revision?: string }\n | { type: \"conflict\"; remote: KeepItem<TMeta>; revision?: string };\n\nexport type KeepConflictContext<TMeta = Record<string, unknown>> = {\n operation: SyncOperation<TMeta>;\n remoteRevision?: string;\n};\n\nexport type KeepConflictResolver<TMeta = Record<string, unknown>> = (\n local: KeepItem<TMeta> | undefined,\n remote: KeepItem<TMeta>,\n context: KeepConflictContext<TMeta>,\n) => KeepItem<TMeta> | undefined | Promise<KeepItem<TMeta> | undefined>;\n\nexport interface RemoteSyncDriver<TMeta = Record<string, unknown>> {\n push(operation: SyncOperation<TMeta>): Promise<RemoteSyncResult<TMeta>>;\n pull?: () => Promise<KeepItem<TMeta>[]>;\n}\n\nexport interface SyncQueueAdapter<TMeta = Record<string, unknown>> {\n getAll(): Promise<SyncOperation<TMeta>[]>;\n setMany(operations: SyncOperation<TMeta>[]): Promise<void>;\n remove(operationIds: string[]): Promise<void>;\n clear(): Promise<void>;\n}\n\nexport interface SyncCapableStorageAdapter<TMeta = Record<string, unknown>> extends StorageAdapter<TMeta> {\n getSyncState(): KeepSyncState;\n subscribeSync(listener: () => void): () => void;\n flushSync(): Promise<void>;\n dispose?(): void;\n}\n\nexport type KeepStorageOperation = \"getAll\" | \"set\" | \"remove\" | \"clear\" | \"merge\";\n\nexport class KeepStorageError extends Error {\n readonly operation: KeepStorageOperation;\n readonly storageKey?: string;\n readonly cause?: unknown;\n\n constructor(\n message: string,\n options: {\n operation: KeepStorageOperation;\n storageKey?: string;\n cause?: unknown;\n },\n ) {\n super(message);\n this.name = \"KeepStorageError\";\n this.operation = options.operation;\n this.storageKey = options.storageKey;\n if (options.cause !== undefined) this.cause = options.cause;\n }\n}\n\nexport class KeepStorageQuotaError extends KeepStorageError {\n constructor(options: { operation: KeepStorageOperation; storageKey?: string; cause?: unknown }) {\n super(\"KeepKit storage quota was exceeded.\", options);\n this.name = \"KeepStorageQuotaError\";\n }\n}\n\nexport class KeepStorageAccessError extends KeepStorageError {\n constructor(options: { operation: KeepStorageOperation; storageKey?: string; cause?: unknown }) {\n super(\"KeepKit could not access the configured storage.\", options);\n this.name = \"KeepStorageAccessError\";\n }\n}\n\nexport class KeepStorageParseError extends KeepStorageError {\n constructor(options: { operation: KeepStorageOperation; storageKey?: string; cause?: unknown }) {\n super(\"KeepKit found invalid data in the configured storage.\", options);\n this.name = \"KeepStorageParseError\";\n }\n}\n\nexport type KeepErrorContext = {\n action: KeepAction;\n id?: string;\n};\n\nexport type KeepErrorHandler = (error: unknown, context: KeepErrorContext) => void;\n\nexport type KeepEventHandlers<TMeta = Record<string, unknown>> = {\n onSave?: (item: KeepItem<TMeta>) => void;\n onRemove?: (item: KeepItem<TMeta>) => void;\n onNoteUpdate?: (id: string, note?: string) => void;\n onTagsUpdate?: (id: string, tags?: string[]) => void;\n onChange?: (context: KeepChangeContext<TMeta>) => void | Promise<void>;\n onError?: KeepErrorHandler;\n};\n\nexport function normalizeKeepTags(tags?: string[]): string[] | undefined {\n if (!tags) return undefined;\n const normalized = [...new Set(tags.map((tag) => tag.trim()).filter(Boolean))];\n return normalized.length > 0 ? normalized : undefined;\n}\n","import type {\n KeepConflictResolver,\n KeepItem,\n KeepSyncState,\n RemoteSyncDriver,\n StorageAdapter,\n SyncCapableStorageAdapter,\n SyncOperation,\n SyncQueueAdapter,\n} from \"../types\";\n\nexport type LocalStorageSyncQueueOptions = {\n key?: string;\n storage?: Storage;\n};\n\nexport type IndexedDBSyncQueueOptions = {\n databaseName?: string;\n storeName?: string;\n version?: number;\n indexedDB?: IDBFactory;\n};\n\nexport type FallbackSyncQueueAdapterOptions<TMeta = Record<string, unknown>> = {\n primary: SyncQueueAdapter<TMeta>;\n fallback: SyncQueueAdapter<TMeta>;\n shouldFallback?: (error: unknown) => boolean;\n};\n\nexport type SyncStorageAdapterOptions<TMeta = Record<string, unknown>> = {\n local: StorageAdapter<TMeta>;\n remote: RemoteSyncDriver<TMeta>;\n queue?: SyncQueueAdapter<TMeta>;\n queueKey?: string;\n queueDatabaseName?: string;\n clientId?: string;\n now?: () => number;\n resolveConflict?: KeepConflictResolver<TMeta>;\n};\n\nexport const DEFAULT_SYNC_QUEUE_KEY = \"keepkit:sync-queue\";\nexport const DEFAULT_SYNC_QUEUE_DATABASE = \"keepkit-sync\";\nexport const DEFAULT_SYNC_QUEUE_STORE = \"sync-queue\";\n\nexport class LocalStorageSyncQueueAdapter<TMeta = Record<string, unknown>> implements SyncQueueAdapter<TMeta> {\n private readonly key: string;\n private readonly storage: Storage | undefined;\n\n constructor(options: LocalStorageSyncQueueOptions = {}) {\n this.key = options.key ?? DEFAULT_SYNC_QUEUE_KEY;\n this.storage = options.storage ?? getBrowserStorage();\n }\n\n async getAll(): Promise<SyncOperation<TMeta>[]> {\n if (!this.storage) return [];\n const raw = this.storage.getItem(this.key);\n if (!raw) return [];\n let value: unknown;\n try {\n value = JSON.parse(raw);\n } catch (cause) {\n throw Object.assign(new Error(\"KeepKit sync queue contains invalid JSON.\"), { cause });\n }\n if (!Array.isArray(value) || !value.every(isSyncOperation)) {\n throw new Error(\"KeepKit sync queue contains invalid operations.\");\n }\n return value as SyncOperation<TMeta>[];\n }\n\n async setMany(operations: SyncOperation<TMeta>[]): Promise<void> {\n if (!this.storage) return;\n this.storage.setItem(this.key, JSON.stringify(operations));\n }\n\n async remove(operationIds: string[]): Promise<void> {\n const ids = new Set(operationIds);\n const current = await this.getAll();\n await this.setMany(current.filter((operation) => !ids.has(operation.operationId)));\n }\n\n async clear(): Promise<void> {\n this.storage?.removeItem(this.key);\n }\n}\n\nexport class IndexedDBSyncQueueAdapter<TMeta = Record<string, unknown>> implements SyncQueueAdapter<TMeta> {\n private readonly databaseName: string;\n private readonly storeName: string;\n private readonly version: number;\n private readonly indexedDB: IDBFactory | undefined;\n private databasePromise: Promise<IDBDatabase | undefined> | undefined;\n\n constructor(options: IndexedDBSyncQueueOptions = {}) {\n this.databaseName = options.databaseName ?? DEFAULT_SYNC_QUEUE_DATABASE;\n this.storeName = options.storeName ?? DEFAULT_SYNC_QUEUE_STORE;\n this.version = options.version ?? 1;\n this.indexedDB = options.indexedDB ?? getBrowserIndexedDB();\n }\n\n async getAll(): Promise<SyncOperation<TMeta>[]> {\n const database = await this.open();\n if (!database) return [];\n const transaction = database.transaction(this.storeName, \"readonly\");\n const value: unknown = await requestToPromise(transaction.objectStore(this.storeName).getAll());\n if (!Array.isArray(value) || !value.every(isSyncOperation)) {\n throw new Error(\"KeepKit sync queue contains invalid operations.\");\n }\n return value as SyncOperation<TMeta>[];\n }\n\n async setMany(operations: SyncOperation<TMeta>[]): Promise<void> {\n const database = await this.open();\n if (!database) return;\n const transaction = database.transaction(this.storeName, \"readwrite\");\n const store = transaction.objectStore(this.storeName);\n for (const operation of operations) store.put(operation);\n await transactionToPromise(transaction);\n }\n\n async remove(operationIds: string[]): Promise<void> {\n const database = await this.open();\n if (!database) return;\n const transaction = database.transaction(this.storeName, \"readwrite\");\n const store = transaction.objectStore(this.storeName);\n for (const operationId of new Set(operationIds)) store.delete(operationId);\n await transactionToPromise(transaction);\n }\n\n async clear(): Promise<void> {\n const database = await this.open();\n if (!database) return;\n const transaction = database.transaction(this.storeName, \"readwrite\");\n transaction.objectStore(this.storeName).clear();\n await transactionToPromise(transaction);\n }\n\n private open(): Promise<IDBDatabase | undefined> {\n if (!this.indexedDB) return Promise.resolve(undefined);\n if (!this.databasePromise) {\n this.databasePromise = new Promise((resolve, reject) => {\n let request: IDBOpenDBRequest;\n try {\n request = this.indexedDB?.open(this.databaseName, this.version) as IDBOpenDBRequest;\n } catch (cause) {\n reject(cause);\n return;\n }\n request.onupgradeneeded = () => {\n if (!request.result.objectStoreNames.contains(this.storeName)) {\n request.result.createObjectStore(this.storeName, { keyPath: \"operationId\" });\n }\n };\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error);\n request.onblocked = () => reject(request.error ?? new Error(\"IndexedDB open was blocked.\"));\n });\n }\n return this.databasePromise.catch((cause) => {\n this.databasePromise = undefined;\n throw cause;\n });\n }\n}\n\n/** Keeps the durable sync queue available when IndexedDB is blocked or fails. */\nexport class FallbackSyncQueueAdapter<TMeta = Record<string, unknown>> implements SyncQueueAdapter<TMeta> {\n private readonly primary: SyncQueueAdapter<TMeta>;\n private readonly fallback: SyncQueueAdapter<TMeta>;\n private readonly shouldFallback: (error: unknown) => boolean;\n private active: \"primary\" | \"fallback\" = \"primary\";\n\n constructor(options: FallbackSyncQueueAdapterOptions<TMeta>) {\n this.primary = options.primary;\n this.fallback = options.fallback;\n this.shouldFallback = options.shouldFallback ?? (() => true);\n }\n\n get isUsingFallback(): boolean {\n return this.active === \"fallback\";\n }\n\n getAll(): Promise<SyncOperation<TMeta>[]> {\n return this.execute((adapter) => adapter.getAll());\n }\n\n setMany(operations: SyncOperation<TMeta>[]): Promise<void> {\n return this.execute((adapter) => adapter.setMany(operations));\n }\n\n remove(operationIds: string[]): Promise<void> {\n return this.execute((adapter) => adapter.remove(operationIds));\n }\n\n clear(): Promise<void> {\n return this.execute((adapter) => adapter.clear());\n }\n\n private async execute<TResult>(operation: (adapter: SyncQueueAdapter<TMeta>) => Promise<TResult>): Promise<TResult> {\n const adapter = this.active === \"primary\" ? this.primary : this.fallback;\n try {\n return await operation(adapter);\n } catch (error) {\n if (this.active !== \"primary\" || !this.shouldFallback(error)) throw error;\n this.active = \"fallback\";\n return operation(this.fallback);\n }\n }\n}\n\n/** A local-first adapter that persists remote operations until they are acknowledged. */\nexport class SyncStorageAdapter<TMeta = Record<string, unknown>> implements SyncCapableStorageAdapter<TMeta> {\n readonly storageKey?: string;\n private readonly local: StorageAdapter<TMeta>;\n private readonly remote: RemoteSyncDriver<TMeta>;\n private readonly queue: SyncQueueAdapter<TMeta>;\n private readonly clientId: string;\n private readonly now: () => number;\n private readonly resolveConflict?: KeepConflictResolver<TMeta>;\n private readonly listeners = new Set<() => void>();\n private readonly dataListeners = new Set<() => void>();\n private queueItems: SyncOperation<TMeta>[] = [];\n private queueLoaded = false;\n private queueLoadPromise: Promise<void> | undefined;\n private flushPromise: Promise<void> | undefined;\n private state: KeepSyncState = { status: \"idle\", pendingCount: 0, conflictIds: [] };\n private onlineHandler?: () => void;\n\n constructor(options: SyncStorageAdapterOptions<TMeta>) {\n this.local = options.local;\n this.remote = options.remote;\n this.queue = options.queue ?? createDefaultQueue<TMeta>(options);\n this.clientId = options.clientId ?? createId();\n this.now = options.now ?? Date.now;\n this.resolveConflict = options.resolveConflict;\n this.storageKey = this.local.storageKey;\n if (typeof window !== \"undefined\") {\n this.onlineHandler = () => void this.flushSync();\n window.addEventListener(\"online\", this.onlineHandler);\n }\n void this.resumeQueue();\n }\n\n getSyncState = (): KeepSyncState => this.state;\n\n subscribeSync = (listener: () => void): (() => void) => {\n this.listeners.add(listener);\n return () => this.listeners.delete(listener);\n };\n\n subscribe = (listener: () => void): (() => void) => {\n this.dataListeners.add(listener);\n const unsubscribeLocal = this.local.subscribe?.(listener) ?? (() => undefined);\n return () => {\n this.dataListeners.delete(listener);\n unsubscribeLocal();\n };\n };\n\n getAll(): Promise<KeepItem<TMeta>[]> {\n return this.local.getAll();\n }\n\n async set(item: KeepItem<TMeta>): Promise<void> {\n const operation = this.createOperation(\"upsert\", item.id, item);\n await this.enqueueBeforeLocalWrite(operation);\n try {\n await this.local.set(item);\n } catch (cause) {\n await this.removeQueued(operation.operationId);\n throw cause;\n }\n this.notifyDataListeners();\n this.setPendingState();\n }\n\n async setMany(items: KeepItem<TMeta>[]): Promise<void> {\n const operations = items.map((item) => this.createOperation(\"upsert\", item.id, item));\n await this.enqueueManyBeforeLocalWrite(operations);\n try {\n if (this.local.setMany) await this.local.setMany(items);\n else for (const item of items) await this.local.set(item);\n } catch (cause) {\n await this.removeQueued(operations.map((operation) => operation.operationId));\n throw cause;\n }\n this.notifyDataListeners();\n this.setPendingState();\n }\n\n async remove(id: string): Promise<void> {\n const operation = this.createOperation(\"remove\", id);\n await this.enqueueBeforeLocalWrite(operation);\n try {\n await this.local.remove(id);\n } catch (cause) {\n await this.removeQueued(operation.operationId);\n throw cause;\n }\n this.notifyDataListeners();\n this.setPendingState();\n }\n\n async removeMany(ids: string[]): Promise<void> {\n const operations = [...new Set(ids)].map((id) => this.createOperation(\"remove\", id));\n await this.enqueueManyBeforeLocalWrite(operations);\n try {\n if (this.local.removeMany) await this.local.removeMany(ids);\n else for (const id of ids) await this.local.remove(id);\n } catch (cause) {\n await this.removeQueued(operations.map((operation) => operation.operationId));\n throw cause;\n }\n this.notifyDataListeners();\n this.setPendingState();\n }\n\n async clear(): Promise<void> {\n const items = await this.local.getAll();\n await this.removeMany(items.map((item) => item.id));\n await this.local.clear();\n }\n\n async merge(localItems: KeepItem<TMeta>[]): Promise<KeepItem<TMeta>[]> {\n const merged = this.local.merge\n ? await this.local.merge(localItems)\n : await mergeLocalItems(localItems, this.local);\n await this.setMany(localItems);\n return merged;\n }\n\n async flushSync(): Promise<void> {\n if (this.flushPromise) return this.flushPromise;\n this.flushPromise = this.runFlush().finally(() => {\n this.flushPromise = undefined;\n });\n return this.flushPromise;\n }\n\n dispose(): void {\n if (this.onlineHandler) window.removeEventListener(\"online\", this.onlineHandler);\n this.listeners.clear();\n this.dataListeners.clear();\n }\n\n private async runFlush(): Promise<void> {\n await this.loadQueue();\n if (!(await this.pullRemote())) return;\n if (this.queueItems.length === 0) {\n this.updateState({ status: \"synced\", pendingCount: 0, error: undefined });\n return;\n }\n this.updateState({ status: \"syncing\", error: undefined });\n for (const operation of [...this.queueItems]) {\n try {\n const result = await this.remote.push(operation);\n if (result.type === \"conflict\") {\n const local = operation.item;\n const resolved = this.resolveConflict\n ? await this.resolveConflict(local, result.remote, {\n operation,\n remoteRevision: result.revision,\n })\n : undefined;\n if (!resolved) {\n this.updateState({\n status: \"conflict\",\n conflictIds: [...new Set([...this.state.conflictIds, operation.id])],\n });\n continue;\n }\n const retry = this.createOperation(\"upsert\", resolved.id, {\n ...resolved,\n revision: result.revision ?? resolved.revision,\n });\n await this.local.set(retry.item as KeepItem<TMeta>);\n await this.replaceQueued(operation, retry);\n continue;\n }\n if (result.item) {\n await this.local.set({\n ...result.item,\n ...(result.revision ? { revision: result.revision } : {}),\n });\n this.notifyDataListeners();\n }\n await this.removeQueued(operation.operationId);\n this.updateState({\n status: this.queueItems.length > 0 ? \"syncing\" : \"synced\",\n lastSyncedAt: this.now(),\n conflictIds: this.state.conflictIds.filter((id) => id !== operation.id),\n });\n } catch (error) {\n this.updateState({ status: \"error\", error });\n return;\n }\n }\n if (this.queueItems.length === 0) this.updateState({ status: \"synced\", pendingCount: 0 });\n }\n\n private createOperation(\n type: SyncOperation<TMeta>[\"type\"],\n id: string,\n item?: KeepItem<TMeta>,\n ): SyncOperation<TMeta> {\n return {\n operationId: `${this.clientId}:${this.now()}:${createId()}`,\n type,\n id,\n ...(item ? { item } : {}),\n createdAt: this.now(),\n ...(item?.revision ? { baseRevision: item.revision } : {}),\n };\n }\n\n private async enqueueBeforeLocalWrite(operation: SyncOperation<TMeta>): Promise<void> {\n await this.enqueueManyBeforeLocalWrite([operation]);\n }\n\n private async enqueueManyBeforeLocalWrite(operations: SyncOperation<TMeta>[]): Promise<void> {\n await this.loadQueue();\n const next = [...this.queueItems];\n for (const operation of operations) {\n for (let index = next.length - 1; index >= 0; index -= 1) {\n if (next[index]?.id !== operation.id) continue;\n next.splice(index, 1);\n }\n next.push(operation);\n }\n await this.persistQueue(next);\n this.updateState({ status: \"pending\", pendingCount: this.queueItems.length });\n }\n\n private async loadQueue(): Promise<void> {\n if (this.queueLoaded) return;\n if (!this.queueLoadPromise) {\n this.queueLoadPromise = this.queue\n .getAll()\n .then((items) => {\n this.queueItems = items;\n this.queueLoaded = true;\n })\n .catch((error) => {\n this.queueLoadPromise = undefined;\n throw error;\n });\n }\n await this.queueLoadPromise;\n }\n\n private async resumeQueue(): Promise<void> {\n try {\n await this.loadQueue();\n if (this.queueItems.length === 0) return;\n this.updateState({ status: \"pending\", pendingCount: this.queueItems.length });\n if (isBrowserOnline()) await this.flushSync();\n } catch (error) {\n this.updateState({ status: \"error\", error });\n }\n }\n\n private async persistQueue(next: SyncOperation<TMeta>[]): Promise<void> {\n const previousIds = new Set(this.queueItems.map((operation) => operation.operationId));\n const nextIds = new Set(next.map((operation) => operation.operationId));\n const removed = [...previousIds].filter((id) => !nextIds.has(id));\n if (removed.length > 0) await this.queue.remove(removed);\n if (next.length > 0) await this.queue.setMany(next);\n this.queueItems = next;\n }\n\n private async removeQueued(operationIds: string | string[]): Promise<void> {\n await this.loadQueue();\n const ids = new Set(typeof operationIds === \"string\" ? [operationIds] : operationIds);\n await this.queue.remove([...ids]);\n this.queueItems = this.queueItems.filter((operation) => !ids.has(operation.operationId));\n this.setPendingState();\n }\n\n private async replaceQueued(previous: SyncOperation<TMeta>, next: SyncOperation<TMeta>): Promise<void> {\n await this.persistQueue(\n this.queueItems.map((operation) => (operation.operationId === previous.operationId ? next : operation)),\n );\n this.setPendingState();\n }\n\n private setPendingState(): void {\n this.updateState({\n status: this.queueItems.length > 0 ? \"pending\" : \"synced\",\n pendingCount: this.queueItems.length,\n });\n }\n\n private async pullRemote(): Promise<boolean> {\n if (!this.remote.pull) return true;\n try {\n const remoteItems = await this.remote.pull();\n const pendingIds = new Set(this.queueItems.map((operation) => operation.id));\n const localItems = await this.local.getAll();\n const localById = new Map(localItems.map((item) => [item.id, item]));\n const incoming = remoteItems.filter((item) => {\n const current = localById.get(item.id);\n return !pendingIds.has(item.id) && (!current || item.updatedAt >= current.updatedAt);\n });\n if (incoming.length === 0) return true;\n if (this.local.setMany) await this.local.setMany(incoming);\n else for (const item of incoming) await this.local.set(item);\n this.notifyDataListeners();\n return true;\n } catch (error) {\n this.updateState({ status: \"error\", error });\n return false;\n }\n }\n\n private notifyDataListeners(): void {\n for (const listener of this.dataListeners) listener();\n }\n\n private updateState(next: Partial<KeepSyncState>): void {\n this.state = {\n ...this.state,\n ...next,\n pendingCount: next.pendingCount ?? this.queueItems.length,\n };\n for (const listener of this.listeners) listener();\n }\n}\n\nasync function mergeLocalItems<TMeta>(\n localItems: KeepItem<TMeta>[],\n target: StorageAdapter<TMeta>,\n): Promise<KeepItem<TMeta>[]> {\n const remoteItems = await target.getAll();\n const byId = new Map(remoteItems.map((item) => [item.id, item]));\n for (const item of localItems) {\n const current = byId.get(item.id);\n if (!current || item.updatedAt > current.updatedAt) byId.set(item.id, item);\n }\n const merged = [...byId.values()].sort((a, b) => b.updatedAt - a.updatedAt);\n if (target.setMany) await target.setMany(merged);\n else for (const item of merged) await target.set(item);\n return merged;\n}\n\nfunction isSyncOperation(value: unknown): value is SyncOperation {\n if (!isRecord(value)) return false;\n return (\n typeof value.operationId === \"string\" &&\n (value.type === \"upsert\" || value.type === \"remove\") &&\n typeof value.id === \"string\" &&\n typeof value.createdAt === \"number\"\n );\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null;\n}\n\nfunction createId(): string {\n if (typeof crypto !== \"undefined\" && \"randomUUID\" in crypto) return crypto.randomUUID();\n return Math.random().toString(36).slice(2);\n}\n\nfunction getBrowserStorage(): Storage | undefined {\n if (typeof window === \"undefined\") return undefined;\n try {\n return window.localStorage;\n } catch {\n return undefined;\n }\n}\n\nfunction getBrowserIndexedDB(): IDBFactory | undefined {\n if (typeof indexedDB === \"undefined\") return undefined;\n return indexedDB;\n}\n\nfunction isBrowserOnline(): boolean {\n return typeof navigator === \"undefined\" || navigator.onLine !== false;\n}\n\nfunction createDefaultQueue<TMeta>(options: SyncStorageAdapterOptions<TMeta>): SyncQueueAdapter<TMeta> {\n const queueKey = options.queueKey ?? `${DEFAULT_SYNC_QUEUE_KEY}:${options.local.storageKey ?? \"default\"}`;\n const fallback = new LocalStorageSyncQueueAdapter<TMeta>({ key: queueKey });\n const indexedDB = getBrowserIndexedDB();\n if (!indexedDB) return fallback;\n return new FallbackSyncQueueAdapter<TMeta>({\n primary: new IndexedDBSyncQueueAdapter<TMeta>({\n databaseName: options.queueDatabaseName,\n indexedDB,\n }),\n fallback,\n });\n}\n\nfunction requestToPromise<T>(request: IDBRequest<T>): Promise<T> {\n return new Promise((resolve, reject) => {\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error);\n });\n}\n\nfunction transactionToPromise(transaction: IDBTransaction): Promise<void> {\n return new Promise((resolve, reject) => {\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error);\n transaction.onabort = () => reject(transaction.error ?? new Error(\"IndexedDB transaction aborted.\"));\n });\n}\n","import {\n type KeepItem,\n KeepStorageAccessError,\n KeepStorageError,\n type KeepStorageOperation,\n KeepStorageParseError,\n KeepStorageQuotaError,\n type StorageAdapter,\n} from \"../types\";\n\nexport const DEFAULT_STORAGE_KEY = \"keepkit:items\";\n\nexport type LocalStorageAdapterOptions = {\n key?: string;\n storage?: Storage;\n};\n\nexport type IndexedDBAdapterOptions = {\n databaseName?: string;\n dbName?: string;\n /** Alias for databaseName, useful when switching from LocalStorageAdapter. */\n key?: string;\n storeName?: string;\n version?: number;\n indexedDB?: IDBFactory;\n};\n\nexport const DEFAULT_INDEXEDDB_DATABASE = \"keepkit\";\nexport const DEFAULT_INDEXEDDB_STORE = \"items\";\n\nexport type StorageAdapterFactoryOptions<TMeta = Record<string, unknown>> = {\n getAll: () => KeepItem<TMeta>[] | Promise<KeepItem<TMeta>[]>;\n set: (item: KeepItem<TMeta>) => void | Promise<void>;\n setMany?: (items: KeepItem<TMeta>[]) => void | Promise<void>;\n remove: (id: string) => void | Promise<void>;\n removeMany?: (ids: string[]) => void | Promise<void>;\n clear: () => void | Promise<void>;\n merge?: (localItems: KeepItem<TMeta>[]) => KeepItem<TMeta>[] | Promise<KeepItem<TMeta>[]>;\n subscribe?: (listener: () => void) => undefined | (() => void);\n storageKey?: string;\n};\n\nexport type FallbackStorageAdapterOptions<TMeta = Record<string, unknown>> = {\n primary: StorageAdapter<TMeta>;\n fallback: StorageAdapter<TMeta>;\n /** Decide which primary adapter failures should activate the fallback. */\n shouldFallback?: (error: unknown) => boolean;\n onFallback?: (error: unknown) => void;\n /** Copy fallback data into an empty primary on the first read. */\n migrateFallbackOnEmpty?: boolean;\n /** Keep the fallback current while the primary is healthy. */\n mirrorWrites?: boolean;\n};\n\n/**\n * A storage adapter that switches to a fallback after an availability failure.\n * Parse errors remain visible so corrupt data is never silently hidden.\n */\nexport class FallbackStorageAdapter<TMeta = Record<string, unknown>> implements StorageAdapter<TMeta> {\n public readonly storageKey: string | undefined;\n private readonly primary: StorageAdapter<TMeta>;\n private readonly fallback: StorageAdapter<TMeta>;\n private readonly shouldFallback: (error: unknown) => boolean;\n private readonly onFallback?: (error: unknown) => void;\n private readonly migrateFallbackOnEmpty: boolean;\n private readonly mirrorWrites: boolean;\n private active: \"primary\" | \"fallback\" = \"primary\";\n private readonly listeners = new Set<() => void>();\n\n constructor(options: FallbackStorageAdapterOptions<TMeta>) {\n this.primary = options.primary;\n this.fallback = options.fallback;\n this.shouldFallback = options.shouldFallback ?? isRecoverableStorageError;\n this.onFallback = options.onFallback;\n this.migrateFallbackOnEmpty = options.migrateFallbackOnEmpty ?? false;\n this.mirrorWrites = options.mirrorWrites ?? false;\n this.storageKey = options.fallback.storageKey ?? options.primary.storageKey;\n }\n\n get isUsingFallback(): boolean {\n return this.active === \"fallback\";\n }\n\n getAll(): Promise<KeepItem<TMeta>[]> {\n return this.readAll();\n }\n\n set(item: KeepItem<TMeta>): Promise<void> {\n return this.executeWrite((adapter) => adapter.set(item));\n }\n\n setMany(items: KeepItem<TMeta>[]): Promise<void> {\n return this.executeWrite((adapter) =>\n adapter.setMany\n ? adapter.setMany(items)\n : Promise.all(items.map((item) => adapter.set(item))).then(() => undefined),\n );\n }\n\n remove(id: string): Promise<void> {\n return this.executeWrite((adapter) => adapter.remove(id));\n }\n\n removeMany(ids: string[]): Promise<void> {\n return this.executeWrite((adapter) =>\n adapter.removeMany\n ? adapter.removeMany(ids)\n : Promise.all(ids.map((id) => adapter.remove(id))).then(() => undefined),\n );\n }\n\n clear(): Promise<void> {\n return this.executeWrite((adapter) => adapter.clear());\n }\n\n merge(localItems: KeepItem<TMeta>[]): Promise<KeepItem<TMeta>[]> {\n return this.execute(async (adapter) => {\n const merged = adapter.merge ? await adapter.merge(localItems) : await mergeItems(adapter, localItems);\n if (this.active === \"primary\" && this.mirrorWrites) {\n try {\n if (this.fallback.setMany) await this.fallback.setMany(merged);\n else for (const item of merged) await this.fallback.set(item);\n } catch {\n // The fallback is best-effort while the primary is healthy.\n }\n }\n return merged;\n });\n }\n\n subscribe(listener: () => void): () => void {\n this.listeners.add(listener);\n const notify = (source: \"primary\" | \"fallback\") => {\n if (source === this.active) listener();\n };\n const unsubscribePrimary = this.primary.subscribe?.(() => notify(\"primary\"));\n const unsubscribeFallback = this.fallback.subscribe?.(() => notify(\"fallback\"));\n return () => {\n this.listeners.delete(listener);\n unsubscribePrimary?.();\n unsubscribeFallback?.();\n };\n }\n\n private async readAll(): Promise<KeepItem<TMeta>[]> {\n if (this.active === \"fallback\") return this.fallback.getAll();\n let items: KeepItem<TMeta>[];\n try {\n items = await this.primary.getAll();\n } catch (error) {\n if (!this.shouldFallback(error)) throw error;\n this.activateFallback(error);\n return this.fallback.getAll();\n }\n if (!this.migrateFallbackOnEmpty || items.length > 0) return items;\n\n const fallbackItems = await this.fallback.getAll();\n if (fallbackItems.length === 0) return items;\n try {\n if (this.primary.setMany) await this.primary.setMany(fallbackItems);\n else for (const item of fallbackItems) await this.primary.set(item);\n return fallbackItems;\n } catch (error) {\n if (!this.shouldFallback(error)) throw error;\n this.activateFallback(error);\n return fallbackItems;\n }\n }\n\n private activateFallback(error: unknown): void {\n this.active = \"fallback\";\n this.onFallback?.(error);\n for (const listener of this.listeners) listener();\n }\n\n private async executeWrite<TResult>(\n operation: (adapter: StorageAdapter<TMeta>) => Promise<TResult>,\n ): Promise<TResult> {\n const result = await this.execute(operation);\n if (this.active === \"primary\" && this.mirrorWrites) {\n try {\n await operation(this.fallback);\n } catch {\n // The fallback is best-effort while the primary is healthy.\n }\n }\n return result;\n }\n\n private async execute<TResult>(operation: (adapter: StorageAdapter<TMeta>) => Promise<TResult>): Promise<TResult> {\n const adapter = this.active === \"primary\" ? this.primary : this.fallback;\n try {\n return await operation(adapter);\n } catch (error) {\n if (this.active !== \"primary\" || !this.shouldFallback(error)) throw error;\n this.activateFallback(error);\n return operation(this.fallback);\n }\n }\n}\n\nexport type BrowserStorageAdapterOptions = {\n key?: string;\n databaseName?: string;\n storeName?: string;\n version?: number;\n indexedDB?: IDBFactory;\n storage?: Storage;\n};\n\n/** IndexedDB-first browser storage with localStorage fallback. */\nexport function createBrowserStorageAdapter<TMeta = Record<string, unknown>>(\n options: BrowserStorageAdapterOptions = {},\n): StorageAdapter<TMeta> {\n const browserIndexedDB = options.indexedDB ?? getBrowserIndexedDB();\n const fallback = new LocalStorageAdapter<TMeta>({ key: options.key, storage: options.storage });\n if (!browserIndexedDB) return fallback;\n return new FallbackStorageAdapter<TMeta>({\n primary: new IndexedDBAdapter<TMeta>({\n databaseName: options.databaseName,\n storeName: options.storeName,\n version: options.version,\n indexedDB: browserIndexedDB,\n }),\n fallback,\n migrateFallbackOnEmpty: true,\n mirrorWrites: true,\n });\n}\n\n/** Adapt sync or async persistence functions to the StorageAdapter contract. */\nexport function createStorageAdapter<TMeta = Record<string, unknown>>(\n options: StorageAdapterFactoryOptions<TMeta>,\n): StorageAdapter<TMeta> {\n const merge = options.merge;\n const subscribe = options.subscribe;\n const setMany = options.setMany;\n const removeMany = options.removeMany;\n return {\n getAll: async () => options.getAll(),\n set: async (item) => options.set(item),\n ...(setMany ? { setMany: async (items: KeepItem<TMeta>[]) => setMany(items) } : {}),\n remove: async (id) => options.remove(id),\n ...(removeMany ? { removeMany: async (ids: string[]) => removeMany(ids) } : {}),\n clear: async () => options.clear(),\n ...(merge ? { merge: async (items: KeepItem<TMeta>[]) => merge(items) } : {}),\n ...(subscribe\n ? {\n subscribe: (listener: () => void) => subscribe(listener) ?? (() => undefined),\n }\n : {}),\n ...(options.storageKey ? { storageKey: options.storageKey } : {}),\n };\n}\n\nasync function mergeItems<TMeta>(\n adapter: StorageAdapter<TMeta>,\n localItems: KeepItem<TMeta>[],\n): Promise<KeepItem<TMeta>[]> {\n const remoteItems = await adapter.getAll();\n const byId = new Map(remoteItems.map((item) => [item.id, item]));\n for (const item of localItems) {\n const current = byId.get(item.id);\n if (!current || item.updatedAt > current.updatedAt) byId.set(item.id, item);\n }\n const merged = [...byId.values()].sort((a, b) => b.updatedAt - a.updatedAt);\n if (adapter.setMany) await adapter.setMany(merged);\n else for (const item of merged) await adapter.set(item);\n return merged;\n}\n\n/** An async StorageAdapter backed by browser localStorage. */\nexport class LocalStorageAdapter<TMeta = Record<string, unknown>> implements StorageAdapter<TMeta> {\n public readonly storageKey: string;\n private readonly storage: Storage | undefined;\n\n constructor(options: LocalStorageAdapterOptions = {}) {\n this.storageKey = options.key ?? DEFAULT_STORAGE_KEY;\n this.storage = options.storage ?? getBrowserStorage();\n }\n\n async getAll(): Promise<KeepItem<TMeta>[]> {\n if (!this.storage) return [];\n\n let raw: string | null;\n try {\n raw = this.storage.getItem(this.storageKey);\n } catch (cause) {\n throw new KeepStorageAccessError({\n operation: \"getAll\",\n storageKey: this.storageKey,\n cause,\n });\n }\n\n if (!raw) return [];\n\n try {\n const value: unknown = JSON.parse(raw);\n if (!isKeepItemArray(value)) {\n throw new KeepStorageParseError({\n operation: \"getAll\",\n storageKey: this.storageKey,\n });\n }\n return value as KeepItem<TMeta>[];\n } catch (cause) {\n if (cause instanceof KeepStorageParseError) throw cause;\n throw new KeepStorageParseError({\n operation: \"getAll\",\n storageKey: this.storageKey,\n cause,\n });\n }\n }\n\n async set(item: KeepItem<TMeta>): Promise<void> {\n await this.setMany([item]);\n }\n\n async setMany(items: KeepItem<TMeta>[]): Promise<void> {\n const current = await this.getAll();\n const byId = new Map(current.map((item) => [item.id, item]));\n for (const item of items) byId.set(item.id, item);\n this.write([...byId.values()], \"set\");\n }\n\n async remove(id: string): Promise<void> {\n await this.removeMany([id]);\n }\n\n async removeMany(ids: string[]): Promise<void> {\n const idSet = new Set(ids);\n const items = await this.getAll();\n this.write(\n items.filter((item) => !idSet.has(item.id)),\n \"remove\",\n );\n }\n\n async clear(): Promise<void> {\n if (!this.storage) return;\n try {\n this.storage.removeItem(this.storageKey);\n } catch (cause) {\n throw new KeepStorageAccessError({\n operation: \"clear\",\n storageKey: this.storageKey,\n cause,\n });\n }\n }\n\n async merge(localItems: KeepItem<TMeta>[]): Promise<KeepItem<TMeta>[]> {\n const remoteItems = await this.getAll();\n const byId = new Map(remoteItems.map((item) => [item.id, item]));\n\n for (const localItem of localItems) {\n const remoteItem = byId.get(localItem.id);\n if (!remoteItem || localItem.updatedAt > remoteItem.updatedAt) {\n byId.set(localItem.id, localItem);\n }\n }\n\n const merged = [...byId.values()].sort((a, b) => b.updatedAt - a.updatedAt);\n this.write(merged, \"merge\");\n return merged;\n }\n\n subscribe(listener: () => void): () => void {\n if (typeof window === \"undefined\") return () => undefined;\n\n const handleStorage = (event: StorageEvent) => {\n if (event.key !== null && event.key !== this.storageKey) return;\n listener();\n };\n\n window.addEventListener(\"storage\", handleStorage);\n return () => window.removeEventListener(\"storage\", handleStorage);\n }\n\n private write(items: KeepItem<TMeta>[], operation: KeepStorageOperation): void {\n if (!this.storage) return;\n try {\n this.storage.setItem(this.storageKey, JSON.stringify(items));\n } catch (cause) {\n if (isQuotaExceededError(cause)) {\n throw new KeepStorageQuotaError({\n operation,\n storageKey: this.storageKey,\n cause,\n });\n }\n throw new KeepStorageAccessError({\n operation,\n storageKey: this.storageKey,\n cause,\n });\n }\n }\n}\n\n/** An async StorageAdapter backed by IndexedDB, with one object store per adapter. */\nexport class IndexedDBAdapter<TMeta = Record<string, unknown>> implements StorageAdapter<TMeta> {\n public readonly storageKey: string;\n private readonly databaseName: string;\n private readonly storeName: string;\n private readonly version: number;\n private readonly indexedDB: IDBFactory | undefined;\n private databasePromise: Promise<IDBDatabase | undefined> | undefined;\n\n constructor(options: IndexedDBAdapterOptions = {}) {\n this.databaseName = options.databaseName ?? options.dbName ?? options.key ?? DEFAULT_INDEXEDDB_DATABASE;\n this.storeName = options.storeName ?? DEFAULT_INDEXEDDB_STORE;\n this.version = options.version ?? 1;\n this.indexedDB = options.indexedDB ?? getBrowserIndexedDB();\n this.storageKey = `${this.databaseName}:${this.storeName}`;\n }\n\n async getAll(): Promise<KeepItem<TMeta>[]> {\n const database = await this.open(\"getAll\");\n if (!database) return [];\n try {\n const transaction = database.transaction(this.storeName, \"readonly\");\n const value: unknown = await requestToPromise(transaction.objectStore(this.storeName).getAll());\n if (!isKeepItemArray(value)) {\n throw new KeepStorageParseError({ operation: \"getAll\", storageKey: this.storageKey });\n }\n return value as KeepItem<TMeta>[];\n } catch (cause) {\n if (cause instanceof KeepStorageParseError) throw cause;\n throw new KeepStorageAccessError({ operation: \"getAll\", storageKey: this.storageKey, cause });\n }\n }\n\n async set(item: KeepItem<TMeta>): Promise<void> {\n return this.setMany([item]);\n }\n\n async setMany(items: KeepItem<TMeta>[]): Promise<void> {\n const database = await this.open(\"set\");\n if (!database) return;\n try {\n const transaction = database.transaction(this.storeName, \"readwrite\");\n const objectStore = transaction.objectStore(this.storeName);\n for (const item of items) objectStore.put(item);\n await transactionToPromise(transaction);\n this.notifySubscribers();\n } catch (cause) {\n if (isQuotaExceededError(cause)) {\n throw new KeepStorageQuotaError({ operation: \"set\", storageKey: this.storageKey, cause });\n }\n throw new KeepStorageAccessError({ operation: \"set\", storageKey: this.storageKey, cause });\n }\n }\n\n async remove(id: string): Promise<void> {\n return this.removeMany([id]);\n }\n\n async removeMany(ids: string[]): Promise<void> {\n const database = await this.open(\"remove\");\n if (!database) return;\n try {\n const transaction = database.transaction(this.storeName, \"readwrite\");\n const objectStore = transaction.objectStore(this.storeName);\n for (const id of new Set(ids)) objectStore.delete(id);\n await transactionToPromise(transaction);\n this.notifySubscribers();\n } catch (cause) {\n throw new KeepStorageAccessError({ operation: \"remove\", storageKey: this.storageKey, cause });\n }\n }\n\n async clear(): Promise<void> {\n const database = await this.open(\"clear\");\n if (!database) return;\n try {\n const transaction = database.transaction(this.storeName, \"readwrite\");\n transaction.objectStore(this.storeName).clear();\n await transactionToPromise(transaction);\n this.notifySubscribers();\n } catch (cause) {\n throw new KeepStorageAccessError({ operation: \"clear\", storageKey: this.storageKey, cause });\n }\n }\n\n async merge(localItems: KeepItem<TMeta>[]): Promise<KeepItem<TMeta>[]> {\n try {\n const remoteItems = await this.getAll();\n const byId = new Map(remoteItems.map((item) => [item.id, item]));\n for (const localItem of localItems) {\n const remoteItem = byId.get(localItem.id);\n if (!remoteItem || localItem.updatedAt > remoteItem.updatedAt) byId.set(localItem.id, localItem);\n }\n const merged = [...byId.values()].sort((a, b) => b.updatedAt - a.updatedAt);\n await this.setMany(merged);\n return merged;\n } catch (cause) {\n if (cause instanceof KeepStorageError) throw cause;\n throw new KeepStorageAccessError({ operation: \"merge\", storageKey: this.storageKey, cause });\n }\n }\n\n subscribe(listener: () => void): () => void {\n if (!this.indexedDB || typeof BroadcastChannel === \"undefined\") return () => undefined;\n const channel = new BroadcastChannel(`keepkit:${this.storageKey}`);\n channel.onmessage = () => listener();\n return () => channel.close();\n }\n\n private notifySubscribers(): void {\n if (!this.indexedDB || typeof BroadcastChannel === \"undefined\") return;\n const channel = new BroadcastChannel(`keepkit:${this.storageKey}`);\n channel.postMessage({ type: \"keepkit:changed\" });\n channel.close();\n }\n\n private open(operation: KeepStorageOperation): Promise<IDBDatabase | undefined> {\n if (!this.indexedDB) return Promise.resolve(undefined);\n if (!this.databasePromise) {\n this.databasePromise = new Promise((resolve, reject) => {\n let request: IDBOpenDBRequest;\n try {\n request = this.indexedDB?.open(this.databaseName, this.version) as IDBOpenDBRequest;\n } catch (cause) {\n reject(new KeepStorageAccessError({ operation, storageKey: this.storageKey, cause }));\n return;\n }\n request.onupgradeneeded = () => {\n if (!request.result.objectStoreNames.contains(this.storeName)) {\n request.result.createObjectStore(this.storeName, { keyPath: \"id\" });\n }\n };\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error);\n request.onblocked = () => reject(request.error ?? new Error(\"IndexedDB open was blocked.\"));\n });\n }\n return this.databasePromise.catch((cause) => {\n this.databasePromise = undefined;\n if (cause instanceof KeepStorageError) throw cause;\n throw new KeepStorageAccessError({ operation, storageKey: this.storageKey, cause });\n });\n }\n}\n\nfunction isKeepItemArray(value: unknown): value is KeepItem[] {\n return (\n Array.isArray(value) &&\n value.every(\n (item) =>\n isRecord(item) &&\n typeof item.id === \"string\" &&\n typeof item.savedAt === \"number\" &&\n Number.isFinite(item.savedAt) &&\n typeof item.updatedAt === \"number\" &&\n Number.isFinite(item.updatedAt) &&\n \"meta\" in item &&\n (item.targetType === undefined || typeof item.targetType === \"string\") &&\n (item.note === undefined || typeof item.note === \"string\") &&\n (item.schemaVersion === undefined ||\n (typeof item.schemaVersion === \"number\" && Number.isFinite(item.schemaVersion))) &&\n (item.revision === undefined || typeof item.revision === \"string\") &&\n (item.metaUpdatedAt === undefined ||\n (typeof item.metaUpdatedAt === \"number\" && Number.isFinite(item.metaUpdatedAt))) &&\n (item.tags === undefined || (Array.isArray(item.tags) && item.tags.every((tag) => typeof tag === \"string\"))),\n )\n );\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null;\n}\n\nfunction getBrowserStorage(): Storage | undefined {\n if (typeof window === \"undefined\") return undefined;\n try {\n return window.localStorage;\n } catch {\n return undefined;\n }\n}\n\nfunction getBrowserIndexedDB(): IDBFactory | undefined {\n if (typeof indexedDB === \"undefined\") return undefined;\n return indexedDB;\n}\n\nfunction isRecoverableStorageError(error: unknown): boolean {\n return error instanceof KeepStorageAccessError || error instanceof KeepStorageQuotaError;\n}\n\nfunction requestToPromise<T>(request: IDBRequest<T>): Promise<T> {\n return new Promise((resolve, reject) => {\n request.onsuccess = () => resolve(request.result);\n request.onerror = () => reject(request.error);\n });\n}\n\nfunction transactionToPromise(transaction: IDBTransaction): Promise<void> {\n return new Promise((resolve, reject) => {\n transaction.oncomplete = () => resolve();\n transaction.onerror = () => reject(transaction.error);\n transaction.onabort = () => reject(transaction.error ?? new Error(\"IndexedDB transaction aborted.\"));\n });\n}\n\nfunction isQuotaExceededError(cause: unknown): boolean {\n if (!isRecord(cause)) return false;\n return (\n cause.name === \"QuotaExceededError\" ||\n cause.name === \"NS_ERROR_DOM_QUOTA_REACHED\" ||\n cause.code === 22 ||\n cause.code === 1014\n );\n}\n\nexport {\n DEFAULT_SYNC_QUEUE_DATABASE,\n DEFAULT_SYNC_QUEUE_KEY,\n DEFAULT_SYNC_QUEUE_STORE,\n FallbackSyncQueueAdapter,\n type FallbackSyncQueueAdapterOptions,\n IndexedDBSyncQueueAdapter,\n type IndexedDBSyncQueueOptions,\n LocalStorageSyncQueueAdapter,\n type LocalStorageSyncQueueOptions,\n SyncStorageAdapter,\n type SyncStorageAdapterOptions,\n} from \"./sync\";\n"],"mappings":";AAsIO,IAAM,mBAAN,cAA+B,MAAM;AAAA,EAK1C,YACE,SACA,SAKA;AACA,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,YAAY,QAAQ;AACzB,SA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,QAAQ;AAAA,EAC7G;AAEJ;AAEA,SAASA,UAAS,OAAkD;AAClE,SAAO,OAAO,UAAU,YAAY,UAAU;AAChD;AAEA,SAASH,qBAAyC;AAChD,MAAI,OAAO,WAAW,YAAa,QAAO;AAC1C,MAAI;AACF,WAAO,OAAO;AAAA,EAChB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAASD,uBAA8C;AACrD,MAAI,OAAO,cAAc,YAAa,QAAO;AAC7C,SAAO;AACT;AAEA,SAAS,0BAA0B,OAAyB;AAC1D,SAAO,iBAAiB,0BAA0B,iBAAiB;AACrE;AAEA,SAASE,kBAAoB,SAAoC;AAC/D,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,YAAQ,YAAY,MAAM,QAAQ,QAAQ,MAAM;AAChD,YAAQ,UAAU,MAAM,OAAO,QAAQ,KAAK;AAAA,EAC9C,CAAC;AACH;AAEA,SAASC,sBAAqB,aAA4C;AACxE,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,gBAAY,aAAa,MAAM,QAAQ;AACvC,gBAAY,UAAU,MAAM,OAAO,YAAY,KAAK;AACpD,gBAAY,UAAU,MAAM,OAAO,YAAY,SAAS,IAAI,MAAM,gCAAgC,CAAC;AAAA,EACrG,CAAC;AACH;AAEA,SAAS,qBAAqB,OAAyB;AACrD,MAAI,CAACC,UAAS,KAAK,EAAG,QAAO;AAC7B,SACE,MAAM,SAAS,wBACf,MAAM,SAAS,gCACf,MAAM,SAAS,MACf,MAAM,SAAS;AAEnB;","names":["indexedDB","getBrowserIndexedDB","getBrowserStorage","requestToPromise","transactionToPromise","isRecord"]}
package/dist/core.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { K as KeepSchema, b as KeepInvalidItemPolicy, a as KeepItem, S as StorageAdapter, c as KeepPluginContext, d as KeepPlugin } from './types-DtILwEQ0.js';
2
- export { e as KeepAction, f as KeepChangeContext, g as KeepChangePhase, h as KeepConflictContext, i as KeepConflictResolver, j as KeepErrorContext, k as KeepErrorHandler, l as KeepEventHandlers, m as KeepItemInput, n as KeepSchemaParseResult, o as KeepStorageAccessError, p as KeepStorageError, q as KeepStorageOperation, r as KeepStorageParseError, s as KeepStorageQuotaError, t as KeepSyncState, u as KeepSyncStatus, R as RemoteSyncDriver, v as RemoteSyncResult, w as SyncCapableStorageAdapter, x as SyncOperation, y as SyncQueueAdapter, z as normalizeKeepTags } from './types-DtILwEQ0.js';
3
- export { K as KeepListOptions, a as KeepStore, b as KeepStoreActions, c as KeepStoreState, Q as QueryKeepItemsResult, g as getTagCounts, q as queryKeepItems } from './store-BAvx_Iaw.js';
1
+ import { K as KeepSchema, b as KeepInvalidItemPolicy, a as KeepItem, S as StorageAdapter, c as KeepPluginContext, d as KeepPlugin } from './types-BRfvVnCA.js';
2
+ export { e as KeepAction, f as KeepChangeContext, g as KeepChangePhase, h as KeepConflictContext, i as KeepConflictResolver, j as KeepErrorContext, k as KeepErrorHandler, l as KeepEventHandlers, m as KeepItemInput, n as KeepSchemaParseResult, o as KeepStorageAccessError, p as KeepStorageError, q as KeepStorageOperation, r as KeepStorageParseError, s as KeepStorageQuotaError, t as KeepSyncState, u as KeepSyncStatus, R as RemoteSyncDriver, v as RemoteSyncResult, w as SyncCapableStorageAdapter, x as SyncOperation, y as SyncQueueAdapter, z as normalizeKeepTags } from './types-BRfvVnCA.js';
3
+ export { K as KeepItemMetadataRefresher, a as KeepItemRevalidationRecord, b as KeepItemRevalidationResult, c as KeepItemRevalidationSummary, d as KeepItemRevalidator, e as KeepItemStatus, f as KeepListOptions, g as KeepStore, h as KeepStoreActions, i as KeepStoreState, Q as QueryKeepItemsResult, R as RevalidateKeepItemsOptions, j as getTagCounts, k as isKeepItemMetadataStale, q as queryKeepItems, r as reconcileKeepItems, l as revalidateKeepItems } from './store-B-pEydSU.js';
4
4
  export { KeepSchemaValidationError, parseKeepMeta, validateKeepItem } from './schema.js';
5
5
  export { BrowserStorageAdapterOptions, DEFAULT_INDEXEDDB_DATABASE, DEFAULT_INDEXEDDB_STORE, DEFAULT_STORAGE_KEY, DEFAULT_SYNC_QUEUE_DATABASE, DEFAULT_SYNC_QUEUE_KEY, DEFAULT_SYNC_QUEUE_STORE, FallbackStorageAdapter, FallbackStorageAdapterOptions, FallbackSyncQueueAdapter, FallbackSyncQueueAdapterOptions, IndexedDBAdapter, IndexedDBAdapterOptions, IndexedDBSyncQueueAdapter, IndexedDBSyncQueueOptions, LocalStorageAdapter, LocalStorageAdapterOptions, LocalStorageSyncQueueAdapter, LocalStorageSyncQueueOptions, StorageAdapterFactoryOptions, SyncStorageAdapter, SyncStorageAdapterOptions, createBrowserStorageAdapter, createStorageAdapter } from './storage.js';
6
6
 
package/dist/core.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import {
2
2
  KeepStore,
3
3
  getTagCounts,
4
- queryKeepItems
5
- } from "./chunk-PLT2WJVM.js";
4
+ isKeepItemMetadataStale,
5
+ queryKeepItems,
6
+ reconcileKeepItems,
7
+ revalidateKeepItems
8
+ } from "./chunk-L4KP6M5R.js";
6
9
  import {
7
10
  KeepSchemaValidationError,
8
11
  parseKeepMeta,
@@ -29,7 +32,7 @@ import {
29
32
  createBrowserStorageAdapter,
30
33
  createStorageAdapter,
31
34
  normalizeKeepTags
32
- } from "./chunk-C3SOQCVW.js";
35
+ } from "./chunk-X4UVKTBK.js";
33
36
 
34
37
  // src/migration.ts
35
38
  async function mergeKeepItems(localItems, target) {
@@ -158,7 +161,7 @@ function parseBackup(data) {
158
161
  }
159
162
  function isKeepItem(value) {
160
163
  if (!isRecord(value)) return false;
161
- return typeof value.id === "string" && typeof value.savedAt === "number" && Number.isFinite(value.savedAt) && typeof value.updatedAt === "number" && Number.isFinite(value.updatedAt) && "meta" in value && (value.targetType === void 0 || typeof value.targetType === "string") && (value.note === void 0 || typeof value.note === "string") && (value.schemaVersion === void 0 || typeof value.schemaVersion === "number" && Number.isFinite(value.schemaVersion)) && (value.revision === void 0 || typeof value.revision === "string") && (value.tags === void 0 || Array.isArray(value.tags) && value.tags.every((tag) => typeof tag === "string"));
164
+ return typeof value.id === "string" && typeof value.savedAt === "number" && Number.isFinite(value.savedAt) && typeof value.updatedAt === "number" && Number.isFinite(value.updatedAt) && "meta" in value && (value.targetType === void 0 || typeof value.targetType === "string") && (value.note === void 0 || typeof value.note === "string") && (value.schemaVersion === void 0 || typeof value.schemaVersion === "number" && Number.isFinite(value.schemaVersion)) && (value.revision === void 0 || typeof value.revision === "string") && (value.metaUpdatedAt === void 0 || typeof value.metaUpdatedAt === "number" && Number.isFinite(value.metaUpdatedAt)) && (value.tags === void 0 || Array.isArray(value.tags) && value.tags.every((tag) => typeof tag === "string"));
162
165
  }
163
166
  function isRecord(value) {
164
167
  return typeof value === "object" && value !== null;
@@ -204,11 +207,14 @@ export {
204
207
  exportItems,
205
208
  getTagCounts,
206
209
  importItems,
210
+ isKeepItemMetadataStale,
207
211
  mergeKeepItems,
208
212
  migrateKeepItems,
209
213
  normalizeKeepTags,
210
214
  parseKeepMeta,
211
215
  queryKeepItems,
216
+ reconcileKeepItems,
217
+ revalidateKeepItems,
212
218
  validateKeepItem
213
219
  };
214
220
  //# sourceMappingURL=core.js.map
package/dist/core.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/migration.ts","../src/backup.ts","../src/integrations.ts"],"sourcesContent":["import type { KeepItem, StorageAdapter } from \"./types\";\n\n/** Merge anonymous local items into a signed-in or remote adapter. */\nexport async function mergeKeepItems<TMeta>(\n localItems: KeepItem<TMeta>[],\n target: StorageAdapter<TMeta>,\n): Promise<KeepItem<TMeta>[]> {\n if (target.merge) return target.merge(localItems);\n\n const remoteItems = await target.getAll();\n const byId = new Map(remoteItems.map((item) => [item.id, item]));\n for (const localItem of localItems) {\n const remoteItem = byId.get(localItem.id);\n if (!remoteItem || localItem.updatedAt > remoteItem.updatedAt) {\n byId.set(localItem.id, localItem);\n }\n }\n\n const merged = [...byId.values()].sort((a, b) => b.updatedAt - a.updatedAt);\n await Promise.all(merged.map((item) => target.set(item)));\n return merged;\n}\n\n/** Read anonymous items, merge them into the target, then clear the source. */\nexport async function migrateKeepItems<TMeta>(\n source: StorageAdapter<TMeta>,\n target: StorageAdapter<TMeta>,\n): Promise<KeepItem<TMeta>[]> {\n const localItems = await source.getAll();\n const merged = await mergeKeepItems(localItems, target);\n await source.clear();\n return merged;\n}\n","import { mergeKeepItems } from \"./migration\";\nimport { validateKeepItem } from \"./schema\";\nimport type { KeepInvalidItemPolicy, KeepItem, KeepSchema, StorageAdapter } from \"./types\";\n\nexport const KEEP_BACKUP_FORMAT = \"keepkit\";\nexport const KEEP_BACKUP_VERSION = 1;\n\nexport type KeepBackup<TMeta = Record<string, unknown>> = {\n format: typeof KEEP_BACKUP_FORMAT;\n version: typeof KEEP_BACKUP_VERSION;\n exportedAt: number;\n items: KeepItem<TMeta>[];\n};\n\nexport type ImportItemsOptions<TMeta = unknown> = {\n mode?: \"replace\" | \"merge\";\n schema?: KeepSchema<TMeta>;\n invalidItemPolicy?: KeepInvalidItemPolicy;\n onInvalidItem?: (error: unknown, item: KeepItem<unknown>) => void;\n};\n\nexport type ImportItemsResult<TMeta = Record<string, unknown>> = {\n mode: \"replace\" | \"merge\";\n imported: number;\n failed: number;\n total: number;\n items: KeepItem<TMeta>[];\n};\n\nexport class KeepBackupParseError extends Error {\n readonly cause?: unknown;\n\n constructor(message: string, options?: { cause?: unknown }) {\n super(message);\n this.name = \"KeepBackupParseError\";\n if (options?.cause !== undefined) this.cause = options.cause;\n }\n}\n\nexport class KeepBackupImportError extends Error {\n readonly mode: \"replace\" | \"merge\";\n readonly imported: number;\n readonly failed: number;\n readonly cause?: unknown;\n\n constructor(\n message: string,\n options: {\n mode: \"replace\" | \"merge\";\n imported: number;\n failed: number;\n cause?: unknown;\n },\n ) {\n super(message);\n this.name = \"KeepBackupImportError\";\n this.mode = options.mode;\n this.imported = options.imported;\n this.failed = options.failed;\n if (options.cause !== undefined) this.cause = options.cause;\n }\n}\n\n/** Serialize all adapter data into a versioned JSON backup. */\nexport async function exportItems<TMeta>(adapter: StorageAdapter<TMeta>): Promise<string> {\n const backup: KeepBackup<TMeta> = {\n format: KEEP_BACKUP_FORMAT,\n version: KEEP_BACKUP_VERSION,\n exportedAt: Date.now(),\n items: await adapter.getAll(),\n };\n return JSON.stringify(backup, null, 2);\n}\n\n/** Validate and restore a backup, either replacing or merging existing data. */\nexport async function importItems<TMeta>(\n adapter: StorageAdapter<TMeta>,\n data: string | KeepBackup<TMeta>,\n options: ImportItemsOptions<TMeta> = {},\n): Promise<ImportItemsResult<TMeta>> {\n const backup = parseBackup<TMeta>(data);\n const mode = options.mode ?? \"merge\";\n const validItems: KeepItem<TMeta>[] = [];\n let failed = 0;\n for (const item of backup.items) {\n if (!options.schema) {\n validItems.push(item);\n continue;\n }\n try {\n validItems.push(await validateKeepItem(item, options.schema));\n } catch (cause) {\n options.onInvalidItem?.(cause, item);\n if ((options.invalidItemPolicy ?? \"error\") === \"drop\") {\n failed += 1;\n continue;\n }\n throw cause;\n }\n }\n let items: KeepItem<TMeta>[];\n\n if (mode === \"merge\") {\n try {\n items = await mergeKeepItems(validItems, adapter);\n } catch (cause) {\n throw new KeepBackupImportError(\"KeepKit could not merge the backup.\", {\n mode,\n imported: 0,\n failed: validItems.length + failed,\n cause,\n });\n }\n } else {\n let imported = 0;\n try {\n await adapter.clear();\n for (const item of validItems) {\n await adapter.set(item);\n imported += 1;\n }\n items = await adapter.getAll();\n } catch (cause) {\n throw new KeepBackupImportError(\"KeepKit could not replace the stored items.\", {\n mode,\n imported,\n failed: validItems.length + failed - imported,\n cause,\n });\n }\n }\n\n return { mode, imported: validItems.length, failed, total: items.length, items };\n}\n\nfunction parseBackup<TMeta>(data: string | KeepBackup<TMeta>): KeepBackup<TMeta> {\n let value: unknown = data;\n if (typeof data === \"string\") {\n try {\n value = JSON.parse(data);\n } catch (cause) {\n throw new KeepBackupParseError(\"KeepKit backup is not valid JSON.\", { cause });\n }\n }\n\n if (!isRecord(value)) throw new KeepBackupParseError(\"KeepKit backup must be an object.\");\n if (value.format !== KEEP_BACKUP_FORMAT || value.version !== KEEP_BACKUP_VERSION) {\n throw new KeepBackupParseError(\"KeepKit backup format or version is unsupported.\");\n }\n if (typeof value.exportedAt !== \"number\" || !Number.isFinite(value.exportedAt)) {\n throw new KeepBackupParseError(\"KeepKit backup has an invalid export timestamp.\");\n }\n if (!Array.isArray(value.items) || !value.items.every(isKeepItem)) {\n throw new KeepBackupParseError(\"KeepKit backup contains invalid items.\");\n }\n return value as KeepBackup<TMeta>;\n}\n\nfunction isKeepItem(value: unknown): value is KeepItem {\n if (!isRecord(value)) return false;\n return (\n typeof value.id === \"string\" &&\n typeof value.savedAt === \"number\" &&\n Number.isFinite(value.savedAt) &&\n typeof value.updatedAt === \"number\" &&\n Number.isFinite(value.updatedAt) &&\n \"meta\" in value &&\n (value.targetType === undefined || typeof value.targetType === \"string\") &&\n (value.note === undefined || typeof value.note === \"string\") &&\n (value.schemaVersion === undefined ||\n (typeof value.schemaVersion === \"number\" && Number.isFinite(value.schemaVersion))) &&\n (value.revision === undefined || typeof value.revision === \"string\") &&\n (value.tags === undefined || (Array.isArray(value.tags) && value.tags.every((tag) => typeof tag === \"string\")))\n );\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null;\n}\n","import type { KeepPlugin, KeepPluginContext } from \"./types\";\n\nexport type KeepInvalidationPluginOptions<TMeta = Record<string, unknown>> = {\n /** Query keys to invalidate after a successful local KeepKit mutation. */\n queryKeys: readonly unknown[] | ((context: KeepPluginContext<TMeta>) => readonly (readonly unknown[])[]);\n /** Connect this callback to queryClient.invalidateQueries or SWR mutate. */\n invalidate: (queryKey: readonly unknown[], context: KeepPluginContext<TMeta>) => void | Promise<void>;\n name?: string;\n};\n\n/** Framework-neutral bridge for TanStack Query, SWR, and similar caches. */\nexport function createKeepInvalidationPlugin<TMeta = Record<string, unknown>>(\n options: KeepInvalidationPluginOptions<TMeta>,\n): KeepPlugin<TMeta> {\n return {\n name: options.name ?? \"keepkit-cache-invalidation\",\n after: async (context) => {\n const keys = typeof options.queryKeys === \"function\" ? options.queryKeys(context) : [options.queryKeys];\n await Promise.all(keys.map((queryKey) => options.invalidate(queryKey, context)));\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,eAAsB,eACpB,YACA,QAC4B;AAC5B,MAAI,OAAO,MAAO,QAAO,OAAO,MAAM,UAAU;AAEhD,QAAM,cAAc,MAAM,OAAO,OAAO;AACxC,QAAM,OAAO,IAAI,IAAI,YAAY,IAAI,CAAC,SAAS,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;AAC/D,aAAW,aAAa,YAAY;AAClC,UAAM,aAAa,KAAK,IAAI,UAAU,EAAE;AACxC,QAAI,CAAC,cAAc,UAAU,YAAY,WAAW,WAAW;AAC7D,WAAK,IAAI,UAAU,IAAI,SAAS;AAAA,IAClC;AAAA,EACF;AAEA,QAAM,SAAS,CAAC,GAAG,KAAK,OAAO,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,YAAY,EAAE,SAAS;AAC1E,QAAM,QAAQ,IAAI,OAAO,IAAI,CAAC,SAAS,OAAO,IAAI,IAAI,CAAC,CAAC;AACxD,SAAO;AACT;AAGA,eAAsB,iBACpB,QACA,QAC4B;AAC5B,QAAM,aAAa,MAAM,OAAO,OAAO;AACvC,QAAM,SAAS,MAAM,eAAe,YAAY,MAAM;AACtD,QAAM,OAAO,MAAM;AACnB,SAAO;AACT;;;AC5BO,IAAM,qBAAqB;AAC3B,IAAM,sBAAsB;AAwB5B,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAG9C,YAAY,SAAiB,SAA+B;AAC1D,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,QAAI,SAAS,UAAU,OAAW,MAAK,QAAQ,QAAQ;AAAA,EACzD;AACF;AAEO,IAAM,wBAAN,cAAoC,MAAM;AAAA,EAM/C,YACE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AO,MAAM,eAAe,cAC9D,MAAM,SAAS,UAAa,OAAO,MAAM,SAAS,cAClD,MAAM,kBAAkB,UACtB,OAAO,MAAM,kBAAkB,YAAY,OAAO,SAAS,MAAM,aAAa,OAChF,MAAM,aAAa,UAAa,OAAO,MAAM,aAAa,cAC1D,MAAM,SAAS,UAAc,MAAM,QAAQ,MAAM,IAAI,KAAK,MAAM,KAAK,MAAM,CAAC,QAAQ,OAAO,QAAQ,QAAQ;AAEhH;AAEA,SAAS,SAAS,OAAkD;AAClE,SAAO,OAAO,UAAU,YAAY,UAAU;AAChD;;;ACvKO,SAAS,6BACd,SACmB;AACnB,SAAO;AAAA,IACL,MAAM,QAAQ,QAAQ;AAAA,IACtB,OAAO,OAAO,YAAY;AACxB,YAAM,OAAO,OAAO,QAAQ,cAAc,aAAa,QAAQ,UAAU,OAAO,IAAI,CAAC,QAAQ,SAAS;AACtG,YAAM,QAAQ,IAAI,KAAK,IAAI,CAAC,aAAa,QAAQ,WAAW,UAAU,OAAO,CAAC,CAAC;AAAA,IACjF;AAAA,EACF;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/migration.ts","../src/backup.ts","../src/integrations.ts"],"sourcesContent":["import type { KeepItem, StorageAdapter } from \"./types\";\n\n/** Merge anonymous local items into a signed-in or remote adapter. */\nexport async function mergeKeepItems<TMeta>(\n localItems: KeepItem<TMeta>[],\n target: StorageAdapter<TMeta>,\n): Promise<KeepItem<TMeta>[]> {\n if (target.merge) return target.merge(localItems);\n\n const remoteItems = await target.getAll();\n const byId = new Map(remoteItems.map((item) => [item.id, item]));\n for (const localItem of localItems) {\n const remoteItem = byId.get(localItem.id);\n if (!remoteItem || localItem.updatedAt > remoteItem.updatedAt) {\n byId.set(localItem.id, localItem);\n }\n }\n\n const merged = [...byId.values()].sort((a, b) => b.updatedAt - a.updatedAt);\n await Promise.all(merged.map((item) => target.set(item)));\n return merged;\n}\n\n/** Read anonymous items, merge them into the target, then clear the source. */\nexport async function migrateKeepItems<TMeta>(\n source: StorageAdapter<TMeta>,\n target: StorageAdapter<TMeta>,\n): Promise<KeepItem<TMeta>[]> {\n const localItems = await source.getAll();\n const merged = await mergeKeepItems(localItems, target);\n await source.clear();\n return merged;\n}\n","import { mergeKeepItems } from \"./migration\";\nimport { validateKeepItem } from \"./schema\";\nimport type { KeepInvalidItemPolicy, KeepItem, KeepSchema, StorageAdapter } from \"./types\";\n\nexport const KEEP_BACKUP_FORMAT = \"keepkit\";\nexport const KEEP_BACKUP_VERSION = 1;\n\nexport type KeepBackup<TMeta = Record<string, unknown>> = {\n format: typeof KEEP_BACKUP_FORMAT;\n version: typeof KEEP_BACKUP_VERSION;\n exportedAt: number;\n items: KeepItem<TMeta>[];\n};\n\nexport type ImportItemsOptions<TMeta = unknown> = {\n mode?: \"replace\" | \"merge\";\n schema?: KeepSchema<TMeta>;\n invalidItemPolicy?: KeepInvalidItemPolicy;\n onInvalidItem?: (error: unknown, item: KeepItem<unknown>) => void;\n};\n\nexport type ImportItemsResult<TMeta = Record<string, unknown>> = {\n mode: \"replace\" | \"merge\";\n imported: number;\n failed: number;\n total: number;\n items: KeepItem<TMeta>[];\n};\n\nexport class KeepBackupParseError extends Error {\n readonly cause?: unknown;\n\n constructor(message: string, options?: { cause?: unknown }) {\n super(message);\n this.name = \"KeepBackupParseError\";\n if (options?.cause !== undefined) this.cause = options.cause;\n }\n}\n\nexport class KeepBackupImportError extends Error {\n readonly mode: \"replace\" | \"merge\";\n readonly imported: number;\n readonly failed: number;\n readonly cause?: unknown;\n\n constructor(\n message: string,\n options: {\n mode: \"replace\" | \"merge\";\n imported: number;\n failed: number;\n cause?: unknown;\n },\n ) {\n super(message);\n this.name = \"KeepBackupImportError\";\n this.mode = options.mode;\n this.imported = options.imported;\n this.failed = options.failed;\n if (options.cause !== undefined) this.cause = options.cause;\n }\n}\n\n/** Serialize all adapter data into a versioned JSON backup. */\nexport async function exportItems<TMeta>(adapter: StorageAdapter<TMeta>): Promise<string> {\n const backup: KeepBackup<TMeta> = {\n format: KEEP_BACKUP_FORMAT,\n version: KEEP_BACKUP_VERSION,\n exportedAt: Date.now(),\n items: await adapter.getAll(),\n };\n return JSON.stringify(backup, null, 2);\n}\n\n/** Validate and restore a backup, either replacing or merging existing data. */\nexport async function importItems<TMeta>(\n adapter: StorageAdapter<TMeta>,\n data: string | KeepBackup<TMeta>,\n options: ImportItemsOptions<TMeta> = {},\n): Promise<ImportItemsResult<TMeta>> {\n const backup = parseBackup<TMeta>(data);\n const mode = options.mode ?? \"merge\";\n const validItems: KeepItem<TMeta>[] = [];\n let failed = 0;\n for (const item of backup.items) {\n if (!options.schema) {\n validItems.push(item);\n continue;\n }\n try {\n validItems.push(await validateKeepItem(item, options.schema));\n } catch (cause) {\n options.onInvalidItem?.(cause, item);\n if ((options.invalidItemPolicy ?? \"error\") === \"drop\") {\n failed += 1;\n continue;\n }\n throw cause;\n }\n }\n let items: KeepItem<TMeta>[];\n\n if (mode === \"merge\") {\n try {\n items = await mergeKeepItems(validItems, adapter);\n } catch (cause) {\n throw new KeepBackupImportError(\"KeepKit could not merge the backup.\", {\n mode,\n imported: 0,\n failed: validItems.length + failed,\n cause,\n });\n }\n } else {\n let imported = 0;\n try {\n await adapter.clear();\n for (const item of validItems) {\n await adapter.set(item);\n imported += 1;\n }\n items = await adapter.getAll();\n } catch (cause) {\n throw new KeepBackupImportError(\"KeepKit could not replace the stored items.\", {\n mode,\n imported,\n failed: validItems.length + failed - imported,\n cause,\n });\n }\n }\n\n return { mode, imported: validItems.length, failed, total: items.length, items };\n}\n\nfunction parseBackup<TMeta>(data: string | KeepBackup<TMeta>): KeepBackup<TMeta> {\n let value: unknown = data;\n if (typeof data === \"string\") {\n try {\n value = JSON.parse(data);\n } catch (cause) {\n throw new KeepBackupParseError(\"KeepKit backup is not valid JSON.\", { cause });\n }\n }\n\n if (!isRecord(value)) throw new KeepBackupParseError(\"KeepKit backup must be an object.\");\n if (value.format !== KEEP_BACKUP_FORMAT || value.version !== KEEP_BACKUP_VERSION) {\n throw new KeepBackupParseError(\"KeepKit backup format or version is unsupported.\");\n }\n if (typeof value.exportedAt !== \"number\" || !Number.isFinite(value.exportedAt)) {\n throw new KeepBackupParseError(\"KeepKit backup has an invalid export timestamp.\");\n }\n if (!Array.isArray(value.items) || !value.items.every(isKeepItem)) {\n throw new KeepBackupParseError(\"KeepKit backup contains invalid items.\");\n }\n return value as KeepBackup<TMeta>;\n}\n\nfunction isKeepItem(value: unknown): value is KeepItem {\n if (!isRecord(value)) return false;\n return (\n typeof value.id === \"string\" &&\n typeof value.savedAt === \"number\" &&\n Number.isFinite(value.savedAt) &&\n typeof value.updatedAt === \"number\" &&\n Number.isFinite(value.updatedAt) &&\n \"meta\" in value &&\n (value.targetType === undefined || typeof value.targetType === \"string\") &&\n (value.note === undefined || typeof value.note === \"string\") &&\n (value.schemaVersion === undefined ||\n (typeof value.schemaVersion === \"number\" && Number.isFinite(value.schemaVersion))) &&\n (value.revision === undefined || typeof value.revision === \"string\") &&\n (value.metaUpdatedAt === undefined ||\n (typeof value.metaUpdatedAt === \"number\" && Number.isFinite(value.metaUpdatedAt))) &&\n (value.tags === undefined || (Array.isArray(value.tags) && value.tags.every((tag) => typeof tag === \"string\")))\n );\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null;\n}\n","import type { KeepPlugin, KeepPluginContext } from \"./types\";\n\nexport type KeepInvalidationPluginOptions<TMeta = Record<string, unknown>> = {\n /** Query keys to invalidate after a successful local KeepKit mutation. */\n queryKeys: readonly unknown[] | ((context: KeepPluginContext<TMeta>) => readonly (readonly unknown[])[]);\n /** Connect this callback to queryClient.invalidateQueries or SWR mutate. */\n invalidate: (queryKey: readonly unknown[], context: KeepPluginContext<TMeta>) => void | Promise<void>;\n name?: string;\n};\n\n/** Framework-neutral bridge for TanStack Query, SWR, and similar caches. */\nexport function createKeepInvalidationPlugin<TMeta = Record<string, unknown>>(\n options: KeepInvalidationPluginOptions<TMeta>,\n): KeepPlugin<TMeta> {\n return {\n name: options.name ?? \"keepkit-cache-invalidation\",\n after: async (context) => {\n const keys = typeof options.queryKeys === \"function\" ? options.queryKeys(context) : [options.queryKeys];\n await Promise.all(keys.map((queryKey) => options.invalidate(queryKey, context)));\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,eAAsB,eACpB,YACA,QAC4B;AAC5B,MAAI,OAAO,MAAO,QAAO,OAAO,MAAM,UAAU;AAEhD,QAAM,cAAc,MAAM,OAAO,OAAO;AACxC,QAAM,OAAO,IAAI,IAAI,YAAY,IAAI,CAAC,SAAS,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC;AAC/D,aAAW,aAAa,YAAY;AAClC,UAAM,aAAa,KAAK,IAAI,UAAU,EAAE;AACxC,QAAI,CAAC,cAAc,UAAU,YAAY,WAAW,WAAW;AAC7D,WAAK,IAAI,UAAU,IAAI,SAAS;AAAA,IAClC;AAAA,EACF;AAEA,QAAM,SAAS,CAAC,GAAG,KAAK,OAAO,CAAC,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,YAAY,EAAE,SAAS;AAC1E,QAAM,QAAQ,IAAI,OAAO,IAAI,CAAC,SAAS,OAAO,IAAI,IAAI,CAAC,CAAC;AACxD,SAAO;AACT;AAGA,eAAsB,iBACpB,QACA,QAC4B;AAC5B,QAAM,aAAa,MAAM,OAAO,OAAO;AACvC,QAAM,SAAS,MAAM,eAAe,YAAY,MAAM;AACtD,QAAM,OAAO,MAAM;AACnB,SAAO;AACT;;;AC5BO,IAAM,qBAAqB;AAC3B,IAAM,sBAAsB;AAwB5B,IAAM,uBAAN,cAAmC,MAAM;AAAA,EAG9C,YAAY,SAAiB,SAA+B;AAC1D,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,QAAI,SAAS,UAAU,OAAW,MAAK,QAAQ,QAAQ;AAAA,EACzD;AACF;AAEO,IAAM,wBAAN,cAAoC,MAAM;AAAA,EAM/C,YACE,SACA,SAMA;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Ae,cAC9D,MAAM,SAAS,UAAa,OAAO,MAAM,SAAS,cAClD,MAAM,kBAAkB,UACtB,OAAO,MAAM,kBAAkB,YAAY,OAAO,SAAS,MAAM,aAAa,OAChF,MAAM,aAAa,UAAa,OAAO,MAAM,aAAa,cAC1D,MAAM,kBAAkB,UACtB,OAAO,MAAM,kBAAkB,YAAY,OAAO,SAAS,MAAM,aAAa,OAChF,MAAM,SAAS,UAAc,MAAM,QAAQ,MAAM,IAAI,KAAK,MAAM,KAAK,MAAM,CAAC,QAAQ,OAAO,QAAQ,QAAQ;AAEhH;AAEA,SAAS,SAAS,OAAkD;AAClE,SAAO,OAAO,UAAU,YAAY,UAAU;AAChD;;;ACzKO,SAAS,6BACd,SACmB;AACnB,SAAO;AAAA,IACL,MAAM,QAAQ,QAAQ;AAAA,IACtB,OAAO,OAAO,YAAY;AACxB,YAAM,OAAO,OAAO,QAAQ,cAAc,aAAa,QAAQ,UAAU,OAAO,IAAI,CAAC,QAAQ,SAAS;AACtG,YAAM,QAAQ,IAAI,KAAK,IAAI,CAAC,aAAa,QAAQ,WAAW,UAAU,OAAO,CAAC,CAAC;AAAA,IACjF;AAAA,EACF;AACF;","names":[]}
package/dist/react.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode, ButtonHTMLAttributes, MouseEvent, HTMLAttributes, ReactElement, PropsWithChildren, ComponentType } from 'react';
3
- import { m as KeepItemInput, a as KeepItem, l as KeepEventHandlers, S as StorageAdapter, d as KeepPlugin, K as KeepSchema, b as KeepInvalidItemPolicy, t as KeepSyncState } from './types-DtILwEQ0.js';
4
- import { K as KeepListOptions, a as KeepStore, b as KeepStoreActions } from './store-BAvx_Iaw.js';
3
+ import { K as KeepItemMetadataRefresher, d as KeepItemRevalidator, R as RevalidateKeepItemsOptions, c as KeepItemRevalidationSummary, f as KeepListOptions, g as KeepStore, h as KeepStoreActions } from './store-B-pEydSU.js';
4
+ export { b as KeepItemRevalidationResult, e as KeepItemStatus, k as isKeepItemMetadataStale } from './store-B-pEydSU.js';
5
+ import { m as KeepItemInput, a as KeepItem, l as KeepEventHandlers, S as StorageAdapter, d as KeepPlugin, K as KeepSchema, b as KeepInvalidItemPolicy, f as KeepChangeContext, t as KeepSyncState } from './types-BRfvVnCA.js';
5
6
 
6
7
  type UseKeepItemResult<TMeta = Record<string, unknown>> = {
7
8
  item: KeepItem<TMeta> | undefined;
@@ -14,6 +15,7 @@ type UseKeepItemResult<TMeta = Record<string, unknown>> = {
14
15
  toggle: () => Promise<void>;
15
16
  updateNote: (note?: string) => Promise<void>;
16
17
  updateTags: (tags?: string[]) => Promise<void>;
18
+ refreshMetadata: (refresh: KeepItemMetadataRefresher<TMeta>) => Promise<void>;
17
19
  };
18
20
  declare function useKeepItem<TMeta = Record<string, unknown>>(id: string, itemPayload?: KeepItemInput<TMeta>): UseKeepItemResult<TMeta>;
19
21
 
@@ -33,6 +35,7 @@ type UseKeepListResult<TMeta = Record<string, unknown>> = {
33
35
  removeTagsBatch: (ids: string[], tags: string[]) => Promise<void>;
34
36
  clear: () => Promise<void>;
35
37
  refresh: () => Promise<void>;
38
+ revalidate: (revalidator: KeepItemRevalidator<TMeta>, options?: RevalidateKeepItemsOptions) => Promise<KeepItemRevalidationSummary<TMeta>>;
36
39
  };
37
40
  declare function useKeepList<TMeta = Record<string, unknown>>(options?: KeepListOptions<TMeta>): UseKeepListResult<TMeta>;
38
41
 
@@ -60,6 +63,9 @@ type KeepButtonSharedProps<TMeta> = {
60
63
  children?: ReactNode | ((state: KeepButtonState<TMeta>) => ReactNode);
61
64
  savedLabel?: ReactNode;
62
65
  unsavedLabel?: ReactNode;
66
+ savedAriaLabel?: string;
67
+ unsavedAriaLabel?: string;
68
+ getAriaLabel?: (state: KeepButtonState<TMeta>) => string;
63
69
  disabled?: boolean;
64
70
  onToggleError?: (error: unknown) => void;
65
71
  };
@@ -84,7 +90,7 @@ type KeepButtonState<TMeta = Record<string, unknown>> = {
84
90
  updateTags: (tags?: string[]) => Promise<void>;
85
91
  };
86
92
  /** A style-free accessible save toggle. Consumers provide all visual styling. */
87
- declare function KeepButton<TMeta = Record<string, unknown>>({ item, children, savedLabel, unsavedLabel, asChild, onToggleError, onClick, disabled, ...buttonProps }: KeepButtonProps<TMeta>): react.JSX.Element;
93
+ declare function KeepButton<TMeta = Record<string, unknown>>({ item, children, savedLabel, unsavedLabel, savedAriaLabel, unsavedAriaLabel, getAriaLabel, asChild, onToggleError, onClick, disabled, ...buttonProps }: KeepButtonProps<TMeta>): react.JSX.Element;
88
94
 
89
95
  type KeepContextValue<TMeta = Record<string, unknown>> = {
90
96
  items: KeepItem<TMeta>[];
@@ -92,6 +98,7 @@ type KeepContextValue<TMeta = Record<string, unknown>> = {
92
98
  isHydrated: boolean;
93
99
  isMutating: boolean;
94
100
  error: unknown | null;
101
+ lastChange?: KeepChangeContext<TMeta>;
95
102
  syncState: KeepSyncState;
96
103
  saveItem: (item: KeepItem<TMeta>) => Promise<void>;
97
104
  updateNote: (id: string, note?: string) => Promise<void>;
@@ -104,6 +111,8 @@ type KeepContextValue<TMeta = Record<string, unknown>> = {
104
111
  clear: () => Promise<void>;
105
112
  refresh: () => Promise<void>;
106
113
  flushSync: () => Promise<void>;
114
+ refreshItemMetadata: (id: string, refresh: KeepItemMetadataRefresher<TMeta>) => Promise<void>;
115
+ revalidateItems: (revalidator: KeepItemRevalidator<TMeta>, options?: RevalidateKeepItemsOptions) => Promise<KeepItemRevalidationSummary<TMeta>>;
107
116
  };
108
117
  type KeepProviderProps<TMeta = Record<string, unknown>> = PropsWithChildren<KeepEventHandlers<TMeta> & {
109
118
  storage?: StorageAdapter<TMeta>;
@@ -148,4 +157,4 @@ type KeepKit<TMeta> = {
148
157
  /** Create an app-specific, fully typed set of KeepKit components and hooks. */
149
158
  declare function createKeepKit<TMeta = Record<string, unknown>>(options?: CreateKeepKitOptions<TMeta>): KeepKit<TMeta>;
150
159
 
151
- export { type CreateKeepKitOptions, KeepButton, type KeepButtonItem, type KeepButtonProps, type KeepButtonState, type KeepContextValue, type KeepKit, KeepListOptions, KeepProvider, type KeepProviderProps, type KeepShortcutModifier, type KeepShortcutOptions, type UseKeepItemResult, type UseKeepListResult, createKeepKit, useKeepContext, useKeepItem, useKeepList, useKeepShortcut, useKeepStore };
160
+ export { type CreateKeepKitOptions, KeepButton, type KeepButtonItem, type KeepButtonProps, type KeepButtonState, type KeepContextValue, KeepItemMetadataRefresher, KeepItemRevalidationSummary, KeepItemRevalidator, type KeepKit, KeepListOptions, KeepProvider, type KeepProviderProps, type KeepShortcutModifier, type KeepShortcutOptions, RevalidateKeepItemsOptions, type UseKeepItemResult, type UseKeepListResult, createKeepKit, useKeepContext, useKeepItem, useKeepList, useKeepShortcut, useKeepStore };