snapcap
API ReferenceClasses

StorageShim

Class: StorageShim

Defined in: storage/storage-shim.ts:49

Web Storage API (localStorage / sessionStorage) implementation backed by a DataStore.

Remarks

Pass an instance into the sandbox via new Sandbox({ dataStore, … }) so the bundle's storage reads/writes route through your DataStore.

Example

import { StorageShim, FileDataStore } from "@snapcap/native";

const store = new FileDataStore(".tmp/auth/auth.json");
const local = new StorageShim(store, "local_");
local.setItem("foo", "bar");
local.getItem("foo"); // "bar"

Implements

Constructors

Constructor

new StorageShim(store: DataStore, prefix: string): StorageShim;

Defined in: storage/storage-shim.ts:62

Parameters

ParameterTypeDescription
storeDataStoreBacking DataStore. Sync access (getSync / setSync / keys) is preferred so this shim can stay strictly synchronous; without it, async writes are fire-and-forget and reads serve from a pre-populated fallback cache.
prefixstringPer-instance key prefix. Two shims sharing a DataStore should use distinct prefixes (e.g. "local_" and "session_") so keys don't collide.

Returns

StorageShim

Accessors

length

Get Signature

get length(): number;

Defined in: storage/storage-shim.ts:82

Number of stored keys under this shim's prefix.

Returns

number

Implementation of

Storage.length

Methods

clear()

clear(): void;

Defined in: storage/storage-shim.ts:150

Web Storage clear() — delete every entry under this shim's prefix.

Returns

void

Implementation of

Storage.clear

getItem()

getItem(key: string): string | null;

Defined in: storage/storage-shim.ts:112

Web Storage getItem(key) — read the string value at key.

Parameters

ParameterTypeDescription
keystringThe (un-prefixed) key.

Returns

string | null

The decoded UTF-8 string, or null if the key is absent.

Implementation of

Storage.getItem

key()

key(index: number): string | null;

Defined in: storage/storage-shim.ts:95

Web Storage key(index) — return the Nth key under this shim's prefix.

Parameters

ParameterTypeDescription
indexnumberZero-based key index.

Returns

string | null

The key (without the prefix) or null if index is out of range.

Implementation of

Storage.key

removeItem()

removeItem(key: string): void;

Defined in: storage/storage-shim.ts:143

Web Storage removeItem(key) — delete the entry at key.

Parameters

ParameterTypeDescription
keystringThe (un-prefixed) key to remove.

Returns

void

Implementation of

Storage.removeItem

setItem()

setItem(key: string, value: string): void;

Defined in: storage/storage-shim.ts:127

Web Storage setItem(key, value) — UTF-8 encode and store.

Parameters

ParameterTypeDescription
keystringThe (un-prefixed) key.
valuestringThe string value to store.

Returns

void

Implementation of

Storage.setItem

On this page