145 lines
5.7 KiB
JavaScript
145 lines
5.7 KiB
JavaScript
;(function (global) {
|
|
'use strict'
|
|
|
|
function safeGet(key, fallback) {
|
|
try {
|
|
const raw = global.localStorage && global.localStorage.getItem(key)
|
|
return raw ? JSON.parse(raw) : fallback
|
|
} catch (e) { return fallback }
|
|
}
|
|
function safeSet(key, value) {
|
|
try {
|
|
if (!global.localStorage) return false
|
|
global.localStorage.setItem(key, JSON.stringify(value))
|
|
return true
|
|
} catch (e) { return false }
|
|
}
|
|
function safeRemove(key) {
|
|
try {
|
|
if (!global.localStorage) return false
|
|
global.localStorage.removeItem(key)
|
|
return true
|
|
} catch (e) { return false }
|
|
}
|
|
function rawGet(key, fallback) {
|
|
try {
|
|
const value = global.localStorage && global.localStorage.getItem(key)
|
|
return value === null || value === undefined ? fallback : value
|
|
} catch (e) { return fallback }
|
|
}
|
|
function rawSet(key, value) {
|
|
try {
|
|
if (!global.localStorage) return false
|
|
global.localStorage.setItem(key, String(value))
|
|
return true
|
|
} catch (e) { return false }
|
|
}
|
|
function bytes() {
|
|
let total = 0
|
|
try {
|
|
if (!global.localStorage) return 0
|
|
for (let i = 0; i < global.localStorage.length; i++) {
|
|
const key = global.localStorage.key(i)
|
|
if (key) total += (key.length + (global.localStorage.getItem(key) || '').length) * 2
|
|
}
|
|
} catch (e) { /* best effort */ }
|
|
return total
|
|
}
|
|
|
|
function createIndexedDb(contract) {
|
|
let db = null
|
|
const open = () => new Promise((resolve) => {
|
|
if (db) return resolve(db)
|
|
if (!global.indexedDB) return resolve(null)
|
|
try {
|
|
const req = global.indexedDB.open(contract.name, contract.version)
|
|
req.onupgradeneeded = () => {
|
|
const result = req.result
|
|
for (const store of contract.stores || []) {
|
|
if (!result.objectStoreNames.contains(store)) result.createObjectStore(store)
|
|
}
|
|
}
|
|
req.onsuccess = () => {
|
|
db = req.result
|
|
db.onclose = () => { db = null }
|
|
resolve(db)
|
|
}
|
|
req.onerror = () => resolve(null)
|
|
} catch (e) { resolve(null) }
|
|
})
|
|
const request = (store, mode, action) => open().then((database) => {
|
|
if (!database) return null
|
|
return new Promise((resolve) => {
|
|
let settled = false
|
|
const finish = (value) => { if (!settled) { settled = true; resolve(value) } }
|
|
try {
|
|
const tx = database.transaction(store, mode)
|
|
const req = action(tx.objectStore(store))
|
|
const hasRequest = !!(req && typeof req.onsuccess !== 'undefined')
|
|
if (hasRequest) {
|
|
req.onsuccess = () => { if (mode === 'readonly') finish(req.result) }
|
|
req.onerror = () => finish(null)
|
|
}
|
|
tx.oncomplete = () => finish(hasRequest && mode === 'readonly' ? req.result : true)
|
|
tx.onerror = () => finish(false)
|
|
tx.onabort = () => finish(false)
|
|
} catch (e) { finish(false) }
|
|
})
|
|
})
|
|
const write = (store, key, value) => request(store, 'readwrite', (objectStore) => objectStore.put(value, key)).then((v) => {
|
|
const ok = v === true
|
|
// 图库落盘自检的会话计数(只认 out_orig_,其它键会被 noteWrite 自己忽略)
|
|
try { global.IWBGalleryAudit && global.IWBGalleryAudit.noteWrite(key, ok, ok ? null : 'IndexedDB 写入失败') } catch (e) { /* 计数失败不影响写入 */ }
|
|
return ok
|
|
})
|
|
const remove = (store, key) => request(store, 'readwrite', (objectStore) => objectStore.delete(key)).then((v) => v === true)
|
|
const read = (store, key) => request(store, 'readonly', (objectStore) => objectStore.get(key))
|
|
return {
|
|
put(key, value) { return write('images', key, value) },
|
|
get(key) { return read('images', key) },
|
|
del(key) { return remove('images', key) },
|
|
keys() { return request('images', 'readonly', (objectStore) => objectStore.getAllKeys()).then((v) => Array.isArray(v) ? v : []) },
|
|
logPut(entry) { return write('logs', entry.id, entry) },
|
|
logAll() { return request('logs', 'readonly', (objectStore) => objectStore.getAll()).then((v) => Array.isArray(v) ? v : []) },
|
|
logClear() { return request('logs', 'readwrite', (objectStore) => objectStore.clear()).then((v) => v === true) },
|
|
imagesAll() { return open().then((database) => {
|
|
if (!database) return []
|
|
return new Promise((resolve) => {
|
|
try {
|
|
const out = []
|
|
const req = database.transaction('images', 'readonly').objectStore('images').openCursor()
|
|
req.onsuccess = () => {
|
|
const cursor = req.result
|
|
if (cursor) { out.push({ key: cursor.key, value: cursor.value }); cursor.continue() } else resolve(out)
|
|
}
|
|
req.onerror = () => resolve(out)
|
|
} catch (e) { resolve([]) }
|
|
})
|
|
}) },
|
|
kvPut(key, value) { return write('kv', key, value) },
|
|
kvGet(key) { return read('kv', key) },
|
|
}
|
|
}
|
|
|
|
function createStorageRuntime(contract) {
|
|
const indexedDb = createIndexedDb(contract.indexedDb)
|
|
return {
|
|
get: safeGet,
|
|
set: safeSet,
|
|
remove: safeRemove,
|
|
rawGet,
|
|
rawSet,
|
|
bytes,
|
|
indexedDb,
|
|
}
|
|
}
|
|
|
|
// app.js consumes a runtime instance; retain the factory for tests and embedders.
|
|
const defaultContract = global.IWBStorageContract || { indexedDb: { name: 'iwb_web', version: 3, stores: ['images', 'logs', 'kv'] } }
|
|
const runtimeInstance = createStorageRuntime(defaultContract)
|
|
const runtimeCompat = function (contract) { return createStorageRuntime(contract || defaultContract) }
|
|
Object.assign(runtimeCompat, runtimeInstance)
|
|
global.IWBStorageRuntimeFactory = createStorageRuntime
|
|
global.IWBStorageRuntime = runtimeCompat
|
|
})(typeof window !== 'undefined' ? window : globalThis)
|