Verify (Gitea) / gate (build · unit · facts) (push) Failing after 51s
同上一条视口 bug 是同一类:桌面端有两套存储入口——lsGet/lsSet 走资料目录(IWBStorageFile.runtime),
而 IWBStorageLocal.getStore() 固定返回 localStorage 门面。用后者的模块(插件数据、插件安装记录、
停靠栏布局、设置、崩溃日志、自动备份)实际写在 WebView localStorage 里:重启不丢但不进资料目录,
多机共享时不同步,清 WebView 数据就全没。
改动(src/storage-local.js):
- getStore():有文件底座就用文件底座(friendly({storage: IWBStorageFile.storage()})),否则退回 localStorage
- 首次绑定文件底座时做一次性迁移:把 localStorage 里 iwb* 开头的键搬到文件侧
(只搬文件侧还没有的键,不覆盖已有数据;写 iwb_local_migrated_v1 标记只跑一次)
- 两套实例分别缓存(sharedStore / sharedFileStore),避免早期调用把 localStorage 实例固化
106 lines
5.7 KiB
JavaScript
106 lines
5.7 KiB
JavaScript
;(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
|
||
let sharedFileStore = null // 桌面端:绑到资料目录底座的共享实例(与 lsGet/lsSet 同源)
|
||
// v3.26.109:getStore() 必须和 lsGet/lsSet 用同一个底座。
|
||
// 桌面端 lsGet 走的是资料目录(IWBStorageFile.runtime),而这里原来固定返回 localStorage 门面 ——
|
||
// 于是「视口」「插件安装状态」「停靠栏布局」等写进 WebView localStorage、读却读资料目录(或反之),
|
||
// 表现为重启后设置丢失、多机不同步。这里改成:有文件底座就用文件底座,并把旧 localStorage 里的
|
||
// iwb* 键一次性搬过去(只在文件侧没有该键时搬,不覆盖已有数据)。
|
||
const fileStorageOf = () => {
|
||
const fs = global.IWBStorageFile
|
||
if (!fs || typeof fs.enabled !== 'function' || !fs.enabled() || typeof fs.storage !== 'function') return null
|
||
try { return fs.storage() || null } catch (e) { return null }
|
||
}
|
||
const migrateLocalOnce = (dst) => {
|
||
try {
|
||
if (!dst || typeof dst.setItem !== 'function') return
|
||
if (dst.getItem('iwb_local_migrated_v1') !== null) return
|
||
const src = global.localStorage
|
||
if (src && src.length) {
|
||
for (let i = 0; i < src.length; i++) {
|
||
const k = src.key(i)
|
||
if (!k || k.indexOf('iwb') !== 0) continue
|
||
if (dst.getItem(k) !== null) continue
|
||
try { dst.setItem(k, src.getItem(k)) } catch (e) { /* 单条失败继续 */ }
|
||
}
|
||
}
|
||
try { dst.setItem('iwb_local_migrated_v1', '1') } catch (e) { /* ignore */ }
|
||
} catch (e) { /* ignore */ }
|
||
}
|
||
const getStore = () => {
|
||
const fsStore = fileStorageOf()
|
||
if (fsStore) {
|
||
if (!sharedFileStore) { sharedFileStore = friendly({ storage: fsStore }); migrateLocalOnce(fsStore) }
|
||
return sharedFileStore
|
||
}
|
||
if (!sharedStore) sharedStore = friendly({})
|
||
return sharedStore
|
||
}
|
||
|
||
global.IWBStorageLocal = { create, friendly, getStore }
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|