import { KeyValueStorage, MemoryStorage, RefScopedStorage, ScopedStorage } from "./basic"; import { BrowserLocalStorage } from "./browser"; import { NodeDirectoryStorage } from "./node"; import { RestRemoteStorage } from "./remote"; /** * Base type for storage usage */ export type TKStorage = KeyValueStorage /** * Exposed classes */ export { MemoryStorage, ScopedStorage, RefScopedStorage, BrowserLocalStorage, NodeDirectoryStorage, RestRemoteStorage }; /** * Get the best "local" storage available */ export function getLocalStorage(appname: string): TKStorage { try { return new ScopedStorage(new BrowserLocalStorage(), appname); } catch { try { return new NodeDirectoryStorage(appname); } catch { console.warn("No persistent storage available, using in-memory volatile storage"); return new MemoryStorage(); } } } /** * Get a remote storage, based on an URI * * If *shared* is set to true, a namespace will be used to avoid collisions, using getLocalStorage to persist it */ export function getRemoteStorage(appname: string, uri: string, options = { shared: false }): TKStorage { let storage: TKStorage = new RestRemoteStorage(appname, uri); storage = new ScopedStorage(storage, appname); if (options.shared) { storage = new RefScopedStorage(getLocalStorage(appname), storage); } return storage; } /** * Get a in-memory volatile storage */ export function getMemoryStorage(): TKStorage { return new MemoryStorage(); }