vafast 0.1.11 → 0.1.12
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/auth/token.d.ts +40 -0
- package/dist/auth/token.js +124 -0
- package/dist/defineRoute.d.ts +2 -0
- package/dist/defineRoute.js +3 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware/auth.d.ts +14 -0
- package/dist/middleware/auth.js +106 -0
- package/dist/middleware/authMiddleware.d.ts +2 -0
- package/dist/middleware/authMiddleware.js +13 -0
- package/dist/middleware/component-renderer.d.ts +6 -0
- package/dist/middleware/component-renderer.js +136 -0
- package/dist/middleware/component-router.d.ts +10 -0
- package/dist/middleware/component-router.js +39 -0
- package/dist/middleware/cors.d.ts +9 -0
- package/dist/middleware/cors.js +30 -0
- package/dist/middleware/rateLimit.d.ts +8 -0
- package/dist/middleware/rateLimit.js +33 -0
- package/dist/middleware.d.ts +18 -0
- package/dist/middleware.js +51 -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.d.ts +17 -0
- package/dist/router.js +74 -0
- package/dist/server/base-server.d.ts +38 -0
- package/dist/server/base-server.js +167 -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 +9 -0
- package/dist/server/server-factory.d.ts +42 -0
- package/dist/server/server-factory.js +70 -0
- package/dist/server/server.d.ts +7 -0
- package/dist/server/server.js +73 -0
- package/dist/types/component-route.d.ts +25 -0
- package/dist/types/component-route.js +1 -0
- package/dist/types/route.d.ts +37 -0
- package/dist/types/route.js +1 -0
- package/dist/types.d.ts +18 -0
- package/dist/types.js +1 -0
- package/dist/utils/base64url.d.ts +2 -0
- package/dist/utils/base64url.js +11 -0
- package/dist/utils/dependency-manager.d.ts +23 -0
- package/dist/utils/dependency-manager.js +67 -0
- package/dist/utils/go-await.d.ts +26 -0
- package/dist/utils/go-await.js +33 -0
- package/dist/utils/handle.d.ts +10 -0
- package/dist/utils/handle.js +24 -0
- package/dist/utils/html-renderer.d.ts +18 -0
- package/dist/utils/html-renderer.js +64 -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 +82 -0
- package/dist/utils/request-validator.d.ts +63 -0
- package/dist/utils/request-validator.js +94 -0
- package/dist/utils/response.d.ts +12 -0
- package/dist/utils/response.js +69 -0
- package/dist/utils/route-handler-factory.d.ts +50 -0
- package/dist/utils/route-handler-factory.js +181 -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 +3 -4
- package/dist/index.cjs +0 -51
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -460
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路由处理器工厂
|
|
3
|
+
*
|
|
4
|
+
* 负责创建和配置路由处理器,处理请求数据解析和验证
|
|
5
|
+
* 提供统一的错误处理和响应格式
|
|
6
|
+
*
|
|
7
|
+
* @author Framework Team
|
|
8
|
+
* @version 1.0.0
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
import { parseQuery, parseHeaders, parseCookies, parseBody } from "./parsers";
|
|
12
|
+
import { goAwait } from "./go-await";
|
|
13
|
+
import { json } from "./response";
|
|
14
|
+
import { validateAllSchemasUltra, precompileSchemasUltra, } from "./validators/schema-validators-ultra";
|
|
15
|
+
// 默认验证错误处理器
|
|
16
|
+
const defaultValidationErrorHandler = (error, field, value, schema) => {
|
|
17
|
+
return json({
|
|
18
|
+
success: false,
|
|
19
|
+
error: "Validation Error",
|
|
20
|
+
message: error instanceof Error ? error.message : "验证失败",
|
|
21
|
+
field,
|
|
22
|
+
receivedValue: value,
|
|
23
|
+
timestamp: new Date().toISOString(),
|
|
24
|
+
}, 400);
|
|
25
|
+
};
|
|
26
|
+
// 预定义的常用响应头,避免重复创建
|
|
27
|
+
const TEXT_HEADERS = { "Content-Type": "text/plain; charset=utf-8" };
|
|
28
|
+
const JSON_HEADERS = { "Content-Type": "application/json" };
|
|
29
|
+
const EMPTY_RESPONSE_204 = new Response(null, { status: 204 });
|
|
30
|
+
// 超高性能的 Response 自动转换函数 - 生产环境推荐使用
|
|
31
|
+
function autoResponseUltra(result) {
|
|
32
|
+
// 快速路径:已经是 Response 对象
|
|
33
|
+
if (result instanceof Response) {
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
// 快速路径:null/undefined - 复用预创建的对象
|
|
37
|
+
if (result === null || result === undefined) {
|
|
38
|
+
return EMPTY_RESPONSE_204;
|
|
39
|
+
}
|
|
40
|
+
// 使用 switch 语句优化类型检查
|
|
41
|
+
switch (typeof result) {
|
|
42
|
+
case "string":
|
|
43
|
+
// 优化:复用预定义的头部对象
|
|
44
|
+
return new Response(result, { headers: TEXT_HEADERS });
|
|
45
|
+
case "number":
|
|
46
|
+
case "boolean":
|
|
47
|
+
// 优化:使用更高效的字符串转换,复用头部
|
|
48
|
+
return new Response(result.toString(), { headers: TEXT_HEADERS });
|
|
49
|
+
case "object":
|
|
50
|
+
// 检查是否是 { data, status, headers } 格式
|
|
51
|
+
if ("data" in result) {
|
|
52
|
+
const { data, status = 200, headers = {} } = result;
|
|
53
|
+
// 无内容
|
|
54
|
+
if (data === null || data === undefined) {
|
|
55
|
+
return new Response("", {
|
|
56
|
+
status: status === 200 ? 204 : status,
|
|
57
|
+
headers,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
// 纯文本类型
|
|
61
|
+
if (typeof data === "string" ||
|
|
62
|
+
typeof data === "number" ||
|
|
63
|
+
typeof data === "boolean") {
|
|
64
|
+
// 优化:减少对象展开操作,直接构建最终对象
|
|
65
|
+
const finalHeaders = {
|
|
66
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
67
|
+
...headers,
|
|
68
|
+
};
|
|
69
|
+
return new Response(data.toString(), {
|
|
70
|
+
status,
|
|
71
|
+
headers: finalHeaders,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
// JSON 类型 - 复用 json 函数
|
|
75
|
+
return json(data, status, headers);
|
|
76
|
+
}
|
|
77
|
+
// 普通对象/数组,复用 json 函数
|
|
78
|
+
return json(result);
|
|
79
|
+
default:
|
|
80
|
+
// 其他类型(如 symbol, function 等)
|
|
81
|
+
return EMPTY_RESPONSE_204;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// 创建路由处理器的通用函数
|
|
85
|
+
export function createRouteHandler(handler, config = {}) {
|
|
86
|
+
// 检查哪些验证器是必需的
|
|
87
|
+
const hasBodySchema = config.body !== undefined;
|
|
88
|
+
const hasQuerySchema = config.query !== undefined;
|
|
89
|
+
const hasParamsSchema = config.params !== undefined;
|
|
90
|
+
const hasHeadersSchema = config.headers !== undefined;
|
|
91
|
+
const hasCookiesSchema = config.cookies !== undefined;
|
|
92
|
+
// 只在有验证器时预编译Schema
|
|
93
|
+
if (hasBodySchema ||
|
|
94
|
+
hasQuerySchema ||
|
|
95
|
+
hasParamsSchema ||
|
|
96
|
+
hasHeadersSchema ||
|
|
97
|
+
hasCookiesSchema) {
|
|
98
|
+
precompileSchemasUltra(config);
|
|
99
|
+
}
|
|
100
|
+
// 获取验证错误处理器
|
|
101
|
+
const errorHandler = config.validationErrorHandler || defaultValidationErrorHandler;
|
|
102
|
+
return async (req) => {
|
|
103
|
+
try {
|
|
104
|
+
let queryObj = {};
|
|
105
|
+
let headers = {};
|
|
106
|
+
let cookies = {};
|
|
107
|
+
let body = undefined;
|
|
108
|
+
let params = {};
|
|
109
|
+
// 默认总是解析所有数据,只有在有验证器时才进行验证
|
|
110
|
+
queryObj = parseQuery(req);
|
|
111
|
+
headers = parseHeaders(req);
|
|
112
|
+
cookies = parseCookies(req);
|
|
113
|
+
// 总是解析 body
|
|
114
|
+
const [, parsedBody] = await goAwait(parseBody(req));
|
|
115
|
+
body = parsedBody;
|
|
116
|
+
// 总是尝试获取路径参数
|
|
117
|
+
params = (req.pathParams ||
|
|
118
|
+
req.params ||
|
|
119
|
+
{});
|
|
120
|
+
// 只在有验证器时执行验证
|
|
121
|
+
if (hasBodySchema ||
|
|
122
|
+
hasQuerySchema ||
|
|
123
|
+
hasParamsSchema ||
|
|
124
|
+
hasHeadersSchema ||
|
|
125
|
+
hasCookiesSchema) {
|
|
126
|
+
const data = { body, query: queryObj, params, headers, cookies };
|
|
127
|
+
validateAllSchemasUltra(config, data);
|
|
128
|
+
}
|
|
129
|
+
// 合并中间件注入的本地上下文
|
|
130
|
+
const extras = (req.__locals ?? {});
|
|
131
|
+
// 调用处理器,传递上下文
|
|
132
|
+
const result = await handler({
|
|
133
|
+
req,
|
|
134
|
+
body,
|
|
135
|
+
query: queryObj,
|
|
136
|
+
params,
|
|
137
|
+
headers,
|
|
138
|
+
cookies,
|
|
139
|
+
...extras,
|
|
140
|
+
});
|
|
141
|
+
return autoResponseUltra(result);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
// 使用用户自定义的验证错误处理器
|
|
145
|
+
if (error instanceof Error && error.message.includes("验证失败")) {
|
|
146
|
+
// 尝试提取字段信息
|
|
147
|
+
const field = extractFieldFromError(error);
|
|
148
|
+
const value = extractValueFromError(error);
|
|
149
|
+
const schema = extractSchemaFromError(error);
|
|
150
|
+
return await errorHandler(error, field, value, schema);
|
|
151
|
+
}
|
|
152
|
+
// 其他错误使用默认处理
|
|
153
|
+
return json({
|
|
154
|
+
success: false,
|
|
155
|
+
error: "Internal Error",
|
|
156
|
+
message: error instanceof Error ? error.message : "未知错误",
|
|
157
|
+
}, 500);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
// 从错误中提取字段信息的辅助函数
|
|
162
|
+
function extractFieldFromError(error) {
|
|
163
|
+
// 尝试从错误消息中提取字段名
|
|
164
|
+
const fieldMatch = error.message.match(/字段\s*(\w+)/);
|
|
165
|
+
return fieldMatch ? fieldMatch[1] : "unknown";
|
|
166
|
+
}
|
|
167
|
+
// 从错误中提取值的辅助函数
|
|
168
|
+
function extractValueFromError(error) {
|
|
169
|
+
// 这里可以根据实际错误类型提取值
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
// 从错误中提取Schema的辅助函数
|
|
173
|
+
function extractSchemaFromError(error) {
|
|
174
|
+
// 这里可以根据实际错误类型提取Schema
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
export function withExtra() {
|
|
178
|
+
return function withExtraHandler(config, handler) {
|
|
179
|
+
return createRouteHandler(handler, config);
|
|
180
|
+
};
|
|
181
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema配置验证器
|
|
3
|
+
*
|
|
4
|
+
* 使用validateSchema函数对SchemaConfig结构的数据进行验证
|
|
5
|
+
* 提供统一的验证接口和错误处理
|
|
6
|
+
*
|
|
7
|
+
* @author Framework Team
|
|
8
|
+
* @version 1.0.0
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
import type { TSchema } from "@sinclair/typebox";
|
|
12
|
+
import { type ValidationResult } from "./validators";
|
|
13
|
+
export interface SchemaConfig {
|
|
14
|
+
body?: TSchema;
|
|
15
|
+
query?: TSchema;
|
|
16
|
+
params?: TSchema;
|
|
17
|
+
headers?: TSchema;
|
|
18
|
+
cookies?: TSchema;
|
|
19
|
+
}
|
|
20
|
+
export interface RequestData {
|
|
21
|
+
body?: unknown;
|
|
22
|
+
query?: unknown;
|
|
23
|
+
params?: unknown;
|
|
24
|
+
headers?: unknown;
|
|
25
|
+
cookies?: unknown;
|
|
26
|
+
}
|
|
27
|
+
export interface SchemaValidationResult {
|
|
28
|
+
success: boolean;
|
|
29
|
+
data?: {
|
|
30
|
+
body?: unknown;
|
|
31
|
+
query?: unknown;
|
|
32
|
+
params?: unknown;
|
|
33
|
+
headers?: unknown;
|
|
34
|
+
cookies?: unknown;
|
|
35
|
+
};
|
|
36
|
+
errors?: Array<{
|
|
37
|
+
field: keyof SchemaConfig;
|
|
38
|
+
error: ValidationResult<unknown>;
|
|
39
|
+
}>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 使用SchemaConfig验证完整的请求数据
|
|
43
|
+
* @param config Schema配置
|
|
44
|
+
* @param data 请求数据
|
|
45
|
+
* @returns 验证结果
|
|
46
|
+
*/
|
|
47
|
+
export declare function validateSchemaConfig(config: SchemaConfig, data: RequestData): SchemaValidationResult;
|
|
48
|
+
/**
|
|
49
|
+
* 异步验证SchemaConfig(支持异步验证器)
|
|
50
|
+
* @param config Schema配置
|
|
51
|
+
* @param data 请求数据
|
|
52
|
+
* @returns Promise<验证结果>
|
|
53
|
+
*/
|
|
54
|
+
export declare function validateSchemaConfigAsync(config: SchemaConfig, data: RequestData): Promise<SchemaValidationResult>;
|
|
55
|
+
/**
|
|
56
|
+
* 创建验证器工厂函数
|
|
57
|
+
* @param config Schema配置
|
|
58
|
+
* @returns 验证器函数
|
|
59
|
+
*/
|
|
60
|
+
export declare function createSchemaValidator(config: SchemaConfig): (data: RequestData) => SchemaValidationResult;
|
|
61
|
+
/**
|
|
62
|
+
* 创建异步验证器工厂函数
|
|
63
|
+
* @param config Schema配置
|
|
64
|
+
* @returns 异步验证器函数
|
|
65
|
+
*/
|
|
66
|
+
export declare function createAsyncSchemaValidator(config: SchemaConfig): (data: RequestData) => Promise<SchemaValidationResult>;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema配置验证器
|
|
3
|
+
*
|
|
4
|
+
* 使用validateSchema函数对SchemaConfig结构的数据进行验证
|
|
5
|
+
* 提供统一的验证接口和错误处理
|
|
6
|
+
*
|
|
7
|
+
* @author Framework Team
|
|
8
|
+
* @version 1.0.0
|
|
9
|
+
* @license MIT
|
|
10
|
+
*/
|
|
11
|
+
import { validateSchema } from "./validators";
|
|
12
|
+
/**
|
|
13
|
+
* 验证单个Schema配置项
|
|
14
|
+
* @param schema Schema定义
|
|
15
|
+
* @param data 要验证的数据
|
|
16
|
+
* @param fieldName 字段名称(用于错误标识)
|
|
17
|
+
* @returns 验证结果
|
|
18
|
+
*/
|
|
19
|
+
function validateSingleSchema(schema, data, fieldName) {
|
|
20
|
+
return validateSchema(schema, data);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 使用SchemaConfig验证完整的请求数据
|
|
24
|
+
* @param config Schema配置
|
|
25
|
+
* @param data 请求数据
|
|
26
|
+
* @returns 验证结果
|
|
27
|
+
*/
|
|
28
|
+
export function validateSchemaConfig(config, data) {
|
|
29
|
+
const errors = [];
|
|
30
|
+
const validatedData = {};
|
|
31
|
+
// 验证body
|
|
32
|
+
if (config.body && data.body !== undefined) {
|
|
33
|
+
const result = validateSingleSchema(config.body, data.body, "body");
|
|
34
|
+
if (result.success) {
|
|
35
|
+
validatedData.body = result.data;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
errors.push({ field: "body", error: result });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else if (data.body !== undefined) {
|
|
42
|
+
validatedData.body = data.body;
|
|
43
|
+
}
|
|
44
|
+
// 验证query
|
|
45
|
+
if (config.query && data.query !== undefined) {
|
|
46
|
+
const result = validateSingleSchema(config.query, data.query, "query");
|
|
47
|
+
if (result.success) {
|
|
48
|
+
validatedData.query = result.data;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
errors.push({ field: "query", error: result });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else if (data.query !== undefined) {
|
|
55
|
+
validatedData.query = data.query;
|
|
56
|
+
}
|
|
57
|
+
// 验证params
|
|
58
|
+
if (config.params && data.params !== undefined) {
|
|
59
|
+
const result = validateSingleSchema(config.params, data.params, "params");
|
|
60
|
+
if (result.success) {
|
|
61
|
+
validatedData.params = result.data;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
errors.push({ field: "params", error: result });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (data.params !== undefined) {
|
|
68
|
+
validatedData.params = data.params;
|
|
69
|
+
}
|
|
70
|
+
// 验证headers
|
|
71
|
+
if (config.headers && data.headers !== undefined) {
|
|
72
|
+
const result = validateSingleSchema(config.headers, data.headers, "headers");
|
|
73
|
+
if (result.success) {
|
|
74
|
+
validatedData.headers = result.data;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
errors.push({ field: "headers", error: result });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else if (data.headers !== undefined) {
|
|
81
|
+
validatedData.headers = data.headers;
|
|
82
|
+
}
|
|
83
|
+
// 验证cookies
|
|
84
|
+
if (config.cookies && data.cookies !== undefined) {
|
|
85
|
+
const result = validateSingleSchema(config.cookies, data.cookies, "cookies");
|
|
86
|
+
if (result.success) {
|
|
87
|
+
validatedData.cookies = result.data;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
errors.push({ field: "cookies", error: result });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else if (data.cookies !== undefined) {
|
|
94
|
+
validatedData.cookies = data.cookies;
|
|
95
|
+
}
|
|
96
|
+
// 添加所有未配置但存在的数据字段
|
|
97
|
+
if (data.body !== undefined && !config.body) {
|
|
98
|
+
validatedData.body = data.body;
|
|
99
|
+
}
|
|
100
|
+
if (data.query !== undefined && !config.query) {
|
|
101
|
+
validatedData.query = data.query;
|
|
102
|
+
}
|
|
103
|
+
if (data.params !== undefined && !config.params) {
|
|
104
|
+
validatedData.params = data.params;
|
|
105
|
+
}
|
|
106
|
+
if (data.headers !== undefined && !config.headers) {
|
|
107
|
+
validatedData.headers = data.headers;
|
|
108
|
+
}
|
|
109
|
+
if (data.cookies !== undefined && !config.cookies) {
|
|
110
|
+
validatedData.cookies = data.cookies;
|
|
111
|
+
}
|
|
112
|
+
// 如果有错误,返回失败结果
|
|
113
|
+
if (errors.length > 0) {
|
|
114
|
+
return {
|
|
115
|
+
success: false,
|
|
116
|
+
errors,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
// 验证成功,返回验证后的数据
|
|
120
|
+
return {
|
|
121
|
+
success: true,
|
|
122
|
+
data: validatedData,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* 异步验证SchemaConfig(支持异步验证器)
|
|
127
|
+
* @param config Schema配置
|
|
128
|
+
* @param data 请求数据
|
|
129
|
+
* @returns Promise<验证结果>
|
|
130
|
+
*/
|
|
131
|
+
export async function validateSchemaConfigAsync(config, data) {
|
|
132
|
+
const errors = [];
|
|
133
|
+
const validatedData = {};
|
|
134
|
+
// 并行验证所有Schema以提高性能
|
|
135
|
+
const validationPromises = [];
|
|
136
|
+
// 收集所有需要验证的字段
|
|
137
|
+
if (config.body && data.body !== undefined) {
|
|
138
|
+
validationPromises.push(Promise.resolve({
|
|
139
|
+
field: "body",
|
|
140
|
+
result: validateSingleSchema(config.body, data.body, "body"),
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
if (config.query && data.query !== undefined) {
|
|
144
|
+
validationPromises.push(Promise.resolve({
|
|
145
|
+
field: "query",
|
|
146
|
+
result: validateSingleSchema(config.query, data.query, "query"),
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
if (config.params && data.params !== undefined) {
|
|
150
|
+
validationPromises.push(Promise.resolve({
|
|
151
|
+
field: "params",
|
|
152
|
+
result: validateSingleSchema(config.params, data.params, "params"),
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
if (config.headers && data.headers !== undefined) {
|
|
156
|
+
validationPromises.push(Promise.resolve({
|
|
157
|
+
field: "headers",
|
|
158
|
+
result: validateSingleSchema(config.headers, data.headers, "headers"),
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
if (config.cookies && data.cookies !== undefined) {
|
|
162
|
+
validationPromises.push(Promise.resolve({
|
|
163
|
+
field: "cookies",
|
|
164
|
+
result: validateSingleSchema(config.cookies, data.cookies, "cookies"),
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
// 等待所有验证完成
|
|
168
|
+
const results = await Promise.all(validationPromises);
|
|
169
|
+
// 处理验证结果
|
|
170
|
+
for (const { field, result } of results) {
|
|
171
|
+
if (result.success) {
|
|
172
|
+
validatedData[field] = result.data;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
errors.push({ field, error: result });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// 添加未验证但存在的数据
|
|
179
|
+
if (data.body !== undefined && !config.body)
|
|
180
|
+
validatedData.body = data.body;
|
|
181
|
+
if (data.query !== undefined && !config.query)
|
|
182
|
+
validatedData.query = data.query;
|
|
183
|
+
if (data.params !== undefined && !config.params)
|
|
184
|
+
validatedData.params = data.params;
|
|
185
|
+
if (data.headers !== undefined && !config.headers)
|
|
186
|
+
validatedData.headers = data.headers;
|
|
187
|
+
if (data.cookies !== undefined && !config.cookies)
|
|
188
|
+
validatedData.cookies = data.cookies;
|
|
189
|
+
// 如果有错误,返回失败结果
|
|
190
|
+
if (errors.length > 0) {
|
|
191
|
+
return {
|
|
192
|
+
success: false,
|
|
193
|
+
errors,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
// 验证成功,返回验证后的数据
|
|
197
|
+
return {
|
|
198
|
+
success: true,
|
|
199
|
+
data: validatedData,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 创建验证器工厂函数
|
|
204
|
+
* @param config Schema配置
|
|
205
|
+
* @returns 验证器函数
|
|
206
|
+
*/
|
|
207
|
+
export function createSchemaValidator(config) {
|
|
208
|
+
return (data) => {
|
|
209
|
+
return validateSchemaConfig(config, data);
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* 创建异步验证器工厂函数
|
|
214
|
+
* @param config Schema配置
|
|
215
|
+
* @returns 异步验证器函数
|
|
216
|
+
*/
|
|
217
|
+
export function createAsyncSchemaValidator(config) {
|
|
218
|
+
return (data) => {
|
|
219
|
+
return validateSchemaConfigAsync(config, data);
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
// 类型已经在上面定义并导出,无需重复导出
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 超优化版Schema验证器 v5.0.0
|
|
3
|
+
*
|
|
4
|
+
* 使用经过验证的优化技术,确保极致性能
|
|
5
|
+
* - 内联函数调用
|
|
6
|
+
* - 预编译缓存优化
|
|
7
|
+
* - 内存池优化
|
|
8
|
+
* - 位运算优化
|
|
9
|
+
* - 类型特化优化
|
|
10
|
+
* - 循环展开优化
|
|
11
|
+
* - 位掩码优化
|
|
12
|
+
* - 字符串池优化
|
|
13
|
+
* - 内联缓存优化
|
|
14
|
+
* - 内存对齐优化
|
|
15
|
+
*
|
|
16
|
+
* @author Framework Team
|
|
17
|
+
* @version 5.0.0
|
|
18
|
+
* @license MIT
|
|
19
|
+
*/
|
|
20
|
+
import type { TSchema } from "@sinclair/typebox";
|
|
21
|
+
export interface SchemaConfig {
|
|
22
|
+
body?: TSchema;
|
|
23
|
+
query?: TSchema;
|
|
24
|
+
params?: TSchema;
|
|
25
|
+
headers?: TSchema;
|
|
26
|
+
cookies?: TSchema;
|
|
27
|
+
}
|
|
28
|
+
export declare function validateSchemaUltra(schema: TSchema | undefined, data: any, context: string): any;
|
|
29
|
+
export declare function validateAllSchemasUltra(config: SchemaConfig, data: {
|
|
30
|
+
body: any;
|
|
31
|
+
query: any;
|
|
32
|
+
params: any;
|
|
33
|
+
headers: any;
|
|
34
|
+
cookies: any;
|
|
35
|
+
}): any;
|
|
36
|
+
export declare function precompileSchemasUltra(config: SchemaConfig): void;
|
|
37
|
+
export declare function createTypedValidatorUltra<T>(schema: TSchema): (data: T) => T;
|
|
38
|
+
export declare function validateBatchUltra<T>(schema: TSchema, dataArray: T[]): T[];
|
|
39
|
+
export declare function getCacheStats(): {
|
|
40
|
+
totalSchemas: number;
|
|
41
|
+
totalHits: number;
|
|
42
|
+
hitRate: string;
|
|
43
|
+
cacheSize: number;
|
|
44
|
+
errorPoolUsage: string;
|
|
45
|
+
stringPoolSize: number;
|
|
46
|
+
memoryEfficiency: string;
|
|
47
|
+
};
|
|
48
|
+
export declare function smartClearUltraCache(keepTop?: number): void;
|
|
49
|
+
export declare function clearUltraCache(): void;
|
|
50
|
+
export declare function withPerformanceMonitoring<T extends (...args: any[]) => any>(fn: T, name: string): T;
|
|
51
|
+
export { validateAllSchemasUltra as validateAllSchemas, createTypedValidatorUltra as createTypedValidator, validateBatchUltra as validateBatch, clearUltraCache as clearCache, smartClearUltraCache as smartClearCache, };
|