@yumerijs/core 1.3.3 → 2.0.1

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/context.d.ts CHANGED
@@ -1,12 +1,14 @@
1
- /**
2
- * @time: 2025/08/14 19:10
3
- * @author: FireGuo
4
- * WindyPear-Team All right reserved
5
- **/
6
1
  import { Core } from './core';
7
2
  import { Route } from './route';
8
3
  import { HookHandler } from './hook';
9
4
  import { Middleware } from './middleware';
5
+ import { Config } from './config';
6
+ interface Plugin {
7
+ apply: (ctx: Context, config: Config) => Promise<void>;
8
+ disable: (ctx: Context) => Promise<void>;
9
+ depend: Array<string>;
10
+ provide: Array<string>;
11
+ }
10
12
  /**
11
13
  * 插件上下文对象
12
14
  * 每个插件一个 Context,用于管理插件注册的命令、路由、事件、组件和中间件
@@ -18,9 +20,10 @@ export declare class Context {
18
20
  private components;
19
21
  private middlewares;
20
22
  private hooks;
21
- /**
22
- * 插件名称
23
- */
23
+ private childContexts;
24
+ private childPlugins;
25
+ private i18ns;
26
+ /** 插件名称 */
24
27
  pluginname: string;
25
28
  /**
26
29
  * 创建 Context 实例
@@ -57,25 +60,19 @@ export declare class Context {
57
60
  * 执行 Hook 钩子
58
61
  * @param name Hook 点名称
59
62
  * @param args Hook 参数
60
- * @returns Promise<any[]>
61
63
  */
62
64
  executeHook(name: string, ...args: any[]): Promise<any[]>;
63
- /**
64
- * 获取 Core 实例
65
- * @returns Core
66
- */
65
+ /** 获取 Core 实例 */
67
66
  getCore(): Core;
68
67
  /**
69
68
  * 触发事件
70
69
  * @param event 事件名称
71
70
  * @param args 事件参数
72
- * @returns Promise<void>
73
71
  */
74
72
  emit(event: string, ...args: any[]): Promise<void>;
75
73
  /**
76
74
  * 获取组件实例
77
75
  * @param name 组件名称
78
- * @returns 组件实例
79
76
  */
80
77
  getComponent(name: string): any;
81
78
  /**
@@ -84,9 +81,26 @@ export declare class Context {
84
81
  * @param component 组件实例
85
82
  */
86
83
  registerComponent(name: string, component: any): void;
84
+ /**
85
+ * 注册子 Context
86
+ * @param name 子 Context 名称
87
+ */
88
+ fork(name?: string): Context;
89
+ /**
90
+ * 注册子插件
91
+ * @param plugin 插件实例
92
+ * @param config 插件配置
93
+ */
94
+ apply(plugin: Plugin, config: Config): Promise<void>;
95
+ /**
96
+ * 注册 i18n
97
+ * @param content 内容(可以是嵌套对象或单个key)
98
+ * @param locale 可选的语言映射
99
+ */
100
+ i18n(content: string | Record<string, any>, locale?: Record<string, string>): void;
87
101
  /**
88
102
  * 卸载插件时清理注册的所有资源
89
- * 包括组件、路由、中间件和事件监听器
90
103
  */
91
- dispose(): void;
104
+ dispose(): Promise<void>;
92
105
  }
106
+ export {};
package/dist/context.js CHANGED
@@ -13,9 +13,10 @@ class Context {
13
13
  components = [];
14
14
  middlewares = [];
15
15
  hooks = {};
16
- /**
17
- * 插件名称
18
- */
16
+ childContexts = [];
17
+ childPlugins = new Map();
18
+ i18ns = [];
19
+ /** 插件名称 */
19
20
  pluginname;
20
21
  /**
21
22
  * 创建 Context 实例
@@ -25,6 +26,7 @@ class Context {
25
26
  constructor(core, pluginname) {
26
27
  this.core = core;
27
28
  this.pluginname = pluginname;
29
+ this.childPlugins = new Map();
28
30
  }
29
31
  /**
30
32
  * 注册路由
@@ -45,9 +47,9 @@ class Context {
45
47
  * @param listener 事件监听器
46
48
  */
47
49
  on(name, listener) {
48
- // 记录插件自己的事件监听器
50
+ if (!listener)
51
+ return;
49
52
  this.eventlisteners.push({ name, listener });
50
- // 注册到 core 的全局事件监听器中
51
53
  this.core.on(name, listener);
52
54
  }
53
55
  /**
@@ -56,6 +58,8 @@ class Context {
56
58
  * @param callback 中间件回调函数
57
59
  */
58
60
  use(name, callback) {
61
+ if (!callback)
62
+ return;
59
63
  this.middlewares.push(name);
60
64
  this.core.use(name, callback);
61
65
  }
@@ -66,22 +70,22 @@ class Context {
66
70
  * @param callback 钩子回调函数
67
71
  */
68
72
  hook(name, hookname, callback) {
73
+ if (!callback || !hookname)
74
+ return;
69
75
  this.core.hook(name, hookname, callback);
76
+ if (!this.hooks[name])
77
+ this.hooks[name] = [];
70
78
  this.hooks[name].push(hookname);
71
79
  }
72
80
  /**
73
81
  * 执行 Hook 钩子
74
82
  * @param name Hook 点名称
75
83
  * @param args Hook 参数
76
- * @returns Promise<any[]>
77
84
  */
78
85
  async executeHook(name, ...args) {
79
86
  return await this.core.hookExecute(name, ...args);
80
87
  }
81
- /**
82
- * 获取 Core 实例
83
- * @returns Core
84
- */
88
+ /** 获取 Core 实例 */
85
89
  getCore() {
86
90
  return this.core;
87
91
  }
@@ -89,7 +93,6 @@ class Context {
89
93
  * 触发事件
90
94
  * @param event 事件名称
91
95
  * @param args 事件参数
92
- * @returns Promise<void>
93
96
  */
94
97
  async emit(event, ...args) {
95
98
  return await this.core.emit(event, ...args);
@@ -97,7 +100,6 @@ class Context {
97
100
  /**
98
101
  * 获取组件实例
99
102
  * @param name 组件名称
100
- * @returns 组件实例
101
103
  */
102
104
  getComponent(name) {
103
105
  return this.core.getComponent(name);
@@ -108,6 +110,8 @@ class Context {
108
110
  * @param component 组件实例
109
111
  */
110
112
  registerComponent(name, component) {
113
+ if (!component)
114
+ return;
111
115
  if (this.core.components[name]) {
112
116
  this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register component "${name}", but it has already been registered.`);
113
117
  return;
@@ -115,41 +119,107 @@ class Context {
115
119
  this.core.components[name] = component;
116
120
  this.components.push(name);
117
121
  }
122
+ /**
123
+ * 注册子 Context
124
+ * @param name 子 Context 名称
125
+ */
126
+ fork(name = this.pluginname) {
127
+ const ctx = new Context(this.core, name);
128
+ this.childContexts.push(ctx);
129
+ return ctx;
130
+ }
131
+ /**
132
+ * 注册子插件
133
+ * @param plugin 插件实例
134
+ * @param config 插件配置
135
+ */
136
+ async apply(plugin, config) {
137
+ if (!plugin || !plugin.apply)
138
+ return;
139
+ const ctx = this.fork();
140
+ await plugin.apply(ctx, config);
141
+ this.childPlugins.set(ctx, plugin);
142
+ }
143
+ /**
144
+ * 注册 i18n
145
+ * @param content 内容(可以是嵌套对象或单个key)
146
+ * @param locale 可选的语言映射
147
+ */
148
+ i18n(content, locale) {
149
+ if (!this.i18ns)
150
+ this.i18ns = [];
151
+ const isLangObject = (obj) => typeof obj === 'object' && Object.values(obj).every(v => typeof v === 'string');
152
+ const flatten = (obj, prefix = '') => {
153
+ const result = {};
154
+ for (const [key, value] of Object.entries(obj)) {
155
+ const fullKey = prefix ? `${prefix}.${key}` : key;
156
+ if (isLangObject(value)) {
157
+ result[fullKey] = value;
158
+ }
159
+ else if (typeof value === 'object') {
160
+ Object.assign(result, flatten(value, fullKey));
161
+ }
162
+ }
163
+ return result;
164
+ };
165
+ if (typeof content === 'string' && locale) {
166
+ this.core.i18n.register(content, locale);
167
+ this.i18ns.push(content);
168
+ }
169
+ else if (typeof content === 'object') {
170
+ const flat = flatten(content);
171
+ this.core.i18n.register(flat);
172
+ this.i18ns.push(...Object.keys(flat));
173
+ }
174
+ }
118
175
  /**
119
176
  * 卸载插件时清理注册的所有资源
120
- * 包括组件、路由、中间件和事件监听器
121
177
  */
122
- dispose() {
178
+ async dispose() {
123
179
  // 删除组件
124
- this.components.forEach((name) => {
125
- delete this.core.components[name];
126
- });
180
+ this.components.forEach((name) => delete this.core.components[name]);
127
181
  // 删除路由
128
- this.routes.forEach((route) => {
129
- delete this.core.routes[route];
130
- });
182
+ this.routes.forEach((route) => delete this.core.routes[route]);
131
183
  // 删除中间件
132
- this.middlewares.forEach((middleware) => {
133
- delete this.core.globalMiddlewares[middleware];
134
- });
184
+ this.middlewares.forEach((middleware) => delete this.core.globalMiddlewares[middleware]);
135
185
  // 删除事件监听器
136
186
  this.eventlisteners.forEach(({ name, listener }) => {
137
- if (this.core.eventListeners?.[name]) {
138
- this.core.eventListeners[name] =
139
- this.core.eventListeners[name].filter((l) => l !== listener);
187
+ const listeners = this.core.eventListeners?.[name];
188
+ if (listeners) {
189
+ this.core.eventListeners[name] = listeners.filter(l => l !== listener);
140
190
  }
141
191
  });
142
192
  // 删除钩子
143
193
  for (const hook in this.hooks) {
144
194
  this.hooks[hook].forEach((hookname) => {
145
- this.core.unhook(hook, hookname);
195
+ if (hookname)
196
+ this.core.unhook(hook, hookname);
146
197
  });
147
198
  }
148
- // 清空 Context 内部记录,避免内存泄漏
199
+ // 卸载子插件(异步即可,不必按顺序)
200
+ this.childPlugins.forEach(async (plugin, ctx) => {
201
+ if (plugin?.disable)
202
+ await plugin.disable(ctx);
203
+ });
204
+ // 删除子上下文
205
+ this.childContexts.forEach((ctx) => {
206
+ if (ctx?.dispose)
207
+ ctx.dispose();
208
+ });
209
+ // 删除i18n
210
+ if (this.i18ns?.length) {
211
+ for (const key of this.i18ns) {
212
+ this.core.i18n.delete(key);
213
+ }
214
+ this.i18ns.length = 0;
215
+ }
216
+ // 清空内部记录
149
217
  this.components = [];
150
218
  this.routes = [];
151
219
  this.middlewares = [];
152
220
  this.eventlisteners = [];
221
+ this.hooks = {};
222
+ this.childContexts = [];
153
223
  }
154
224
  }
155
225
  exports.Context = Context;
package/dist/core.d.ts CHANGED
@@ -6,42 +6,27 @@ import { Route } from './route';
6
6
  import { Context } from './context';
7
7
  import { HookHandler, Hook } from './hook';
8
8
  import { Server as CoreServer } from './server';
9
+ import { I18n } from './i18n';
9
10
  interface Plugin {
10
11
  apply: (ctx: Context, config: Config) => Promise<void>;
11
12
  disable: (ctx: Context) => Promise<void>;
12
13
  depend: Array<string>;
13
14
  provide: Array<string>;
14
15
  }
15
- interface PluginLoader {
16
- load(pluginName: string): Promise<Plugin>;
17
- unloadPlugin(pluginName: string): Promise<void>;
18
- checkPluginDependencies(pluginPath: string): Promise<boolean>;
19
- installPluginDependencies(pluginName: string): Promise<void>;
20
- logger: Logger;
21
- }
22
16
  interface CoreOptions {
23
17
  port: number;
24
18
  host: string;
25
19
  staticDir: string;
26
20
  enableCors: boolean;
27
21
  enableWs: boolean;
22
+ lang: string[];
28
23
  }
29
- /**
30
- * 定义插件状态枚举
31
- */
32
24
  export declare const enum PluginStatus {
33
- ENABLED = "enabled",// 正常启用
34
- DISABLED = "disabled",// 已禁用
25
+ ENABLED = "enabled",
26
+ DISABLED = "disabled",
35
27
  PENDING = "pending"
36
28
  }
37
29
  export declare class Core {
38
- plugins: {
39
- [name: string]: Plugin & {
40
- depend?: string[];
41
- provide?: string[];
42
- };
43
- };
44
- config: any;
45
30
  eventListeners: {
46
31
  [event: string]: ((...args: any[]) => Promise<void>)[];
47
32
  };
@@ -49,162 +34,28 @@ export declare class Core {
49
34
  [name: string]: any;
50
35
  };
51
36
  routes: Record<string, Route>;
52
- pluginLoader: PluginLoader;
53
37
  logger: Logger;
54
38
  globalMiddlewares: Record<string, Middleware>;
55
- pluginStatus: Record<string, PluginStatus>;
56
39
  hooks: Record<string, Hook>;
57
40
  coreConfig: CoreOptions;
58
41
  server: CoreServer;
59
- private pluginWatchers;
60
- private pluginModules;
61
- private configPath;
62
- /**
63
- * 插件名 -> Context 映射
64
- */
65
- private pluginContexts;
66
- constructor(pluginLoader?: PluginLoader, coreConfig?: CoreOptions);
67
- /**
68
- * 获取或创建插件对应 Context
69
- */
70
- getContext(pluginName: string): Context;
71
- /**
72
- * 清理指定插件注册的所有内容
73
- */
74
- unregall(pluginName: string): void;
75
- /**
76
- * 加载配置文件
77
- * @param configPath 配置文件的路径
78
- */
79
- loadConfig(configPath: string): Promise<void>;
80
- /**
81
- * 启动应用
82
- * @returns Promise<void>
83
- */
42
+ i18n: I18n;
43
+ loader: any;
44
+ constructor(loader?: any, coreConfig?: CoreOptions, setCore?: boolean);
84
45
  runCore(): Promise<void>;
85
- /**
86
- * 监听配置文件变化,实现热重载
87
- * @param configPath 配置文件的路径
88
- */
89
- private watchConfig;
90
- /**
91
- * 获取指定插件的配置
92
- * @param pluginName 插件名称
93
- * @returns 插件的配置对象
94
- */
95
- getPluginConfig(pluginName: string): Promise<Config>;
96
- /**
97
- * 加载所有在配置文件中启用的插件
98
- * @returns Promise<void>
99
- */
100
- loadPlugins(): Promise<void>;
101
- /**
102
- * 加载单个插件
103
- * @param pluginName 要加载的插件名
104
- * @param triggerPendingCheck 是否在加载后触发对其他待定插件的检查,默认为 true
105
- * @returns Promise<boolean> 是否加载成功
106
- */
107
- loadSinglePlugin(pluginName: string, triggerPendingCheck?: boolean, onlypending?: boolean): Promise<boolean>;
108
- /**
109
- * 尝试加载所有处于 PENDING 状态的插件
110
- * @returns Promise<void>
111
- */
112
- private _loadPendingPlugins;
113
- /**
114
- * 卸载插件,并递归卸载依赖于它的其他插件
115
- * @param pluginNameToUnload 要卸载的插件名
116
- */
117
- unloadPlugin(pluginNameToUnload: string, ispending?: boolean): Promise<void>;
118
- /**
119
- * 执行单个插件的卸载逻辑
120
- * @param pluginName 插件名
121
- * @param ispending 是否处于 PENDING 状态
122
- */
123
- private _unloadSinglePlugin;
124
- /**
125
- * 重新加载插件
126
- * @param pluginName 插件名称
127
- */
128
- reloadPlugin(pluginName: string): Promise<void>;
129
- /**
130
- * 监听插件文件变化
131
- * @param pluginName 插件名称
132
- * @param pluginPath 插件路径
133
- */
134
- private watchPlugin;
135
- /**
136
- * 注册组件
137
- * @param name 组件名称
138
- * @param component 组件实例
139
- */
46
+ getShortPluginName(pluginName: string): string;
47
+ plugin(pluginInstance: Plugin, context: Context, config: Config): Promise<void>;
140
48
  registerComponent(name: string, component: any): void;
141
- /**
142
- * 获取组件
143
- * @param name 组件名称
144
- * @returns 组件实例
145
- */
146
49
  getComponent(name: string): any;
147
- /**
148
- * 注销组件
149
- * @param name 组件名称
150
- */
151
50
  unregisterComponent(name: string): void;
152
- /**
153
- * 注册事件监听器
154
- * @param event 事件名称
155
- * @param listener 事件回调函数
156
- */
157
51
  on(event: string, listener: (...args: any[]) => Promise<void>): void;
158
- /**
159
- * 触发事件
160
- * @param event 事件名称
161
- * @param args 传递给事件监听器的参数
162
- */
163
52
  emit(event: string, ...args: any[]): Promise<void>;
164
- /**
165
- * 注册全局中间件
166
- * @param name 中间件名称
167
- * @param middleware 中间件函数
168
- * @returns Core 实例
169
- */
170
53
  use(name: string, middleware: Middleware): Core;
171
- /**
172
- * 注册路由
173
- * @param path 路由路径
174
- * @returns Route 实例
175
- */
176
54
  route(path: string): Route;
177
- /**
178
- * 注册 Hook 钩子
179
- * @param name Hook 点名称
180
- * @param hookname 钩子名称
181
- * @param callback 钩子函数
182
- */
183
55
  hook(name: string, hookname: string, callback: HookHandler): any;
184
- /**
185
- * 消除 Hook 钩子
186
- */
187
56
  unhook(name: string, hookname: string): any;
188
- /**
189
- * 执行 Hook 钩子
190
- * @param name Hook 点名称
191
- * @param args 参数
192
- * @return Promise<any[]>
193
- */
194
57
  hookExecute(name: string, ...args: any[]): Promise<any[]>;
195
- /**
196
- * 执行路由
197
- * @param pathname 需要匹配的URL
198
- * @param session 当前会话
199
- * @param queryParams URL参数
200
- * @returns 是否匹配成功
201
- */
202
58
  executeRoute(pathname: string, session: Session, queryParams: URLSearchParams): Promise<boolean>;
203
- /**
204
- * 获取路由
205
- * @param path 匹配路径
206
- * @return Route 实例或 false
207
- */
208
59
  getRoute(path: string): Route | false;
209
60
  }
210
61
  export {};