@classic-homes/offline-core 0.1.1
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 +272 -0
- package/dist/cache/index.d.ts +198 -0
- package/dist/cache/index.js +2 -0
- package/dist/chunk-4QWZX3LM.js +498 -0
- package/dist/chunk-IIG4QLLN.js +178 -0
- package/dist/chunk-U27TKOYZ.js +312 -0
- package/dist/chunk-UQUCUBV2.js +297 -0
- package/dist/chunk-UXUGWW74.js +1432 -0
- package/dist/chunk-XHMX3BYT.js +1040 -0
- package/dist/chunk-ZC7F2HDV.js +100 -0
- package/dist/drafts/index.d.ts +203 -0
- package/dist/drafts/index.js +3 -0
- package/dist/files/index.d.ts +181 -0
- package/dist/files/index.js +2 -0
- package/dist/index.d.ts +249 -0
- package/dist/index.js +107 -0
- package/dist/storage/index.d.ts +198 -0
- package/dist/storage/index.js +2 -0
- package/dist/sync/index.d.ts +621 -0
- package/dist/sync/index.js +3 -0
- package/dist/types-B2FJpQuN.d.ts +179 -0
- package/dist/types-CWuauHAr.d.ts +204 -0
- package/package.json +77 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { S as StorageAdapter, O as OfflineStorageConfig, a as OfflineStorageOptions, b as StorageAdapterName } from './types-CWuauHAr.js';
|
|
2
|
+
export { B as BlobMetadata, d as BlobWithMetadata, C as CleanupResult, J as JSONEntry, f as StorageAdapterOptions, c as StorageQuota, e as StoreJSONOptions } from './types-CWuauHAr.js';
|
|
3
|
+
import { FileStorageService } from './files/index.js';
|
|
4
|
+
export { DeleteFilesResult, FileConstraints, FileValidationError, GetFilesOptions, PersistedFile, StoreFileOptions, StoredFile, createFileStorageService } from './files/index.js';
|
|
5
|
+
import { DraftStorageService, DraftManagerService } from './drafts/index.js';
|
|
6
|
+
export { createDraftManagerService, createDraftStorageService } from './drafts/index.js';
|
|
7
|
+
import { CacheService } from './cache/index.js';
|
|
8
|
+
export { CACHE_PREFIX, CacheEntry, CacheGetResult, CacheOptions, CacheStats, GetOrSetOptions, createCacheService } from './cache/index.js';
|
|
9
|
+
export { CreateAdapterOptions, CreateAdapterResult, createAdapter, detectAvailableAdapters } from './storage/index.js';
|
|
10
|
+
export { C as CreateDraftOptions, D as DRAFT_PREFIX, g as DRAFT_V2_PREFIX, d as DraftMetadata, c as DraftSyncStatus, f as ListDraftsOptions, L as LoadDraftOptions, b as LoadDraftResult, a as SaveDraftOptions, S as StoredDraft, e as StoredDraftV2, h as generateDraftId, i as getOrCreateDeviceId } from './types-B2FJpQuN.js';
|
|
11
|
+
export { ConflictResolution, ConnectivityMonitor, ConnectivityState, DraftConflict, DraftPayload, PendingSyncOperation, RemoteDraft, SyncAdapter, SyncConfig, SyncEvent, SyncEventListener, SyncEventType, SyncQueue, SyncQueueState, SyncRequest, SyncResponse, SyncResult, SyncService, applyResolution, compareDrafts, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict } from './sync/index.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Error types for the offline storage package
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Error codes for offline storage operations
|
|
18
|
+
*/
|
|
19
|
+
type OfflineStorageErrorCode = 'NOT_INITIALIZED' | 'ADAPTER_UNAVAILABLE' | 'STORAGE_FULL' | 'BLOB_NOT_FOUND' | 'JSON_NOT_FOUND' | 'SERIALIZATION_ERROR' | 'WORKER_ERROR' | 'OPFS_ERROR' | 'IDB_ERROR' | 'CLEANUP_ERROR' | 'UNKNOWN';
|
|
20
|
+
/**
|
|
21
|
+
* Custom error class for offline storage operations
|
|
22
|
+
*/
|
|
23
|
+
declare class OfflineStorageError extends Error {
|
|
24
|
+
readonly code: OfflineStorageErrorCode;
|
|
25
|
+
readonly cause?: Error;
|
|
26
|
+
constructor(code: OfflineStorageErrorCode, message: string, cause?: Error);
|
|
27
|
+
/**
|
|
28
|
+
* Create an error for uninitialized storage
|
|
29
|
+
*/
|
|
30
|
+
static notInitialized(): OfflineStorageError;
|
|
31
|
+
/**
|
|
32
|
+
* Create an error for unavailable adapter
|
|
33
|
+
*/
|
|
34
|
+
static adapterUnavailable(name: string): OfflineStorageError;
|
|
35
|
+
/**
|
|
36
|
+
* Create an error for storage quota exceeded
|
|
37
|
+
*/
|
|
38
|
+
static storageFull(cause?: Error): OfflineStorageError;
|
|
39
|
+
/**
|
|
40
|
+
* Create an error for blob not found
|
|
41
|
+
*/
|
|
42
|
+
static blobNotFound(key: string): OfflineStorageError;
|
|
43
|
+
/**
|
|
44
|
+
* Create an error for JSON not found
|
|
45
|
+
*/
|
|
46
|
+
static jsonNotFound(key: string): OfflineStorageError;
|
|
47
|
+
/**
|
|
48
|
+
* Create an error for serialization failures
|
|
49
|
+
*/
|
|
50
|
+
static serializationError(cause?: Error): OfflineStorageError;
|
|
51
|
+
/**
|
|
52
|
+
* Create an error for worker communication failures
|
|
53
|
+
*/
|
|
54
|
+
static workerError(message: string, cause?: Error): OfflineStorageError;
|
|
55
|
+
/**
|
|
56
|
+
* Create an error for OPFS operations
|
|
57
|
+
*/
|
|
58
|
+
static opfsError(message: string, cause?: Error): OfflineStorageError;
|
|
59
|
+
/**
|
|
60
|
+
* Create an error for IndexedDB operations
|
|
61
|
+
*/
|
|
62
|
+
static idbError(message: string, cause?: Error): OfflineStorageError;
|
|
63
|
+
/**
|
|
64
|
+
* Create an error for cleanup operations
|
|
65
|
+
*/
|
|
66
|
+
static cleanupError(message: string, cause?: Error): OfflineStorageError;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Expiration and cleanup utilities
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Cleanup scheduler for automatic expiration handling
|
|
75
|
+
*/
|
|
76
|
+
declare class CleanupScheduler {
|
|
77
|
+
private intervalId;
|
|
78
|
+
private readonly adapters;
|
|
79
|
+
private readonly intervalMs;
|
|
80
|
+
private isRunning;
|
|
81
|
+
constructor(intervalMs?: number);
|
|
82
|
+
/**
|
|
83
|
+
* Register an adapter for automatic cleanup
|
|
84
|
+
*/
|
|
85
|
+
registerAdapter(adapter: StorageAdapter): void;
|
|
86
|
+
/**
|
|
87
|
+
* Unregister an adapter
|
|
88
|
+
*/
|
|
89
|
+
unregisterAdapter(adapter: StorageAdapter): void;
|
|
90
|
+
/**
|
|
91
|
+
* Start the cleanup scheduler
|
|
92
|
+
*/
|
|
93
|
+
start(): void;
|
|
94
|
+
/**
|
|
95
|
+
* Stop the cleanup scheduler
|
|
96
|
+
*/
|
|
97
|
+
stop(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Run cleanup on all registered adapters
|
|
100
|
+
*/
|
|
101
|
+
runCleanup(): Promise<{
|
|
102
|
+
totalDeleted: number;
|
|
103
|
+
totalFreed: number;
|
|
104
|
+
}>;
|
|
105
|
+
/**
|
|
106
|
+
* Check if scheduler is active
|
|
107
|
+
*/
|
|
108
|
+
isActive(): boolean;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Calculate expiration timestamp from TTL
|
|
112
|
+
*/
|
|
113
|
+
declare function calculateExpiration(ttlMs?: number): number | null;
|
|
114
|
+
/**
|
|
115
|
+
* Check if a timestamp has expired
|
|
116
|
+
*/
|
|
117
|
+
declare function isExpired(expiresAt: number | null | undefined): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Default expiration times (in milliseconds)
|
|
120
|
+
*/
|
|
121
|
+
declare const DEFAULT_EXPIRATIONS: {
|
|
122
|
+
/** Form draft expiration: 7 days */
|
|
123
|
+
readonly DRAFT: number;
|
|
124
|
+
/** File attachment expiration: 24 hours */
|
|
125
|
+
readonly FILE: number;
|
|
126
|
+
/** Cache entry expiration: 1 hour */
|
|
127
|
+
readonly CACHE: number;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Offline storage initialization and singleton management
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Offline storage instance containing all services
|
|
136
|
+
*/
|
|
137
|
+
interface OfflineStorage {
|
|
138
|
+
/** The underlying storage adapter */
|
|
139
|
+
readonly adapter: StorageAdapter;
|
|
140
|
+
/** Configuration used for initialization */
|
|
141
|
+
readonly config: OfflineStorageConfig;
|
|
142
|
+
/** File storage service */
|
|
143
|
+
readonly files: FileStorageService;
|
|
144
|
+
/** Draft storage service (legacy single-draft) */
|
|
145
|
+
readonly drafts: DraftStorageService;
|
|
146
|
+
/** Draft manager service (multi-draft V2) */
|
|
147
|
+
readonly draftManager: DraftManagerService;
|
|
148
|
+
/** Cache service */
|
|
149
|
+
readonly cache: CacheService;
|
|
150
|
+
/** Cleanup scheduler */
|
|
151
|
+
readonly cleanup: CleanupScheduler;
|
|
152
|
+
/** Dispose all resources */
|
|
153
|
+
dispose(): Promise<void>;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Initialize the offline storage system
|
|
157
|
+
*
|
|
158
|
+
* This creates a singleton instance that can be accessed via getOfflineStorage().
|
|
159
|
+
* If already initialized, returns the existing instance.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* // Initialize at app startup
|
|
164
|
+
* const storage = await initOfflineStorage();
|
|
165
|
+
*
|
|
166
|
+
* // Use the services
|
|
167
|
+
* await storage.files.storeFile(file, { formKey: 'my-form', fieldName: 'attachment' });
|
|
168
|
+
* await storage.drafts.saveDraft('my-form', formData);
|
|
169
|
+
* const user = await storage.cache.getOrSet('user', fetchUser, { ttl: 3600000 });
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
declare function initOfflineStorage(options?: OfflineStorageOptions): Promise<OfflineStorage>;
|
|
173
|
+
/**
|
|
174
|
+
* Get the initialized offline storage instance
|
|
175
|
+
*
|
|
176
|
+
* @throws {OfflineStorageError} If storage has not been initialized
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* // After initialization
|
|
181
|
+
* const storage = getOfflineStorage();
|
|
182
|
+
* await storage.cache.set('key', value);
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
declare function getOfflineStorage(): OfflineStorage;
|
|
186
|
+
/**
|
|
187
|
+
* Check if offline storage has been initialized
|
|
188
|
+
*/
|
|
189
|
+
declare function isOfflineStorageInitialized(): boolean;
|
|
190
|
+
/**
|
|
191
|
+
* Get the offline storage instance if initialized, otherwise null
|
|
192
|
+
*
|
|
193
|
+
* Useful when you want to avoid the error from getOfflineStorage()
|
|
194
|
+
*/
|
|
195
|
+
declare function getOfflineStorageOrNull(): OfflineStorage | null;
|
|
196
|
+
/**
|
|
197
|
+
* Reset the offline storage singleton (primarily for testing)
|
|
198
|
+
*/
|
|
199
|
+
declare function resetOfflineStorage(): Promise<void>;
|
|
200
|
+
/**
|
|
201
|
+
* Get which storage adapter would be used without initializing
|
|
202
|
+
*
|
|
203
|
+
* Useful for feature detection or UI hints
|
|
204
|
+
*/
|
|
205
|
+
declare function detectStorageAdapter(preferredAdapters?: StorageAdapterName[]): Promise<{
|
|
206
|
+
adapterName: StorageAdapterName;
|
|
207
|
+
available: StorageAdapterName[];
|
|
208
|
+
unavailable: StorageAdapterName[];
|
|
209
|
+
}>;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Browser environment and feature detection utilities
|
|
213
|
+
*/
|
|
214
|
+
/**
|
|
215
|
+
* Check if running in a browser environment
|
|
216
|
+
*/
|
|
217
|
+
declare function isBrowser(): boolean;
|
|
218
|
+
/**
|
|
219
|
+
* Check if running in a Web Worker
|
|
220
|
+
*/
|
|
221
|
+
declare function isWorker(): boolean;
|
|
222
|
+
/**
|
|
223
|
+
* Check if running in a server-side rendering context
|
|
224
|
+
*/
|
|
225
|
+
declare function isSSR(): boolean;
|
|
226
|
+
/**
|
|
227
|
+
* Check if OPFS (Origin Private File System) is supported
|
|
228
|
+
* Note: Safari requires OPFS access from a Web Worker
|
|
229
|
+
*/
|
|
230
|
+
declare function supportsOPFS(): Promise<boolean>;
|
|
231
|
+
/**
|
|
232
|
+
* Check if IndexedDB is supported
|
|
233
|
+
*/
|
|
234
|
+
declare function supportsIndexedDB(): boolean;
|
|
235
|
+
/**
|
|
236
|
+
* Check if localStorage is available
|
|
237
|
+
*/
|
|
238
|
+
declare function supportsLocalStorage(): boolean;
|
|
239
|
+
/**
|
|
240
|
+
* Detect browser type for Safari-specific workarounds
|
|
241
|
+
*/
|
|
242
|
+
declare function detectBrowser(): {
|
|
243
|
+
isSafari: boolean;
|
|
244
|
+
isChrome: boolean;
|
|
245
|
+
isFirefox: boolean;
|
|
246
|
+
isEdge: boolean;
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
export { CacheService, CleanupScheduler, DEFAULT_EXPIRATIONS, DraftManagerService, DraftStorageService, FileStorageService, type OfflineStorage, OfflineStorageConfig, OfflineStorageError, type OfflineStorageErrorCode, OfflineStorageOptions, StorageAdapter, StorageAdapterName, calculateExpiration, detectBrowser, detectStorageAdapter, getOfflineStorage, getOfflineStorageOrNull, initOfflineStorage, isBrowser, isExpired, isOfflineStorageInitialized, isSSR, isWorker, resetOfflineStorage, supportsIndexedDB, supportsLocalStorage, supportsOPFS };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { createAdapter, OfflineStorageError } from './chunk-UXUGWW74.js';
|
|
2
|
+
export { OfflineStorageError, createAdapter, detectAvailableAdapters, detectBrowser, isBrowser, isSSR, isWorker, supportsIndexedDB, supportsLocalStorage, supportsOPFS } from './chunk-UXUGWW74.js';
|
|
3
|
+
import { createFileStorageService } from './chunk-U27TKOYZ.js';
|
|
4
|
+
export { FileStorageService, createFileStorageService } from './chunk-U27TKOYZ.js';
|
|
5
|
+
import { createDraftStorageService } from './chunk-IIG4QLLN.js';
|
|
6
|
+
export { DraftStorageService, createDraftStorageService } from './chunk-IIG4QLLN.js';
|
|
7
|
+
import { createCacheService } from './chunk-UQUCUBV2.js';
|
|
8
|
+
export { CACHE_PREFIX, CacheService, createCacheService } from './chunk-UQUCUBV2.js';
|
|
9
|
+
export { ConnectivityMonitor, SyncQueue, SyncService, applyResolution, compareDrafts, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict } from './chunk-XHMX3BYT.js';
|
|
10
|
+
import { createDraftManagerService } from './chunk-4QWZX3LM.js';
|
|
11
|
+
export { DRAFT_PREFIX, DRAFT_V2_PREFIX, DraftManagerService, createDraftManagerService, generateDraftId, getOrCreateDeviceId } from './chunk-4QWZX3LM.js';
|
|
12
|
+
import { CleanupScheduler } from './chunk-ZC7F2HDV.js';
|
|
13
|
+
export { CleanupScheduler, DEFAULT_EXPIRATIONS, calculateExpiration, isExpired } from './chunk-ZC7F2HDV.js';
|
|
14
|
+
|
|
15
|
+
// src/init.ts
|
|
16
|
+
var instance = null;
|
|
17
|
+
var initPromise = null;
|
|
18
|
+
var DEFAULT_OPTIONS = {
|
|
19
|
+
preferredAdapters: ["opfs", "indexeddb", "memory"],
|
|
20
|
+
dbName: "classic-offline",
|
|
21
|
+
autoCleanup: true,
|
|
22
|
+
cleanupIntervalMs: 6e4
|
|
23
|
+
// 1 minute
|
|
24
|
+
};
|
|
25
|
+
async function initOfflineStorage(options) {
|
|
26
|
+
if (instance) {
|
|
27
|
+
return instance;
|
|
28
|
+
}
|
|
29
|
+
if (initPromise) {
|
|
30
|
+
return initPromise;
|
|
31
|
+
}
|
|
32
|
+
initPromise = doInitialize(options);
|
|
33
|
+
try {
|
|
34
|
+
instance = await initPromise;
|
|
35
|
+
return instance;
|
|
36
|
+
} finally {
|
|
37
|
+
initPromise = null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
async function doInitialize(options) {
|
|
41
|
+
const mergedOptions = { ...DEFAULT_OPTIONS, ...options };
|
|
42
|
+
const { adapter, adapterName } = await createAdapter({
|
|
43
|
+
preferredAdapters: mergedOptions.preferredAdapters,
|
|
44
|
+
dbName: mergedOptions.dbName
|
|
45
|
+
});
|
|
46
|
+
await adapter.initialize();
|
|
47
|
+
const files = createFileStorageService(adapter);
|
|
48
|
+
const drafts = createDraftStorageService(adapter);
|
|
49
|
+
const draftManager = createDraftManagerService(adapter);
|
|
50
|
+
const cache = createCacheService(adapter);
|
|
51
|
+
const cleanup = new CleanupScheduler(mergedOptions.cleanupIntervalMs);
|
|
52
|
+
cleanup.registerAdapter(adapter);
|
|
53
|
+
if (mergedOptions.autoCleanup && mergedOptions.cleanupIntervalMs > 0) {
|
|
54
|
+
cleanup.start();
|
|
55
|
+
}
|
|
56
|
+
const config = {
|
|
57
|
+
adapterName,
|
|
58
|
+
dbName: mergedOptions.dbName,
|
|
59
|
+
autoCleanup: mergedOptions.autoCleanup,
|
|
60
|
+
cleanupIntervalMs: mergedOptions.cleanupIntervalMs
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
adapter,
|
|
64
|
+
config,
|
|
65
|
+
files,
|
|
66
|
+
drafts,
|
|
67
|
+
draftManager,
|
|
68
|
+
cache,
|
|
69
|
+
cleanup,
|
|
70
|
+
async dispose() {
|
|
71
|
+
cleanup.stop();
|
|
72
|
+
await adapter.dispose();
|
|
73
|
+
instance = null;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function getOfflineStorage() {
|
|
78
|
+
if (!instance) {
|
|
79
|
+
throw OfflineStorageError.notInitialized();
|
|
80
|
+
}
|
|
81
|
+
return instance;
|
|
82
|
+
}
|
|
83
|
+
function isOfflineStorageInitialized() {
|
|
84
|
+
return instance !== null;
|
|
85
|
+
}
|
|
86
|
+
function getOfflineStorageOrNull() {
|
|
87
|
+
return instance;
|
|
88
|
+
}
|
|
89
|
+
async function resetOfflineStorage() {
|
|
90
|
+
if (instance) {
|
|
91
|
+
await instance.dispose();
|
|
92
|
+
instance = null;
|
|
93
|
+
}
|
|
94
|
+
initPromise = null;
|
|
95
|
+
}
|
|
96
|
+
async function detectStorageAdapter(preferredAdapters) {
|
|
97
|
+
const { adapterName, unavailable } = await createAdapter({
|
|
98
|
+
preferredAdapters
|
|
99
|
+
});
|
|
100
|
+
const available = [];
|
|
101
|
+
if (!unavailable.includes("opfs")) available.push("opfs");
|
|
102
|
+
if (!unavailable.includes("indexeddb")) available.push("indexeddb");
|
|
103
|
+
available.push("memory");
|
|
104
|
+
return { adapterName, available, unavailable };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export { detectStorageAdapter, getOfflineStorage, getOfflineStorageOrNull, initOfflineStorage, isOfflineStorageInitialized, resetOfflineStorage };
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { S as StorageAdapter, f as StorageAdapterOptions, B as BlobMetadata, d as BlobWithMetadata, e as StoreJSONOptions, J as JSONEntry, C as CleanupResult, c as StorageQuota, b as StorageAdapterName } from '../types-CWuauHAr.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Memory storage adapter
|
|
5
|
+
*
|
|
6
|
+
* Provides in-memory storage that works in SSR and as a fallback.
|
|
7
|
+
* Data is lost when the page is refreshed.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Memory storage adapter implementation
|
|
12
|
+
*/
|
|
13
|
+
declare class MemoryAdapter implements StorageAdapter {
|
|
14
|
+
readonly name: "memory";
|
|
15
|
+
private blobs;
|
|
16
|
+
private json;
|
|
17
|
+
private initialized;
|
|
18
|
+
constructor(_options?: StorageAdapterOptions);
|
|
19
|
+
storeBlob(key: string, blob: Blob, metadata?: BlobMetadata): Promise<void>;
|
|
20
|
+
getBlob(key: string): Promise<BlobWithMetadata | null>;
|
|
21
|
+
deleteBlob(key: string): Promise<void>;
|
|
22
|
+
listBlobs(prefix?: string): Promise<string[]>;
|
|
23
|
+
storeJSON<T>(key: string, data: T, options?: StoreJSONOptions): Promise<void>;
|
|
24
|
+
getJSON<T>(key: string): Promise<JSONEntry<T> | null>;
|
|
25
|
+
deleteJSON(key: string): Promise<void>;
|
|
26
|
+
listJSON(prefix?: string): Promise<string[]>;
|
|
27
|
+
deleteJSONByTag(tag: string): Promise<number>;
|
|
28
|
+
cleanup(): Promise<CleanupResult>;
|
|
29
|
+
getQuota(): Promise<StorageQuota>;
|
|
30
|
+
isAvailable(): Promise<boolean>;
|
|
31
|
+
initialize(): Promise<void>;
|
|
32
|
+
dispose(): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Check if initialized (for testing)
|
|
35
|
+
*/
|
|
36
|
+
isInitialized(): boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Create a memory storage adapter
|
|
40
|
+
*/
|
|
41
|
+
declare function createMemoryAdapter(options?: StorageAdapterOptions): MemoryAdapter;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* IndexedDB storage adapter
|
|
45
|
+
*
|
|
46
|
+
* Provides persistent storage using IndexedDB.
|
|
47
|
+
* Works in all modern browsers, including private browsing mode (with limitations).
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* IndexedDB storage adapter implementation
|
|
52
|
+
*/
|
|
53
|
+
declare class IndexedDBAdapter implements StorageAdapter {
|
|
54
|
+
readonly name: "indexeddb";
|
|
55
|
+
private db;
|
|
56
|
+
private readonly dbName;
|
|
57
|
+
constructor(options?: StorageAdapterOptions);
|
|
58
|
+
/**
|
|
59
|
+
* Get the database, initializing if needed
|
|
60
|
+
*/
|
|
61
|
+
private getDB;
|
|
62
|
+
storeBlob(key: string, blob: Blob, metadata?: BlobMetadata): Promise<void>;
|
|
63
|
+
getBlob(key: string): Promise<BlobWithMetadata | null>;
|
|
64
|
+
deleteBlob(key: string): Promise<void>;
|
|
65
|
+
listBlobs(prefix?: string): Promise<string[]>;
|
|
66
|
+
listBlobsByFormKey(formKey: string, fieldName?: string): Promise<{
|
|
67
|
+
key: string;
|
|
68
|
+
metadata: BlobMetadata;
|
|
69
|
+
}[]>;
|
|
70
|
+
storeJSON<T>(key: string, data: T, options?: StoreJSONOptions): Promise<void>;
|
|
71
|
+
getJSON<T>(key: string): Promise<JSONEntry<T> | null>;
|
|
72
|
+
deleteJSON(key: string): Promise<void>;
|
|
73
|
+
listJSON(prefix?: string): Promise<string[]>;
|
|
74
|
+
deleteJSONByTag(tag: string): Promise<number>;
|
|
75
|
+
cleanup(): Promise<CleanupResult>;
|
|
76
|
+
getQuota(): Promise<StorageQuota>;
|
|
77
|
+
isAvailable(): Promise<boolean>;
|
|
78
|
+
initialize(): Promise<void>;
|
|
79
|
+
dispose(): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Create an IndexedDB storage adapter
|
|
83
|
+
*/
|
|
84
|
+
declare function createIndexedDBAdapter(options?: StorageAdapterOptions): IndexedDBAdapter;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* OPFS (Origin Private File System) storage adapter
|
|
88
|
+
*
|
|
89
|
+
* Provides high-performance file storage using OPFS.
|
|
90
|
+
* Uses a Web Worker for Safari compatibility (Safari requires OPFS in worker context).
|
|
91
|
+
*
|
|
92
|
+
* OPFS is only used for blob storage - JSON storage falls back to IndexedDB
|
|
93
|
+
* for better indexing support.
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* OPFS storage adapter implementation
|
|
98
|
+
*/
|
|
99
|
+
declare class OPFSAdapter implements StorageAdapter {
|
|
100
|
+
readonly name: "opfs";
|
|
101
|
+
private worker;
|
|
102
|
+
private workerBlobUrl;
|
|
103
|
+
private pendingRequests;
|
|
104
|
+
private useWorker;
|
|
105
|
+
private initialized;
|
|
106
|
+
private rootHandle;
|
|
107
|
+
private blobsHandle;
|
|
108
|
+
private metadataHandle;
|
|
109
|
+
private jsonAdapter;
|
|
110
|
+
constructor(options?: StorageAdapterOptions);
|
|
111
|
+
/**
|
|
112
|
+
* Send a message to the worker and wait for response
|
|
113
|
+
*/
|
|
114
|
+
private sendWorkerMessage;
|
|
115
|
+
/**
|
|
116
|
+
* Handle messages from the worker
|
|
117
|
+
*/
|
|
118
|
+
private handleWorkerMessage;
|
|
119
|
+
/**
|
|
120
|
+
* Encode key to safe filename
|
|
121
|
+
*/
|
|
122
|
+
private encodeKey;
|
|
123
|
+
/**
|
|
124
|
+
* Decode filename back to key
|
|
125
|
+
*/
|
|
126
|
+
private decodeKey;
|
|
127
|
+
storeBlob(key: string, blob: Blob, metadata?: BlobMetadata): Promise<void>;
|
|
128
|
+
getBlob(key: string): Promise<BlobWithMetadata | null>;
|
|
129
|
+
deleteBlob(key: string): Promise<void>;
|
|
130
|
+
listBlobs(prefix?: string): Promise<string[]>;
|
|
131
|
+
storeJSON<T>(key: string, data: T, options?: StoreJSONOptions): Promise<void>;
|
|
132
|
+
getJSON<T>(key: string): Promise<JSONEntry<T> | null>;
|
|
133
|
+
deleteJSON(key: string): Promise<void>;
|
|
134
|
+
listJSON(prefix?: string): Promise<string[]>;
|
|
135
|
+
deleteJSONByTag(tag: string): Promise<number>;
|
|
136
|
+
cleanup(): Promise<CleanupResult>;
|
|
137
|
+
private cleanupBlobsDirect;
|
|
138
|
+
getQuota(): Promise<StorageQuota>;
|
|
139
|
+
isAvailable(): Promise<boolean>;
|
|
140
|
+
initialize(): Promise<void>;
|
|
141
|
+
private initializeWorker;
|
|
142
|
+
dispose(): Promise<void>;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Create an OPFS storage adapter
|
|
146
|
+
*/
|
|
147
|
+
declare function createOPFSAdapter(options?: StorageAdapterOptions): OPFSAdapter;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Storage adapters and factory
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Options for the adapter factory
|
|
155
|
+
*/
|
|
156
|
+
interface CreateAdapterOptions extends StorageAdapterOptions {
|
|
157
|
+
/**
|
|
158
|
+
* Preferred adapter order. First available adapter is used.
|
|
159
|
+
* @default ['opfs', 'indexeddb', 'memory']
|
|
160
|
+
*/
|
|
161
|
+
preferredAdapters?: StorageAdapterName[];
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Result of adapter creation
|
|
165
|
+
*/
|
|
166
|
+
interface CreateAdapterResult {
|
|
167
|
+
/** The created adapter */
|
|
168
|
+
adapter: StorageAdapter;
|
|
169
|
+
/** The adapter name that was selected */
|
|
170
|
+
adapterName: StorageAdapterName;
|
|
171
|
+
/** Names of adapters that were tried but unavailable */
|
|
172
|
+
unavailable: StorageAdapterName[];
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Create the best available storage adapter
|
|
176
|
+
*
|
|
177
|
+
* Tries adapters in preference order, falling back to the next option
|
|
178
|
+
* if an adapter is unavailable.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* const { adapter, adapterName } = await createAdapter();
|
|
183
|
+
* console.log(`Using ${adapterName} adapter`);
|
|
184
|
+
*
|
|
185
|
+
* await adapter.initialize();
|
|
186
|
+
* await adapter.storeJSON('key', { value: 42 });
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
declare function createAdapter(options?: CreateAdapterOptions): Promise<CreateAdapterResult>;
|
|
190
|
+
/**
|
|
191
|
+
* Detect which adapters are available in the current environment
|
|
192
|
+
*/
|
|
193
|
+
declare function detectAvailableAdapters(): Promise<{
|
|
194
|
+
available: StorageAdapterName[];
|
|
195
|
+
unavailable: StorageAdapterName[];
|
|
196
|
+
}>;
|
|
197
|
+
|
|
198
|
+
export { type CreateAdapterOptions, type CreateAdapterResult, IndexedDBAdapter, MemoryAdapter, OPFSAdapter, StorageAdapter, StorageAdapterOptions, createAdapter, createIndexedDBAdapter, createMemoryAdapter, createOPFSAdapter, detectAvailableAdapters };
|