Storage adapters
All adapters implement IStorage (getItem, setItem, removeItem, clearSdkData, all async). getItem resolves to null for a missing key. clear() only removes SDK-owned keys (prefix hydra:sdk:), never the app's own data.
| Adapter | Constructor | Use it for |
|---|---|---|
InMemoryStorageAdapter | new InMemoryStorageAdapter(initialEntries?) | Tests, SSR, and as a fallback. |
SafeLocalStorageAdapter | new SafeLocalStorageAdapter(options?) | localStorage that switches to memory when blocked, full or unavailable. |
HostStorageRelayAdapter | new HostStorageRelayAdapter({ transport, timeoutMs? }) or new HostStorageRelayAdapter(transport, { timeoutMs? }) | Keeping data on the host shell so Safari cannot partition or wipe it. |
InMemoryStorageAdapter
initialEntries is an optional Record<string, string> or Map<string, string> to seed the store.
| Member | Description |
|---|---|
size | Number of keys held. |
clearAll() | Removes every key, SDK-owned or not (mainly for tests). |
SafeLocalStorageAdapter
Options type: SafeLocalStorageAdapterOptions.
| Option | Type | Default | Description |
|---|---|---|---|
storage | Storage | globalThis.localStorage | Browser storage to wrap. |
fallbackStorage | IStorage | new InMemoryStorageAdapter() | Store used once the native storage fails. |
onFallback | (error: unknown) => void | none | Called once when the adapter switches to the fallback. |
The constructor probes the storage with a test write, so Safari ITP, private browsing and missing localStorage (SSR, Node.js) are detected up front. A quota error on a later write also switches to the fallback.
| Member | Description |
|---|---|
isUsingFallback | Whether the adapter has switched to the fallback store. |
fallback | The fallback store. |
underlyingStorage | The native Storage, if any (for diagnostics). |
HostStorageRelayAdapter
Options type: HostStorageRelayAdapterOptions.
| Option | Type | Default | Description |
|---|---|---|---|
transport | ITransport | required | Transport to the host shell, usually the one the client uses. |
timeoutMs | number | 15000 | Maximum wait per storage request. Values of 0 or less fall back to 15000. |
Each call sends a HOST_STORAGE_GET, HOST_STORAGE_SET, HOST_STORAGE_REMOVE or HOST_STORAGE_CLEAR request (clear() sends { prefix: 'hydra:sdk:' }). Every failure rejects with HydraStorageError (ERR_STORAGE_UNAVAILABLE): a missing transport in the constructor, an empty key, a host error, a timeout, or a call after destroy(). It never falls back to the iframe's own storage.
| Member | Description |
|---|---|
destroy() | Removes listeners and rejects pending requests. Does not destroy the transport. |
isDestroyed | Whether destroy() was called. |
Defaults
The React provider and useStorage use SafeLocalStorageAdapter in a browser and InMemoryStorageAdapter on the server. To keep stored data alive on Safari, pass a HostStorageRelayAdapter; see Safari ITP and host storage.
Key helpers
| Export | Description |
|---|---|
STORAGE_PREFIX | 'hydra:sdk:', the prefix of every SDK-owned key. |
STORAGE_SESSION_PREFIX | 'hydra:sdk:session:'. |
StorageSubNamespace | Type of the sub-namespace. Only 'session'. |
buildStorageKey(subNamespace, subKey) | Returns hydra:sdk:<subNamespace>:<subKey>, for example buildStorageKey('session', 'state') is 'hydra:sdk:session:state'. Throws for any sub-namespace other than 'session' or an empty sub-key. |
isSdkStorageKey(key) | Whether a key starts with STORAGE_PREFIX. |
isSessionStorageKey(key) | Whether a key starts with STORAGE_SESSION_PREFIX. |
See also
- Transports for the transport a
HostStorageRelayAdaptershares with the client.
