Docs Reference

Storage adapters

The IStorage adapters the SDK ships and when to use each one.

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.

AdapterConstructorUse it for
InMemoryStorageAdapternew InMemoryStorageAdapter(initialEntries?)Tests, SSR, and as a fallback.
SafeLocalStorageAdapternew SafeLocalStorageAdapter(options?)localStorage that switches to memory when blocked, full or unavailable.
HostStorageRelayAdapternew 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.

MemberDescription
sizeNumber of keys held.
clearAll()Removes every key, SDK-owned or not (mainly for tests).

SafeLocalStorageAdapter

Options type: SafeLocalStorageAdapterOptions.

OptionTypeDefaultDescription
storageStorageglobalThis.localStorageBrowser storage to wrap.
fallbackStorageIStoragenew InMemoryStorageAdapter()Store used once the native storage fails.
onFallback(error: unknown) => voidnoneCalled 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.

MemberDescription
isUsingFallbackWhether the adapter has switched to the fallback store.
fallbackThe fallback store.
underlyingStorageThe native Storage, if any (for diagnostics).

HostStorageRelayAdapter

Options type: HostStorageRelayAdapterOptions.

OptionTypeDefaultDescription
transportITransportrequiredTransport to the host shell, usually the one the client uses.
timeoutMsnumber15000Maximum 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.

MemberDescription
destroy()Removes listeners and rejects pending requests. Does not destroy the transport.
isDestroyedWhether 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

ExportDescription
STORAGE_PREFIX'hydra:sdk:', the prefix of every SDK-owned key.
STORAGE_SESSION_PREFIX'hydra:sdk:session:'.
StorageSubNamespaceType 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 HostStorageRelayAdapter shares with the client.