vafast 0.1.17 → 0.2.3
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,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 类型安全的路由处理器工厂
|
|
3
|
+
*
|
|
4
|
+
* 非柯里化设计,API 更简洁
|
|
5
|
+
*
|
|
6
|
+
* @author Framework Team
|
|
7
|
+
* @version 3.0.0
|
|
8
|
+
* @license MIT
|
|
9
|
+
*/
|
|
10
|
+
import { parseBody, parseQuery, parseHeaders, parseCookies } from "./parsers";
|
|
11
|
+
import { goAwait } from "./go-await";
|
|
12
|
+
import { json } from "./response";
|
|
13
|
+
import { validateAllSchemasUltra, precompileSchemasUltra, } from "./validators/schema-validators-ultra";
|
|
14
|
+
/**
|
|
15
|
+
* 自动响应转换
|
|
16
|
+
* 将各种返回值类型转换为 Response 对象
|
|
17
|
+
*/
|
|
18
|
+
function autoResponse(result) {
|
|
19
|
+
// 已经是 Response
|
|
20
|
+
if (result instanceof Response) {
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
// null/undefined -> 204
|
|
24
|
+
if (result === null || result === undefined) {
|
|
25
|
+
return new Response(null, { status: 204 });
|
|
26
|
+
}
|
|
27
|
+
// 字符串 -> text/plain
|
|
28
|
+
if (typeof result === "string") {
|
|
29
|
+
return new Response(result, {
|
|
30
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
// 数字/布尔 -> text/plain
|
|
34
|
+
if (typeof result === "number" || typeof result === "boolean") {
|
|
35
|
+
return new Response(String(result), {
|
|
36
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" },
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
// 对象 -> 检查是否是 { data, status, headers } 格式
|
|
40
|
+
if (typeof result === "object") {
|
|
41
|
+
const obj = result;
|
|
42
|
+
if ("data" in obj && ("status" in obj || "headers" in obj)) {
|
|
43
|
+
const { data, status = 200, headers = {} } = obj;
|
|
44
|
+
if (data === null || data === undefined) {
|
|
45
|
+
return new Response(null, {
|
|
46
|
+
status: status === 200 ? 204 : status,
|
|
47
|
+
headers: headers,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (typeof data === "string" ||
|
|
51
|
+
typeof data === "number" ||
|
|
52
|
+
typeof data === "boolean") {
|
|
53
|
+
return new Response(String(data), {
|
|
54
|
+
status: status,
|
|
55
|
+
headers: {
|
|
56
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
57
|
+
...headers,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return json(data, status, headers);
|
|
62
|
+
}
|
|
63
|
+
// 普通对象 -> JSON
|
|
64
|
+
return json(result);
|
|
65
|
+
}
|
|
66
|
+
// 其他类型 -> 204
|
|
67
|
+
return new Response(null, { status: 204 });
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 处理验证错误
|
|
71
|
+
*/
|
|
72
|
+
function handleValidationError(error) {
|
|
73
|
+
return json({
|
|
74
|
+
success: false,
|
|
75
|
+
error: "Validation Error",
|
|
76
|
+
message: error.message,
|
|
77
|
+
timestamp: new Date().toISOString(),
|
|
78
|
+
}, 400);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 处理内部错误
|
|
82
|
+
*/
|
|
83
|
+
function handleInternalError(error) {
|
|
84
|
+
return json({
|
|
85
|
+
success: false,
|
|
86
|
+
error: "Internal Error",
|
|
87
|
+
message: error instanceof Error ? error.message : "未知错误",
|
|
88
|
+
}, 500);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 判断是否为 handler 函数
|
|
92
|
+
*/
|
|
93
|
+
function isHandler(value) {
|
|
94
|
+
return typeof value === "function";
|
|
95
|
+
}
|
|
96
|
+
// 实现
|
|
97
|
+
export function createHandler(schemaOrHandler, maybeHandler) {
|
|
98
|
+
// 判断调用方式
|
|
99
|
+
const hasSchema = !isHandler(schemaOrHandler);
|
|
100
|
+
const schema = hasSchema ? schemaOrHandler : {};
|
|
101
|
+
const handler = hasSchema
|
|
102
|
+
? maybeHandler
|
|
103
|
+
: schemaOrHandler;
|
|
104
|
+
// 预编译 schema
|
|
105
|
+
if (schema.body ||
|
|
106
|
+
schema.query ||
|
|
107
|
+
schema.params ||
|
|
108
|
+
schema.headers ||
|
|
109
|
+
schema.cookies) {
|
|
110
|
+
precompileSchemasUltra(schema);
|
|
111
|
+
}
|
|
112
|
+
return async (req) => {
|
|
113
|
+
try {
|
|
114
|
+
// 解析请求数据
|
|
115
|
+
const query = parseQuery(req);
|
|
116
|
+
const headers = parseHeaders(req);
|
|
117
|
+
const cookies = parseCookies(req);
|
|
118
|
+
const params = req.params || {};
|
|
119
|
+
// 解析请求体
|
|
120
|
+
let body = undefined;
|
|
121
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
122
|
+
const [, parsedBody] = await goAwait(parseBody(req));
|
|
123
|
+
body = parsedBody;
|
|
124
|
+
}
|
|
125
|
+
// 验证 schema
|
|
126
|
+
const data = { body, query, params, headers, cookies };
|
|
127
|
+
if (schema.body ||
|
|
128
|
+
schema.query ||
|
|
129
|
+
schema.params ||
|
|
130
|
+
schema.headers ||
|
|
131
|
+
schema.cookies) {
|
|
132
|
+
validateAllSchemasUltra(schema, data);
|
|
133
|
+
}
|
|
134
|
+
// 调用 handler
|
|
135
|
+
const result = await handler({
|
|
136
|
+
req,
|
|
137
|
+
body: body,
|
|
138
|
+
query: query,
|
|
139
|
+
params: params,
|
|
140
|
+
headers: headers,
|
|
141
|
+
cookies: cookies,
|
|
142
|
+
});
|
|
143
|
+
return autoResponse(result);
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
if (error instanceof Error && error.message.includes("验证失败")) {
|
|
147
|
+
return handleValidationError(error);
|
|
148
|
+
}
|
|
149
|
+
return handleInternalError(error);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
// 实现
|
|
154
|
+
export function createHandlerWithExtra(schemaOrHandler, maybeHandler) {
|
|
155
|
+
// 判断调用方式
|
|
156
|
+
const hasSchema = !isHandler(schemaOrHandler);
|
|
157
|
+
const schema = hasSchema ? schemaOrHandler : {};
|
|
158
|
+
const handler = hasSchema
|
|
159
|
+
? maybeHandler
|
|
160
|
+
: schemaOrHandler;
|
|
161
|
+
// 预编译 schema
|
|
162
|
+
if (schema.body ||
|
|
163
|
+
schema.query ||
|
|
164
|
+
schema.params ||
|
|
165
|
+
schema.headers ||
|
|
166
|
+
schema.cookies) {
|
|
167
|
+
precompileSchemasUltra(schema);
|
|
168
|
+
}
|
|
169
|
+
return async (req) => {
|
|
170
|
+
try {
|
|
171
|
+
// 解析请求数据
|
|
172
|
+
const query = parseQuery(req);
|
|
173
|
+
const headers = parseHeaders(req);
|
|
174
|
+
const cookies = parseCookies(req);
|
|
175
|
+
const params = req.params || {};
|
|
176
|
+
// 解析请求体
|
|
177
|
+
let body = undefined;
|
|
178
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
179
|
+
const [, parsedBody] = await goAwait(parseBody(req));
|
|
180
|
+
body = parsedBody;
|
|
181
|
+
}
|
|
182
|
+
// 验证 schema
|
|
183
|
+
const data = { body, query, params, headers, cookies };
|
|
184
|
+
if (schema.body ||
|
|
185
|
+
schema.query ||
|
|
186
|
+
schema.params ||
|
|
187
|
+
schema.headers ||
|
|
188
|
+
schema.cookies) {
|
|
189
|
+
validateAllSchemasUltra(schema, data);
|
|
190
|
+
}
|
|
191
|
+
// 获取中间件注入的额外数据
|
|
192
|
+
const extras = (req.__locals ??
|
|
193
|
+
{});
|
|
194
|
+
// 调用 handler
|
|
195
|
+
const result = await handler({
|
|
196
|
+
req,
|
|
197
|
+
body: body,
|
|
198
|
+
query: query,
|
|
199
|
+
params: params,
|
|
200
|
+
headers: headers,
|
|
201
|
+
cookies: cookies,
|
|
202
|
+
...extras,
|
|
203
|
+
});
|
|
204
|
+
return autoResponse(result);
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
if (error instanceof Error && error.message.includes("验证失败")) {
|
|
208
|
+
return handleValidationError(error);
|
|
209
|
+
}
|
|
210
|
+
return handleInternalError(error);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 简单的路由处理器 (无 schema 验证,只有 req)
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* simpleHandler(({ req }) => {
|
|
220
|
+
* return { message: "Hello World" };
|
|
221
|
+
* })
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
export function simpleHandler(handler) {
|
|
225
|
+
return async (req) => {
|
|
226
|
+
try {
|
|
227
|
+
const result = await handler({ req });
|
|
228
|
+
return autoResponse(result);
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
return handleInternalError(error);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 依赖管理器
|
|
3
|
+
* 负责按需加载和管理框架依赖
|
|
4
|
+
*/
|
|
5
|
+
export declare class DependencyManager {
|
|
6
|
+
private dependencyCache;
|
|
7
|
+
/**
|
|
8
|
+
* 按需获取框架依赖
|
|
9
|
+
*/
|
|
10
|
+
getFrameworkDeps(framework: "vue" | "react"): Promise<any>;
|
|
11
|
+
/**
|
|
12
|
+
* 检测组件类型
|
|
13
|
+
*/
|
|
14
|
+
detectComponentType(component: any): "vue" | "react";
|
|
15
|
+
/**
|
|
16
|
+
* 清除缓存
|
|
17
|
+
*/
|
|
18
|
+
clearCache(): void;
|
|
19
|
+
/**
|
|
20
|
+
* 获取缓存状态
|
|
21
|
+
*/
|
|
22
|
+
getCacheStatus(): Record<string, boolean>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 依赖管理器
|
|
3
|
+
* 负责按需加载和管理框架依赖
|
|
4
|
+
*/
|
|
5
|
+
export class DependencyManager {
|
|
6
|
+
dependencyCache = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* 按需获取框架依赖
|
|
9
|
+
*/
|
|
10
|
+
async getFrameworkDeps(framework) {
|
|
11
|
+
if (this.dependencyCache.has(framework)) {
|
|
12
|
+
return this.dependencyCache.get(framework);
|
|
13
|
+
}
|
|
14
|
+
console.log(`📦 按需加载 ${framework} 依赖...`);
|
|
15
|
+
try {
|
|
16
|
+
let deps;
|
|
17
|
+
switch (framework) {
|
|
18
|
+
case "vue":
|
|
19
|
+
deps = await Promise.all([
|
|
20
|
+
import("vue"),
|
|
21
|
+
import("@vue/server-renderer"),
|
|
22
|
+
]);
|
|
23
|
+
break;
|
|
24
|
+
case "react":
|
|
25
|
+
deps = await Promise.all([
|
|
26
|
+
import("react"),
|
|
27
|
+
import("react-dom/server"),
|
|
28
|
+
]);
|
|
29
|
+
break;
|
|
30
|
+
default:
|
|
31
|
+
throw new Error(`不支持的框架: ${framework}`);
|
|
32
|
+
}
|
|
33
|
+
this.dependencyCache.set(framework, deps);
|
|
34
|
+
console.log(`✅ ${framework} 依赖加载完成`);
|
|
35
|
+
return deps;
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error(`❌ ${framework} 依赖加载失败:`, error);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 检测组件类型
|
|
44
|
+
*/
|
|
45
|
+
detectComponentType(component) {
|
|
46
|
+
// 简单的组件类型检测
|
|
47
|
+
if (component.render && typeof component.render === "function") {
|
|
48
|
+
return "vue";
|
|
49
|
+
}
|
|
50
|
+
if (component.$$typeof) {
|
|
51
|
+
return "react";
|
|
52
|
+
}
|
|
53
|
+
// 默认使用 Vue
|
|
54
|
+
return "vue";
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 清除缓存
|
|
58
|
+
*/
|
|
59
|
+
clearCache() {
|
|
60
|
+
this.dependencyCache.clear();
|
|
61
|
+
console.log("🧹 依赖缓存已清除");
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 获取缓存状态
|
|
65
|
+
*/
|
|
66
|
+
getCacheStatus() {
|
|
67
|
+
const status = {};
|
|
68
|
+
for (const [framework] of this.dependencyCache) {
|
|
69
|
+
status[framework] = true;
|
|
70
|
+
}
|
|
71
|
+
return status;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Go 风格的错误处理工具
|
|
3
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
4
|
+
*
|
|
5
|
+
* @author Framework Team
|
|
6
|
+
* @version 1.0.0
|
|
7
|
+
* @license MIT
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Go 风格的错误处理工具
|
|
11
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
12
|
+
*
|
|
13
|
+
* @param promise 要处理的 Promise
|
|
14
|
+
* @returns [Error | null, T | undefined] 元组,第一个元素是错误,第二个是结果
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const [error, result] = await goAwait(someAsyncFunction());
|
|
19
|
+
* if (error) {
|
|
20
|
+
* console.error("操作失败:", error);
|
|
21
|
+
* } else {
|
|
22
|
+
* console.log("操作成功:", result);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function goAwait<T>(promise: Promise<T>): Promise<[Error | null, T | undefined]>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Go 风格的错误处理工具
|
|
3
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
4
|
+
*
|
|
5
|
+
* @author Framework Team
|
|
6
|
+
* @version 1.0.0
|
|
7
|
+
* @license MIT
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Go 风格的错误处理工具
|
|
11
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
12
|
+
*
|
|
13
|
+
* @param promise 要处理的 Promise
|
|
14
|
+
* @returns [Error | null, T | undefined] 元组,第一个元素是错误,第二个是结果
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const [error, result] = await goAwait(someAsyncFunction());
|
|
19
|
+
* if (error) {
|
|
20
|
+
* console.error("操作失败:", error);
|
|
21
|
+
* } else {
|
|
22
|
+
* console.log("操作成功:", result);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function goAwait(promise) {
|
|
27
|
+
return promise
|
|
28
|
+
.then((data) => [null, data])
|
|
29
|
+
.catch((err) => [err instanceof Error ? err : new Error(String(err)), undefined]);
|
|
30
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** 获取单个 Cookie 值 */
|
|
1
2
|
export declare function getCookie(req: Request, key: string): string | null;
|
|
2
3
|
/** 生成 Set-Cookie 头 */
|
|
3
4
|
export declare function setCookie(key: string, value: string, options?: {
|
|
@@ -6,3 +7,5 @@ export declare function setCookie(key: string, value: string, options?: {
|
|
|
6
7
|
maxAge?: number;
|
|
7
8
|
secure?: boolean;
|
|
8
9
|
}): string;
|
|
10
|
+
export declare function setLocals<T extends object>(req: Request, extras: T): void;
|
|
11
|
+
export declare function getLocals<T extends object>(req: Request): T;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { parseCookies } from "./parsers";
|
|
2
|
+
/** 获取单个 Cookie 值 */
|
|
3
|
+
export function getCookie(req, key) {
|
|
4
|
+
const cookies = parseCookies(req);
|
|
5
|
+
return cookies[key] || null;
|
|
6
|
+
}
|
|
7
|
+
/** 生成 Set-Cookie 头 */
|
|
8
|
+
export function setCookie(key, value, options = {}) {
|
|
9
|
+
let cookie = `${key}=${encodeURIComponent(value)}`;
|
|
10
|
+
if (options.path)
|
|
11
|
+
cookie += `; Path=${options.path}`;
|
|
12
|
+
if (options.httpOnly)
|
|
13
|
+
cookie += `; HttpOnly`;
|
|
14
|
+
if (options.secure)
|
|
15
|
+
cookie += `; Secure`;
|
|
16
|
+
if (options.maxAge)
|
|
17
|
+
cookie += `; Max-Age=${options.maxAge}`;
|
|
18
|
+
return cookie;
|
|
19
|
+
}
|
|
20
|
+
// 提供给中间件写入"局部上下文"的工具函数
|
|
21
|
+
export function setLocals(req, extras) {
|
|
22
|
+
const target = req;
|
|
23
|
+
target.__locals = { ...(target.__locals ?? {}), ...extras };
|
|
24
|
+
}
|
|
25
|
+
// 获取中间件注入的局部上下文
|
|
26
|
+
export function getLocals(req) {
|
|
27
|
+
const target = req;
|
|
28
|
+
return (target.__locals ?? {});
|
|
29
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML渲染工具类
|
|
3
|
+
* 提供统一的HTML模板生成功能
|
|
4
|
+
*/
|
|
5
|
+
export declare class HtmlRenderer {
|
|
6
|
+
/**
|
|
7
|
+
* 生成基础HTML模板
|
|
8
|
+
*/
|
|
9
|
+
static generateBaseHtml(content: string, context: any, clientScriptPath?: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* 生成Vue组件HTML
|
|
12
|
+
*/
|
|
13
|
+
static generateVueHtml(content: string, context: any, clientScriptPath?: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* 生成React组件HTML
|
|
16
|
+
*/
|
|
17
|
+
static generateReactHtml(content: string, context: any, clientScriptPath?: string): string;
|
|
18
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML渲染工具类
|
|
3
|
+
* 提供统一的HTML模板生成功能
|
|
4
|
+
*/
|
|
5
|
+
export class HtmlRenderer {
|
|
6
|
+
/**
|
|
7
|
+
* 生成基础HTML模板
|
|
8
|
+
*/
|
|
9
|
+
static generateBaseHtml(content, context, clientScriptPath = "/client.js") {
|
|
10
|
+
return `
|
|
11
|
+
<!doctype html>
|
|
12
|
+
<html>
|
|
13
|
+
<head>
|
|
14
|
+
<meta charset="utf-8">
|
|
15
|
+
<title>Vafast SSR App</title>
|
|
16
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
17
|
+
</head>
|
|
18
|
+
<body>
|
|
19
|
+
<div id="app">${content}</div>
|
|
20
|
+
<script>
|
|
21
|
+
window.__ROUTE_INFO__ = {
|
|
22
|
+
params: ${JSON.stringify(context.params || {})},
|
|
23
|
+
query: ${JSON.stringify(context.query || {})},
|
|
24
|
+
pathname: '${context.pathname}'
|
|
25
|
+
};
|
|
26
|
+
</script>
|
|
27
|
+
<script type="module" src="${clientScriptPath}"></script>
|
|
28
|
+
</body>
|
|
29
|
+
</html>
|
|
30
|
+
`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 生成Vue组件HTML
|
|
34
|
+
*/
|
|
35
|
+
static generateVueHtml(content, context, clientScriptPath = "/client.js") {
|
|
36
|
+
return this.generateBaseHtml(content, context, clientScriptPath);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 生成React组件HTML
|
|
40
|
+
*/
|
|
41
|
+
static generateReactHtml(content, context, clientScriptPath = "/client.js") {
|
|
42
|
+
return `
|
|
43
|
+
<!doctype html>
|
|
44
|
+
<html>
|
|
45
|
+
<head>
|
|
46
|
+
<meta charset="utf-8">
|
|
47
|
+
<title>Vafast SSR App</title>
|
|
48
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
49
|
+
</head>
|
|
50
|
+
<body>
|
|
51
|
+
<div id="root">${content}</div>
|
|
52
|
+
<script>
|
|
53
|
+
window.__ROUTE_INFO__ = {
|
|
54
|
+
params: ${JSON.stringify(context.params || {})},
|
|
55
|
+
query: ${JSON.stringify(context.query || {})},
|
|
56
|
+
pathname: '${context.pathname}'
|
|
57
|
+
};
|
|
58
|
+
</script>
|
|
59
|
+
<script type="module" src="${clientScriptPath}"></script>
|
|
60
|
+
</body>
|
|
61
|
+
</html>
|
|
62
|
+
`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具函数模块导出
|
|
3
|
+
*/
|
|
4
|
+
export { createHandler, createHandlerWithExtra, simpleHandler, } from "./create-handler";
|
|
5
|
+
export { parseBody, parseQuery, parseHeaders, parseCookies } from "./parsers";
|
|
6
|
+
export { json, text, html, redirect, empty, stream } from "./response";
|
|
7
|
+
export { goAwait } from "./go-await";
|
|
8
|
+
export { base64urlEncode, base64urlDecode } from "./base64url";
|
|
9
|
+
export { setLocals, getLocals } from "./handle";
|
|
10
|
+
export { parseRequest, validateRequest, parseAndValidateRequest, createRequestValidator, } from "./request-validator";
|
|
11
|
+
export { HtmlRenderer } from "./html-renderer";
|
|
12
|
+
export { DependencyManager } from "./dependency-manager";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具函数模块导出
|
|
3
|
+
*/
|
|
4
|
+
// 处理器工厂
|
|
5
|
+
export { createHandler, createHandlerWithExtra, simpleHandler, } from "./create-handler";
|
|
6
|
+
// 请求解析
|
|
7
|
+
export { parseBody, parseQuery, parseHeaders, parseCookies } from "./parsers";
|
|
8
|
+
// 响应工具
|
|
9
|
+
export { json, text, html, redirect, empty, stream } from "./response";
|
|
10
|
+
// Go 风格错误处理
|
|
11
|
+
export { goAwait } from "./go-await";
|
|
12
|
+
// Base64 编码
|
|
13
|
+
export { base64urlEncode, base64urlDecode } from "./base64url";
|
|
14
|
+
// 请求上下文
|
|
15
|
+
export { setLocals, getLocals } from "./handle";
|
|
16
|
+
// 请求验证
|
|
17
|
+
export { parseRequest, validateRequest, parseAndValidateRequest, createRequestValidator, } from "./request-validator";
|
|
18
|
+
// HTML 渲染 (SSR)
|
|
19
|
+
export { HtmlRenderer } from "./html-renderer";
|
|
20
|
+
// 依赖管理
|
|
21
|
+
export { DependencyManager } from "./dependency-manager";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface FileInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
type: string;
|
|
4
|
+
size: number;
|
|
5
|
+
data: ArrayBuffer;
|
|
6
|
+
}
|
|
7
|
+
export interface FormData {
|
|
8
|
+
fields: Record<string, string>;
|
|
9
|
+
files: Record<string, FileInfo>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 简化的请求体解析函数
|
|
13
|
+
* 优先简洁性,处理最常见的场景
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseBody(req: Request): Promise<unknown>;
|
|
16
|
+
/**
|
|
17
|
+
* 解析请求体为特定类型
|
|
18
|
+
* 提供类型安全的解析方法
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseBodyAs<T>(req: Request): Promise<T>;
|
|
21
|
+
/**
|
|
22
|
+
* 解析请求体为表单数据
|
|
23
|
+
* 专门用于处理 multipart/form-data
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseFormData(req: Request): Promise<FormData>;
|
|
26
|
+
/**
|
|
27
|
+
* 解析请求体为文件
|
|
28
|
+
* 专门用于处理文件上传
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseFile(req: Request): Promise<FileInfo>;
|
|
31
|
+
/** 获取查询字符串,直接返回对象 */
|
|
32
|
+
export declare function parseQuery(req: Request): Record<string, any>;
|
|
33
|
+
/** 解析请求头,返回对象 */
|
|
34
|
+
export declare function parseHeaders(req: Request): Record<string, string>;
|
|
35
|
+
/** 使用cookie库解析Cookie,保证可靠性 */
|
|
36
|
+
export declare function parseCookies(req: Request): Record<string, string>;
|