@yumerijs/core 2.2.0 → 2.2.2

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
@@ -2,6 +2,7 @@ import { Core } from './core.js';
2
2
  import { Route } from './route.js';
3
3
  import { HookHandler } from './hook.js';
4
4
  import { Middleware } from './middleware.js';
5
+ import { IRenderer } from '@yumerijs/types';
5
6
  interface Plugin {
6
7
  apply: (ctx: Context, config: any) => any;
7
8
  disable: (ctx: Context) => Promise<void>;
@@ -25,7 +26,9 @@ export declare class Context {
25
26
  private childPlugins;
26
27
  private i18ns;
27
28
  component: Components;
29
+ renderer?: IRenderer;
28
30
  instance: any;
31
+ childpath: string;
29
32
  /** 插件名称 */
30
33
  pluginname: string;
31
34
  /**
@@ -45,7 +48,7 @@ export declare class Context {
45
48
  * @param path 路由路径
46
49
  * @returns Route 实例
47
50
  */
48
- route(path: string): Route;
51
+ route(routepath: string): Route;
49
52
  /**
50
53
  * 注册事件
51
54
  * @param name 事件名称
@@ -95,7 +98,7 @@ export declare class Context {
95
98
  * 注册子 Context
96
99
  * @param name 子 Context 名称
97
100
  */
98
- fork(name?: string): Context;
101
+ fork(name?: string, path?: string): Context;
99
102
  /**
100
103
  * 注册子插件
101
104
  * @param plugin 插件实例
package/dist/context.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Route } from './route.js';
2
+ import path from 'path';
2
3
  /**
3
4
  * 插件上下文对象
4
5
  * 每个插件一个 Context,用于管理插件注册的命令、路由、事件、组件和中间件
@@ -14,7 +15,9 @@ export class Context {
14
15
  childPlugins = new Map();
15
16
  i18ns = [];
16
17
  component;
18
+ renderer;
17
19
  instance;
20
+ childpath = '/';
18
21
  /** 插件名称 */
19
22
  pluginname;
20
23
  /**
@@ -42,13 +45,14 @@ export class Context {
42
45
  * @param path 路由路径
43
46
  * @returns Route 实例
44
47
  */
45
- route(path) {
46
- if (this.core.routes[path]) {
48
+ route(routepath) {
49
+ const realpath = path.join(this.childpath, routepath);
50
+ if (this.core.routes[realpath]) {
47
51
  this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register route "${path}", but it has already been registered.`);
48
- return new Route(path, this);
52
+ return new Route(realpath, this);
49
53
  }
50
- this.routes.push(path);
51
- return this.core.route(path, this);
54
+ this.routes.push(realpath);
55
+ return this.core.route(realpath, this);
52
56
  }
53
57
  /**
54
58
  * 注册事件
@@ -133,8 +137,9 @@ export class Context {
133
137
  * 注册子 Context
134
138
  * @param name 子 Context 名称
135
139
  */
136
- fork(name = this.pluginname) {
140
+ fork(name = this.pluginname, path) {
137
141
  const ctx = new Context(this.core, name);
142
+ ctx.childpath = path;
138
143
  this.childContexts.push(ctx);
139
144
  return ctx;
140
145
  }
package/dist/core.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
- import { Config } from './config.js';
2
+ import { Config, Schema } from './config.js';
3
3
  import { Logger } from './logger.js';
4
4
  import { Session } from './session.js';
5
5
  import { Middleware } from './middleware.js';
@@ -15,7 +15,7 @@ interface Plugin {
15
15
  depend: Array<string>;
16
16
  provide: Array<string>;
17
17
  }
18
- interface CoreOptions {
18
+ export interface CoreOptions {
19
19
  port: number;
20
20
  host: string;
21
21
  staticDir: string;
@@ -23,6 +23,7 @@ interface CoreOptions {
23
23
  enableWs: boolean;
24
24
  lang: string[];
25
25
  }
26
+ export declare const coreConfigSchema: Schema<CoreOptions>;
26
27
  export declare const enum PluginStatus {
27
28
  ENABLED = "enabled",
28
29
  DISABLED = "disabled",
package/dist/core.js CHANGED
@@ -1,8 +1,17 @@
1
1
  import { EventEmitter } from 'events';
2
+ import { Schema } from './config.js';
2
3
  import { Logger } from './logger.js';
3
4
  import { Route } from './route.js';
4
5
  import { Hook } from './hook.js';
5
6
  import { Server as CoreServer } from './server.js';
7
+ export const coreConfigSchema = Schema.object({
8
+ port: Schema.number('监听端口').default(14510),
9
+ host: Schema.string('监听地址').default('0.0.0.0'),
10
+ staticDir: Schema.string('静态目录').default('public'),
11
+ enableCors: Schema.boolean('启用跨域').default(false),
12
+ enableWs: Schema.boolean('启用 WebSocket').default(false),
13
+ lang: Schema.array(Schema.string(), '语言列表').default(['zh', 'en']),
14
+ });
6
15
  export class Core {
7
16
  emitter = new EventEmitter();
8
17
  components = {};
package/dist/logger.d.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  * WindyPear-Team All right reserved
5
5
  **/
6
6
  import { Core } from './core.js';
7
+ type LogLevel = 'info' | 'warn' | 'error' | 'debug';
7
8
  export declare class Logger {
8
9
  private title;
9
10
  private titleColor;
@@ -13,12 +14,16 @@ export declare class Logger {
13
14
  message: string;
14
15
  timestamp: string;
15
16
  }[];
17
+ static level: LogLevel;
16
18
  static setCore(core: Core): void;
19
+ static setLevel(level: LogLevel): void;
17
20
  constructor(title: string);
18
21
  private getColorByName;
19
22
  private getTimestamp;
20
23
  private log;
24
+ debug(...args: any[]): void;
21
25
  info(...args: any[]): void;
22
26
  warn(...args: any[]): void;
23
27
  error(...args: any[]): void;
24
28
  }
29
+ export {};
package/dist/logger.js CHANGED
@@ -11,12 +11,16 @@ export class Logger {
11
11
  titleColor;
12
12
  static coreInstance = null;
13
13
  static logs = [];
14
+ static level = 'info';
14
15
  static setCore(core) {
15
16
  if (Logger.coreInstance !== null) {
16
17
  return;
17
18
  }
18
19
  Logger.coreInstance = core;
19
20
  }
21
+ static setLevel(level) {
22
+ Logger.level = level;
23
+ }
20
24
  constructor(title) {
21
25
  this.title = title;
22
26
  this.titleColor = this.getColorByName(this.title);
@@ -48,7 +52,22 @@ export class Logger {
48
52
  Logger.coreInstance?.emit('log', { level, message: args.join(' '), timestamp });
49
53
  Logger.logs.push({ level, message: args.join(' '), timestamp });
50
54
  }
51
- info(...args) { this.log('I', ...args); }
52
- warn(...args) { this.log('W', ...args); }
53
- error(...args) { this.log('E', ...args); }
55
+ debug(...args) {
56
+ if (Logger.level === 'debug') {
57
+ this.log('D', ...args);
58
+ }
59
+ }
60
+ info(...args) {
61
+ if (Logger.level === 'info' || Logger.level === 'debug') {
62
+ this.log('I', ...args);
63
+ }
64
+ }
65
+ warn(...args) {
66
+ if (Logger.level !== 'error') {
67
+ this.log('W', ...args);
68
+ }
69
+ }
70
+ error(...args) {
71
+ this.log('E', ...args);
72
+ }
54
73
  }
package/dist/session.d.ts CHANGED
@@ -153,5 +153,6 @@ export declare class Session {
153
153
  * @param data The data/props to pass to the component.
154
154
  */
155
155
  renderView(component: any, data?: Record<string, any>): Promise<void>;
156
+ renderFile(filePath: string, data?: any): Promise<void>;
156
157
  }
157
158
  export {};
package/dist/session.js CHANGED
@@ -393,15 +393,10 @@ export class Session {
393
393
  if (!this.pluginContext) {
394
394
  throw new Error(`Cannot call 'renderView' because the session is not associated with a plugin context.`);
395
395
  }
396
- const rendererName = this.pluginContext.instance.render;
396
+ const renderer = this.pluginContext.renderer || null;
397
397
  const pluginName = this.pluginContext.pluginname;
398
- if (!rendererName) {
399
- this.server.core.logger.error(`Plugin "${pluginName}" uses 'renderView' but did not declare a renderer. Please add 'export const render = "your-renderer-name";' to your plugin's entry file.`);
400
- // throw new Error(`Plugin "${pluginName}" did not declare a renderer.`);
401
- }
402
- const renderer = this.server.core.renderers.get(rendererName);
403
398
  if (!renderer) {
404
- this.server.core.logger.error(`Renderer "${rendererName}" declared by plugin "${pluginName}" is not registered. Have you installed the renderer package (e.g., '@yumerijs/vue-renderer')?`);
399
+ this.server.core.logger.error(`Renderer declared by plugin "${pluginName}" is not registered. Have you installed the renderer package (e.g., '@yumerijs/vue-renderer')?`);
405
400
  // throw new Error(`Renderer "${rendererName}" is not registered.`);
406
401
  }
407
402
  const renderOptions = {
@@ -417,4 +412,21 @@ export class Session {
417
412
  throw error;
418
413
  }
419
414
  }
415
+ async renderFile(filePath, data) {
416
+ const renderer = this.pluginContext.renderer;
417
+ const pluginName = this.pluginContext.pluginname;
418
+ if (!renderer) {
419
+ this.server.core.logger.error(`Renderer declared by plugin "${pluginName}" is not registered. Have you installed the renderer package (e.g., '@yumerijs/vue-renderer')?`);
420
+ // throw new Error(`Renderer "${rendererName}" is not registered.`);
421
+ }
422
+ try {
423
+ const html = await renderer.renderFile(filePath, data);
424
+ this.setMime('text/html');
425
+ this.response(html, 'plain');
426
+ }
427
+ catch (error) {
428
+ this.server.core.logger.error(`Error while rendering file "${filePath}":`, error);
429
+ throw error;
430
+ }
431
+ }
420
432
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yumerijs/core",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "Core module for yumeri",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",