vafast 0.1.17 → 0.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/README.md +167 -185
- package/dist/auth/token.d.ts +39 -2
- package/dist/auth/token.js +124 -0
- package/dist/defineRoute.js +3 -0
- package/dist/index.d.ts +3 -4
- package/dist/index.js +13 -323
- package/dist/middleware/auth.d.ts +6 -0
- package/dist/middleware/auth.js +106 -0
- package/dist/middleware/authMiddleware.js +13 -0
- package/dist/middleware/component-renderer.d.ts +6 -0
- package/dist/middleware/component-renderer.js +132 -0
- package/dist/middleware/component-router.d.ts +10 -0
- package/dist/middleware/component-router.js +42 -0
- package/dist/middleware/cors.js +30 -0
- package/dist/middleware/rateLimit.js +33 -0
- package/dist/middleware.d.ts +1 -1
- package/dist/middleware.js +56 -0
- package/dist/monitoring/index.d.ts +29 -0
- package/dist/monitoring/index.js +24 -0
- package/dist/monitoring/native-monitor.d.ts +38 -0
- package/dist/monitoring/native-monitor.js +176 -0
- package/dist/monitoring/types.d.ts +146 -0
- package/dist/monitoring/types.js +8 -0
- package/dist/router/index.d.ts +5 -0
- package/dist/router/index.js +7 -0
- package/dist/router/radix-tree.d.ts +51 -0
- package/dist/router/radix-tree.js +186 -0
- package/dist/router.d.ts +43 -6
- package/dist/router.js +86 -0
- package/dist/server/base-server.d.ts +34 -0
- package/dist/server/base-server.js +145 -0
- package/dist/server/component-server.d.ts +32 -0
- package/dist/server/component-server.js +146 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.js +11 -0
- package/dist/server/server-factory.d.ts +42 -0
- package/dist/server/server-factory.js +70 -0
- package/dist/server/server.d.ts +35 -0
- package/dist/server/server.js +97 -0
- package/dist/types/component-route.d.ts +25 -0
- package/dist/types/component-route.js +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.js +4 -0
- package/dist/types/route.d.ts +39 -0
- package/dist/types/route.js +11 -0
- package/dist/types/schema.d.ts +75 -0
- package/dist/types/schema.js +10 -0
- package/dist/types/types.d.ts +22 -0
- package/dist/types/types.js +1 -0
- package/dist/utils/base64url.js +11 -0
- package/dist/utils/create-handler.d.ts +74 -0
- package/dist/utils/create-handler.js +234 -0
- package/dist/utils/dependency-manager.d.ts +23 -0
- package/dist/utils/dependency-manager.js +73 -0
- package/dist/utils/go-await.d.ts +26 -0
- package/dist/utils/go-await.js +30 -0
- package/dist/{cookie.d.ts → utils/handle.d.ts} +3 -0
- package/dist/utils/handle.js +29 -0
- package/dist/utils/html-renderer.d.ts +18 -0
- package/dist/utils/html-renderer.js +64 -0
- package/dist/utils/index.d.ts +12 -0
- package/dist/utils/index.js +21 -0
- package/dist/utils/parsers.d.ts +36 -0
- package/dist/utils/parsers.js +126 -0
- package/dist/utils/path-matcher.d.ts +23 -0
- package/dist/utils/path-matcher.js +83 -0
- package/dist/utils/request-validator.d.ts +63 -0
- package/dist/utils/request-validator.js +94 -0
- package/dist/utils/response.d.ts +17 -0
- package/dist/utils/response.js +110 -0
- package/dist/utils/validators/schema-validator.d.ts +66 -0
- package/dist/utils/validators/schema-validator.js +222 -0
- package/dist/utils/validators/schema-validators-ultra.d.ts +51 -0
- package/dist/utils/validators/schema-validators-ultra.js +289 -0
- package/dist/utils/validators/validators.d.ts +30 -0
- package/dist/utils/validators/validators.js +54 -0
- package/package.json +50 -14
- package/dist/server.d.ts +0 -9
- package/dist/types.d.ts +0 -9
- package/dist/util.d.ts +0 -7
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Radix Tree 路由匹配器
|
|
3
|
+
*
|
|
4
|
+
* 高性能路由匹配实现,时间复杂度 O(k),k 为路径段数
|
|
5
|
+
*
|
|
6
|
+
* 支持的路由模式:
|
|
7
|
+
* - 静态路径: /users, /api/v1/health
|
|
8
|
+
* - 动态参数: /users/:id, /posts/:postId/comments/:commentId
|
|
9
|
+
* - 通配符: /files/*, /static/*filepath
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Radix Tree 路由器
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const router = new RadixRouter();
|
|
17
|
+
* router.register("GET", "/users/:id", handler);
|
|
18
|
+
* const result = router.match("GET", "/users/123");
|
|
19
|
+
* // result.params = { id: "123" }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export class RadixRouter {
|
|
23
|
+
root;
|
|
24
|
+
constructor() {
|
|
25
|
+
this.root = this.createNode("");
|
|
26
|
+
}
|
|
27
|
+
createNode(path) {
|
|
28
|
+
return {
|
|
29
|
+
path,
|
|
30
|
+
children: Object.create(null),
|
|
31
|
+
handlers: Object.create(null),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** 分割路径 */
|
|
35
|
+
splitPath(path) {
|
|
36
|
+
return path.split("/").filter(Boolean);
|
|
37
|
+
}
|
|
38
|
+
/** 注册路由 */
|
|
39
|
+
register(method, pattern, handler, middleware = []) {
|
|
40
|
+
const segments = this.splitPath(pattern);
|
|
41
|
+
let node = this.root;
|
|
42
|
+
for (const segment of segments) {
|
|
43
|
+
const firstChar = segment[0];
|
|
44
|
+
if (firstChar === ":") {
|
|
45
|
+
// 动态参数节点
|
|
46
|
+
if (!node.paramChild) {
|
|
47
|
+
node.paramChild = this.createNode(segment);
|
|
48
|
+
node.paramChild.paramName = segment.substring(1);
|
|
49
|
+
}
|
|
50
|
+
node = node.paramChild;
|
|
51
|
+
}
|
|
52
|
+
else if (firstChar === "*") {
|
|
53
|
+
// 通配符节点
|
|
54
|
+
if (!node.wildcardChild) {
|
|
55
|
+
node.wildcardChild = this.createNode(segment);
|
|
56
|
+
node.wildcardChild.paramName =
|
|
57
|
+
segment.length > 1 ? segment.substring(1) : "*";
|
|
58
|
+
}
|
|
59
|
+
node = node.wildcardChild;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
// 静态路径节点
|
|
64
|
+
if (!node.children[segment]) {
|
|
65
|
+
node.children[segment] = this.createNode(segment);
|
|
66
|
+
}
|
|
67
|
+
node = node.children[segment];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
node.handlers[method] = { handler, middleware };
|
|
71
|
+
}
|
|
72
|
+
/** 匹配路由 */
|
|
73
|
+
match(method, path) {
|
|
74
|
+
const segments = this.splitPath(path);
|
|
75
|
+
const params = Object.create(null);
|
|
76
|
+
const node = this.matchNode(this.root, segments, 0, params);
|
|
77
|
+
if (!node)
|
|
78
|
+
return null;
|
|
79
|
+
const routeHandler = node.handlers[method];
|
|
80
|
+
if (!routeHandler)
|
|
81
|
+
return null;
|
|
82
|
+
return {
|
|
83
|
+
handler: routeHandler.handler,
|
|
84
|
+
middleware: routeHandler.middleware,
|
|
85
|
+
params,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** 递归匹配节点 (优先级: 静态 > 动态参数 > 通配符) */
|
|
89
|
+
matchNode(node, segments, index, params) {
|
|
90
|
+
if (index === segments.length) {
|
|
91
|
+
for (const method in node.handlers) {
|
|
92
|
+
if (node.handlers[method])
|
|
93
|
+
return node;
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const segment = segments[index];
|
|
98
|
+
// 1. 静态路径
|
|
99
|
+
const staticChild = node.children[segment];
|
|
100
|
+
if (staticChild) {
|
|
101
|
+
const result = this.matchNode(staticChild, segments, index + 1, params);
|
|
102
|
+
if (result)
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
// 2. 动态参数
|
|
106
|
+
if (node.paramChild) {
|
|
107
|
+
const paramName = node.paramChild.paramName;
|
|
108
|
+
const oldValue = params[paramName];
|
|
109
|
+
params[paramName] = segment;
|
|
110
|
+
const result = this.matchNode(node.paramChild, segments, index + 1, params);
|
|
111
|
+
if (result)
|
|
112
|
+
return result;
|
|
113
|
+
// 回溯
|
|
114
|
+
if (oldValue === undefined) {
|
|
115
|
+
delete params[paramName];
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
params[paramName] = oldValue;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// 3. 通配符
|
|
122
|
+
if (node.wildcardChild) {
|
|
123
|
+
params[node.wildcardChild.paramName || "*"] = segments
|
|
124
|
+
.slice(index)
|
|
125
|
+
.join("/");
|
|
126
|
+
return node.wildcardChild;
|
|
127
|
+
}
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
/** 获取路径允许的 HTTP 方法 */
|
|
131
|
+
getAllowedMethods(path) {
|
|
132
|
+
const segments = this.splitPath(path);
|
|
133
|
+
const node = this.findNode(segments);
|
|
134
|
+
if (!node)
|
|
135
|
+
return [];
|
|
136
|
+
const methods = [];
|
|
137
|
+
for (const method in node.handlers) {
|
|
138
|
+
if (node.handlers[method]) {
|
|
139
|
+
methods.push(method);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return methods;
|
|
143
|
+
}
|
|
144
|
+
/** 查找节点(不提取参数) */
|
|
145
|
+
findNode(segments) {
|
|
146
|
+
let node = this.root;
|
|
147
|
+
for (const segment of segments) {
|
|
148
|
+
if (node.children[segment]) {
|
|
149
|
+
node = node.children[segment];
|
|
150
|
+
}
|
|
151
|
+
else if (node.paramChild) {
|
|
152
|
+
node = node.paramChild;
|
|
153
|
+
}
|
|
154
|
+
else if (node.wildcardChild) {
|
|
155
|
+
return node.wildcardChild;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return node;
|
|
162
|
+
}
|
|
163
|
+
/** 获取所有已注册的路由 */
|
|
164
|
+
getRoutes() {
|
|
165
|
+
const routes = [];
|
|
166
|
+
this.collectRoutes(this.root, "", routes);
|
|
167
|
+
return routes;
|
|
168
|
+
}
|
|
169
|
+
collectRoutes(node, prefix, routes) {
|
|
170
|
+
const currentPath = prefix + (node.path ? "/" + node.path : "");
|
|
171
|
+
for (const method in node.handlers) {
|
|
172
|
+
if (node.handlers[method]) {
|
|
173
|
+
routes.push({ method: method, path: currentPath || "/" });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const key in node.children) {
|
|
177
|
+
this.collectRoutes(node.children[key], currentPath, routes);
|
|
178
|
+
}
|
|
179
|
+
if (node.paramChild) {
|
|
180
|
+
this.collectRoutes(node.paramChild, currentPath, routes);
|
|
181
|
+
}
|
|
182
|
+
if (node.wildcardChild) {
|
|
183
|
+
this.collectRoutes(node.wildcardChild, currentPath, routes);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
package/dist/router.d.ts
CHANGED
|
@@ -1,8 +1,45 @@
|
|
|
1
|
-
export interface MatchResult {
|
|
2
|
-
matched: boolean;
|
|
3
|
-
params: Record<string, string>;
|
|
4
|
-
}
|
|
5
1
|
/**
|
|
6
|
-
*
|
|
2
|
+
* 路由工具函数
|
|
3
|
+
*
|
|
4
|
+
* 提供路由处理的基础工具
|
|
7
5
|
*/
|
|
8
|
-
|
|
6
|
+
import type { Route, NestedRoute, FlattenedRoute } from "./types";
|
|
7
|
+
/**
|
|
8
|
+
* 扁平化嵌套路由
|
|
9
|
+
*
|
|
10
|
+
* 将嵌套路由结构转换为扁平数组,计算完整路径和中间件链
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const routes = flattenNestedRoutes([
|
|
15
|
+
* {
|
|
16
|
+
* path: "/api",
|
|
17
|
+
* middleware: [authMiddleware],
|
|
18
|
+
* children: [
|
|
19
|
+
* { path: "/users", method: "GET", handler: getUsers },
|
|
20
|
+
* { path: "/users/:id", method: "GET", handler: getUser },
|
|
21
|
+
* ],
|
|
22
|
+
* },
|
|
23
|
+
* ]);
|
|
24
|
+
* // 结果:
|
|
25
|
+
* // [
|
|
26
|
+
* // { fullPath: "/api/users", method: "GET", ... },
|
|
27
|
+
* // { fullPath: "/api/users/:id", method: "GET", ... },
|
|
28
|
+
* // ]
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function flattenNestedRoutes(routes: (Route | NestedRoute)[]): FlattenedRoute[];
|
|
32
|
+
/**
|
|
33
|
+
* 标准化路径
|
|
34
|
+
*
|
|
35
|
+
* - 解码 URL 编码字符
|
|
36
|
+
* - 去除重复斜杠
|
|
37
|
+
* - 处理结尾斜杠
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* normalizePath("//api//users/") // "/api/users"
|
|
42
|
+
* normalizePath("/api/%20test") // "/api/ test"
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function normalizePath(path: string): string;
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由工具函数
|
|
3
|
+
*
|
|
4
|
+
* 提供路由处理的基础工具
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 扁平化嵌套路由
|
|
8
|
+
*
|
|
9
|
+
* 将嵌套路由结构转换为扁平数组,计算完整路径和中间件链
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const routes = flattenNestedRoutes([
|
|
14
|
+
* {
|
|
15
|
+
* path: "/api",
|
|
16
|
+
* middleware: [authMiddleware],
|
|
17
|
+
* children: [
|
|
18
|
+
* { path: "/users", method: "GET", handler: getUsers },
|
|
19
|
+
* { path: "/users/:id", method: "GET", handler: getUser },
|
|
20
|
+
* ],
|
|
21
|
+
* },
|
|
22
|
+
* ]);
|
|
23
|
+
* // 结果:
|
|
24
|
+
* // [
|
|
25
|
+
* // { fullPath: "/api/users", method: "GET", ... },
|
|
26
|
+
* // { fullPath: "/api/users/:id", method: "GET", ... },
|
|
27
|
+
* // ]
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function flattenNestedRoutes(routes) {
|
|
31
|
+
const flattened = [];
|
|
32
|
+
function processRoute(route, parentPath = "", parentMiddleware = []) {
|
|
33
|
+
// 计算当前完整路径
|
|
34
|
+
const currentPath = normalizePath(parentPath + route.path);
|
|
35
|
+
// 合并中间件链
|
|
36
|
+
const currentMiddleware = [
|
|
37
|
+
...parentMiddleware,
|
|
38
|
+
...(route.middleware || []),
|
|
39
|
+
];
|
|
40
|
+
if ("method" in route && "handler" in route) {
|
|
41
|
+
// 叶子路由(有处理函数)
|
|
42
|
+
flattened.push({
|
|
43
|
+
...route,
|
|
44
|
+
fullPath: currentPath,
|
|
45
|
+
middlewareChain: currentMiddleware,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
else if ("children" in route && route.children) {
|
|
49
|
+
// 分组路由,递归处理子路由
|
|
50
|
+
for (const child of route.children) {
|
|
51
|
+
processRoute(child, currentPath, currentMiddleware);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const route of routes) {
|
|
56
|
+
processRoute(route);
|
|
57
|
+
}
|
|
58
|
+
return flattened;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 标准化路径
|
|
62
|
+
*
|
|
63
|
+
* - 解码 URL 编码字符
|
|
64
|
+
* - 去除重复斜杠
|
|
65
|
+
* - 处理结尾斜杠
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* normalizePath("//api//users/") // "/api/users"
|
|
70
|
+
* normalizePath("/api/%20test") // "/api/ test"
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export function normalizePath(path) {
|
|
74
|
+
// 解码 URL 编码
|
|
75
|
+
let normalized = decodeURIComponent(path);
|
|
76
|
+
// 去除重复斜杠
|
|
77
|
+
normalized = normalized.replace(/\/+/g, "/");
|
|
78
|
+
// 空路径转为根路径
|
|
79
|
+
if (normalized === "")
|
|
80
|
+
return "/";
|
|
81
|
+
// 去除结尾斜杠(根路径除外)
|
|
82
|
+
if (normalized !== "/" && normalized.endsWith("/")) {
|
|
83
|
+
normalized = normalized.slice(0, -1);
|
|
84
|
+
}
|
|
85
|
+
return normalized;
|
|
86
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Middleware } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* 服务器基类
|
|
4
|
+
* 包含所有服务器类型的公共逻辑
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class BaseServer {
|
|
7
|
+
protected globalMiddleware: Middleware[];
|
|
8
|
+
use(mw: Middleware): void;
|
|
9
|
+
/**
|
|
10
|
+
* 打印扁平化后的路由信息,用于调试
|
|
11
|
+
*/
|
|
12
|
+
protected logFlattenedRoutes(routes: any[], type?: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* 检测路由冲突
|
|
15
|
+
* 检查是否有路径相同但方法不同的路由,以及潜在的路径冲突
|
|
16
|
+
*/
|
|
17
|
+
protected detectRouteConflicts(routes: any[]): void;
|
|
18
|
+
/**
|
|
19
|
+
* 检测动态路由的潜在冲突
|
|
20
|
+
*/
|
|
21
|
+
private detectDynamicRouteConflicts;
|
|
22
|
+
/**
|
|
23
|
+
* 判断两个路径是否可能冲突
|
|
24
|
+
*/
|
|
25
|
+
private pathsMayConflict;
|
|
26
|
+
/**
|
|
27
|
+
* 路径匹配
|
|
28
|
+
*/
|
|
29
|
+
protected matchPath(pattern: string, path: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* 提取路径参数
|
|
32
|
+
*/
|
|
33
|
+
protected extractParams(pattern: string, path: string): Record<string, string>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 服务器基类
|
|
3
|
+
* 包含所有服务器类型的公共逻辑
|
|
4
|
+
*/
|
|
5
|
+
export class BaseServer {
|
|
6
|
+
globalMiddleware = [];
|
|
7
|
+
use(mw) {
|
|
8
|
+
this.globalMiddleware.push(mw);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 打印扁平化后的路由信息,用于调试
|
|
12
|
+
*/
|
|
13
|
+
logFlattenedRoutes(routes, type = "路由") {
|
|
14
|
+
console.log(`🚀 扁平化后的${type}:`);
|
|
15
|
+
for (const route of routes) {
|
|
16
|
+
const method = route.method || "GET";
|
|
17
|
+
const path = route.fullPath || route.path;
|
|
18
|
+
console.log(` ${method} ${path}`);
|
|
19
|
+
if (route.middlewareChain && route.middlewareChain.length > 0) {
|
|
20
|
+
console.log(` 中间件链: ${route.middlewareChain.length} 个`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
console.log("");
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 检测路由冲突
|
|
27
|
+
* 检查是否有路径相同但方法不同的路由,以及潜在的路径冲突
|
|
28
|
+
*/
|
|
29
|
+
detectRouteConflicts(routes) {
|
|
30
|
+
const pathGroups = new Map();
|
|
31
|
+
// 按路径分组
|
|
32
|
+
for (const route of routes) {
|
|
33
|
+
const path = route.fullPath || route.path;
|
|
34
|
+
const method = route.method || "GET";
|
|
35
|
+
if (!pathGroups.has(path)) {
|
|
36
|
+
pathGroups.set(path, []);
|
|
37
|
+
}
|
|
38
|
+
pathGroups.get(path).push({ ...route, method });
|
|
39
|
+
}
|
|
40
|
+
// 检查冲突
|
|
41
|
+
for (const [path, routeList] of pathGroups) {
|
|
42
|
+
if (routeList.length > 1) {
|
|
43
|
+
const methods = routeList.map((r) => r.method);
|
|
44
|
+
const uniqueMethods = [...new Set(methods)];
|
|
45
|
+
if (uniqueMethods.length === 1) {
|
|
46
|
+
// 相同路径、相同方法 - 这是冲突!
|
|
47
|
+
console.warn(`⚠️ 路由冲突: ${uniqueMethods[0]} ${path} 定义了 ${routeList.length} 次`);
|
|
48
|
+
routeList.forEach((route, index) => {
|
|
49
|
+
console.warn(` ${index + 1}. ${route.method} ${path}`);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
// 相同路径、不同方法 - 这是正常的
|
|
54
|
+
console.log(`ℹ️ 路径 ${path} 支持方法: ${uniqueMethods.join(", ")}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// 检查潜在的路径冲突(动态路由可能冲突)
|
|
59
|
+
this.detectDynamicRouteConflicts(routes);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 检测动态路由的潜在冲突
|
|
63
|
+
*/
|
|
64
|
+
detectDynamicRouteConflicts(routes) {
|
|
65
|
+
const dynamicRoutes = routes.filter((r) => {
|
|
66
|
+
const path = r.fullPath || r.path;
|
|
67
|
+
return path.includes(":") || path.includes("*");
|
|
68
|
+
});
|
|
69
|
+
for (let i = 0; i < dynamicRoutes.length; i++) {
|
|
70
|
+
for (let j = i + 1; j < dynamicRoutes.length; j++) {
|
|
71
|
+
const route1 = dynamicRoutes[i];
|
|
72
|
+
const route2 = dynamicRoutes[j];
|
|
73
|
+
const method1 = route1.method || "GET";
|
|
74
|
+
const method2 = route2.method || "GET";
|
|
75
|
+
if (method1 === method2) {
|
|
76
|
+
const path1 = route1.fullPath || route1.path;
|
|
77
|
+
const path2 = route2.fullPath || route2.path;
|
|
78
|
+
// 检查路径是否可能冲突
|
|
79
|
+
if (this.pathsMayConflict(path1, path2)) {
|
|
80
|
+
console.warn(`⚠️ 潜在路由冲突: ${method1} ${path1} 可能与 ${path2} 冲突`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 判断两个路径是否可能冲突
|
|
88
|
+
*/
|
|
89
|
+
pathsMayConflict(path1, path2) {
|
|
90
|
+
const parts1 = path1.split("/").filter(Boolean);
|
|
91
|
+
const parts2 = path2.split("/").filter(Boolean);
|
|
92
|
+
if (parts1.length !== parts2.length)
|
|
93
|
+
return false;
|
|
94
|
+
for (let i = 0; i < parts1.length; i++) {
|
|
95
|
+
const p1 = parts1[i];
|
|
96
|
+
const p2 = parts2[i];
|
|
97
|
+
// 如果两个部分都是静态的且不同,则不会冲突
|
|
98
|
+
if (!p1.startsWith(":") &&
|
|
99
|
+
!p1.startsWith("*") &&
|
|
100
|
+
!p2.startsWith(":") &&
|
|
101
|
+
!p2.startsWith("*") &&
|
|
102
|
+
p1 !== p2) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
// 如果一个是通配符,另一个是动态参数,可能冲突
|
|
106
|
+
if ((p1 === "*" && p2.startsWith(":")) ||
|
|
107
|
+
(p2 === "*" && p1.startsWith(":"))) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 路径匹配
|
|
115
|
+
*/
|
|
116
|
+
matchPath(pattern, path) {
|
|
117
|
+
const patternParts = pattern.split("/").filter(Boolean);
|
|
118
|
+
const pathParts = path.split("/").filter(Boolean);
|
|
119
|
+
if (patternParts.length !== pathParts.length) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
123
|
+
if (patternParts[i] !== pathParts[i] &&
|
|
124
|
+
!patternParts[i].startsWith(":")) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* 提取路径参数
|
|
132
|
+
*/
|
|
133
|
+
extractParams(pattern, path) {
|
|
134
|
+
const params = {};
|
|
135
|
+
const patternParts = pattern.split("/").filter(Boolean);
|
|
136
|
+
const pathParts = path.split("/").filter(Boolean);
|
|
137
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
138
|
+
if (patternParts[i].startsWith(":")) {
|
|
139
|
+
const paramName = patternParts[i].slice(1);
|
|
140
|
+
params[paramName] = pathParts[i];
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return params;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ComponentRoute, NestedComponentRoute } from "../types/component-route";
|
|
2
|
+
import { BaseServer } from "./base-server";
|
|
3
|
+
import { DependencyManager } from "../utils/dependency-manager";
|
|
4
|
+
/**
|
|
5
|
+
* 组件路由服务器
|
|
6
|
+
* 专门处理声明式组件路由
|
|
7
|
+
*/
|
|
8
|
+
export declare class ComponentServer extends BaseServer {
|
|
9
|
+
private routes;
|
|
10
|
+
private dependencyManager;
|
|
11
|
+
constructor(routes: (ComponentRoute | NestedComponentRoute)[]);
|
|
12
|
+
/**
|
|
13
|
+
* 处理请求
|
|
14
|
+
*/
|
|
15
|
+
fetch(req: Request): Promise<Response>;
|
|
16
|
+
/**
|
|
17
|
+
* 执行中间件链
|
|
18
|
+
*/
|
|
19
|
+
private executeMiddlewareChain;
|
|
20
|
+
/**
|
|
21
|
+
* 渲染 Vue 组件
|
|
22
|
+
*/
|
|
23
|
+
private renderVueComponent;
|
|
24
|
+
/**
|
|
25
|
+
* 渲染 React 组件
|
|
26
|
+
*/
|
|
27
|
+
private renderReactComponent;
|
|
28
|
+
/**
|
|
29
|
+
* 获取依赖管理器(用于外部访问)
|
|
30
|
+
*/
|
|
31
|
+
getDependencyManager(): DependencyManager;
|
|
32
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { flattenComponentRoutes } from "../middleware/component-router";
|
|
2
|
+
import { BaseServer } from "./base-server";
|
|
3
|
+
import { PathMatcher } from "../utils/path-matcher";
|
|
4
|
+
import { HtmlRenderer } from "../utils/html-renderer";
|
|
5
|
+
import { DependencyManager } from "../utils/dependency-manager";
|
|
6
|
+
/**
|
|
7
|
+
* 组件路由服务器
|
|
8
|
+
* 专门处理声明式组件路由
|
|
9
|
+
*/
|
|
10
|
+
export class ComponentServer extends BaseServer {
|
|
11
|
+
routes;
|
|
12
|
+
dependencyManager;
|
|
13
|
+
constructor(routes) {
|
|
14
|
+
super();
|
|
15
|
+
this.routes = flattenComponentRoutes(routes);
|
|
16
|
+
this.dependencyManager = new DependencyManager();
|
|
17
|
+
// 检测路由冲突
|
|
18
|
+
this.detectRouteConflicts(this.routes);
|
|
19
|
+
this.logFlattenedRoutes(this.routes, "组件路由");
|
|
20
|
+
console.log("🚀 依赖按需加载,服务器启动完成");
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 处理请求
|
|
24
|
+
*/
|
|
25
|
+
async fetch(req) {
|
|
26
|
+
const url = new URL(req.url);
|
|
27
|
+
const pathname = url.pathname;
|
|
28
|
+
const method = req.method;
|
|
29
|
+
// 只支持 GET 请求
|
|
30
|
+
if (method !== "GET") {
|
|
31
|
+
return new Response("Method Not Allowed", { status: 405 });
|
|
32
|
+
}
|
|
33
|
+
// 查找匹配的路由
|
|
34
|
+
let matchedRoute = null;
|
|
35
|
+
for (const route of this.routes) {
|
|
36
|
+
if (PathMatcher.matchPath(route.fullPath, pathname)) {
|
|
37
|
+
matchedRoute = route;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!matchedRoute) {
|
|
42
|
+
return new Response("Not Found", { status: 404 });
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
// 创建中间件上下文
|
|
46
|
+
const context = {
|
|
47
|
+
req,
|
|
48
|
+
params: PathMatcher.extractParams(matchedRoute.fullPath, pathname),
|
|
49
|
+
query: Object.fromEntries(url.searchParams),
|
|
50
|
+
pathname,
|
|
51
|
+
};
|
|
52
|
+
// 执行中间件链,中间件会处理组件渲染
|
|
53
|
+
return await this.executeMiddlewareChain(matchedRoute.middlewareChain, context, matchedRoute.component);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.error("组件渲染失败:", error);
|
|
57
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 执行中间件链
|
|
62
|
+
*/
|
|
63
|
+
async executeMiddlewareChain(middlewareChain, context, componentImport) {
|
|
64
|
+
// 创建最终的渲染函数
|
|
65
|
+
const renderComponent = async () => {
|
|
66
|
+
const componentModule = await componentImport();
|
|
67
|
+
const component = componentModule.default || componentModule;
|
|
68
|
+
// 自动检测组件类型
|
|
69
|
+
const componentType = this.dependencyManager.detectComponentType(component);
|
|
70
|
+
// 按需加载依赖
|
|
71
|
+
const deps = await this.dependencyManager.getFrameworkDeps(componentType);
|
|
72
|
+
// 根据组件类型渲染
|
|
73
|
+
if (componentType === "vue") {
|
|
74
|
+
return await this.renderVueComponent(component, context, deps);
|
|
75
|
+
}
|
|
76
|
+
else if (componentType === "react") {
|
|
77
|
+
return await this.renderReactComponent(component, context, deps);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
throw new Error(`不支持的组件类型: ${componentType}`);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
// 执行中间件链
|
|
84
|
+
let index = 0;
|
|
85
|
+
const next = async () => {
|
|
86
|
+
if (index >= middlewareChain.length) {
|
|
87
|
+
return await renderComponent();
|
|
88
|
+
}
|
|
89
|
+
const middleware = middlewareChain[index++];
|
|
90
|
+
return await middleware(context.req, next);
|
|
91
|
+
};
|
|
92
|
+
return await next();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 渲染 Vue 组件
|
|
96
|
+
*/
|
|
97
|
+
async renderVueComponent(component, context, deps) {
|
|
98
|
+
try {
|
|
99
|
+
const [vue, renderer] = deps;
|
|
100
|
+
const app = vue.createSSRApp(component);
|
|
101
|
+
// 提供路由信息
|
|
102
|
+
app.provide("routeInfo", {
|
|
103
|
+
params: context.params || {},
|
|
104
|
+
query: context.query || {},
|
|
105
|
+
pathname: context.pathname,
|
|
106
|
+
});
|
|
107
|
+
const html = await renderer.renderToString(app);
|
|
108
|
+
const fullHtml = HtmlRenderer.generateVueHtml(html, context);
|
|
109
|
+
return new Response(fullHtml, {
|
|
110
|
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
console.error("Vue 组件渲染失败:", error);
|
|
115
|
+
return new Response("Vue Component Render Error", { status: 500 });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 渲染 React 组件
|
|
120
|
+
*/
|
|
121
|
+
async renderReactComponent(component, context, deps) {
|
|
122
|
+
try {
|
|
123
|
+
const [react, renderer] = deps;
|
|
124
|
+
const content = react.createElement(component, {
|
|
125
|
+
req: context.req,
|
|
126
|
+
params: context.params || {},
|
|
127
|
+
query: context.query || {},
|
|
128
|
+
});
|
|
129
|
+
const html = renderer.renderToString(content);
|
|
130
|
+
const fullHtml = HtmlRenderer.generateReactHtml(html, context);
|
|
131
|
+
return new Response(fullHtml, {
|
|
132
|
+
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
console.error("React 组件渲染失败:", error);
|
|
137
|
+
return new Response("React Component Render Error", { status: 500 });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 获取依赖管理器(用于外部访问)
|
|
142
|
+
*/
|
|
143
|
+
getDependencyManager() {
|
|
144
|
+
return this.dependencyManager;
|
|
145
|
+
}
|
|
146
|
+
}
|