refactor(app): 插件加载/市场/备份抽成 src 模块(app.js 8880→8764 行);备份包含插件
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 1m31s
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 1m31s
This commit is contained in:
1 parent
0ec11bfca8
commit
ce575d2640
11 files changed
+297
-149
No files matched your search
@@ -0,0 +1,120 @@
|
||||
/* runtime-plugin-loader.js —— 插件脚本加载(内置 / 外部 / 本地开发)
|
||||
*
|
||||
* 从 app.js 抽出来:app.js 只做装配,加载细节在这里。
|
||||
* 三个入口都返回 boolean,并把状态写回传进来的 record(status/error),供插件面板展示。
|
||||
* · loadBuiltin :同源 /plugins/<file>,走 SRI(integrity 强制校验)
|
||||
* · loadExternal :<资料目录>/plugins/<file>,先读文本 → WebCrypto 校验 sha256 → blob 注入
|
||||
* · loadLocal :开发者模式,code 已在内存里(免签名),blob 注入
|
||||
*/
|
||||
;(function (global) {
|
||||
'use strict'
|
||||
|
||||
function host () {
|
||||
return global.IWBPluginHost || null
|
||||
}
|
||||
|
||||
// 外部插件源(<资料目录>/plugins/ 的读写封装)
|
||||
function source () {
|
||||
const F = global.IWBStorageFile || null
|
||||
return (F && F.plugins && typeof F.plugins.read === 'function') ? F.plugins : null
|
||||
}
|
||||
|
||||
async function verifyCode (code, integrity) {
|
||||
if (!integrity) return false
|
||||
const expect = String(integrity).replace(/^sha256-/, '')
|
||||
const buf = await global.crypto.subtle.digest('SHA-256', new global.TextEncoder().encode(code))
|
||||
const u8 = new Uint8Array(buf)
|
||||
let bin = ''
|
||||
for (let i = 0; i < u8.length; i++) bin += String.fromCharCode(u8[i])
|
||||
return global.btoa(bin) === expect
|
||||
}
|
||||
|
||||
// 注入一段脚本并等它执行完(插件在加载期自我注册)
|
||||
function inject (attrs) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
const script = global.document.createElement('script')
|
||||
script.src = attrs.src
|
||||
script.async = false
|
||||
if (attrs.integrity) { script.integrity = attrs.integrity; script.crossOrigin = 'anonymous' }
|
||||
script.onload = function () { resolve() }
|
||||
script.onerror = function () { reject(new Error(attrs.error || '脚本加载失败')) }
|
||||
global.document.head.appendChild(script)
|
||||
})
|
||||
}
|
||||
|
||||
function markResult (record) {
|
||||
const H = host()
|
||||
const registered = !!(H && typeof H.list === 'function' && H.list().some(function (p) { return p.id === record.id }))
|
||||
record.status = registered ? 'loaded' : 'not-registered'
|
||||
if (registered && typeof H.isEnabled === 'function' && H.isEnabled(record.id)) H.setEnabled(record.id, true)
|
||||
return record.status === 'loaded'
|
||||
}
|
||||
|
||||
async function loadBuiltin (record) {
|
||||
record.status = 'loading'
|
||||
const integrity = record.integrity
|
||||
try {
|
||||
await inject({
|
||||
src: '/plugins/' + record.file,
|
||||
integrity: integrity,
|
||||
error: integrity ? '脚本完整性校验失败(integrity 不符或加载失败)' : '脚本加载失败'
|
||||
})
|
||||
return markResult(record)
|
||||
} catch (err) {
|
||||
record.status = 'error'
|
||||
record.error = String((err && err.message) || err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadExternal (record) {
|
||||
record.status = 'loading'
|
||||
const src = source()
|
||||
if (!src) { record.status = 'error'; record.error = '外部插件不可用'; return false }
|
||||
try {
|
||||
const code = await src.read(record.file)
|
||||
if (record.integrity) {
|
||||
const ok = await verifyCode(code, record.integrity)
|
||||
if (!ok) throw new Error('完整性校验失败(外部插件)')
|
||||
}
|
||||
const url = global.URL.createObjectURL(new global.Blob([code], { type: 'text/javascript' }))
|
||||
try {
|
||||
await inject({ src: url, error: '外部插件脚本执行失败' })
|
||||
} finally {
|
||||
setTimeout(function () { try { global.URL.revokeObjectURL(url) } catch (e) { /* ignore */ } }, 30000)
|
||||
}
|
||||
return markResult(record)
|
||||
} catch (err) {
|
||||
record.status = 'error'
|
||||
record.error = String((err && err.message) || err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadLocal (record) {
|
||||
record.status = 'loading'
|
||||
try {
|
||||
const code = String(record.code || '')
|
||||
if (!code) throw new Error('插件源码为空')
|
||||
const url = global.URL.createObjectURL(new global.Blob([code], { type: 'text/javascript' }))
|
||||
try {
|
||||
await inject({ src: url, error: '本地插件脚本执行失败' })
|
||||
} finally {
|
||||
setTimeout(function () { try { global.URL.revokeObjectURL(url) } catch (e) { /* ignore */ } }, 30000)
|
||||
}
|
||||
return markResult(record)
|
||||
} catch (err) {
|
||||
record.status = 'error'
|
||||
record.error = String((err && err.message) || err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
global.IWBRuntimePluginLoader = {
|
||||
source: source,
|
||||
verifyCode: verifyCode,
|
||||
loadBuiltin: loadBuiltin,
|
||||
loadExternal: loadExternal,
|
||||
loadLocal: loadLocal
|
||||
}
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
Reference in new issue
Block a user