Files
image-workbench-desktop/src/runtime-plugin-loader.js
T
2026-11-17 15:14:46 +08:00

121 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 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)