@yumerijs/core 2.1.1 → 2.2.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/config.js +3 -8
- package/dist/context.d.ts +6 -4
- package/dist/context.js +4 -7
- package/dist/core.d.ts +9 -9
- package/dist/core.js +12 -16
- package/dist/hook.js +1 -5
- package/dist/i18n.js +1 -5
- package/dist/index.d.ts +9 -9
- package/dist/index.js +9 -25
- package/dist/logger.d.ts +6 -1
- package/dist/logger.js +32 -18
- package/dist/middleware.d.ts +1 -1
- package/dist/middleware.js +1 -2
- package/dist/route.d.ts +3 -3
- package/dist/route.js +3 -7
- package/dist/server.d.ts +1 -1
- package/dist/server.js +15 -55
- package/dist/session.d.ts +3 -2
- package/dist/session.js +30 -58
- package/package.json +1 -1
package/dist/config.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConfigSchema = exports.Schema = void 0;
|
|
4
|
-
exports.fallback = fallback;
|
|
5
1
|
function isNullable(value) {
|
|
6
2
|
return value === null || value === undefined;
|
|
7
3
|
}
|
|
8
|
-
function fallback(schema, config) {
|
|
4
|
+
export function fallback(schema, config) {
|
|
9
5
|
if (!schema)
|
|
10
6
|
return config;
|
|
11
7
|
let result = config;
|
|
@@ -29,7 +25,7 @@ function fallback(schema, config) {
|
|
|
29
25
|
}
|
|
30
26
|
return result;
|
|
31
27
|
}
|
|
32
|
-
class Schema {
|
|
28
|
+
export class Schema {
|
|
33
29
|
_type; // Phantom type
|
|
34
30
|
type;
|
|
35
31
|
isRequired;
|
|
@@ -79,5 +75,4 @@ class Schema {
|
|
|
79
75
|
return this;
|
|
80
76
|
}
|
|
81
77
|
}
|
|
82
|
-
|
|
83
|
-
exports.ConfigSchema = Schema;
|
|
78
|
+
export { Schema as ConfigSchema };
|
package/dist/context.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Core } from './core';
|
|
2
|
-
import { Route } from './route';
|
|
3
|
-
import { HookHandler } from './hook';
|
|
4
|
-
import { Middleware } from './middleware';
|
|
1
|
+
import { Core } from './core.js';
|
|
2
|
+
import { Route } from './route.js';
|
|
3
|
+
import { HookHandler } from './hook.js';
|
|
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,6 +26,7 @@ export declare class Context {
|
|
|
25
26
|
private childPlugins;
|
|
26
27
|
private i18ns;
|
|
27
28
|
component: Components;
|
|
29
|
+
renderer?: IRenderer;
|
|
28
30
|
instance: any;
|
|
29
31
|
/** 插件名称 */
|
|
30
32
|
pluginname: string;
|
package/dist/context.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Context = void 0;
|
|
4
|
-
const route_1 = require("./route");
|
|
1
|
+
import { Route } from './route.js';
|
|
5
2
|
/**
|
|
6
3
|
* 插件上下文对象
|
|
7
4
|
* 每个插件一个 Context,用于管理插件注册的命令、路由、事件、组件和中间件
|
|
8
5
|
*/
|
|
9
|
-
class Context {
|
|
6
|
+
export class Context {
|
|
10
7
|
core;
|
|
11
8
|
routes = [];
|
|
12
9
|
eventlisteners = [];
|
|
@@ -17,6 +14,7 @@ class Context {
|
|
|
17
14
|
childPlugins = new Map();
|
|
18
15
|
i18ns = [];
|
|
19
16
|
component;
|
|
17
|
+
renderer;
|
|
20
18
|
instance;
|
|
21
19
|
/** 插件名称 */
|
|
22
20
|
pluginname;
|
|
@@ -48,7 +46,7 @@ class Context {
|
|
|
48
46
|
route(path) {
|
|
49
47
|
if (this.core.routes[path]) {
|
|
50
48
|
this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register route "${path}", but it has already been registered.`);
|
|
51
|
-
return new
|
|
49
|
+
return new Route(path, this);
|
|
52
50
|
}
|
|
53
51
|
this.routes.push(path);
|
|
54
52
|
return this.core.route(path, this);
|
|
@@ -232,4 +230,3 @@ class Context {
|
|
|
232
230
|
this.childContexts = [];
|
|
233
231
|
}
|
|
234
232
|
}
|
|
235
|
-
exports.Context = Context;
|
package/dist/core.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
|
-
import { Config } from './config';
|
|
3
|
-
import { Logger } from './logger';
|
|
4
|
-
import { Session } from './session';
|
|
5
|
-
import { Middleware } from './middleware';
|
|
6
|
-
import { Route } from './route';
|
|
7
|
-
import { Context } from './context';
|
|
8
|
-
import { HookHandler, Hook } from './hook';
|
|
9
|
-
import { Server as CoreServer } from './server';
|
|
10
|
-
import { I18n } from './i18n';
|
|
2
|
+
import { Config } from './config.js';
|
|
3
|
+
import { Logger } from './logger.js';
|
|
4
|
+
import { Session } from './session.js';
|
|
5
|
+
import { Middleware } from './middleware.js';
|
|
6
|
+
import { Route } from './route.js';
|
|
7
|
+
import { Context } from './context.js';
|
|
8
|
+
import { HookHandler, Hook } from './hook.js';
|
|
9
|
+
import { Server as CoreServer } from './server.js';
|
|
10
|
+
import { I18n } from './i18n.js';
|
|
11
11
|
import { IRenderer } from '@yumerijs/types';
|
|
12
12
|
interface Plugin {
|
|
13
13
|
apply: (ctx: Context, config: Config) => Promise<void>;
|
package/dist/core.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const server_1 = require("./server");
|
|
9
|
-
class Core {
|
|
10
|
-
emitter = new events_1.EventEmitter();
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { Logger } from './logger.js';
|
|
3
|
+
import { Route } from './route.js';
|
|
4
|
+
import { Hook } from './hook.js';
|
|
5
|
+
import { Server as CoreServer } from './server.js';
|
|
6
|
+
export class Core {
|
|
7
|
+
emitter = new EventEmitter();
|
|
11
8
|
components = {};
|
|
12
9
|
routes = {};
|
|
13
|
-
logger = new
|
|
10
|
+
logger = new Logger('core');
|
|
14
11
|
globalMiddlewares = {};
|
|
15
12
|
hooks = {};
|
|
16
13
|
coreConfig;
|
|
@@ -23,7 +20,7 @@ class Core {
|
|
|
23
20
|
this.coreConfig = coreConfig || {};
|
|
24
21
|
this.loader = loader;
|
|
25
22
|
if (setCore)
|
|
26
|
-
|
|
23
|
+
Logger.setCore(this);
|
|
27
24
|
}
|
|
28
25
|
addRenderer(renderer) {
|
|
29
26
|
if (this.renderers.has(renderer.name)) {
|
|
@@ -35,7 +32,7 @@ class Core {
|
|
|
35
32
|
return this.pluginRenderers.get(pluginName);
|
|
36
33
|
}
|
|
37
34
|
async runCore() {
|
|
38
|
-
this.server = new
|
|
35
|
+
this.server = new CoreServer(this, {
|
|
39
36
|
port: this.coreConfig.port || 14510,
|
|
40
37
|
host: this.coreConfig.host || '0.0.0.0',
|
|
41
38
|
enableCors: this.coreConfig.enableCors || false,
|
|
@@ -93,13 +90,13 @@ class Core {
|
|
|
93
90
|
return this;
|
|
94
91
|
}
|
|
95
92
|
route(path, context) {
|
|
96
|
-
const route = new
|
|
93
|
+
const route = new Route(path, context);
|
|
97
94
|
this.routes[path] = route;
|
|
98
95
|
return route;
|
|
99
96
|
}
|
|
100
97
|
hook(name, hookname, callback) {
|
|
101
98
|
if (!this.hooks[name]) {
|
|
102
|
-
this.hooks[name] = new
|
|
99
|
+
this.hooks[name] = new Hook(name);
|
|
103
100
|
}
|
|
104
101
|
this.hooks[name].add(hookname, callback);
|
|
105
102
|
}
|
|
@@ -196,4 +193,3 @@ class Core {
|
|
|
196
193
|
return false;
|
|
197
194
|
}
|
|
198
195
|
}
|
|
199
|
-
exports.Core = Core;
|
package/dist/hook.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @time: 2025/08/14 18:33
|
|
4
3
|
* @author: FireGuo
|
|
5
4
|
* WindyPear-Team All right reserved
|
|
6
5
|
**/
|
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.Hook = void 0;
|
|
9
6
|
/**
|
|
10
7
|
* Hook 类
|
|
11
8
|
*/
|
|
12
|
-
class Hook {
|
|
9
|
+
export class Hook {
|
|
13
10
|
name;
|
|
14
11
|
handlers = {};
|
|
15
12
|
/**
|
|
@@ -48,4 +45,3 @@ class Hook {
|
|
|
48
45
|
return result;
|
|
49
46
|
}
|
|
50
47
|
}
|
|
51
|
-
exports.Hook = Hook;
|
package/dist/i18n.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @time: 2025/10/28 22:13
|
|
4
3
|
* @author: FireGuo
|
|
5
4
|
* WindyPear-Team All right reserved
|
|
6
5
|
**/
|
|
7
|
-
|
|
8
|
-
exports.I18n = void 0;
|
|
9
|
-
class I18n {
|
|
6
|
+
export class I18n {
|
|
10
7
|
data = {};
|
|
11
8
|
fallback;
|
|
12
9
|
constructor(fallback = ['en']) {
|
|
@@ -77,4 +74,3 @@ class I18n {
|
|
|
77
74
|
return this.data;
|
|
78
75
|
}
|
|
79
76
|
}
|
|
80
|
-
exports.I18n = I18n;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
* @author: FireGuo
|
|
4
4
|
* WindyPear-Team All right reserved
|
|
5
5
|
**/
|
|
6
|
-
export * from './core';
|
|
7
|
-
export * from './session';
|
|
8
|
-
export * from './route';
|
|
9
|
-
export * from './config';
|
|
10
|
-
export * from './logger';
|
|
11
|
-
export * from './context';
|
|
12
|
-
export * from './hook';
|
|
13
|
-
export * from './middleware';
|
|
14
|
-
export * from './i18n';
|
|
6
|
+
export * from './core.js';
|
|
7
|
+
export * from './session.js';
|
|
8
|
+
export * from './route.js';
|
|
9
|
+
export * from './config.js';
|
|
10
|
+
export * from './logger.js';
|
|
11
|
+
export * from './context.js';
|
|
12
|
+
export * from './hook.js';
|
|
13
|
+
export * from './middleware.js';
|
|
14
|
+
export * from './i18n.js';
|
package/dist/index.js
CHANGED
|
@@ -1,30 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @time: 2025/08/14 09:48
|
|
4
3
|
* @author: FireGuo
|
|
5
4
|
* WindyPear-Team All right reserved
|
|
6
5
|
**/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
o[k2] = m[k];
|
|
17
|
-
}));
|
|
18
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
-
};
|
|
21
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
__exportStar(require("./core"), exports);
|
|
23
|
-
__exportStar(require("./session"), exports);
|
|
24
|
-
__exportStar(require("./route"), exports);
|
|
25
|
-
__exportStar(require("./config"), exports);
|
|
26
|
-
__exportStar(require("./logger"), exports);
|
|
27
|
-
__exportStar(require("./context"), exports);
|
|
28
|
-
__exportStar(require("./hook"), exports);
|
|
29
|
-
__exportStar(require("./middleware"), exports);
|
|
30
|
-
__exportStar(require("./i18n"), exports);
|
|
6
|
+
export * from './core.js';
|
|
7
|
+
export * from './session.js';
|
|
8
|
+
export * from './route.js';
|
|
9
|
+
export * from './config.js';
|
|
10
|
+
export * from './logger.js';
|
|
11
|
+
export * from './context.js';
|
|
12
|
+
export * from './hook.js';
|
|
13
|
+
export * from './middleware.js';
|
|
14
|
+
export * from './i18n.js';
|
package/dist/logger.d.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
* @author: FireGuo
|
|
4
4
|
* WindyPear-Team All right reserved
|
|
5
5
|
**/
|
|
6
|
-
import { Core } from './core';
|
|
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
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @time: 2025/08/14 09:48
|
|
4
3
|
* @author: FireGuo
|
|
5
4
|
* WindyPear-Team All right reserved
|
|
6
5
|
**/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
exports.Logger = void 0;
|
|
12
|
-
const picocolors_1 = __importDefault(require("picocolors"));
|
|
13
|
-
class Logger {
|
|
6
|
+
import * as pcc from 'picocolors';
|
|
7
|
+
const { createColors } = pcc;
|
|
8
|
+
const pc = createColors();
|
|
9
|
+
export class Logger {
|
|
14
10
|
title;
|
|
15
11
|
titleColor;
|
|
16
12
|
static coreInstance = null;
|
|
17
13
|
static logs = [];
|
|
14
|
+
static level = 'info';
|
|
18
15
|
static setCore(core) {
|
|
19
16
|
if (Logger.coreInstance !== null) {
|
|
20
17
|
return;
|
|
21
18
|
}
|
|
22
19
|
Logger.coreInstance = core;
|
|
23
20
|
}
|
|
21
|
+
static setLevel(level) {
|
|
22
|
+
Logger.level = level;
|
|
23
|
+
}
|
|
24
24
|
constructor(title) {
|
|
25
25
|
this.title = title;
|
|
26
26
|
this.titleColor = this.getColorByName(this.title);
|
|
27
27
|
}
|
|
28
28
|
getColorByName(name) {
|
|
29
29
|
const availableColors = [
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
pc.red, pc.green, pc.yellow, pc.blue,
|
|
31
|
+
pc.magenta, pc.cyan, pc.white,
|
|
32
|
+
pc.redBright, pc.greenBright, pc.yellowBright,
|
|
33
|
+
pc.blueBright, pc.magentaBright, pc.cyanBright
|
|
34
34
|
];
|
|
35
35
|
// Simple hash function to ensure consistent color for the same title
|
|
36
36
|
let hash = 0;
|
|
@@ -46,14 +46,28 @@ class Logger {
|
|
|
46
46
|
`${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}:${String(now.getSeconds()).padStart(2, '0')}`;
|
|
47
47
|
}
|
|
48
48
|
log(level, ...args) {
|
|
49
|
-
const levelColor = level === 'E' ?
|
|
49
|
+
const levelColor = level === 'E' ? pc.red : level === 'W' ? pc.yellow : pc.cyan;
|
|
50
50
|
const timestamp = this.getTimestamp();
|
|
51
|
-
console.log(`${
|
|
51
|
+
console.log(`${pc.gray(timestamp)} [${levelColor(level)}] ${this.titleColor(this.title)}`, ...args);
|
|
52
52
|
Logger.coreInstance?.emit('log', { level, message: args.join(' '), timestamp });
|
|
53
53
|
Logger.logs.push({ level, message: args.join(' '), timestamp });
|
|
54
54
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
+
}
|
|
58
73
|
}
|
|
59
|
-
exports.Logger = Logger;
|
package/dist/middleware.d.ts
CHANGED
package/dist/middleware.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/dist/route.d.ts
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* @author: FireGuo
|
|
4
4
|
* WindyPear-Team All right reserved
|
|
5
5
|
**/
|
|
6
|
-
import { Session } from './session';
|
|
7
|
-
import { Middleware } from './middleware';
|
|
6
|
+
import { Session } from './session.js';
|
|
7
|
+
import { Middleware } from './middleware.js';
|
|
8
8
|
import { WebSocketServer } from 'ws';
|
|
9
|
-
import { Context } from './context';
|
|
9
|
+
import { Context } from './context.js';
|
|
10
10
|
export type RouteHandler = (session: Session, queryParams: URLSearchParams, ...pathParams: string[]) => Promise<void> | void;
|
|
11
11
|
/**
|
|
12
12
|
* Route class using segment-based matching (no fragile capture-group-index mapping).
|
package/dist/route.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Route = void 0;
|
|
4
|
-
const ws_1 = require("ws");
|
|
1
|
+
import { WebSocketServer } from 'ws';
|
|
5
2
|
function parsePatternToSegments(pattern) {
|
|
6
3
|
// Normalize: remove leading/trailing slashes for consistent splitting
|
|
7
4
|
const norm = pattern.replace(/^\/+|\/+$/g, '');
|
|
@@ -33,7 +30,7 @@ function parsePatternToSegments(pattern) {
|
|
|
33
30
|
* :param* (zero or more segments)
|
|
34
31
|
* - honors "no trailing slash" expectation (we normalize input)
|
|
35
32
|
*/
|
|
36
|
-
class Route {
|
|
33
|
+
export class Route {
|
|
37
34
|
path;
|
|
38
35
|
segments;
|
|
39
36
|
paramsInfo;
|
|
@@ -234,9 +231,8 @@ class Route {
|
|
|
234
231
|
*/
|
|
235
232
|
wsOn(event, handler) {
|
|
236
233
|
if (!this.ws)
|
|
237
|
-
this.ws = new
|
|
234
|
+
this.ws = new WebSocketServer({ noServer: true });
|
|
238
235
|
this.ws.on(event, handler);
|
|
239
236
|
return this;
|
|
240
237
|
}
|
|
241
238
|
}
|
|
242
|
-
exports.Route = Route;
|
package/dist/server.d.ts
CHANGED
package/dist/server.js
CHANGED
|
@@ -1,55 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.Server = void 0;
|
|
40
|
-
const logger_1 = require("./logger");
|
|
41
|
-
const session_1 = require("./session");
|
|
42
|
-
const http_1 = __importDefault(require("http"));
|
|
43
|
-
const fs = __importStar(require("fs"));
|
|
44
|
-
const path = __importStar(require("path"));
|
|
45
|
-
const url_1 = require("url");
|
|
46
|
-
const mime = __importStar(require("mime-types"));
|
|
47
|
-
const types_1 = require("@yumerijs/types");
|
|
48
|
-
const logger = new logger_1.Logger('server');
|
|
1
|
+
import { Logger } from './logger.js';
|
|
2
|
+
import { Session } from './session.js';
|
|
3
|
+
import http from 'http';
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import { URL } from 'url';
|
|
7
|
+
import * as mime from 'mime-types';
|
|
8
|
+
import { resolveVirtualAsset } from '@yumerijs/types';
|
|
9
|
+
const logger = new Logger('server');
|
|
49
10
|
function isStream(value) {
|
|
50
11
|
return value && typeof value.pipe === "function";
|
|
51
12
|
}
|
|
52
|
-
class Server {
|
|
13
|
+
export class Server {
|
|
53
14
|
core;
|
|
54
15
|
port;
|
|
55
16
|
host;
|
|
@@ -67,7 +28,7 @@ class Server {
|
|
|
67
28
|
return this.staticDir;
|
|
68
29
|
}
|
|
69
30
|
createSession(ip, cookies, res, req, pathname, pluginContext, extra = {}) {
|
|
70
|
-
const session = new
|
|
31
|
+
const session = new Session(ip, cookies, this, req, res, pathname, undefined, pluginContext);
|
|
71
32
|
Object.assign(session.properties, extra);
|
|
72
33
|
session.protocol = extra.protocol ?? 'http';
|
|
73
34
|
return session;
|
|
@@ -101,7 +62,7 @@ class Server {
|
|
|
101
62
|
});
|
|
102
63
|
}
|
|
103
64
|
async start() {
|
|
104
|
-
this.httpServer =
|
|
65
|
+
this.httpServer = http.createServer(async (req, res) => {
|
|
105
66
|
if (req.method === 'OPTIONS' && this.enableCors) {
|
|
106
67
|
res.writeHead(204, {
|
|
107
68
|
'Access-Control-Allow-Origin': '*',
|
|
@@ -111,13 +72,13 @@ class Server {
|
|
|
111
72
|
res.end();
|
|
112
73
|
return;
|
|
113
74
|
}
|
|
114
|
-
const url = new
|
|
75
|
+
const url = new URL(req.url || '/', `http://${req.headers.host}`);
|
|
115
76
|
const pathname = url.pathname;
|
|
116
77
|
const queryParams = url.searchParams;
|
|
117
78
|
const ip = this.getClientIP(req);
|
|
118
79
|
const cookies = this.parseCookies(req);
|
|
119
80
|
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
120
|
-
const virtualAsset = await
|
|
81
|
+
const virtualAsset = await resolveVirtualAsset(pathname);
|
|
121
82
|
if (virtualAsset) {
|
|
122
83
|
const headers = virtualAsset.headers || {};
|
|
123
84
|
headers['Content-Type'] = headers['Content-Type'] || virtualAsset.contentType || 'application/octet-stream';
|
|
@@ -191,7 +152,7 @@ class Server {
|
|
|
191
152
|
}
|
|
192
153
|
});
|
|
193
154
|
this.httpServer.on('upgrade', async (req, socket, head) => {
|
|
194
|
-
const url = new
|
|
155
|
+
const url = new URL(req.url || '/', `http://${req.headers.host}`);
|
|
195
156
|
const pathname = url.pathname;
|
|
196
157
|
const queryParams = url.searchParams;
|
|
197
158
|
const ip = this.getClientIP(req);
|
|
@@ -229,4 +190,3 @@ class Server {
|
|
|
229
190
|
this.httpServer.close(() => logger.info('Server stopped'));
|
|
230
191
|
}
|
|
231
192
|
}
|
|
232
|
-
exports.Server = Server;
|
package/dist/session.d.ts
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* @author: FireGuo
|
|
4
4
|
* WindyPear-Team All right reserved
|
|
5
5
|
**/
|
|
6
|
-
import { Server } from './server';
|
|
6
|
+
import { Server } from './server.js';
|
|
7
7
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
8
8
|
import { Stream } from "stream";
|
|
9
|
-
import { Context } from './context';
|
|
9
|
+
import { Context } from './context.js';
|
|
10
10
|
type ParsedParams = Record<string, string | string[] | undefined>;
|
|
11
11
|
type MissingMode = 'keep-template' | 'keep-key' | 'remove';
|
|
12
12
|
export interface Client {
|
|
@@ -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
|
@@ -1,51 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @time: 2025/03/26 12:36
|
|
4
3
|
* @author: FireGuo
|
|
5
4
|
* WindyPear-Team All right reserved
|
|
6
5
|
**/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
-
}
|
|
13
|
-
Object.defineProperty(o, k2, desc);
|
|
14
|
-
}) : (function(o, m, k, k2) {
|
|
15
|
-
if (k2 === undefined) k2 = k;
|
|
16
|
-
o[k2] = m[k];
|
|
17
|
-
}));
|
|
18
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
-
}) : function(o, v) {
|
|
21
|
-
o["default"] = v;
|
|
22
|
-
});
|
|
23
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
-
var ownKeys = function(o) {
|
|
25
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
-
var ar = [];
|
|
27
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
-
return ar;
|
|
29
|
-
};
|
|
30
|
-
return ownKeys(o);
|
|
31
|
-
};
|
|
32
|
-
return function (mod) {
|
|
33
|
-
if (mod && mod.__esModule) return mod;
|
|
34
|
-
var result = {};
|
|
35
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
-
__setModuleDefault(result, mod);
|
|
37
|
-
return result;
|
|
38
|
-
};
|
|
39
|
-
})();
|
|
40
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
-
};
|
|
43
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.Session = void 0;
|
|
45
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
46
|
-
const formidable = __importStar(require("formidable"));
|
|
47
|
-
const fs_1 = __importDefault(require("fs"));
|
|
48
|
-
class Session {
|
|
6
|
+
import crypto from 'crypto';
|
|
7
|
+
import * as formidable from 'formidable';
|
|
8
|
+
import fs from 'fs';
|
|
9
|
+
export class Session {
|
|
49
10
|
ip;
|
|
50
11
|
cookie;
|
|
51
12
|
query;
|
|
@@ -241,7 +202,7 @@ class Session {
|
|
|
241
202
|
}
|
|
242
203
|
// MD5 加密
|
|
243
204
|
md5(str) {
|
|
244
|
-
const hash =
|
|
205
|
+
const hash = crypto.createHash('md5');
|
|
245
206
|
hash.update(str);
|
|
246
207
|
return hash.digest('hex');
|
|
247
208
|
}
|
|
@@ -359,13 +320,13 @@ class Session {
|
|
|
359
320
|
file(path, option) {
|
|
360
321
|
if (this.client.headers['If-Modified-Since']) {
|
|
361
322
|
const modified = new Date(this.client.headers['If-Modified-Since']);
|
|
362
|
-
const moditime =
|
|
323
|
+
const moditime = fs.statSync(path).mtime;
|
|
363
324
|
if (modified.getTime() === moditime.getTime()) {
|
|
364
325
|
this.status = 304;
|
|
365
326
|
return;
|
|
366
327
|
}
|
|
367
328
|
}
|
|
368
|
-
const etag =
|
|
329
|
+
const etag = crypto.createHash(option.etagType || 'md5').update(fs.readFileSync(path)).digest('hex');
|
|
369
330
|
if (this.client.headers['If-None-Match']) {
|
|
370
331
|
if (option.etag) {
|
|
371
332
|
if (this.client.headers['If-None-Match'] === etag) {
|
|
@@ -375,13 +336,13 @@ class Session {
|
|
|
375
336
|
}
|
|
376
337
|
}
|
|
377
338
|
this.setCache({
|
|
378
|
-
modified:
|
|
339
|
+
modified: fs.statSync(path).mtime,
|
|
379
340
|
etag,
|
|
380
341
|
maxAge: option.maxAge,
|
|
381
342
|
smaxAge: option.smaxAge,
|
|
382
343
|
cacheControl: 'public'
|
|
383
344
|
});
|
|
384
|
-
this.response(
|
|
345
|
+
this.response(fs.createReadStream(path), 'stream');
|
|
385
346
|
}
|
|
386
347
|
/**
|
|
387
348
|
* 发送普通文件
|
|
@@ -391,10 +352,10 @@ class Session {
|
|
|
391
352
|
sendFile(path, isStream = false) {
|
|
392
353
|
if (isStream) {
|
|
393
354
|
this.setMime('application/octet-stream');
|
|
394
|
-
this.response(
|
|
355
|
+
this.response(fs.createReadStream(path), 'stream');
|
|
395
356
|
}
|
|
396
357
|
else {
|
|
397
|
-
this.response(
|
|
358
|
+
this.response(fs.readFileSync(path), 'buffer');
|
|
398
359
|
}
|
|
399
360
|
}
|
|
400
361
|
/**
|
|
@@ -432,15 +393,10 @@ class Session {
|
|
|
432
393
|
if (!this.pluginContext) {
|
|
433
394
|
throw new Error(`Cannot call 'renderView' because the session is not associated with a plugin context.`);
|
|
434
395
|
}
|
|
435
|
-
const
|
|
396
|
+
const renderer = this.pluginContext.renderer;
|
|
436
397
|
const pluginName = this.pluginContext.pluginname;
|
|
437
|
-
if (!rendererName) {
|
|
438
|
-
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.`);
|
|
439
|
-
// throw new Error(`Plugin "${pluginName}" did not declare a renderer.`);
|
|
440
|
-
}
|
|
441
|
-
const renderer = this.server.core.renderers.get(rendererName);
|
|
442
398
|
if (!renderer) {
|
|
443
|
-
this.server.core.logger.error(`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')?`);
|
|
444
400
|
// throw new Error(`Renderer "${rendererName}" is not registered.`);
|
|
445
401
|
}
|
|
446
402
|
const renderOptions = {
|
|
@@ -456,5 +412,21 @@ class Session {
|
|
|
456
412
|
throw error;
|
|
457
413
|
}
|
|
458
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
|
+
}
|
|
459
432
|
}
|
|
460
|
-
exports.Session = Session;
|