74 lines
3.3 KiB
JavaScript
74 lines
3.3 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)
|
||
function marketFetch (url) {
|
||
const T = (global.__TAURI__ && global.__TAURI__.http) ? global.__TAURI__.http : null
|
||
if (T && typeof T.fetch === 'function') return T.fetch(url)
|
||
return global.fetch(url, { cache: 'no-store' })
|
||
}
|
||
|
||
async function list () {
|
||
const resp = await marketFetch(MARKET_URL)
|
||
if (!resp.ok) throw new Error('市场不可达:HTTP ' + resp.status)
|
||
const j = await resp.json()
|
||
return Array.isArray(j && j.plugins) ? j.plugins : []
|
||
}
|
||
|
||
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, installed: installed, install: install, uninstall: uninstall }
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|