;(function (global) { 'use strict' const result = global.IWBStorageResult const create = (options) => { const storage = options && options.storage const json = (value) => JSON.stringify(value) const parse = (raw, fallback) => { if (raw === null || raw === undefined || raw === '') return result.ok(fallback) try { return result.ok(JSON.parse(raw)) } catch (e) { return result.fail(result.error('validation', 'invalid-json', '本地存储数据格式无效', false)) } } return { get(key, fallback) { try { return parse(storage && storage.getItem(key), fallback) } catch (e) { return result.fail(result.from(e, 'unavailable'), { degraded: true }) } }, set(key, value) { try { if (!storage) return result.fail(result.error('unavailable', 'localstorage-unavailable', '浏览器本地存储不可用', false), { degraded: true }) storage.setItem(key, json(value)); return result.ok(true) } catch (e) { return result.fail(result.from(e, 'quota'), { degraded: true }) } }, remove(key) { try { if (storage) storage.removeItem(key); return result.ok(true) } catch (e) { return result.fail(result.from(e, 'unavailable'), { degraded: true }) } }, rawGet(key, fallback) { try { return result.ok(storage && storage.getItem(key) !== null ? storage.getItem(key) : fallback) } catch (e) { return result.fail(result.from(e, 'unavailable'), { degraded: true }) } }, rawSet(key, value) { try { if (!storage) return result.fail(result.error('unavailable', 'localstorage-unavailable', '浏览器本地存储不可用', false), { degraded: true }); storage.setItem(key, String(value)); return result.ok(true) } catch (e) { return result.fail(result.from(e, 'quota'), { degraded: true }) } }, bytes() { let bytes = 0 try { if (storage) for (let i = 0; i < storage.length; i++) { const key = storage.key(i); if (key) bytes += (key.length + (storage.getItem(key) || '').length) * 2 } return result.ok(bytes) } catch (e) { return result.fail(result.from(e, 'unavailable'), { degraded: true }) } }, health() { const key = '__iwb_storage_health__' const written = this.rawSet(key, '1') if (!written.ok) return written const read = this.rawGet(key, null) this.remove(key) return result.ok({ ok: read.ok && read.value === '1', bytes: this.bytes().value || 0 }) }, } } // 便利门面(2026-11-06 存储入口收敛):把 Result 风格包成"直取值", // 并补 keys()/bytes(),让 src 模块不必各自写 try/catch + JSON.parse + localStorage 裸调。 const friendly = (options) => { const op = options || {} const storage = op.storage || (global && global.localStorage) || null const inst = create({ storage: storage }) return { get(key, fallback) { const r = inst.get(key, fallback); return r && r.ok === true ? r.value : fallback }, set(key, value) { const r = inst.set(key, value); return !!(r && r.ok === true) }, remove(key) { const r = inst.remove(key); return !!(r && r.ok === true) }, rawGet(key, fallback) { const r = inst.rawGet(key, fallback); return r && r.ok === true ? r.value : fallback }, rawSet(key, value) { const r = inst.rawSet(key, value); return !!(r && r.ok === true) }, keys() { const out = [] try { if (storage) for (let i = 0; i < storage.length; i++) { const k = storage.key(i); if (k) out.push(k) } } catch (e) { /* ignore */ } return out }, bytes() { const r = inst.bytes(); return r && r.ok === true ? (r.value || 0) : 0 }, health() { return inst.health() }, } } // 共享实例(懒创建一次):app.js 与各 src 模块都走它,配额/异常处理集中在此 let sharedStore = null const getStore = () => { if (!sharedStore) sharedStore = friendly({}); return sharedStore } global.IWBStorageLocal = { create, friendly, getStore } })(typeof window !== 'undefined' ? window : globalThis)