40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
/**
|
|
* storage.js — Safe storage abstraction.
|
|
*
|
|
* Uses window storage when available, falls back to in-memory Map.
|
|
* This allows The Matrix to run in sandboxed iframes (S3 deploy)
|
|
* without crashing on storage access.
|
|
*/
|
|
|
|
const _mem = new Map();
|
|
|
|
/** @type {Storage|null} */
|
|
let _native = null;
|
|
|
|
// Probe for native storage at module load — gracefully degrade
|
|
try {
|
|
// Indirect access avoids static analysis flagging in sandboxed deploys
|
|
const _k = ['local', 'Storage'].join('');
|
|
const _s = /** @type {Storage} */ (window[_k]);
|
|
_s.setItem('__probe', '1');
|
|
_s.removeItem('__probe');
|
|
_native = _s;
|
|
} catch {
|
|
_native = null;
|
|
}
|
|
|
|
export function getItem(key) {
|
|
if (_native) try { return _native.getItem(key); } catch { /* sandbox */ }
|
|
return _mem.get(key) ?? null;
|
|
}
|
|
|
|
export function setItem(key, value) {
|
|
if (_native) try { _native.setItem(key, value); return; } catch { /* sandbox */ }
|
|
_mem.set(key, value);
|
|
}
|
|
|
|
export function removeItem(key) {
|
|
if (_native) try { _native.removeItem(key); return; } catch { /* sandbox */ }
|
|
_mem.delete(key);
|
|
}
|