103 lines
4.5 KiB
JavaScript
103 lines
4.5 KiB
JavaScript
/* runtime-plugin-market.js —— 插件市场(下载 / 校验 / 安装 / 卸载)
|
||
*
|
||
* 索引:https://market.maqiart.eu.org/index.json,产物同域 plugin-<id>.<hash>.js
|
||
* 落点:<资料目录>/plugins/(清单 index.json + 插件文件)
|
||
* 校验:市场索引里的 sha256(hex)与下载文本比对,不符直接拒绝
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
const MARKET_URL = 'https://market.maqiart.eu.org/index.json'
|
||
|
||
function store () {
|
||
const F = global.IWBStorageFile || null
|
||
return (F && F.plugins) ? F.plugins : null
|
||
}
|
||
|
||
// 桌面端优先走 Tauri 原生 HTTP(WKWebView 在代理/fake-IP 环境下 fetch 会 Load failed);
|
||
// 原生失败时再退回 WebView fetch(有些 Windows 公司网络/代理只放行后者),两个错误都带上,便于定位。
|
||
async function marketFetch (url) {
|
||
const T = (global.__TAURI__ && global.__TAURI__.http) ? global.__TAURI__.http : null
|
||
let nativeErr = ''
|
||
if (T && typeof T.fetch === 'function') {
|
||
try { return await T.fetch(url) } catch (e) { nativeErr = String((e && e.message) || e) }
|
||
}
|
||
try {
|
||
return await global.fetch(url, { cache: 'no-store' })
|
||
} catch (e2) {
|
||
const webErr = String((e2 && e2.message) || e2)
|
||
throw new Error('无法访问 ' + url + (nativeErr ? ('(原生 HTTP:' + nativeErr + ')') : '') + '(网页请求:' + webErr + ')')
|
||
}
|
||
}
|
||
|
||
// 本地缓存:上次成功拉到的市场列表(毫秒级出图,后台再刷新)
|
||
const CACHE_KEY = 'iwb_market_cache'
|
||
function localStore () {
|
||
const L = global.IWBStorageLocal || null
|
||
return (L && typeof L.getStore === 'function') ? L.getStore() : null
|
||
}
|
||
function cached () {
|
||
try {
|
||
const st = localStore()
|
||
if (!st) return null
|
||
const v = st.get(CACHE_KEY)
|
||
return (Array.isArray(v) && v.length) ? v : null
|
||
} catch (e) { return null }
|
||
}
|
||
function saveCache (list) {
|
||
try { const st = localStore(); if (st && Array.isArray(list) && list.length) st.set(CACHE_KEY, list) } catch (e) { /* ignore */ }
|
||
}
|
||
|
||
async function list () {
|
||
const resp = await marketFetch(MARKET_URL)
|
||
if (!resp.ok) throw new Error('市场不可达:HTTP ' + resp.status)
|
||
const j = await resp.json()
|
||
const items = Array.isArray(j && j.plugins) ? j.plugins : []
|
||
saveCache(items)
|
||
return items
|
||
}
|
||
|
||
async function installed () {
|
||
const s = store()
|
||
if (!s) return []
|
||
return await s.manifest()
|
||
}
|
||
|
||
async function install (id) {
|
||
try {
|
||
const items = await list()
|
||
const item = items.find(function (p) { return p && p.id === id })
|
||
if (!item) return { ok: false, error: '市场里没有这个插件' }
|
||
const base = MARKET_URL.replace(/index\.json$/, '')
|
||
const resp = await marketFetch(base + item.file)
|
||
if (!resp.ok) throw new Error('下载失败:HTTP ' + resp.status)
|
||
const text = await resp.text()
|
||
const buf = await global.crypto.subtle.digest('SHA-256', new global.TextEncoder().encode(text))
|
||
const hex = Array.from(new Uint8Array(buf)).map(function (b) { return b.toString(16).padStart(2, '0') }).join('')
|
||
if (item.sha256 && hex !== item.sha256) return { ok: false, error: '校验失败(sha256 不符)' }
|
||
const s = store()
|
||
if (!s) return { ok: false, error: '外部插件目录不可用' }
|
||
const file = String(item.file).split('/').pop()
|
||
await s.write(file, text)
|
||
const cur = await s.manifest()
|
||
const next = cur.filter(function (p) { return p && p.id !== id }).concat([{ id: id, file: file, version: item.version, integrity: item.integrity }])
|
||
await s.write('index.json', JSON.stringify(next, null, 2) + '\n')
|
||
return { ok: true, restart: true, file: file }
|
||
} catch (e) { return { ok: false, error: String((e && e.message) || e) } }
|
||
}
|
||
|
||
async function uninstall (id) {
|
||
try {
|
||
const s = store()
|
||
if (!s) return { ok: false, error: '外部插件目录不可用' }
|
||
const cur = await s.manifest()
|
||
const hit = cur.find(function (p) { return p && p.id === id })
|
||
const next = cur.filter(function (p) { return p && p.id !== id })
|
||
await s.write('index.json', JSON.stringify(next, null, 2) + '\n')
|
||
if (hit && hit.file) await s.remove(hit.file)
|
||
return { ok: true, restart: true }
|
||
} catch (e) { return { ok: false, error: String((e && e.message) || e) } }
|
||
}
|
||
|
||
global.IWBRuntimePluginMarket = { list: list, cached: cached, installed: installed, install: install, uninstall: uninstall }
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|