@vlian/framework 1.2.59 → 1.2.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analytics/index.cjs +1 -22
- package/dist/analytics.umd.js +1 -1
- package/dist/components/LocaleSwitch.cjs +1 -116
- package/dist/components/ThemeSwitch.cjs +1 -115
- package/dist/components/index.cjs +1 -20
- package/dist/components/persistence.cjs +1 -58
- package/dist/core/Test.cjs +1 -64
- package/dist/core/app/AppContext.cjs +1 -348
- package/dist/core/app/AppContext.types.cjs +1 -4
- package/dist/core/app/BasicLayout.cjs +1 -122
- package/dist/core/app/DefaultApp.cjs +1 -150
- package/dist/core/app/index.cjs +1 -41
- package/dist/core/config/AppConfig.cjs +1 -139
- package/dist/core/config/ConfigLoader.cjs +1 -323
- package/dist/core/config/ConfigValidator.cjs +2 -133
- package/dist/core/config/index.cjs +1 -28
- package/dist/core/dev/DevTools.cjs +1 -226
- package/dist/core/error/ErrorBoundary.cjs +1 -401
- package/dist/core/error/ErrorHandler.cjs +1 -275
- package/dist/core/error/index.cjs +1 -34
- package/dist/core/event/AppEventBus.cjs +1 -444
- package/dist/core/event/frameworkEvents.cjs +1 -141
- package/dist/core/event/hooks.cjs +1 -69
- package/dist/core/event/index.cjs +1 -41
- package/dist/core/event/types.cjs +1 -62
- package/dist/core/event/useEventBus.cjs +1 -25
- package/dist/core/index.cjs +1 -140
- package/dist/core/initialization/InitializationErrorThrower.cjs +1 -75
- package/dist/core/initialization/index.cjs +1 -26
- package/dist/core/initialization/initialization.cjs +1 -64
- package/dist/core/initialization/initializationErrorState.cjs +1 -66
- package/dist/core/kernel/defaultAdapters.cjs +1 -184
- package/dist/core/kernel/errors.cjs +1 -69
- package/dist/core/kernel/index.cjs +1 -20
- package/dist/core/kernel/startKernel.cjs +1 -200
- package/dist/core/kernel/types.cjs +1 -4
- package/dist/core/middleware.cjs +1 -73
- package/dist/core/plugin/PluginEventBus.cjs +1 -298
- package/dist/core/plugin/PluginSandbox.cjs +1 -137
- package/dist/core/plugin.cjs +1 -494
- package/dist/core/router/RouterManager.cjs +1 -286
- package/dist/core/router/adapter/AdapterManager.cjs +1 -235
- package/dist/core/router/adapter/index.cjs +1 -22
- package/dist/core/router/adapter/react-router/ReactRouterAdapter.cjs +1 -128
- package/dist/core/router/adapter/react-router/index.cjs +1 -20
- package/dist/core/router/adapter/types.cjs +1 -6
- package/dist/core/router/dev/RouterDevTools.cjs +1 -260
- package/dist/core/router/dev/index.cjs +1 -20
- package/dist/core/router/dynamic/DynamicRouteManager.cjs +1 -193
- package/dist/core/router/dynamic/index.cjs +1 -20
- package/dist/core/router/errors/RouterError.cjs +1 -61
- package/dist/core/router/errors/index.cjs +1 -20
- package/dist/core/router/index.cjs +1 -36
- package/dist/core/router/lifecycle/RouterLifecycleManager.cjs +1 -144
- package/dist/core/router/lifecycle/index.cjs +1 -20
- package/dist/core/router/middleware/RouterMiddlewareManager.cjs +1 -193
- package/dist/core/router/middleware/auth.cjs +1 -66
- package/dist/core/router/middleware/index.cjs +1 -22
- package/dist/core/router/middleware/types.cjs +1 -6
- package/dist/core/router/monitoring/RouterMonitoring.cjs +1 -227
- package/dist/core/router/monitoring/index.cjs +1 -20
- package/dist/core/router/navigation/RouterNavigation.cjs +1 -239
- package/dist/core/router/navigation/index.cjs +1 -20
- package/dist/core/router/performance/RouteCache.cjs +1 -305
- package/dist/core/router/performance/RoutePreloader.cjs +1 -292
- package/dist/core/router/performance/index.cjs +1 -21
- package/dist/core/router/plugin/RouterPluginManager.cjs +1 -262
- package/dist/core/router/plugin/index.cjs +1 -21
- package/dist/core/router/plugin/types.cjs +1 -39
- package/dist/core/router/types.cjs +1 -4
- package/dist/core/router/utils/adapters/react-router/RouteErrorBoundary.cjs +1 -129
- package/dist/core/router/utils/adapters/react-router/transform.cjs +1 -250
- package/dist/core/router/utils/transform.cjs +1 -780
- package/dist/core/router/validation/RouterConfigValidator.cjs +2 -83
- package/dist/core/router/validation/index.cjs +1 -21
- package/dist/core/router/validation/schema.cjs +1 -159
- package/dist/core/router/version/RouteVersionManager.cjs +1 -205
- package/dist/core/router/version/index.cjs +1 -20
- package/dist/core/splash/SplashScreen.cjs +1 -341
- package/dist/core/splash/index.cjs +1 -22
- package/dist/core/splash/splashScreenUtils.cjs +1 -38
- package/dist/core/startup/AppInstance.cjs +1 -239
- package/dist/core/startup/environment.cjs +1 -169
- package/dist/core/startup/index.cjs +1 -23
- package/dist/core/startup/initializeServices.cjs +1 -226
- package/dist/core/startup/performanceTracker.cjs +1 -179
- package/dist/core/startup/renderApp.cjs +1 -314
- package/dist/core/startup/startApp.cjs +1 -317
- package/dist/core/types.cjs +1 -4
- package/dist/index.cjs +1 -52
- package/dist/index.umd.cjs +1 -27
- package/dist/index.umd.js +1 -1
- package/dist/kernel/constants.cjs +1 -65
- package/dist/kernel/index.cjs +1 -38
- package/dist/kernel/kernel.cjs +1 -295
- package/dist/kernel/manager/cacheManager.cjs +1 -46
- package/dist/kernel/manager/i18n/I18nManager.cjs +1 -91
- package/dist/kernel/manager/i18n/i18n.persistence.cjs +1 -60
- package/dist/kernel/manager/i18n/i18n.schema.cjs +1 -86
- package/dist/kernel/manager/i18n/index.cjs +1 -11
- package/dist/kernel/manager/i18nManager.cjs +1 -11
- package/dist/kernel/manager/index.cjs +1 -28
- package/dist/kernel/manager/logger/LoggerManager.cjs +1 -107
- package/dist/kernel/manager/logger/index.cjs +1 -11
- package/dist/kernel/manager/logger/logger.persistence.cjs +1 -62
- package/dist/kernel/manager/logger/logger.schema.cjs +1 -74
- package/dist/kernel/manager/loggerManager.cjs +1 -11
- package/dist/kernel/manager/theme/ThemeManager.cjs +1 -84
- package/dist/kernel/manager/theme/index.cjs +1 -11
- package/dist/kernel/manager/theme/theme.dom.cjs +1 -61
- package/dist/kernel/manager/theme/theme.persistence.cjs +1 -57
- package/dist/kernel/manager/theme/theme.schema.cjs +1 -122
- package/dist/kernel/manager/themeManager.cjs +1 -11
- package/dist/kernel/types.cjs +1 -4
- package/dist/library/index.cjs +1 -19
- package/dist/library/locale/index.cjs +1 -39
- package/dist/library/locale/langs/en-us/index.cjs +1 -32
- package/dist/library/locale/langs/zh-cn/index.cjs +1 -32
- package/dist/library/locale/types.cjs +1 -4
- package/dist/library/storage/cache.cjs +1 -243
- package/dist/library/storage/encryption.cjs +1 -147
- package/dist/library/storage/index.cjs +1 -124
- package/dist/state/StateManager.cjs +1 -166
- package/dist/state/adapters/AdapterFactory.cjs +1 -89
- package/dist/state/adapters/DefaultAdapter.cjs +1 -75
- package/dist/state/adapters/ReduxAdapter.cjs +1 -443
- package/dist/state/adapters/ZustandAdapter.cjs +1 -69
- package/dist/state/adapters/index.cjs +1 -44
- package/dist/state/adapters/types.cjs +1 -20
- package/dist/state/core/DerivedStateInstance.cjs +1 -174
- package/dist/state/core/StateInstance.cjs +1 -170
- package/dist/state/core/StateRegistry.cjs +1 -110
- package/dist/state/core/StateScope.cjs +1 -137
- package/dist/state/core/index.cjs +1 -30
- package/dist/state/index.cjs +1 -30
- package/dist/state/types.cjs +1 -12
- package/dist/state.umd.js +1 -1
- package/dist/types.cjs +1 -4
- package/dist/utils/analytics.cjs +1 -217
- package/dist/utils/configSecurity.cjs +3 -182
- package/dist/utils/csrf.cjs +1 -18
- package/dist/utils/errors/ErrorCodes.cjs +1 -25
- package/dist/utils/errors.cjs +1 -111
- package/dist/utils/index.cjs +1 -135
- package/dist/utils/logger.cjs +1 -25
- package/dist/utils/logger.types.cjs +1 -11
- package/dist/utils/monitoring.cjs +1 -18
- package/dist/utils/performance.cjs +1 -22
- package/dist/utils/resourceLoader.cjs +1 -22
- package/dist/utils/runtimeSecurity.cjs +1 -11
- package/dist/utils/security.cjs +1 -19
- package/dist/utils/traceId.cjs +1 -37
- package/dist/utils/validation.cjs +1 -19
- package/package.json +7 -3
package/dist/core/plugin.cjs
CHANGED
|
@@ -1,494 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get PluginEventBus () {
|
|
13
|
-
return _PluginEventBus.PluginEventBus;
|
|
14
|
-
},
|
|
15
|
-
get PluginManager () {
|
|
16
|
-
return PluginManager;
|
|
17
|
-
},
|
|
18
|
-
get PluginPermission () {
|
|
19
|
-
return _PluginSandbox.PluginPermission;
|
|
20
|
-
},
|
|
21
|
-
get PluginSandbox () {
|
|
22
|
-
return _PluginSandbox.PluginSandbox;
|
|
23
|
-
},
|
|
24
|
-
get pluginEventBus () {
|
|
25
|
-
return _PluginEventBus.pluginEventBus;
|
|
26
|
-
},
|
|
27
|
-
get pluginManager () {
|
|
28
|
-
return pluginManager;
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
const _PluginSandbox = require("./plugin/PluginSandbox");
|
|
32
|
-
const _PluginEventBus = require("./plugin/PluginEventBus");
|
|
33
|
-
function _define_property(obj, key, value) {
|
|
34
|
-
if (key in obj) {
|
|
35
|
-
Object.defineProperty(obj, key, {
|
|
36
|
-
value: value,
|
|
37
|
-
enumerable: true,
|
|
38
|
-
configurable: true,
|
|
39
|
-
writable: true
|
|
40
|
-
});
|
|
41
|
-
} else {
|
|
42
|
-
obj[key] = value;
|
|
43
|
-
}
|
|
44
|
-
return obj;
|
|
45
|
-
}
|
|
46
|
-
function _getRequireWildcardCache(nodeInterop) {
|
|
47
|
-
if (typeof WeakMap !== "function") return null;
|
|
48
|
-
var cacheBabelInterop = new WeakMap();
|
|
49
|
-
var cacheNodeInterop = new WeakMap();
|
|
50
|
-
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
51
|
-
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
52
|
-
})(nodeInterop);
|
|
53
|
-
}
|
|
54
|
-
function _interop_require_wildcard(obj, nodeInterop) {
|
|
55
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
|
56
|
-
return obj;
|
|
57
|
-
}
|
|
58
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
59
|
-
return {
|
|
60
|
-
default: obj
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
var cache = _getRequireWildcardCache(nodeInterop);
|
|
64
|
-
if (cache && cache.has(obj)) {
|
|
65
|
-
return cache.get(obj);
|
|
66
|
-
}
|
|
67
|
-
var newObj = {
|
|
68
|
-
__proto__: null
|
|
69
|
-
};
|
|
70
|
-
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
71
|
-
for(var key in obj){
|
|
72
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
73
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
74
|
-
if (desc && (desc.get || desc.set)) {
|
|
75
|
-
Object.defineProperty(newObj, key, desc);
|
|
76
|
-
} else {
|
|
77
|
-
newObj[key] = obj[key];
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
newObj.default = obj;
|
|
82
|
-
if (cache) {
|
|
83
|
-
cache.set(obj, newObj);
|
|
84
|
-
}
|
|
85
|
-
return newObj;
|
|
86
|
-
}
|
|
87
|
-
let PluginManager = class PluginManager {
|
|
88
|
-
/**
|
|
89
|
-
* 注册插件
|
|
90
|
-
*
|
|
91
|
-
* 优化:检查插件依赖是否已注册
|
|
92
|
-
*/ register(plugin) {
|
|
93
|
-
if (this.plugins.has(plugin.name)) {
|
|
94
|
-
console.warn(`插件 ${plugin.name} 已存在,将被覆盖`);
|
|
95
|
-
}
|
|
96
|
-
// 检查依赖是否已注册
|
|
97
|
-
if (plugin.dependencies && plugin.dependencies.length > 0) {
|
|
98
|
-
const missingDeps = plugin.dependencies.filter((dep)=>!this.plugins.has(dep));
|
|
99
|
-
if (missingDeps.length > 0) {
|
|
100
|
-
console.warn(`插件 ${plugin.name} 的依赖未注册: ${missingDeps.join(', ')}`);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
this.plugins.set(plugin.name, plugin);
|
|
104
|
-
this.pluginStatus.set(plugin.name, 'registered');
|
|
105
|
-
// 创建插件沙箱
|
|
106
|
-
if (plugin.sandbox) {
|
|
107
|
-
const sandbox = new _PluginSandbox.PluginSandbox({
|
|
108
|
-
...plugin.sandbox,
|
|
109
|
-
name: plugin.name
|
|
110
|
-
});
|
|
111
|
-
this.sandboxes.set(plugin.name, sandbox);
|
|
112
|
-
} else {
|
|
113
|
-
// 如果没有配置沙箱,创建默认沙箱(非严格模式)
|
|
114
|
-
const sandbox = new _PluginSandbox.PluginSandbox({
|
|
115
|
-
strictMode: false,
|
|
116
|
-
name: plugin.name
|
|
117
|
-
});
|
|
118
|
-
this.sandboxes.set(plugin.name, sandbox);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* 获取插件加载顺序(考虑依赖和优先级)
|
|
123
|
-
*
|
|
124
|
-
* @returns 插件名称数组,按加载顺序排列
|
|
125
|
-
*/ getLoadOrder() {
|
|
126
|
-
const plugins = Array.from(this.plugins.values());
|
|
127
|
-
// 按优先级排序
|
|
128
|
-
const sortedPlugins = plugins.sort((a, b)=>{
|
|
129
|
-
const priorityA = a.priority ?? 100;
|
|
130
|
-
const priorityB = b.priority ?? 100;
|
|
131
|
-
return priorityA - priorityB;
|
|
132
|
-
});
|
|
133
|
-
// 构建依赖图并拓扑排序
|
|
134
|
-
const visited = new Set();
|
|
135
|
-
const visiting = new Set();
|
|
136
|
-
const result = [];
|
|
137
|
-
const visit = (pluginName)=>{
|
|
138
|
-
if (visiting.has(pluginName)) {
|
|
139
|
-
console.warn(`检测到循环依赖,涉及插件: ${pluginName}`);
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
if (visited.has(pluginName)) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
visiting.add(pluginName);
|
|
146
|
-
const plugin = this.plugins.get(pluginName);
|
|
147
|
-
if (plugin?.dependencies) {
|
|
148
|
-
for (const dep of plugin.dependencies){
|
|
149
|
-
visit(dep);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
visiting.delete(pluginName);
|
|
153
|
-
visited.add(pluginName);
|
|
154
|
-
result.push(pluginName);
|
|
155
|
-
};
|
|
156
|
-
for (const plugin of sortedPlugins){
|
|
157
|
-
visit(plugin.name);
|
|
158
|
-
}
|
|
159
|
-
return result;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* 注销插件
|
|
163
|
-
*/ unregister(name) {
|
|
164
|
-
const plugin = this.plugins.get(name);
|
|
165
|
-
if (plugin && plugin.destroy) {
|
|
166
|
-
plugin.destroy();
|
|
167
|
-
}
|
|
168
|
-
this.plugins.delete(name);
|
|
169
|
-
this.sandboxes.delete(name);
|
|
170
|
-
this.pluginStatus.delete(name);
|
|
171
|
-
this.pluginLoaders.delete(name);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* 动态加载插件(热插拔)
|
|
175
|
-
*
|
|
176
|
-
* @param options - 插件加载选项
|
|
177
|
-
* @returns 加载的插件
|
|
178
|
-
*/ async loadPlugin(options) {
|
|
179
|
-
let plugin;
|
|
180
|
-
if (options.url) {
|
|
181
|
-
// 从URL加载插件
|
|
182
|
-
const response = await fetch(options.url);
|
|
183
|
-
if (!response.ok) {
|
|
184
|
-
throw new Error(`无法加载插件: ${options.url} (${response.status})`);
|
|
185
|
-
}
|
|
186
|
-
const pluginModule = await response.json();
|
|
187
|
-
plugin = pluginModule.default || pluginModule;
|
|
188
|
-
} else if (options.modulePath) {
|
|
189
|
-
// 从模块路径动态导入
|
|
190
|
-
const pluginModule = await Promise.resolve(/* @vite-ignore */ options.modulePath).then((p)=>/*#__PURE__*/ _interop_require_wildcard(require(p)));
|
|
191
|
-
plugin = pluginModule.default || pluginModule;
|
|
192
|
-
} else {
|
|
193
|
-
throw new Error('必须提供 url 或 modulePath');
|
|
194
|
-
}
|
|
195
|
-
if (!plugin || !plugin.name) {
|
|
196
|
-
throw new Error('插件格式无效:必须包含 name 属性');
|
|
197
|
-
}
|
|
198
|
-
// 检查插件是否已加载
|
|
199
|
-
if (this.plugins.has(plugin.name)) {
|
|
200
|
-
throw new Error(`插件 ${plugin.name} 已加载`);
|
|
201
|
-
}
|
|
202
|
-
// 设置加载状态
|
|
203
|
-
this.pluginStatus.set(plugin.name, 'loading');
|
|
204
|
-
try {
|
|
205
|
-
// 注册插件
|
|
206
|
-
this.register(plugin);
|
|
207
|
-
// 如果要求立即初始化
|
|
208
|
-
if (options.initialize && options.initContext && options.startOptions) {
|
|
209
|
-
await this.initializePlugin(plugin.name, options.startOptions, options.initContext);
|
|
210
|
-
}
|
|
211
|
-
return plugin;
|
|
212
|
-
} catch (error) {
|
|
213
|
-
this.pluginStatus.delete(plugin.name);
|
|
214
|
-
throw error;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* 初始化单个插件
|
|
219
|
-
*
|
|
220
|
-
* @param name - 插件名称
|
|
221
|
-
* @param options - 启动选项
|
|
222
|
-
* @param context - 初始化上下文
|
|
223
|
-
*/ async initializePlugin(name, options, context) {
|
|
224
|
-
const plugin = this.plugins.get(name);
|
|
225
|
-
if (!plugin) {
|
|
226
|
-
throw new Error(`插件 ${name} 未注册`);
|
|
227
|
-
}
|
|
228
|
-
const status = this.pluginStatus.get(name);
|
|
229
|
-
if (status === 'initialized') {
|
|
230
|
-
return; // 已经初始化
|
|
231
|
-
}
|
|
232
|
-
try {
|
|
233
|
-
const sandbox = this.sandboxes.get(name);
|
|
234
|
-
if (!sandbox) {
|
|
235
|
-
throw new Error(`插件 ${name} 的沙箱未找到`);
|
|
236
|
-
}
|
|
237
|
-
// 执行初始化钩子
|
|
238
|
-
if (plugin.init) {
|
|
239
|
-
const filteredOptions = sandbox.filterConfig(options);
|
|
240
|
-
await plugin.init(filteredOptions, context, sandbox, _PluginEventBus.pluginEventBus);
|
|
241
|
-
}
|
|
242
|
-
this.pluginStatus.set(name, 'initialized');
|
|
243
|
-
} catch (error) {
|
|
244
|
-
console.error(`插件 ${name} 初始化失败:`, error);
|
|
245
|
-
throw error;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* 卸载插件(热插拔)
|
|
250
|
-
*
|
|
251
|
-
* @param name - 插件名称
|
|
252
|
-
* @param force - 是否强制卸载(即使有依赖)
|
|
253
|
-
*/ async unloadPlugin(name, force = false) {
|
|
254
|
-
const plugin = this.plugins.get(name);
|
|
255
|
-
if (!plugin) {
|
|
256
|
-
throw new Error(`插件 ${name} 未加载`);
|
|
257
|
-
}
|
|
258
|
-
// 检查是否有其他插件依赖此插件
|
|
259
|
-
if (!force) {
|
|
260
|
-
const dependents = Array.from(this.plugins.values()).filter((p)=>p.dependencies && p.dependencies.includes(name));
|
|
261
|
-
if (dependents.length > 0) {
|
|
262
|
-
throw new Error(`无法卸载插件 ${name},以下插件依赖它: ${dependents.map((p)=>p.name).join(', ')}`);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
// 执行销毁钩子
|
|
266
|
-
if (plugin.destroy) {
|
|
267
|
-
try {
|
|
268
|
-
await plugin.destroy();
|
|
269
|
-
} catch (error) {
|
|
270
|
-
console.error(`插件 ${name} 的 destroy 钩子执行失败:`, error);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
// 从事件总线移除相关监听器(如果有)
|
|
274
|
-
// 注意:事件总线是全局的,这里只移除插件相关的事件
|
|
275
|
-
// 实际实现可能需要更细粒度的控制
|
|
276
|
-
// 清理插件
|
|
277
|
-
this.unregister(name);
|
|
278
|
-
this.pluginStatus.set(name, 'destroyed');
|
|
279
|
-
}
|
|
280
|
-
/**
|
|
281
|
-
* 检查插件是否可以热加载
|
|
282
|
-
*
|
|
283
|
-
* @param plugin - 插件
|
|
284
|
-
* @returns 是否可以热加载
|
|
285
|
-
*/ canHotLoad(plugin) {
|
|
286
|
-
// 检查依赖是否都已加载
|
|
287
|
-
if (plugin.dependencies && plugin.dependencies.length > 0) {
|
|
288
|
-
const missingDeps = plugin.dependencies.filter((dep)=>!this.plugins.has(dep));
|
|
289
|
-
if (missingDeps.length > 0) {
|
|
290
|
-
return false;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return true;
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* 注册插件加载器(用于延迟加载)
|
|
297
|
-
*
|
|
298
|
-
* @param name - 插件名称
|
|
299
|
-
* @param loader - 加载器函数
|
|
300
|
-
*/ registerLoader(name, loader) {
|
|
301
|
-
this.pluginLoaders.set(name, loader);
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* 使用加载器加载插件
|
|
305
|
-
*
|
|
306
|
-
* @param name - 插件名称
|
|
307
|
-
* @param options - 加载选项
|
|
308
|
-
* @returns 加载的插件
|
|
309
|
-
*/ async loadPluginByLoader(name, options) {
|
|
310
|
-
const loader = this.pluginLoaders.get(name);
|
|
311
|
-
if (!loader) {
|
|
312
|
-
throw new Error(`插件 ${name} 的加载器未注册`);
|
|
313
|
-
}
|
|
314
|
-
const plugin = await loader();
|
|
315
|
-
if (!plugin || !plugin.name) {
|
|
316
|
-
throw new Error('插件格式无效:必须包含 name 属性');
|
|
317
|
-
}
|
|
318
|
-
if (plugin.name !== name) {
|
|
319
|
-
throw new Error(`插件名称不匹配: 期望 ${name},实际 ${plugin.name}`);
|
|
320
|
-
}
|
|
321
|
-
// 检查插件是否已加载
|
|
322
|
-
if (this.plugins.has(plugin.name)) {
|
|
323
|
-
return this.plugins.get(plugin.name);
|
|
324
|
-
}
|
|
325
|
-
// 设置加载状态
|
|
326
|
-
this.pluginStatus.set(plugin.name, 'loading');
|
|
327
|
-
try {
|
|
328
|
-
// 注册插件
|
|
329
|
-
this.register(plugin);
|
|
330
|
-
// 如果要求立即初始化
|
|
331
|
-
if (options?.initialize && options.initContext && options.startOptions) {
|
|
332
|
-
await this.initializePlugin(plugin.name, options.startOptions, options.initContext);
|
|
333
|
-
}
|
|
334
|
-
return plugin;
|
|
335
|
-
} catch (error) {
|
|
336
|
-
this.pluginStatus.delete(plugin.name);
|
|
337
|
-
throw error;
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
/**
|
|
341
|
-
* 获取插件
|
|
342
|
-
*/ get(name) {
|
|
343
|
-
return this.plugins.get(name);
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* 获取所有插件
|
|
347
|
-
*/ getAll() {
|
|
348
|
-
return Array.from(this.plugins.values());
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* 执行初始化前钩子
|
|
352
|
-
*
|
|
353
|
-
* 优化:按依赖顺序执行
|
|
354
|
-
*/ async executeBeforeInit(options) {
|
|
355
|
-
const loadOrder = this.getLoadOrder();
|
|
356
|
-
for (const pluginName of loadOrder){
|
|
357
|
-
const plugin = this.plugins.get(pluginName);
|
|
358
|
-
if (!plugin || !plugin.beforeInit) {
|
|
359
|
-
continue;
|
|
360
|
-
}
|
|
361
|
-
try {
|
|
362
|
-
const sandbox = this.sandboxes.get(plugin.name);
|
|
363
|
-
if (!sandbox) {
|
|
364
|
-
console.warn(`插件 ${plugin.name} 的沙箱未找到`);
|
|
365
|
-
continue;
|
|
366
|
-
}
|
|
367
|
-
// 根据沙箱配置过滤选项
|
|
368
|
-
const filteredOptions = sandbox.filterConfig(options);
|
|
369
|
-
await plugin.beforeInit(filteredOptions, sandbox, _PluginEventBus.pluginEventBus);
|
|
370
|
-
} catch (error) {
|
|
371
|
-
console.error(`插件 ${plugin.name} 的 beforeInit 钩子执行失败:`, error);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* 执行初始化钩子
|
|
377
|
-
*
|
|
378
|
-
* 优化:按依赖顺序执行,并更新插件状态
|
|
379
|
-
*/ async executeInit(options, context) {
|
|
380
|
-
const loadOrder = this.getLoadOrder();
|
|
381
|
-
for (const pluginName of loadOrder){
|
|
382
|
-
const plugin = this.plugins.get(pluginName);
|
|
383
|
-
if (!plugin || !plugin.init) {
|
|
384
|
-
continue;
|
|
385
|
-
}
|
|
386
|
-
try {
|
|
387
|
-
const sandbox = this.sandboxes.get(plugin.name);
|
|
388
|
-
if (!sandbox) {
|
|
389
|
-
console.warn(`插件 ${plugin.name} 的沙箱未找到`);
|
|
390
|
-
continue;
|
|
391
|
-
}
|
|
392
|
-
// 根据沙箱配置过滤选项
|
|
393
|
-
const filteredOptions = sandbox.filterConfig(options);
|
|
394
|
-
await plugin.init(filteredOptions, context, sandbox, _PluginEventBus.pluginEventBus);
|
|
395
|
-
this.pluginStatus.set(plugin.name, 'initialized');
|
|
396
|
-
} catch (error) {
|
|
397
|
-
console.error(`插件 ${plugin.name} 的 init 钩子执行失败:`, error);
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* 执行初始化后钩子
|
|
403
|
-
*
|
|
404
|
-
* 优化:按依赖顺序执行(逆序,确保依赖插件先执行)
|
|
405
|
-
*/ async executeAfterInit(options, context) {
|
|
406
|
-
const loadOrder = this.getLoadOrder();
|
|
407
|
-
// 逆序执行,确保依赖插件先执行
|
|
408
|
-
const reverseOrder = [
|
|
409
|
-
...loadOrder
|
|
410
|
-
].reverse();
|
|
411
|
-
for (const pluginName of reverseOrder){
|
|
412
|
-
const plugin = this.plugins.get(pluginName);
|
|
413
|
-
if (!plugin || !plugin.afterInit) {
|
|
414
|
-
continue;
|
|
415
|
-
}
|
|
416
|
-
try {
|
|
417
|
-
const sandbox = this.sandboxes.get(plugin.name);
|
|
418
|
-
if (!sandbox) {
|
|
419
|
-
console.warn(`插件 ${plugin.name} 的沙箱未找到`);
|
|
420
|
-
continue;
|
|
421
|
-
}
|
|
422
|
-
// 根据沙箱配置过滤选项
|
|
423
|
-
const filteredOptions = sandbox.filterConfig(options);
|
|
424
|
-
await plugin.afterInit(filteredOptions, context, sandbox, _PluginEventBus.pluginEventBus);
|
|
425
|
-
} catch (error) {
|
|
426
|
-
console.error(`插件 ${plugin.name} 的 afterInit 钩子执行失败:`, error);
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* 获取插件状态
|
|
432
|
-
*
|
|
433
|
-
* @param name - 插件名称
|
|
434
|
-
* @returns 插件状态
|
|
435
|
-
*/ getPluginStatus(name) {
|
|
436
|
-
return this.pluginStatus.get(name);
|
|
437
|
-
}
|
|
438
|
-
/**
|
|
439
|
-
* 检查插件是否已加载
|
|
440
|
-
*
|
|
441
|
-
* @param name - 插件名称
|
|
442
|
-
* @returns 是否已加载
|
|
443
|
-
*/ isLoaded(name) {
|
|
444
|
-
return this.plugins.has(name);
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* 获取已加载的插件列表
|
|
448
|
-
*
|
|
449
|
-
* @returns 插件名称数组
|
|
450
|
-
*/ getLoadedPlugins() {
|
|
451
|
-
return Array.from(this.plugins.keys());
|
|
452
|
-
}
|
|
453
|
-
/**
|
|
454
|
-
* 获取所有插件状态
|
|
455
|
-
*
|
|
456
|
-
* @returns 插件状态映射
|
|
457
|
-
*/ getAllPluginStatus() {
|
|
458
|
-
return new Map(this.pluginStatus);
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* 获取插件沙箱
|
|
462
|
-
*
|
|
463
|
-
* @param name - 插件名称
|
|
464
|
-
* @returns 插件沙箱或undefined
|
|
465
|
-
*/ getSandbox(name) {
|
|
466
|
-
return this.sandboxes.get(name);
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* 获取事件总线
|
|
470
|
-
*/ getEventBus() {
|
|
471
|
-
return _PluginEventBus.pluginEventBus;
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* 销毁所有插件
|
|
475
|
-
*/ async destroyAll() {
|
|
476
|
-
for (const plugin of this.plugins.values()){
|
|
477
|
-
if (plugin.destroy) {
|
|
478
|
-
try {
|
|
479
|
-
await plugin.destroy();
|
|
480
|
-
} catch (error) {
|
|
481
|
-
console.error(`插件 ${plugin.name} 的 destroy 钩子执行失败:`, error);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
this.plugins.clear();
|
|
486
|
-
}
|
|
487
|
-
constructor(){
|
|
488
|
-
_define_property(this, "plugins", new Map());
|
|
489
|
-
_define_property(this, "sandboxes", new Map());
|
|
490
|
-
_define_property(this, "pluginStatus", new Map());
|
|
491
|
-
_define_property(this, "pluginLoaders", new Map());
|
|
492
|
-
}
|
|
493
|
-
};
|
|
494
|
-
const pluginManager = new PluginManager();
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});function _export(target,all){for(var name in all)Object.defineProperty(target,name,{enumerable:true,get:Object.getOwnPropertyDescriptor(all,name).get})}_export(exports,{get PluginEventBus(){return _PluginEventBus.PluginEventBus},get PluginManager(){return PluginManager},get PluginPermission(){return _PluginSandbox.PluginPermission},get PluginSandbox(){return _PluginSandbox.PluginSandbox},get pluginEventBus(){return _PluginEventBus.pluginEventBus},get pluginManager(){return pluginManager}});const _PluginSandbox=require("./plugin/PluginSandbox");const _PluginEventBus=require("./plugin/PluginEventBus");function _define_property(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _getRequireWildcardCache(nodeInterop){if(typeof WeakMap!=="function")return null;var cacheBabelInterop=new WeakMap;var cacheNodeInterop=new WeakMap;return(_getRequireWildcardCache=function(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop})(nodeInterop)}function _interop_require_wildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule){return obj}if(obj===null||typeof obj!=="object"&&typeof obj!=="function"){return{default:obj}}var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj)){return cache.get(obj)}var newObj={__proto__:null};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(key!=="default"&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc)}else{newObj[key]=obj[key]}}}newObj.default=obj;if(cache){cache.set(obj,newObj)}return newObj}let PluginManager=class PluginManager{register(plugin){if(this.plugins.has(plugin.name)){console.warn(`插件 ${plugin.name} 已存在,将被覆盖`)}if(plugin.dependencies&&plugin.dependencies.length>0){const missingDeps=plugin.dependencies.filter(dep=>!this.plugins.has(dep));if(missingDeps.length>0){console.warn(`插件 ${plugin.name} 的依赖未注册: ${missingDeps.join(", ")}`)}}this.plugins.set(plugin.name,plugin);this.pluginStatus.set(plugin.name,"registered");if(plugin.sandbox){const sandbox=new _PluginSandbox.PluginSandbox({...plugin.sandbox,name:plugin.name});this.sandboxes.set(plugin.name,sandbox)}else{const sandbox=new _PluginSandbox.PluginSandbox({strictMode:false,name:plugin.name});this.sandboxes.set(plugin.name,sandbox)}}getLoadOrder(){const plugins=Array.from(this.plugins.values());const sortedPlugins=plugins.sort((a,b)=>{const priorityA=a.priority??100;const priorityB=b.priority??100;return priorityA-priorityB});const visited=new Set;const visiting=new Set;const result=[];const visit=pluginName=>{if(visiting.has(pluginName)){console.warn(`检测到循环依赖,涉及插件: ${pluginName}`);return}if(visited.has(pluginName)){return}visiting.add(pluginName);const plugin=this.plugins.get(pluginName);if(plugin?.dependencies){for(const dep of plugin.dependencies){visit(dep)}}visiting.delete(pluginName);visited.add(pluginName);result.push(pluginName)};for(const plugin of sortedPlugins){visit(plugin.name)}return result}unregister(name){const plugin=this.plugins.get(name);if(plugin&&plugin.destroy){plugin.destroy()}this.plugins.delete(name);this.sandboxes.delete(name);this.pluginStatus.delete(name);this.pluginLoaders.delete(name)}async loadPlugin(options){let plugin;if(options.url){const response=await fetch(options.url);if(!response.ok){throw new Error(`无法加载插件: ${options.url} (${response.status})`)}const pluginModule=await response.json();plugin=pluginModule.default||pluginModule}else if(options.modulePath){const pluginModule=await Promise.resolve(options.modulePath).then(p=>/*#__PURE__*/_interop_require_wildcard(require(p)));plugin=pluginModule.default||pluginModule}else{throw new Error("必须提供 url 或 modulePath")}if(!plugin||!plugin.name){throw new Error("插件格式无效:必须包含 name 属性")}if(this.plugins.has(plugin.name)){throw new Error(`插件 ${plugin.name} 已加载`)}this.pluginStatus.set(plugin.name,"loading");try{this.register(plugin);if(options.initialize&&options.initContext&&options.startOptions){await this.initializePlugin(plugin.name,options.startOptions,options.initContext)}return plugin}catch(error){this.pluginStatus.delete(plugin.name);throw error}}async initializePlugin(name,options,context){const plugin=this.plugins.get(name);if(!plugin){throw new Error(`插件 ${name} 未注册`)}const status=this.pluginStatus.get(name);if(status==="initialized"){return}try{const sandbox=this.sandboxes.get(name);if(!sandbox){throw new Error(`插件 ${name} 的沙箱未找到`)}if(plugin.init){const filteredOptions=sandbox.filterConfig(options);await plugin.init(filteredOptions,context,sandbox,_PluginEventBus.pluginEventBus)}this.pluginStatus.set(name,"initialized")}catch(error){console.error(`插件 ${name} 初始化失败:`,error);throw error}}async unloadPlugin(name,force=false){const plugin=this.plugins.get(name);if(!plugin){throw new Error(`插件 ${name} 未加载`)}if(!force){const dependents=Array.from(this.plugins.values()).filter(p=>p.dependencies&&p.dependencies.includes(name));if(dependents.length>0){throw new Error(`无法卸载插件 ${name},以下插件依赖它: ${dependents.map(p=>p.name).join(", ")}`)}}if(plugin.destroy){try{await plugin.destroy()}catch(error){console.error(`插件 ${name} 的 destroy 钩子执行失败:`,error)}}this.unregister(name);this.pluginStatus.set(name,"destroyed")}canHotLoad(plugin){if(plugin.dependencies&&plugin.dependencies.length>0){const missingDeps=plugin.dependencies.filter(dep=>!this.plugins.has(dep));if(missingDeps.length>0){return false}}return true}registerLoader(name,loader){this.pluginLoaders.set(name,loader)}async loadPluginByLoader(name,options){const loader=this.pluginLoaders.get(name);if(!loader){throw new Error(`插件 ${name} 的加载器未注册`)}const plugin=await loader();if(!plugin||!plugin.name){throw new Error("插件格式无效:必须包含 name 属性")}if(plugin.name!==name){throw new Error(`插件名称不匹配: 期望 ${name},实际 ${plugin.name}`)}if(this.plugins.has(plugin.name)){return this.plugins.get(plugin.name)}this.pluginStatus.set(plugin.name,"loading");try{this.register(plugin);if(options?.initialize&&options.initContext&&options.startOptions){await this.initializePlugin(plugin.name,options.startOptions,options.initContext)}return plugin}catch(error){this.pluginStatus.delete(plugin.name);throw error}}get(name){return this.plugins.get(name)}getAll(){return Array.from(this.plugins.values())}async executeBeforeInit(options){const loadOrder=this.getLoadOrder();for(const pluginName of loadOrder){const plugin=this.plugins.get(pluginName);if(!plugin||!plugin.beforeInit){continue}try{const sandbox=this.sandboxes.get(plugin.name);if(!sandbox){console.warn(`插件 ${plugin.name} 的沙箱未找到`);continue}const filteredOptions=sandbox.filterConfig(options);await plugin.beforeInit(filteredOptions,sandbox,_PluginEventBus.pluginEventBus)}catch(error){console.error(`插件 ${plugin.name} 的 beforeInit 钩子执行失败:`,error)}}}async executeInit(options,context){const loadOrder=this.getLoadOrder();for(const pluginName of loadOrder){const plugin=this.plugins.get(pluginName);if(!plugin||!plugin.init){continue}try{const sandbox=this.sandboxes.get(plugin.name);if(!sandbox){console.warn(`插件 ${plugin.name} 的沙箱未找到`);continue}const filteredOptions=sandbox.filterConfig(options);await plugin.init(filteredOptions,context,sandbox,_PluginEventBus.pluginEventBus);this.pluginStatus.set(plugin.name,"initialized")}catch(error){console.error(`插件 ${plugin.name} 的 init 钩子执行失败:`,error)}}}async executeAfterInit(options,context){const loadOrder=this.getLoadOrder();const reverseOrder=[...loadOrder].reverse();for(const pluginName of reverseOrder){const plugin=this.plugins.get(pluginName);if(!plugin||!plugin.afterInit){continue}try{const sandbox=this.sandboxes.get(plugin.name);if(!sandbox){console.warn(`插件 ${plugin.name} 的沙箱未找到`);continue}const filteredOptions=sandbox.filterConfig(options);await plugin.afterInit(filteredOptions,context,sandbox,_PluginEventBus.pluginEventBus)}catch(error){console.error(`插件 ${plugin.name} 的 afterInit 钩子执行失败:`,error)}}}getPluginStatus(name){return this.pluginStatus.get(name)}isLoaded(name){return this.plugins.has(name)}getLoadedPlugins(){return Array.from(this.plugins.keys())}getAllPluginStatus(){return new Map(this.pluginStatus)}getSandbox(name){return this.sandboxes.get(name)}getEventBus(){return _PluginEventBus.pluginEventBus}async destroyAll(){for(const plugin of this.plugins.values()){if(plugin.destroy){try{await plugin.destroy()}catch(error){console.error(`插件 ${plugin.name} 的 destroy 钩子执行失败:`,error)}}}this.plugins.clear()}constructor(){_define_property(this,"plugins",new Map);_define_property(this,"sandboxes",new Map);_define_property(this,"pluginStatus",new Map);_define_property(this,"pluginLoaders",new Map)}};const pluginManager=new PluginManager;
|