vafast 0.3.2 → 0.3.4
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 +13 -11
- package/dist/auth/token.js +118 -111
- package/dist/auth/token.js.map +1 -0
- package/dist/defineRoute.d.ts +5 -2
- package/dist/defineRoute.js +7 -2
- package/dist/defineRoute.js.map +1 -0
- package/dist/index.d.ts +30 -14
- package/dist/index.js +2247 -15
- package/dist/index.js.map +1 -0
- package/dist/middleware/auth.d.ts +8 -6
- package/dist/middleware/auth.js +198 -99
- package/dist/middleware/auth.js.map +1 -0
- package/dist/middleware/authMiddleware.d.ts +5 -2
- package/dist/middleware/authMiddleware.js +55 -11
- package/dist/middleware/authMiddleware.js.map +1 -0
- package/dist/middleware/component-renderer.d.ts +4 -2
- package/dist/middleware/component-renderer.js +87 -80
- package/dist/middleware/component-renderer.js.map +1 -0
- package/dist/middleware/component-router.d.ts +8 -3
- package/dist/middleware/component-router.js +33 -39
- package/dist/middleware/component-router.js.map +1 -0
- package/dist/middleware/cors.d.ts +6 -3
- package/dist/middleware/cors.js +42 -29
- package/dist/middleware/cors.js.map +1 -0
- package/dist/middleware/rateLimit.d.ts +5 -3
- package/dist/middleware/rateLimit.js +45 -29
- package/dist/middleware/rateLimit.js.map +1 -0
- package/dist/middleware.d.ts +6 -3
- package/dist/middleware.js +97 -51
- package/dist/middleware.js.map +1 -0
- package/dist/monitoring/index.d.ts +11 -4
- package/dist/monitoring/index.js +1299 -17
- package/dist/monitoring/index.js.map +1 -0
- package/dist/monitoring/native-monitor.d.ts +12 -6
- package/dist/monitoring/native-monitor.js +1258 -161
- package/dist/monitoring/native-monitor.js.map +1 -0
- package/dist/monitoring/types.d.ts +8 -6
- package/dist/monitoring/types.js +1 -1
- package/dist/monitoring/types.js.map +1 -0
- package/dist/node-server/index.d.ts +4 -22
- package/dist/node-server/index.js +254 -21
- package/dist/node-server/index.js.map +1 -0
- package/dist/node-server/request.d.ts +6 -2
- package/dist/node-server/request.js +102 -134
- package/dist/node-server/request.js.map +1 -0
- package/dist/node-server/response.d.ts +7 -3
- package/dist/node-server/response.js +67 -89
- package/dist/node-server/response.js.map +1 -0
- package/dist/node-server/serve.d.ts +11 -7
- package/dist/node-server/serve.js +231 -82
- package/dist/node-server/serve.js.map +1 -0
- package/dist/router/index.d.ts +3 -5
- package/dist/router/index.js +228 -7
- package/dist/router/index.js.map +1 -0
- package/dist/router/radix-tree.d.ts +7 -4
- package/dist/router/radix-tree.js +186 -218
- package/dist/router/radix-tree.js.map +1 -0
- package/dist/router.d.ts +7 -3
- package/dist/router.js +37 -83
- package/dist/router.js.map +1 -0
- package/dist/serve.d.ts +2 -12
- package/dist/serve.js +237 -11
- package/dist/serve.js.map +1 -0
- package/dist/server/base-server.d.ts +5 -2
- package/dist/server/base-server.js +124 -135
- package/dist/server/base-server.js.map +1 -0
- package/dist/server/component-server.d.ts +9 -4
- package/dist/server/component-server.js +481 -139
- package/dist/server/component-server.js.map +1 -0
- package/dist/server/index.d.ts +8 -7
- package/dist/server/index.js +985 -11
- package/dist/server/index.js.map +1 -0
- package/dist/server/server-factory.d.ts +11 -5
- package/dist/server/server-factory.js +979 -67
- package/dist/server/server-factory.js.map +1 -0
- package/dist/server/server.d.ts +7 -3
- package/dist/server/server.js +553 -112
- package/dist/server/server.js.map +1 -0
- package/dist/types/component-route.d.ts +8 -4
- package/dist/types/component-route.js +1 -1
- package/dist/types/component-route.js.map +1 -0
- package/dist/types/index.d.ts +5 -5
- package/dist/types/index.js +21 -4
- package/dist/types/index.js.map +1 -0
- package/dist/types/route.d.ts +13 -10
- package/dist/types/route.js +10 -9
- package/dist/types/route.js.map +1 -0
- package/dist/types/schema.d.ts +11 -7
- package/dist/types/schema.js +1 -1
- package/dist/types/schema.js.map +1 -0
- package/dist/types/types.d.ts +11 -9
- package/dist/types/types.js +1 -1
- package/dist/types/types.js.map +1 -0
- package/dist/utils/base64url.d.ts +4 -2
- package/dist/utils/base64url.js +12 -9
- package/dist/utils/base64url.js.map +1 -0
- package/dist/utils/create-handler.d.ts +11 -7
- package/dist/utils/create-handler.js +393 -217
- package/dist/utils/create-handler.js.map +1 -0
- package/dist/utils/dependency-manager.d.ts +3 -1
- package/dist/utils/dependency-manager.js +67 -69
- package/dist/utils/dependency-manager.js.map +1 -0
- package/dist/utils/go-await.d.ts +3 -1
- package/dist/utils/go-await.js +8 -22
- package/dist/utils/go-await.js.map +1 -0
- package/dist/utils/handle.d.ts +6 -4
- package/dist/utils/handle.js +44 -25
- package/dist/utils/handle.js.map +1 -0
- package/dist/utils/html-renderer.d.ts +3 -1
- package/dist/utils/html-renderer.js +25 -24
- package/dist/utils/html-renderer.js.map +1 -0
- package/dist/utils/index.d.ts +13 -13
- package/dist/utils/index.js +832 -21
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/parsers.d.ts +15 -13
- package/dist/utils/parsers.js +138 -188
- package/dist/utils/parsers.js.map +1 -0
- package/dist/utils/path-matcher.d.ts +3 -1
- package/dist/utils/path-matcher.js +68 -78
- package/dist/utils/path-matcher.js.map +1 -0
- package/dist/utils/request-validator.d.ts +13 -10
- package/dist/utils/request-validator.js +234 -84
- package/dist/utils/request-validator.js.map +1 -0
- package/dist/utils/response.d.ts +9 -7
- package/dist/utils/response.js +93 -102
- package/dist/utils/response.js.map +1 -0
- package/dist/utils/validators/schema-validator.d.ts +13 -9
- package/dist/utils/validators/schema-validator.js +228 -209
- package/dist/utils/validators/schema-validator.js.map +1 -0
- package/dist/utils/validators/schema-validators-ultra.d.ts +15 -12
- package/dist/utils/validators/schema-validators-ultra.js +233 -256
- package/dist/utils/validators/schema-validators-ultra.js.map +1 -0
- package/dist/utils/validators/validators.d.ts +15 -12
- package/dist/utils/validators/validators.js +81 -122
- package/dist/utils/validators/validators.js.map +1 -0
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -1,15 +1,2247 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
// src/router.ts
|
|
2
|
+
function flattenNestedRoutes(routes) {
|
|
3
|
+
const flattened = [];
|
|
4
|
+
function processRoute(route, parentPath = "", parentMiddleware = []) {
|
|
5
|
+
const currentPath = normalizePath(parentPath + route.path);
|
|
6
|
+
const currentMiddleware = [
|
|
7
|
+
...parentMiddleware,
|
|
8
|
+
...route.middleware || []
|
|
9
|
+
];
|
|
10
|
+
if ("method" in route && "handler" in route) {
|
|
11
|
+
flattened.push({
|
|
12
|
+
...route,
|
|
13
|
+
fullPath: currentPath,
|
|
14
|
+
middlewareChain: currentMiddleware
|
|
15
|
+
});
|
|
16
|
+
} else if ("children" in route && route.children) {
|
|
17
|
+
for (const child of route.children) {
|
|
18
|
+
processRoute(child, currentPath, currentMiddleware);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
for (const route of routes) {
|
|
23
|
+
processRoute(route);
|
|
24
|
+
}
|
|
25
|
+
return flattened;
|
|
26
|
+
}
|
|
27
|
+
function normalizePath(path) {
|
|
28
|
+
let normalized = decodeURIComponent(path);
|
|
29
|
+
normalized = normalized.replace(/\/+/g, "/");
|
|
30
|
+
if (normalized === "") return "/";
|
|
31
|
+
if (normalized !== "/" && normalized.endsWith("/")) {
|
|
32
|
+
normalized = normalized.slice(0, -1);
|
|
33
|
+
}
|
|
34
|
+
return normalized;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/utils/response.ts
|
|
38
|
+
function json(data, status = 200, headers = {}) {
|
|
39
|
+
const body = JSON.stringify(data);
|
|
40
|
+
if (Object.keys(headers).length === 0) {
|
|
41
|
+
return new Response(body, {
|
|
42
|
+
status,
|
|
43
|
+
headers: { "Content-Type": "application/json" }
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const h = new Headers({
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
...headers
|
|
49
|
+
});
|
|
50
|
+
return new Response(body, {
|
|
51
|
+
status,
|
|
52
|
+
headers: h
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
var JSON_HEADERS = { "Content-Type": "application/json" };
|
|
56
|
+
var TEXT_HEADERS = { "Content-Type": "text/plain" };
|
|
57
|
+
function mapResponse(response) {
|
|
58
|
+
if (response instanceof Response) return response;
|
|
59
|
+
switch (response?.constructor?.name) {
|
|
60
|
+
case "String":
|
|
61
|
+
return new Response(response, { headers: TEXT_HEADERS });
|
|
62
|
+
case "Object":
|
|
63
|
+
case "Array":
|
|
64
|
+
return new Response(JSON.stringify(response), { headers: JSON_HEADERS });
|
|
65
|
+
case "Number":
|
|
66
|
+
case "Boolean":
|
|
67
|
+
return new Response(String(response), { headers: TEXT_HEADERS });
|
|
68
|
+
case void 0:
|
|
69
|
+
return new Response(null, { status: 204 });
|
|
70
|
+
case "ReadableStream":
|
|
71
|
+
return new Response(response);
|
|
72
|
+
case "Blob":
|
|
73
|
+
return new Response(response);
|
|
74
|
+
case "ArrayBuffer":
|
|
75
|
+
return new Response(response);
|
|
76
|
+
case "Uint8Array":
|
|
77
|
+
return new Response(response);
|
|
78
|
+
default:
|
|
79
|
+
if (response instanceof Promise) {
|
|
80
|
+
return response.then(mapResponse);
|
|
81
|
+
}
|
|
82
|
+
return new Response(JSON.stringify(response), { headers: JSON_HEADERS });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function redirect(location, status = 302) {
|
|
86
|
+
return new Response(null, {
|
|
87
|
+
status,
|
|
88
|
+
headers: {
|
|
89
|
+
Location: location
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function text(content, status = 200, headers = {}) {
|
|
94
|
+
const h = new Headers({
|
|
95
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
96
|
+
...headers
|
|
97
|
+
});
|
|
98
|
+
return new Response(content, {
|
|
99
|
+
status,
|
|
100
|
+
headers: h
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
function html(content, status = 200, headers = {}) {
|
|
104
|
+
const h = new Headers({
|
|
105
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
106
|
+
...headers
|
|
107
|
+
});
|
|
108
|
+
return new Response(content, {
|
|
109
|
+
status,
|
|
110
|
+
headers: h
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function empty(status = 204, headers = {}) {
|
|
114
|
+
return new Response(null, {
|
|
115
|
+
status,
|
|
116
|
+
headers
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function stream(stream2, status = 200, headers = {}) {
|
|
120
|
+
const h = new Headers({
|
|
121
|
+
"Content-Type": "application/octet-stream",
|
|
122
|
+
...headers
|
|
123
|
+
});
|
|
124
|
+
return new Response(stream2, {
|
|
125
|
+
status,
|
|
126
|
+
headers: h
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/middleware.ts
|
|
131
|
+
var VafastError = class extends Error {
|
|
132
|
+
status;
|
|
133
|
+
type;
|
|
134
|
+
expose;
|
|
135
|
+
constructor(message, options = {}) {
|
|
136
|
+
super(message);
|
|
137
|
+
this.name = "VafastError";
|
|
138
|
+
this.status = options.status ?? 500;
|
|
139
|
+
this.type = options.type ?? "internal_error";
|
|
140
|
+
this.expose = options.expose ?? false;
|
|
141
|
+
if (options.cause) this.cause = options.cause;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
function composeMiddleware(middleware, finalHandler) {
|
|
145
|
+
const all = [errorHandler, ...middleware];
|
|
146
|
+
return function composedHandler(req) {
|
|
147
|
+
let i = -1;
|
|
148
|
+
const dispatch = (index) => {
|
|
149
|
+
if (index <= i)
|
|
150
|
+
return Promise.reject(new Error("next() called multiple times"));
|
|
151
|
+
i = index;
|
|
152
|
+
if (index < all.length) {
|
|
153
|
+
const mw = all[index];
|
|
154
|
+
return Promise.resolve(mw(req, () => dispatch(index + 1)));
|
|
155
|
+
}
|
|
156
|
+
return Promise.resolve(finalHandler(req)).then(mapResponse);
|
|
157
|
+
};
|
|
158
|
+
return dispatch(0);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
var errorHandler = async (req, next) => {
|
|
162
|
+
try {
|
|
163
|
+
return await next();
|
|
164
|
+
} catch (err) {
|
|
165
|
+
console.error("\u672A\u5904\u7406\u7684\u9519\u8BEF:", err);
|
|
166
|
+
if (err instanceof VafastError) {
|
|
167
|
+
return json(
|
|
168
|
+
{
|
|
169
|
+
error: err.type,
|
|
170
|
+
message: err.expose ? err.message : "\u53D1\u751F\u4E86\u4E00\u4E2A\u9519\u8BEF"
|
|
171
|
+
},
|
|
172
|
+
err.status
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
return json({ error: "internal_error", message: "\u51FA\u73B0\u4E86\u4E00\u4E9B\u95EE\u9898" }, 500);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// src/server/base-server.ts
|
|
180
|
+
var BaseServer = class {
|
|
181
|
+
globalMiddleware = [];
|
|
182
|
+
use(mw) {
|
|
183
|
+
this.globalMiddleware.push(mw);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* 打印扁平化后的路由信息,用于调试
|
|
187
|
+
*/
|
|
188
|
+
logFlattenedRoutes(routes, type = "\u8DEF\u7531") {
|
|
189
|
+
console.log(`\u{1F680} \u6241\u5E73\u5316\u540E\u7684${type}:`);
|
|
190
|
+
for (const route of routes) {
|
|
191
|
+
const method = route.method || "GET";
|
|
192
|
+
const path = route.fullPath || route.path;
|
|
193
|
+
console.log(` ${method} ${path}`);
|
|
194
|
+
if (route.middlewareChain && route.middlewareChain.length > 0) {
|
|
195
|
+
console.log(` \u4E2D\u95F4\u4EF6\u94FE: ${route.middlewareChain.length} \u4E2A`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
console.log("");
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 检测路由冲突
|
|
202
|
+
* 检查是否有路径相同但方法不同的路由,以及潜在的路径冲突
|
|
203
|
+
*/
|
|
204
|
+
detectRouteConflicts(routes) {
|
|
205
|
+
const pathGroups = /* @__PURE__ */ new Map();
|
|
206
|
+
for (const route of routes) {
|
|
207
|
+
const path = route.fullPath || route.path;
|
|
208
|
+
const method = route.method || "GET";
|
|
209
|
+
if (!pathGroups.has(path)) {
|
|
210
|
+
pathGroups.set(path, []);
|
|
211
|
+
}
|
|
212
|
+
pathGroups.get(path).push({ ...route, method });
|
|
213
|
+
}
|
|
214
|
+
for (const [path, routeList] of pathGroups) {
|
|
215
|
+
if (routeList.length > 1) {
|
|
216
|
+
const methods = routeList.map((r) => r.method);
|
|
217
|
+
const uniqueMethods = [...new Set(methods)];
|
|
218
|
+
if (uniqueMethods.length === 1) {
|
|
219
|
+
console.warn(
|
|
220
|
+
`\u26A0\uFE0F \u8DEF\u7531\u51B2\u7A81: ${uniqueMethods[0]} ${path} \u5B9A\u4E49\u4E86 ${routeList.length} \u6B21`
|
|
221
|
+
);
|
|
222
|
+
routeList.forEach((route, index) => {
|
|
223
|
+
console.warn(` ${index + 1}. ${route.method} ${path}`);
|
|
224
|
+
});
|
|
225
|
+
} else {
|
|
226
|
+
console.log(`\u2139\uFE0F \u8DEF\u5F84 ${path} \u652F\u6301\u65B9\u6CD5: ${uniqueMethods.join(", ")}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
this.detectDynamicRouteConflicts(routes);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 检测动态路由的潜在冲突
|
|
234
|
+
*/
|
|
235
|
+
detectDynamicRouteConflicts(routes) {
|
|
236
|
+
const dynamicRoutes = routes.filter((r) => {
|
|
237
|
+
const path = r.fullPath || r.path;
|
|
238
|
+
return path.includes(":") || path.includes("*");
|
|
239
|
+
});
|
|
240
|
+
for (let i = 0; i < dynamicRoutes.length; i++) {
|
|
241
|
+
for (let j = i + 1; j < dynamicRoutes.length; j++) {
|
|
242
|
+
const route1 = dynamicRoutes[i];
|
|
243
|
+
const route2 = dynamicRoutes[j];
|
|
244
|
+
const method1 = route1.method || "GET";
|
|
245
|
+
const method2 = route2.method || "GET";
|
|
246
|
+
if (method1 === method2) {
|
|
247
|
+
const path1 = route1.fullPath || route1.path;
|
|
248
|
+
const path2 = route2.fullPath || route2.path;
|
|
249
|
+
if (this.pathsMayConflict(path1, path2)) {
|
|
250
|
+
console.warn(
|
|
251
|
+
`\u26A0\uFE0F \u6F5C\u5728\u8DEF\u7531\u51B2\u7A81: ${method1} ${path1} \u53EF\u80FD\u4E0E ${path2} \u51B2\u7A81`
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* 判断两个路径是否可能冲突
|
|
260
|
+
*/
|
|
261
|
+
pathsMayConflict(path1, path2) {
|
|
262
|
+
const parts1 = path1.split("/").filter(Boolean);
|
|
263
|
+
const parts2 = path2.split("/").filter(Boolean);
|
|
264
|
+
if (parts1.length !== parts2.length) return false;
|
|
265
|
+
for (let i = 0; i < parts1.length; i++) {
|
|
266
|
+
const p1 = parts1[i];
|
|
267
|
+
const p2 = parts2[i];
|
|
268
|
+
if (!p1.startsWith(":") && !p1.startsWith("*") && !p2.startsWith(":") && !p2.startsWith("*") && p1 !== p2) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
if (p1 === "*" && p2.startsWith(":") || p2 === "*" && p1.startsWith(":")) {
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* 路径匹配
|
|
279
|
+
*/
|
|
280
|
+
matchPath(pattern, path) {
|
|
281
|
+
const patternParts = pattern.split("/").filter(Boolean);
|
|
282
|
+
const pathParts = path.split("/").filter(Boolean);
|
|
283
|
+
if (patternParts.length !== pathParts.length) {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
287
|
+
if (patternParts[i] !== pathParts[i] && !patternParts[i].startsWith(":")) {
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* 提取路径参数
|
|
295
|
+
*/
|
|
296
|
+
extractParams(pattern, path) {
|
|
297
|
+
const params = {};
|
|
298
|
+
const patternParts = pattern.split("/").filter(Boolean);
|
|
299
|
+
const pathParts = path.split("/").filter(Boolean);
|
|
300
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
301
|
+
if (patternParts[i].startsWith(":")) {
|
|
302
|
+
const paramName = patternParts[i].slice(1);
|
|
303
|
+
params[paramName] = pathParts[i];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return params;
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
// src/router/radix-tree.ts
|
|
311
|
+
var RadixRouter = class {
|
|
312
|
+
root;
|
|
313
|
+
constructor() {
|
|
314
|
+
this.root = this.createNode("");
|
|
315
|
+
}
|
|
316
|
+
createNode(path) {
|
|
317
|
+
return {
|
|
318
|
+
path,
|
|
319
|
+
children: /* @__PURE__ */ Object.create(null),
|
|
320
|
+
handlers: /* @__PURE__ */ Object.create(null)
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
/** 分割路径 */
|
|
324
|
+
splitPath(path) {
|
|
325
|
+
return path.split("/").filter(Boolean);
|
|
326
|
+
}
|
|
327
|
+
/** 编译器函数 - 用于预编译中间件链 */
|
|
328
|
+
compiler;
|
|
329
|
+
/** 设置中间件编译器 */
|
|
330
|
+
setCompiler(compiler) {
|
|
331
|
+
this.compiler = compiler;
|
|
332
|
+
}
|
|
333
|
+
/** 注册路由 */
|
|
334
|
+
register(method, pattern, handler, middleware = []) {
|
|
335
|
+
const segments = this.splitPath(pattern);
|
|
336
|
+
let node = this.root;
|
|
337
|
+
for (const segment of segments) {
|
|
338
|
+
const firstChar = segment[0];
|
|
339
|
+
if (firstChar === ":") {
|
|
340
|
+
if (!node.paramChild) {
|
|
341
|
+
node.paramChild = this.createNode(segment);
|
|
342
|
+
node.paramChild.paramName = segment.substring(1);
|
|
343
|
+
}
|
|
344
|
+
node = node.paramChild;
|
|
345
|
+
} else if (firstChar === "*") {
|
|
346
|
+
if (!node.wildcardChild) {
|
|
347
|
+
node.wildcardChild = this.createNode(segment);
|
|
348
|
+
node.wildcardChild.paramName = segment.length > 1 ? segment.substring(1) : "*";
|
|
349
|
+
}
|
|
350
|
+
node = node.wildcardChild;
|
|
351
|
+
break;
|
|
352
|
+
} else {
|
|
353
|
+
if (!node.children[segment]) {
|
|
354
|
+
node.children[segment] = this.createNode(segment);
|
|
355
|
+
}
|
|
356
|
+
node = node.children[segment];
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
const routeHandler = { handler, middleware };
|
|
360
|
+
if (this.compiler && middleware.length === 0) {
|
|
361
|
+
routeHandler.compiled = this.compiler([], handler);
|
|
362
|
+
}
|
|
363
|
+
node.handlers[method] = routeHandler;
|
|
364
|
+
}
|
|
365
|
+
/** 预编译所有路由(在添加全局中间件后调用) */
|
|
366
|
+
precompileAll(globalMiddleware) {
|
|
367
|
+
if (!this.compiler) return;
|
|
368
|
+
this.precompileNode(this.root, globalMiddleware);
|
|
369
|
+
}
|
|
370
|
+
precompileNode(node, globalMiddleware) {
|
|
371
|
+
for (const method in node.handlers) {
|
|
372
|
+
const routeHandler = node.handlers[method];
|
|
373
|
+
if (routeHandler) {
|
|
374
|
+
const allMiddleware = [...globalMiddleware, ...routeHandler.middleware];
|
|
375
|
+
routeHandler.compiled = this.compiler(
|
|
376
|
+
allMiddleware,
|
|
377
|
+
routeHandler.handler
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
for (const key in node.children) {
|
|
382
|
+
this.precompileNode(node.children[key], globalMiddleware);
|
|
383
|
+
}
|
|
384
|
+
if (node.paramChild) {
|
|
385
|
+
this.precompileNode(node.paramChild, globalMiddleware);
|
|
386
|
+
}
|
|
387
|
+
if (node.wildcardChild) {
|
|
388
|
+
this.precompileNode(node.wildcardChild, globalMiddleware);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/** 匹配路由 */
|
|
392
|
+
match(method, path) {
|
|
393
|
+
const segments = this.splitPath(path);
|
|
394
|
+
const params = /* @__PURE__ */ Object.create(null);
|
|
395
|
+
const node = this.matchNode(this.root, segments, 0, params);
|
|
396
|
+
if (!node) return null;
|
|
397
|
+
const routeHandler = node.handlers[method];
|
|
398
|
+
if (!routeHandler) return null;
|
|
399
|
+
return {
|
|
400
|
+
handler: routeHandler.handler,
|
|
401
|
+
middleware: routeHandler.middleware,
|
|
402
|
+
params,
|
|
403
|
+
compiled: routeHandler.compiled
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
/** 递归匹配节点 (优先级: 静态 > 动态参数 > 通配符) */
|
|
407
|
+
matchNode(node, segments, index, params) {
|
|
408
|
+
if (index === segments.length) {
|
|
409
|
+
for (const method in node.handlers) {
|
|
410
|
+
if (node.handlers[method]) return node;
|
|
411
|
+
}
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
const segment = segments[index];
|
|
415
|
+
const staticChild = node.children[segment];
|
|
416
|
+
if (staticChild) {
|
|
417
|
+
const result = this.matchNode(staticChild, segments, index + 1, params);
|
|
418
|
+
if (result) return result;
|
|
419
|
+
}
|
|
420
|
+
if (node.paramChild) {
|
|
421
|
+
const paramName = node.paramChild.paramName;
|
|
422
|
+
const oldValue = params[paramName];
|
|
423
|
+
params[paramName] = segment;
|
|
424
|
+
const result = this.matchNode(
|
|
425
|
+
node.paramChild,
|
|
426
|
+
segments,
|
|
427
|
+
index + 1,
|
|
428
|
+
params
|
|
429
|
+
);
|
|
430
|
+
if (result) return result;
|
|
431
|
+
if (oldValue === void 0) {
|
|
432
|
+
delete params[paramName];
|
|
433
|
+
} else {
|
|
434
|
+
params[paramName] = oldValue;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (node.wildcardChild) {
|
|
438
|
+
params[node.wildcardChild.paramName || "*"] = segments.slice(index).join("/");
|
|
439
|
+
return node.wildcardChild;
|
|
440
|
+
}
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
/** 获取路径允许的 HTTP 方法 */
|
|
444
|
+
getAllowedMethods(path) {
|
|
445
|
+
const segments = this.splitPath(path);
|
|
446
|
+
const node = this.findNode(segments);
|
|
447
|
+
if (!node) return [];
|
|
448
|
+
const methods = [];
|
|
449
|
+
for (const method in node.handlers) {
|
|
450
|
+
if (node.handlers[method]) {
|
|
451
|
+
methods.push(method);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return methods;
|
|
455
|
+
}
|
|
456
|
+
/** 查找节点(不提取参数) */
|
|
457
|
+
findNode(segments) {
|
|
458
|
+
let node = this.root;
|
|
459
|
+
for (const segment of segments) {
|
|
460
|
+
if (node.children[segment]) {
|
|
461
|
+
node = node.children[segment];
|
|
462
|
+
} else if (node.paramChild) {
|
|
463
|
+
node = node.paramChild;
|
|
464
|
+
} else if (node.wildcardChild) {
|
|
465
|
+
return node.wildcardChild;
|
|
466
|
+
} else {
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return node;
|
|
471
|
+
}
|
|
472
|
+
/** 获取所有已注册的路由 */
|
|
473
|
+
getRoutes() {
|
|
474
|
+
const routes = [];
|
|
475
|
+
this.collectRoutes(this.root, "", routes);
|
|
476
|
+
return routes;
|
|
477
|
+
}
|
|
478
|
+
collectRoutes(node, prefix, routes) {
|
|
479
|
+
const currentPath = prefix + (node.path ? "/" + node.path : "");
|
|
480
|
+
for (const method in node.handlers) {
|
|
481
|
+
if (node.handlers[method]) {
|
|
482
|
+
routes.push({ method, path: currentPath || "/" });
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
for (const key in node.children) {
|
|
486
|
+
this.collectRoutes(node.children[key], currentPath, routes);
|
|
487
|
+
}
|
|
488
|
+
if (node.paramChild) {
|
|
489
|
+
this.collectRoutes(node.paramChild, currentPath, routes);
|
|
490
|
+
}
|
|
491
|
+
if (node.wildcardChild) {
|
|
492
|
+
this.collectRoutes(node.wildcardChild, currentPath, routes);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
// src/server/server.ts
|
|
498
|
+
var Server = class extends BaseServer {
|
|
499
|
+
router;
|
|
500
|
+
routes;
|
|
501
|
+
/** 是否已预编译 */
|
|
502
|
+
isCompiled = false;
|
|
503
|
+
/** 预编译时的全局中间件数量 */
|
|
504
|
+
compiledWithMiddlewareCount = 0;
|
|
505
|
+
constructor(routes = []) {
|
|
506
|
+
super();
|
|
507
|
+
this.router = new RadixRouter();
|
|
508
|
+
this.routes = [];
|
|
509
|
+
this.router.setCompiler(
|
|
510
|
+
(middleware, handler) => composeMiddleware(middleware, handler)
|
|
511
|
+
);
|
|
512
|
+
if (routes.length > 0) {
|
|
513
|
+
this.registerRoutes(routes);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* 预编译所有路由处理链
|
|
518
|
+
* 在添加所有路由和全局中间件后调用,可提升运行时性能
|
|
519
|
+
*/
|
|
520
|
+
compile() {
|
|
521
|
+
this.router.precompileAll(this.globalMiddleware);
|
|
522
|
+
this.isCompiled = true;
|
|
523
|
+
this.compiledWithMiddlewareCount = this.globalMiddleware.length;
|
|
524
|
+
return this;
|
|
525
|
+
}
|
|
526
|
+
registerRoutes(routes) {
|
|
527
|
+
const flattened = flattenNestedRoutes(routes);
|
|
528
|
+
this.routes.push(...flattened);
|
|
529
|
+
for (const route of flattened) {
|
|
530
|
+
this.router.register(
|
|
531
|
+
route.method,
|
|
532
|
+
route.fullPath,
|
|
533
|
+
route.handler,
|
|
534
|
+
route.middlewareChain || []
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
this.detectRouteConflicts(flattened);
|
|
538
|
+
this.logFlattenedRoutes(flattened);
|
|
539
|
+
if (this.globalMiddleware.length === 0 && !this.isCompiled) {
|
|
540
|
+
this.compile();
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
/** 快速提取 pathname */
|
|
544
|
+
extractPathname(url) {
|
|
545
|
+
let start = url.indexOf("://");
|
|
546
|
+
start = start === -1 ? 0 : start + 3;
|
|
547
|
+
const pathStart = url.indexOf("/", start);
|
|
548
|
+
if (pathStart === -1) return "/";
|
|
549
|
+
let end = url.indexOf("?", pathStart);
|
|
550
|
+
if (end === -1) end = url.indexOf("#", pathStart);
|
|
551
|
+
if (end === -1) end = url.length;
|
|
552
|
+
return url.substring(pathStart, end) || "/";
|
|
553
|
+
}
|
|
554
|
+
/** 处理请求 */
|
|
555
|
+
fetch = async (req) => {
|
|
556
|
+
const pathname = this.extractPathname(req.url);
|
|
557
|
+
const method = req.method;
|
|
558
|
+
const match = this.router.match(method, pathname);
|
|
559
|
+
if (match) {
|
|
560
|
+
req.params = match.params;
|
|
561
|
+
if (match.compiled && this.globalMiddleware.length === this.compiledWithMiddlewareCount) {
|
|
562
|
+
return match.compiled(req);
|
|
563
|
+
}
|
|
564
|
+
const allMiddleware = [...this.globalMiddleware, ...match.middleware];
|
|
565
|
+
const handler = composeMiddleware(allMiddleware, match.handler);
|
|
566
|
+
return handler(req);
|
|
567
|
+
}
|
|
568
|
+
const allowedMethods = this.router.getAllowedMethods(pathname);
|
|
569
|
+
if (allowedMethods.length > 0) {
|
|
570
|
+
return json(
|
|
571
|
+
{
|
|
572
|
+
success: false,
|
|
573
|
+
error: "Method Not Allowed",
|
|
574
|
+
message: `Method ${method} not allowed for this endpoint`,
|
|
575
|
+
allowedMethods
|
|
576
|
+
},
|
|
577
|
+
405,
|
|
578
|
+
{ Allow: allowedMethods.join(", ") }
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
return json({ success: false, error: "Not Found" }, 404);
|
|
582
|
+
};
|
|
583
|
+
addRoute(route) {
|
|
584
|
+
const flattenedRoute = {
|
|
585
|
+
...route,
|
|
586
|
+
fullPath: route.path,
|
|
587
|
+
middlewareChain: route.middleware || []
|
|
588
|
+
};
|
|
589
|
+
this.routes.push(flattenedRoute);
|
|
590
|
+
this.router.register(
|
|
591
|
+
route.method,
|
|
592
|
+
route.path,
|
|
593
|
+
route.handler,
|
|
594
|
+
route.middleware || []
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
addRoutes(routes) {
|
|
598
|
+
this.registerRoutes(routes);
|
|
599
|
+
}
|
|
600
|
+
getRoutes() {
|
|
601
|
+
return this.router.getRoutes();
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
// src/middleware/component-router.ts
|
|
606
|
+
function flattenComponentRoutes(routes) {
|
|
607
|
+
const flattened = [];
|
|
608
|
+
function processRoute(route, parentPath = "", parentMiddleware = []) {
|
|
609
|
+
const currentPath = parentPath + route.path;
|
|
610
|
+
const currentMiddleware = [
|
|
611
|
+
...parentMiddleware,
|
|
612
|
+
...route.middleware || []
|
|
613
|
+
];
|
|
614
|
+
if ("component" in route) {
|
|
615
|
+
flattened.push({
|
|
616
|
+
...route,
|
|
617
|
+
fullPath: currentPath,
|
|
618
|
+
middlewareChain: currentMiddleware
|
|
619
|
+
});
|
|
620
|
+
} else if ("children" in route && route.children) {
|
|
621
|
+
for (const child of route.children) {
|
|
622
|
+
processRoute(child, currentPath, currentMiddleware);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
for (const route of routes) {
|
|
627
|
+
processRoute(route);
|
|
628
|
+
}
|
|
629
|
+
return flattened;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// src/utils/path-matcher.ts
|
|
633
|
+
var PathMatcher = class {
|
|
634
|
+
/**
|
|
635
|
+
* 路径匹配
|
|
636
|
+
*/
|
|
637
|
+
static matchPath(pattern, path) {
|
|
638
|
+
const patternParts = pattern.split("/").filter(Boolean);
|
|
639
|
+
const pathParts = path.split("/").filter(Boolean);
|
|
640
|
+
if (patternParts.length !== pathParts.length) {
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
644
|
+
if (patternParts[i] !== pathParts[i] && !patternParts[i].startsWith(":")) {
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return true;
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* 提取路径参数
|
|
652
|
+
*/
|
|
653
|
+
static extractParams(pattern, path) {
|
|
654
|
+
const params = {};
|
|
655
|
+
const patternParts = pattern.split("/").filter(Boolean);
|
|
656
|
+
const pathParts = path.split("/").filter(Boolean);
|
|
657
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
658
|
+
if (patternParts[i].startsWith(":")) {
|
|
659
|
+
const paramName = patternParts[i].slice(1);
|
|
660
|
+
params[paramName] = pathParts[i];
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
return params;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* 计算路径特异性分数
|
|
667
|
+
* 用于路由排序:静态 > 动态(:param) > 通配符(*)
|
|
668
|
+
*/
|
|
669
|
+
static calculatePathScore(path) {
|
|
670
|
+
const parts = path.split("/").filter(Boolean);
|
|
671
|
+
let score = 0;
|
|
672
|
+
for (const p of parts) {
|
|
673
|
+
if (p === "*")
|
|
674
|
+
score += 1;
|
|
675
|
+
else if (p.startsWith(":"))
|
|
676
|
+
score += 2;
|
|
677
|
+
else score += 3;
|
|
678
|
+
}
|
|
679
|
+
return score * 10 + parts.length;
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* 判断两个路径是否可能冲突
|
|
683
|
+
*/
|
|
684
|
+
static pathsMayConflict(path1, path2) {
|
|
685
|
+
const parts1 = path1.split("/").filter(Boolean);
|
|
686
|
+
const parts2 = path2.split("/").filter(Boolean);
|
|
687
|
+
if (parts1.length !== parts2.length) return false;
|
|
688
|
+
for (let i = 0; i < parts1.length; i++) {
|
|
689
|
+
const p1 = parts1[i];
|
|
690
|
+
const p2 = parts2[i];
|
|
691
|
+
if (!p1.startsWith(":") && !p1.startsWith("*") && !p2.startsWith(":") && !p2.startsWith("*") && p1 !== p2) {
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
if (p1 === "*" && p2.startsWith(":") || p2 === "*" && p1.startsWith(":")) {
|
|
695
|
+
return true;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
return false;
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
// src/utils/html-renderer.ts
|
|
703
|
+
var HtmlRenderer = class {
|
|
704
|
+
/**
|
|
705
|
+
* 生成基础HTML模板
|
|
706
|
+
*/
|
|
707
|
+
static generateBaseHtml(content, context, clientScriptPath = "/client.js") {
|
|
708
|
+
return `
|
|
709
|
+
<!doctype html>
|
|
710
|
+
<html>
|
|
711
|
+
<head>
|
|
712
|
+
<meta charset="utf-8">
|
|
713
|
+
<title>Vafast SSR App</title>
|
|
714
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
715
|
+
</head>
|
|
716
|
+
<body>
|
|
717
|
+
<div id="app">${content}</div>
|
|
718
|
+
<script>
|
|
719
|
+
window.__ROUTE_INFO__ = {
|
|
720
|
+
params: ${JSON.stringify(context.params || {})},
|
|
721
|
+
query: ${JSON.stringify(context.query || {})},
|
|
722
|
+
pathname: '${context.pathname}'
|
|
723
|
+
};
|
|
724
|
+
</script>
|
|
725
|
+
<script type="module" src="${clientScriptPath}"></script>
|
|
726
|
+
</body>
|
|
727
|
+
</html>
|
|
728
|
+
`;
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* 生成Vue组件HTML
|
|
732
|
+
*/
|
|
733
|
+
static generateVueHtml(content, context, clientScriptPath = "/client.js") {
|
|
734
|
+
return this.generateBaseHtml(content, context, clientScriptPath);
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* 生成React组件HTML
|
|
738
|
+
*/
|
|
739
|
+
static generateReactHtml(content, context, clientScriptPath = "/client.js") {
|
|
740
|
+
return `
|
|
741
|
+
<!doctype html>
|
|
742
|
+
<html>
|
|
743
|
+
<head>
|
|
744
|
+
<meta charset="utf-8">
|
|
745
|
+
<title>Vafast SSR App</title>
|
|
746
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
747
|
+
</head>
|
|
748
|
+
<body>
|
|
749
|
+
<div id="root">${content}</div>
|
|
750
|
+
<script>
|
|
751
|
+
window.__ROUTE_INFO__ = {
|
|
752
|
+
params: ${JSON.stringify(context.params || {})},
|
|
753
|
+
query: ${JSON.stringify(context.query || {})},
|
|
754
|
+
pathname: '${context.pathname}'
|
|
755
|
+
};
|
|
756
|
+
</script>
|
|
757
|
+
<script type="module" src="${clientScriptPath}"></script>
|
|
758
|
+
</body>
|
|
759
|
+
</html>
|
|
760
|
+
`;
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
|
|
764
|
+
// src/utils/dependency-manager.ts
|
|
765
|
+
var DependencyManager = class {
|
|
766
|
+
dependencyCache = /* @__PURE__ */ new Map();
|
|
767
|
+
/**
|
|
768
|
+
* 按需获取框架依赖
|
|
769
|
+
*/
|
|
770
|
+
async getFrameworkDeps(framework) {
|
|
771
|
+
if (this.dependencyCache.has(framework)) {
|
|
772
|
+
return this.dependencyCache.get(framework);
|
|
773
|
+
}
|
|
774
|
+
console.log(`\u{1F4E6} \u6309\u9700\u52A0\u8F7D ${framework} \u4F9D\u8D56...`);
|
|
775
|
+
try {
|
|
776
|
+
let deps;
|
|
777
|
+
switch (framework) {
|
|
778
|
+
case "vue":
|
|
779
|
+
deps = await Promise.all([
|
|
780
|
+
import("vue"),
|
|
781
|
+
import("@vue/server-renderer")
|
|
782
|
+
]);
|
|
783
|
+
break;
|
|
784
|
+
case "react":
|
|
785
|
+
deps = await Promise.all([
|
|
786
|
+
import("react"),
|
|
787
|
+
import("react-dom/server")
|
|
788
|
+
]);
|
|
789
|
+
break;
|
|
790
|
+
default:
|
|
791
|
+
throw new Error(`\u4E0D\u652F\u6301\u7684\u6846\u67B6: ${framework}`);
|
|
792
|
+
}
|
|
793
|
+
this.dependencyCache.set(framework, deps);
|
|
794
|
+
console.log(`\u2705 ${framework} \u4F9D\u8D56\u52A0\u8F7D\u5B8C\u6210`);
|
|
795
|
+
return deps;
|
|
796
|
+
} catch (error) {
|
|
797
|
+
console.error(`\u274C ${framework} \u4F9D\u8D56\u52A0\u8F7D\u5931\u8D25:`, error);
|
|
798
|
+
throw error;
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* 检测组件类型
|
|
803
|
+
*/
|
|
804
|
+
detectComponentType(component) {
|
|
805
|
+
if (component.render && typeof component.render === "function") {
|
|
806
|
+
return "vue";
|
|
807
|
+
}
|
|
808
|
+
if (component.$$typeof) {
|
|
809
|
+
return "react";
|
|
810
|
+
}
|
|
811
|
+
return "vue";
|
|
812
|
+
}
|
|
813
|
+
/**
|
|
814
|
+
* 清除缓存
|
|
815
|
+
*/
|
|
816
|
+
clearCache() {
|
|
817
|
+
this.dependencyCache.clear();
|
|
818
|
+
console.log("\u{1F9F9} \u4F9D\u8D56\u7F13\u5B58\u5DF2\u6E05\u9664");
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* 获取缓存状态
|
|
822
|
+
*/
|
|
823
|
+
getCacheStatus() {
|
|
824
|
+
const status = {};
|
|
825
|
+
for (const [framework] of this.dependencyCache) {
|
|
826
|
+
status[framework] = true;
|
|
827
|
+
}
|
|
828
|
+
return status;
|
|
829
|
+
}
|
|
830
|
+
};
|
|
831
|
+
|
|
832
|
+
// src/server/component-server.ts
|
|
833
|
+
var ComponentServer = class extends BaseServer {
|
|
834
|
+
routes;
|
|
835
|
+
dependencyManager;
|
|
836
|
+
constructor(routes) {
|
|
837
|
+
super();
|
|
838
|
+
this.routes = flattenComponentRoutes(routes);
|
|
839
|
+
this.dependencyManager = new DependencyManager();
|
|
840
|
+
this.detectRouteConflicts(this.routes);
|
|
841
|
+
this.logFlattenedRoutes(this.routes, "\u7EC4\u4EF6\u8DEF\u7531");
|
|
842
|
+
console.log("\u{1F680} \u4F9D\u8D56\u6309\u9700\u52A0\u8F7D\uFF0C\u670D\u52A1\u5668\u542F\u52A8\u5B8C\u6210");
|
|
843
|
+
}
|
|
844
|
+
/**
|
|
845
|
+
* 处理请求
|
|
846
|
+
*/
|
|
847
|
+
async fetch(req) {
|
|
848
|
+
const url = new URL(req.url);
|
|
849
|
+
const pathname = url.pathname;
|
|
850
|
+
const method = req.method;
|
|
851
|
+
if (method !== "GET") {
|
|
852
|
+
return new Response("Method Not Allowed", { status: 405 });
|
|
853
|
+
}
|
|
854
|
+
let matchedRoute = null;
|
|
855
|
+
for (const route of this.routes) {
|
|
856
|
+
if (PathMatcher.matchPath(route.fullPath, pathname)) {
|
|
857
|
+
matchedRoute = route;
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
if (!matchedRoute) {
|
|
862
|
+
return new Response("Not Found", { status: 404 });
|
|
863
|
+
}
|
|
864
|
+
try {
|
|
865
|
+
const context = {
|
|
866
|
+
req,
|
|
867
|
+
params: PathMatcher.extractParams(matchedRoute.fullPath, pathname),
|
|
868
|
+
query: Object.fromEntries(url.searchParams),
|
|
869
|
+
pathname
|
|
870
|
+
};
|
|
871
|
+
return await this.executeMiddlewareChain(
|
|
872
|
+
matchedRoute.middlewareChain,
|
|
873
|
+
context,
|
|
874
|
+
matchedRoute.component
|
|
875
|
+
);
|
|
876
|
+
} catch (error) {
|
|
877
|
+
console.error("\u7EC4\u4EF6\u6E32\u67D3\u5931\u8D25:", error);
|
|
878
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* 执行中间件链
|
|
883
|
+
*/
|
|
884
|
+
async executeMiddlewareChain(middlewareChain, context, componentImport) {
|
|
885
|
+
const renderComponent = async () => {
|
|
886
|
+
const componentModule = await componentImport();
|
|
887
|
+
const component = componentModule.default || componentModule;
|
|
888
|
+
const componentType = this.dependencyManager.detectComponentType(component);
|
|
889
|
+
const deps = await this.dependencyManager.getFrameworkDeps(componentType);
|
|
890
|
+
if (componentType === "vue") {
|
|
891
|
+
return await this.renderVueComponent(component, context, deps);
|
|
892
|
+
} else if (componentType === "react") {
|
|
893
|
+
return await this.renderReactComponent(component, context, deps);
|
|
894
|
+
} else {
|
|
895
|
+
throw new Error(`\u4E0D\u652F\u6301\u7684\u7EC4\u4EF6\u7C7B\u578B: ${componentType}`);
|
|
896
|
+
}
|
|
897
|
+
};
|
|
898
|
+
let index = 0;
|
|
899
|
+
const next = async () => {
|
|
900
|
+
if (index >= middlewareChain.length) {
|
|
901
|
+
return await renderComponent();
|
|
902
|
+
}
|
|
903
|
+
const middleware = middlewareChain[index++];
|
|
904
|
+
return await middleware(context.req, next);
|
|
905
|
+
};
|
|
906
|
+
return await next();
|
|
907
|
+
}
|
|
908
|
+
/**
|
|
909
|
+
* 渲染 Vue 组件
|
|
910
|
+
*/
|
|
911
|
+
async renderVueComponent(component, context, deps) {
|
|
912
|
+
try {
|
|
913
|
+
const [vue, renderer] = deps;
|
|
914
|
+
const app = vue.createSSRApp(component);
|
|
915
|
+
app.provide("routeInfo", {
|
|
916
|
+
params: context.params || {},
|
|
917
|
+
query: context.query || {},
|
|
918
|
+
pathname: context.pathname
|
|
919
|
+
});
|
|
920
|
+
const html2 = await renderer.renderToString(app);
|
|
921
|
+
const fullHtml = HtmlRenderer.generateVueHtml(html2, context);
|
|
922
|
+
return new Response(fullHtml, {
|
|
923
|
+
headers: { "Content-Type": "text/html; charset=utf-8" }
|
|
924
|
+
});
|
|
925
|
+
} catch (error) {
|
|
926
|
+
console.error("Vue \u7EC4\u4EF6\u6E32\u67D3\u5931\u8D25:", error);
|
|
927
|
+
return new Response("Vue Component Render Error", { status: 500 });
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
/**
|
|
931
|
+
* 渲染 React 组件
|
|
932
|
+
*/
|
|
933
|
+
async renderReactComponent(component, context, deps) {
|
|
934
|
+
try {
|
|
935
|
+
const [react, renderer] = deps;
|
|
936
|
+
const content = react.createElement(component, {
|
|
937
|
+
req: context.req,
|
|
938
|
+
params: context.params || {},
|
|
939
|
+
query: context.query || {}
|
|
940
|
+
});
|
|
941
|
+
const html2 = renderer.renderToString(content);
|
|
942
|
+
const fullHtml = HtmlRenderer.generateReactHtml(html2, context);
|
|
943
|
+
return new Response(fullHtml, {
|
|
944
|
+
headers: { "Content-Type": "text/html; charset=utf-8" }
|
|
945
|
+
});
|
|
946
|
+
} catch (error) {
|
|
947
|
+
console.error("React \u7EC4\u4EF6\u6E32\u67D3\u5931\u8D25:", error);
|
|
948
|
+
return new Response("React Component Render Error", { status: 500 });
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* 获取依赖管理器(用于外部访问)
|
|
953
|
+
*/
|
|
954
|
+
getDependencyManager() {
|
|
955
|
+
return this.dependencyManager;
|
|
956
|
+
}
|
|
957
|
+
};
|
|
958
|
+
|
|
959
|
+
// src/server/server-factory.ts
|
|
960
|
+
var ServerFactory = class {
|
|
961
|
+
servers = /* @__PURE__ */ new Map();
|
|
962
|
+
/**
|
|
963
|
+
* 创建标准REST API服务器
|
|
964
|
+
*/
|
|
965
|
+
createRestServer(routes) {
|
|
966
|
+
const server = new Server(routes);
|
|
967
|
+
this.servers.set("rest", server);
|
|
968
|
+
return server;
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* 创建组件服务器
|
|
972
|
+
*/
|
|
973
|
+
createComponentServer(routes) {
|
|
974
|
+
const server = new ComponentServer(routes);
|
|
975
|
+
this.servers.set("component", server);
|
|
976
|
+
return server;
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* 获取指定类型的服务器
|
|
980
|
+
*/
|
|
981
|
+
getServer(type) {
|
|
982
|
+
return this.servers.get(type);
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* 获取所有服务器
|
|
986
|
+
*/
|
|
987
|
+
getAllServers() {
|
|
988
|
+
return this.servers;
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* 移除指定类型的服务器
|
|
992
|
+
*/
|
|
993
|
+
removeServer(type) {
|
|
994
|
+
return this.servers.delete(type);
|
|
995
|
+
}
|
|
996
|
+
/**
|
|
997
|
+
* 清除所有服务器
|
|
998
|
+
*/
|
|
999
|
+
clearServers() {
|
|
1000
|
+
this.servers.clear();
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* 获取服务器状态信息
|
|
1004
|
+
*/
|
|
1005
|
+
getServerStatus() {
|
|
1006
|
+
const status = {};
|
|
1007
|
+
for (const [name, server] of this.servers) {
|
|
1008
|
+
if (server instanceof Server) {
|
|
1009
|
+
status[name] = {
|
|
1010
|
+
type: "REST API",
|
|
1011
|
+
routes: server.routes?.length || 0
|
|
1012
|
+
};
|
|
1013
|
+
} else if (server instanceof ComponentServer) {
|
|
1014
|
+
status[name] = {
|
|
1015
|
+
type: "Component",
|
|
1016
|
+
routes: server.routes?.length || 0
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
return status;
|
|
1021
|
+
}
|
|
1022
|
+
};
|
|
1023
|
+
|
|
1024
|
+
// src/utils/parsers.ts
|
|
1025
|
+
import qs from "qs";
|
|
1026
|
+
import cookie from "cookie";
|
|
1027
|
+
async function parseBody(req) {
|
|
1028
|
+
const contentType = req.headers.get("content-type") || "";
|
|
1029
|
+
if (contentType.includes("application/json")) {
|
|
1030
|
+
return await req.json();
|
|
1031
|
+
}
|
|
1032
|
+
if (contentType.includes("application/x-www-form-urlencoded")) {
|
|
1033
|
+
const text2 = await req.text();
|
|
1034
|
+
return Object.fromEntries(new URLSearchParams(text2));
|
|
1035
|
+
}
|
|
1036
|
+
return await req.text();
|
|
1037
|
+
}
|
|
1038
|
+
function extractQueryString(url) {
|
|
1039
|
+
const qIndex = url.indexOf("?");
|
|
1040
|
+
if (qIndex === -1) return "";
|
|
1041
|
+
const hashIndex = url.indexOf("#", qIndex);
|
|
1042
|
+
return hashIndex === -1 ? url.substring(qIndex + 1) : url.substring(qIndex + 1, hashIndex);
|
|
1043
|
+
}
|
|
1044
|
+
function parseQuery(req) {
|
|
1045
|
+
const queryString = extractQueryString(req.url);
|
|
1046
|
+
if (!queryString) return {};
|
|
1047
|
+
return qs.parse(queryString);
|
|
1048
|
+
}
|
|
1049
|
+
function parseQueryFast(req) {
|
|
1050
|
+
const queryString = extractQueryString(req.url);
|
|
1051
|
+
if (!queryString) return {};
|
|
1052
|
+
const result = /* @__PURE__ */ Object.create(null);
|
|
1053
|
+
const pairs = queryString.split("&");
|
|
1054
|
+
for (const pair of pairs) {
|
|
1055
|
+
const eqIndex = pair.indexOf("=");
|
|
1056
|
+
if (eqIndex === -1) {
|
|
1057
|
+
result[decodeURIComponent(pair)] = "";
|
|
1058
|
+
} else {
|
|
1059
|
+
const key = decodeURIComponent(pair.substring(0, eqIndex));
|
|
1060
|
+
const value = decodeURIComponent(pair.substring(eqIndex + 1));
|
|
1061
|
+
result[key] = value;
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
return result;
|
|
1065
|
+
}
|
|
1066
|
+
function parseHeaders(req) {
|
|
1067
|
+
const headers = /* @__PURE__ */ Object.create(null);
|
|
1068
|
+
req.headers.forEach((value, key) => {
|
|
1069
|
+
headers[key] = value;
|
|
1070
|
+
});
|
|
1071
|
+
return headers;
|
|
1072
|
+
}
|
|
1073
|
+
function getHeader(req, name) {
|
|
1074
|
+
return req.headers.get(name);
|
|
1075
|
+
}
|
|
1076
|
+
function parseCookies(req) {
|
|
1077
|
+
const cookieHeader = req.headers.get("cookie");
|
|
1078
|
+
if (!cookieHeader) return {};
|
|
1079
|
+
try {
|
|
1080
|
+
const parsed = cookie.parse(cookieHeader);
|
|
1081
|
+
const result = {};
|
|
1082
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
1083
|
+
if (value !== void 0 && value !== null) {
|
|
1084
|
+
result[key] = value;
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
return result;
|
|
1088
|
+
} catch {
|
|
1089
|
+
return {};
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
function parseCookiesFast(req) {
|
|
1093
|
+
const cookieHeader = req.headers.get("cookie");
|
|
1094
|
+
if (!cookieHeader) return {};
|
|
1095
|
+
const result = /* @__PURE__ */ Object.create(null);
|
|
1096
|
+
const pairs = cookieHeader.split(";");
|
|
1097
|
+
for (const pair of pairs) {
|
|
1098
|
+
const trimmed = pair.trim();
|
|
1099
|
+
const eqIndex = trimmed.indexOf("=");
|
|
1100
|
+
if (eqIndex > 0) {
|
|
1101
|
+
const key = trimmed.substring(0, eqIndex).trim();
|
|
1102
|
+
const value = trimmed.substring(eqIndex + 1).trim();
|
|
1103
|
+
result[key] = value.startsWith('"') && value.endsWith('"') ? value.slice(1, -1) : value;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
return result;
|
|
1107
|
+
}
|
|
1108
|
+
function getCookie(req, name) {
|
|
1109
|
+
const cookieHeader = req.headers.get("cookie");
|
|
1110
|
+
if (!cookieHeader) return null;
|
|
1111
|
+
const prefix = `${name}=`;
|
|
1112
|
+
const pairs = cookieHeader.split(";");
|
|
1113
|
+
for (const pair of pairs) {
|
|
1114
|
+
const trimmed = pair.trim();
|
|
1115
|
+
if (trimmed.startsWith(prefix)) {
|
|
1116
|
+
const value = trimmed.substring(prefix.length).trim();
|
|
1117
|
+
return value.startsWith('"') && value.endsWith('"') ? value.slice(1, -1) : value;
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
return null;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
// src/utils/go-await.ts
|
|
1124
|
+
function goAwait(promise) {
|
|
1125
|
+
return promise.then((data) => [null, data]).catch((err) => [err instanceof Error ? err : new Error(String(err)), void 0]);
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// src/utils/validators/schema-validators-ultra.ts
|
|
1129
|
+
import { TypeCompiler } from "@sinclair/typebox/compiler";
|
|
1130
|
+
var SCHEMA_FLAGS = {
|
|
1131
|
+
BODY: 1 << 0,
|
|
1132
|
+
// 1
|
|
1133
|
+
QUERY: 1 << 1,
|
|
1134
|
+
// 2
|
|
1135
|
+
PARAMS: 1 << 2,
|
|
1136
|
+
// 4
|
|
1137
|
+
HEADERS: 1 << 3,
|
|
1138
|
+
// 8
|
|
1139
|
+
COOKIES: 1 << 4
|
|
1140
|
+
// 16
|
|
1141
|
+
};
|
|
1142
|
+
var ultraSchemaCache = /* @__PURE__ */ new Map();
|
|
1143
|
+
var schemaCacheHits = /* @__PURE__ */ new Map();
|
|
1144
|
+
var errorPool = [];
|
|
1145
|
+
var ERROR_POOL_SIZE = 200;
|
|
1146
|
+
var errorMessagePool = /* @__PURE__ */ new Map();
|
|
1147
|
+
var commonMessages = [
|
|
1148
|
+
"\u8BF7\u6C42\u4F53\u9A8C\u8BC1\u5931\u8D25",
|
|
1149
|
+
"Query\u53C2\u6570\u9A8C\u8BC1\u5931\u8D25",
|
|
1150
|
+
"\u8DEF\u5F84\u53C2\u6570\u9A8C\u8BC1\u5931\u8D25",
|
|
1151
|
+
"\u8BF7\u6C42\u5934\u9A8C\u8BC1\u5931\u8D25",
|
|
1152
|
+
"Cookie\u9A8C\u8BC1\u5931\u8D25",
|
|
1153
|
+
"\u7C7B\u578B\u9A8C\u8BC1\u5931\u8D25",
|
|
1154
|
+
"Schema\u7F16\u8BD1\u5931\u8D25",
|
|
1155
|
+
"\u672A\u77E5\u9519\u8BEF"
|
|
1156
|
+
];
|
|
1157
|
+
commonMessages.forEach((msg) => errorMessagePool.set(msg, msg));
|
|
1158
|
+
for (let i = 0; i < ERROR_POOL_SIZE; i++) {
|
|
1159
|
+
errorPool.push(new Error());
|
|
1160
|
+
}
|
|
1161
|
+
var errorPoolIndex = 0;
|
|
1162
|
+
function getErrorFromPool(message) {
|
|
1163
|
+
const error = errorPool[errorPoolIndex];
|
|
1164
|
+
error.message = message;
|
|
1165
|
+
errorPoolIndex = (errorPoolIndex + 1) % ERROR_POOL_SIZE;
|
|
1166
|
+
return error;
|
|
1167
|
+
}
|
|
1168
|
+
function getPooledString(key) {
|
|
1169
|
+
let pooled = errorMessagePool.get(key);
|
|
1170
|
+
if (!pooled) {
|
|
1171
|
+
pooled = key;
|
|
1172
|
+
errorMessagePool.set(key, key);
|
|
1173
|
+
}
|
|
1174
|
+
return pooled;
|
|
1175
|
+
}
|
|
1176
|
+
function getUltraSchemaCompiler(schema) {
|
|
1177
|
+
let compiler = ultraSchemaCache.get(schema);
|
|
1178
|
+
if (compiler) {
|
|
1179
|
+
schemaCacheHits.set(schema, (schemaCacheHits.get(schema) || 0) + 1);
|
|
1180
|
+
return compiler;
|
|
1181
|
+
}
|
|
1182
|
+
try {
|
|
1183
|
+
compiler = TypeCompiler.Compile(schema);
|
|
1184
|
+
ultraSchemaCache.set(schema, compiler);
|
|
1185
|
+
return compiler;
|
|
1186
|
+
} catch (error) {
|
|
1187
|
+
return null;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
function getSchemaFlags(config) {
|
|
1191
|
+
let flags = 0;
|
|
1192
|
+
if (config.body) flags |= SCHEMA_FLAGS.BODY;
|
|
1193
|
+
if (config.query) flags |= SCHEMA_FLAGS.QUERY;
|
|
1194
|
+
if (config.params) flags |= SCHEMA_FLAGS.PARAMS;
|
|
1195
|
+
if (config.headers) flags |= SCHEMA_FLAGS.HEADERS;
|
|
1196
|
+
if (config.cookies) flags |= SCHEMA_FLAGS.COOKIES;
|
|
1197
|
+
return flags;
|
|
1198
|
+
}
|
|
1199
|
+
function validateSchemaUltra(schema, data, context) {
|
|
1200
|
+
if (!schema) return data;
|
|
1201
|
+
try {
|
|
1202
|
+
let compiler = ultraSchemaCache.get(schema);
|
|
1203
|
+
if (!compiler) {
|
|
1204
|
+
try {
|
|
1205
|
+
compiler = TypeCompiler.Compile(schema);
|
|
1206
|
+
ultraSchemaCache.set(schema, compiler);
|
|
1207
|
+
} catch (error) {
|
|
1208
|
+
const message = getPooledString(`${context}\u9A8C\u8BC1\u5931\u8D25: Schema\u7F16\u8BD1\u5931\u8D25`);
|
|
1209
|
+
throw getErrorFromPool(message);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
const result = compiler.Check(data);
|
|
1213
|
+
if (!result) {
|
|
1214
|
+
const message = getPooledString(`${context}\u9A8C\u8BC1\u5931\u8D25`);
|
|
1215
|
+
throw getErrorFromPool(message);
|
|
1216
|
+
}
|
|
1217
|
+
return data;
|
|
1218
|
+
} catch (error) {
|
|
1219
|
+
if (error instanceof Error && error.message.includes("\u9A8C\u8BC1\u5931\u8D25")) {
|
|
1220
|
+
throw error;
|
|
1221
|
+
}
|
|
1222
|
+
const message = getPooledString(
|
|
1223
|
+
`${context}\u9A8C\u8BC1\u5931\u8D25: ${error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"}`
|
|
1224
|
+
);
|
|
1225
|
+
throw getErrorFromPool(message);
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
function validateAllSchemasUltra(config, data) {
|
|
1229
|
+
const flags = getSchemaFlags(config);
|
|
1230
|
+
if (flags & SCHEMA_FLAGS.BODY) {
|
|
1231
|
+
validateSchemaUltra(config.body, data.body, "\u8BF7\u6C42\u4F53");
|
|
1232
|
+
}
|
|
1233
|
+
if (flags & SCHEMA_FLAGS.QUERY) {
|
|
1234
|
+
validateSchemaUltra(config.query, data.query, "Query\u53C2\u6570");
|
|
1235
|
+
}
|
|
1236
|
+
if (flags & SCHEMA_FLAGS.PARAMS) {
|
|
1237
|
+
validateSchemaUltra(config.params, data.params, "\u8DEF\u5F84\u53C2\u6570");
|
|
1238
|
+
}
|
|
1239
|
+
if (flags & SCHEMA_FLAGS.HEADERS) {
|
|
1240
|
+
validateSchemaUltra(config.headers, data.headers, "\u8BF7\u6C42\u5934");
|
|
1241
|
+
}
|
|
1242
|
+
if (flags & SCHEMA_FLAGS.COOKIES) {
|
|
1243
|
+
validateSchemaUltra(config.cookies, data.cookies, "Cookie");
|
|
1244
|
+
}
|
|
1245
|
+
return data;
|
|
1246
|
+
}
|
|
1247
|
+
function precompileSchemasUltra(config) {
|
|
1248
|
+
const flags = getSchemaFlags(config);
|
|
1249
|
+
if (flags & SCHEMA_FLAGS.BODY && config.body) {
|
|
1250
|
+
getUltraSchemaCompiler(config.body);
|
|
1251
|
+
}
|
|
1252
|
+
if (flags & SCHEMA_FLAGS.QUERY && config.query) {
|
|
1253
|
+
getUltraSchemaCompiler(config.query);
|
|
1254
|
+
}
|
|
1255
|
+
if (flags & SCHEMA_FLAGS.PARAMS && config.params) {
|
|
1256
|
+
getUltraSchemaCompiler(config.params);
|
|
1257
|
+
}
|
|
1258
|
+
if (flags & SCHEMA_FLAGS.HEADERS && config.headers) {
|
|
1259
|
+
getUltraSchemaCompiler(config.headers);
|
|
1260
|
+
}
|
|
1261
|
+
if (flags & SCHEMA_FLAGS.COOKIES && config.cookies) {
|
|
1262
|
+
getUltraSchemaCompiler(config.cookies);
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
// src/utils/create-handler.ts
|
|
1267
|
+
function autoResponse(result) {
|
|
1268
|
+
if (result instanceof Response) {
|
|
1269
|
+
return result;
|
|
1270
|
+
}
|
|
1271
|
+
if (result === null || result === void 0) {
|
|
1272
|
+
return new Response(null, { status: 204 });
|
|
1273
|
+
}
|
|
1274
|
+
if (typeof result === "string") {
|
|
1275
|
+
return new Response(result, {
|
|
1276
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
if (typeof result === "number" || typeof result === "boolean") {
|
|
1280
|
+
return new Response(String(result), {
|
|
1281
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
1282
|
+
});
|
|
1283
|
+
}
|
|
1284
|
+
if (typeof result === "object") {
|
|
1285
|
+
const obj = result;
|
|
1286
|
+
if ("data" in obj && ("status" in obj || "headers" in obj)) {
|
|
1287
|
+
const { data, status = 200, headers = {} } = obj;
|
|
1288
|
+
if (data === null || data === void 0) {
|
|
1289
|
+
return new Response(null, {
|
|
1290
|
+
status: status === 200 ? 204 : status,
|
|
1291
|
+
headers
|
|
1292
|
+
});
|
|
1293
|
+
}
|
|
1294
|
+
if (typeof data === "string" || typeof data === "number" || typeof data === "boolean") {
|
|
1295
|
+
return new Response(String(data), {
|
|
1296
|
+
status,
|
|
1297
|
+
headers: {
|
|
1298
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
1299
|
+
...headers
|
|
1300
|
+
}
|
|
1301
|
+
});
|
|
1302
|
+
}
|
|
1303
|
+
return json(data, status, headers);
|
|
1304
|
+
}
|
|
1305
|
+
return json(result);
|
|
1306
|
+
}
|
|
1307
|
+
return new Response(null, { status: 204 });
|
|
1308
|
+
}
|
|
1309
|
+
function handleValidationError(error) {
|
|
1310
|
+
return json(
|
|
1311
|
+
{
|
|
1312
|
+
success: false,
|
|
1313
|
+
error: "Validation Error",
|
|
1314
|
+
message: error.message,
|
|
1315
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1316
|
+
},
|
|
1317
|
+
400
|
|
1318
|
+
);
|
|
1319
|
+
}
|
|
1320
|
+
function handleInternalError(error) {
|
|
1321
|
+
return json(
|
|
1322
|
+
{
|
|
1323
|
+
success: false,
|
|
1324
|
+
error: "Internal Error",
|
|
1325
|
+
message: error instanceof Error ? error.message : "\u672A\u77E5\u9519\u8BEF"
|
|
1326
|
+
},
|
|
1327
|
+
500
|
|
1328
|
+
);
|
|
1329
|
+
}
|
|
1330
|
+
function isHandler(value) {
|
|
1331
|
+
return typeof value === "function";
|
|
1332
|
+
}
|
|
1333
|
+
function createHandler(schemaOrHandler, maybeHandler) {
|
|
1334
|
+
const hasSchema = !isHandler(schemaOrHandler);
|
|
1335
|
+
const schema = hasSchema ? schemaOrHandler : {};
|
|
1336
|
+
const handler = hasSchema ? maybeHandler : schemaOrHandler;
|
|
1337
|
+
if (schema.body || schema.query || schema.params || schema.headers || schema.cookies) {
|
|
1338
|
+
precompileSchemasUltra(schema);
|
|
1339
|
+
}
|
|
1340
|
+
return async (req) => {
|
|
1341
|
+
try {
|
|
1342
|
+
const query = parseQuery(req);
|
|
1343
|
+
const headers = parseHeaders(req);
|
|
1344
|
+
const cookies = parseCookies(req);
|
|
1345
|
+
const params = req.params || {};
|
|
1346
|
+
let body = void 0;
|
|
1347
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
1348
|
+
const [, parsedBody] = await goAwait(parseBody(req));
|
|
1349
|
+
body = parsedBody;
|
|
1350
|
+
}
|
|
1351
|
+
const data = { body, query, params, headers, cookies };
|
|
1352
|
+
if (schema.body || schema.query || schema.params || schema.headers || schema.cookies) {
|
|
1353
|
+
validateAllSchemasUltra(schema, data);
|
|
1354
|
+
}
|
|
1355
|
+
const result = await handler({
|
|
1356
|
+
req,
|
|
1357
|
+
body,
|
|
1358
|
+
query,
|
|
1359
|
+
params,
|
|
1360
|
+
headers,
|
|
1361
|
+
cookies
|
|
1362
|
+
});
|
|
1363
|
+
return autoResponse(result);
|
|
1364
|
+
} catch (error) {
|
|
1365
|
+
if (error instanceof Error && error.message.includes("\u9A8C\u8BC1\u5931\u8D25")) {
|
|
1366
|
+
return handleValidationError(error);
|
|
1367
|
+
}
|
|
1368
|
+
return handleInternalError(error);
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
}
|
|
1372
|
+
function createHandlerWithExtra(schemaOrHandler, maybeHandler) {
|
|
1373
|
+
const hasSchema = !isHandler(schemaOrHandler);
|
|
1374
|
+
const schema = hasSchema ? schemaOrHandler : {};
|
|
1375
|
+
const handler = hasSchema ? maybeHandler : schemaOrHandler;
|
|
1376
|
+
if (schema.body || schema.query || schema.params || schema.headers || schema.cookies) {
|
|
1377
|
+
precompileSchemasUltra(schema);
|
|
1378
|
+
}
|
|
1379
|
+
return async (req) => {
|
|
1380
|
+
try {
|
|
1381
|
+
const query = parseQuery(req);
|
|
1382
|
+
const headers = parseHeaders(req);
|
|
1383
|
+
const cookies = parseCookies(req);
|
|
1384
|
+
const params = req.params || {};
|
|
1385
|
+
let body = void 0;
|
|
1386
|
+
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
1387
|
+
const [, parsedBody] = await goAwait(parseBody(req));
|
|
1388
|
+
body = parsedBody;
|
|
1389
|
+
}
|
|
1390
|
+
const data = { body, query, params, headers, cookies };
|
|
1391
|
+
if (schema.body || schema.query || schema.params || schema.headers || schema.cookies) {
|
|
1392
|
+
validateAllSchemasUltra(schema, data);
|
|
1393
|
+
}
|
|
1394
|
+
const extras = req.__locals ?? {};
|
|
1395
|
+
const result = await handler({
|
|
1396
|
+
req,
|
|
1397
|
+
body,
|
|
1398
|
+
query,
|
|
1399
|
+
params,
|
|
1400
|
+
headers,
|
|
1401
|
+
cookies,
|
|
1402
|
+
...extras
|
|
1403
|
+
});
|
|
1404
|
+
return autoResponse(result);
|
|
1405
|
+
} catch (error) {
|
|
1406
|
+
if (error instanceof Error && error.message.includes("\u9A8C\u8BC1\u5931\u8D25")) {
|
|
1407
|
+
return handleValidationError(error);
|
|
1408
|
+
}
|
|
1409
|
+
return handleInternalError(error);
|
|
1410
|
+
}
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
function simpleHandler(handler) {
|
|
1414
|
+
return async (req) => {
|
|
1415
|
+
try {
|
|
1416
|
+
const result = await handler({ req });
|
|
1417
|
+
return autoResponse(result);
|
|
1418
|
+
} catch (error) {
|
|
1419
|
+
return handleInternalError(error);
|
|
1420
|
+
}
|
|
1421
|
+
};
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
// src/utils/base64url.ts
|
|
1425
|
+
function base64urlEncode(str) {
|
|
1426
|
+
return btoa(str).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
1427
|
+
}
|
|
1428
|
+
function base64urlDecode(str) {
|
|
1429
|
+
const pad = str.length % 4 === 0 ? "" : "=".repeat(4 - str.length % 4);
|
|
1430
|
+
const base64 = str.replace(/-/g, "+").replace(/_/g, "/") + pad;
|
|
1431
|
+
return atob(base64);
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
// src/utils/handle.ts
|
|
1435
|
+
function getCookie2(req, key) {
|
|
1436
|
+
const cookies = parseCookies(req);
|
|
1437
|
+
return cookies[key] || null;
|
|
1438
|
+
}
|
|
1439
|
+
function setLocals(req, extras) {
|
|
1440
|
+
const target = req;
|
|
1441
|
+
target.__locals = { ...target.__locals ?? {}, ...extras };
|
|
1442
|
+
}
|
|
1443
|
+
function getLocals(req) {
|
|
1444
|
+
const target = req;
|
|
1445
|
+
return target.__locals ?? {};
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
// src/utils/request-validator.ts
|
|
1449
|
+
async function parseRequest(request, params) {
|
|
1450
|
+
const requestData = {
|
|
1451
|
+
body: void 0,
|
|
1452
|
+
query: parseQuery(request),
|
|
1453
|
+
params: params || {},
|
|
1454
|
+
headers: parseHeaders(request),
|
|
1455
|
+
cookies: parseCookies(request)
|
|
1456
|
+
};
|
|
1457
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
1458
|
+
requestData.body = await parseBody(request);
|
|
1459
|
+
}
|
|
1460
|
+
return requestData;
|
|
1461
|
+
}
|
|
1462
|
+
function validateRequest(config, requestData) {
|
|
1463
|
+
try {
|
|
1464
|
+
const validatedData = validateAllSchemasUltra(config, requestData);
|
|
1465
|
+
return {
|
|
1466
|
+
success: true,
|
|
1467
|
+
data: validatedData
|
|
1468
|
+
};
|
|
1469
|
+
} catch (error) {
|
|
1470
|
+
return {
|
|
1471
|
+
success: false,
|
|
1472
|
+
errors: [
|
|
1473
|
+
{
|
|
1474
|
+
field: "unknown",
|
|
1475
|
+
message: error instanceof Error ? error.message : "\u9A8C\u8BC1\u5931\u8D25"
|
|
1476
|
+
}
|
|
1477
|
+
]
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
async function parseAndValidateRequest(request, config, params) {
|
|
1482
|
+
try {
|
|
1483
|
+
const requestData = await parseRequest(request, params);
|
|
1484
|
+
return validateRequest(config, requestData);
|
|
1485
|
+
} catch (error) {
|
|
1486
|
+
return {
|
|
1487
|
+
success: false,
|
|
1488
|
+
errors: [
|
|
1489
|
+
{
|
|
1490
|
+
field: "unknown",
|
|
1491
|
+
message: error instanceof Error ? error.message : "\u8BF7\u6C42\u89E3\u6790\u5931\u8D25"
|
|
1492
|
+
}
|
|
1493
|
+
]
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
function createRequestValidator(config) {
|
|
1498
|
+
return async (request, params) => {
|
|
1499
|
+
return parseAndValidateRequest(request, config, params);
|
|
1500
|
+
};
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
// src/utils/validators/validators.ts
|
|
1504
|
+
import { Type } from "@sinclair/typebox";
|
|
1505
|
+
import { TypeCompiler as TypeCompiler2 } from "@sinclair/typebox/compiler";
|
|
1506
|
+
var compilerCache = /* @__PURE__ */ new WeakMap();
|
|
1507
|
+
function getCompiledValidator(schema) {
|
|
1508
|
+
let compiler = compilerCache.get(schema);
|
|
1509
|
+
if (!compiler) {
|
|
1510
|
+
compiler = TypeCompiler2.Compile(schema);
|
|
1511
|
+
compilerCache.set(schema, compiler);
|
|
1512
|
+
}
|
|
1513
|
+
return compiler;
|
|
1514
|
+
}
|
|
1515
|
+
function precompileSchemas(schemas) {
|
|
1516
|
+
for (const schema of schemas) {
|
|
1517
|
+
getCompiledValidator(schema);
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
function validateSchema(schema, data) {
|
|
1521
|
+
try {
|
|
1522
|
+
const compiler = getCompiledValidator(schema);
|
|
1523
|
+
if (compiler.Check(data)) {
|
|
1524
|
+
return {
|
|
1525
|
+
success: true,
|
|
1526
|
+
data
|
|
1527
|
+
};
|
|
1528
|
+
}
|
|
1529
|
+
const errors = [];
|
|
1530
|
+
const errorIterator = compiler.Errors(data);
|
|
1531
|
+
for (const error of errorIterator) {
|
|
1532
|
+
errors.push({
|
|
1533
|
+
path: error.path,
|
|
1534
|
+
message: error.message,
|
|
1535
|
+
code: "VALIDATION_FAILED",
|
|
1536
|
+
value: error.value,
|
|
1537
|
+
schema: error.schema
|
|
1538
|
+
});
|
|
1539
|
+
}
|
|
1540
|
+
return {
|
|
1541
|
+
success: false,
|
|
1542
|
+
errors
|
|
1543
|
+
};
|
|
1544
|
+
} catch (error) {
|
|
1545
|
+
return {
|
|
1546
|
+
success: false,
|
|
1547
|
+
errors: [
|
|
1548
|
+
{
|
|
1549
|
+
path: "",
|
|
1550
|
+
message: error instanceof Error ? error.message : "Unknown validation error",
|
|
1551
|
+
code: "VALIDATION_EXCEPTION",
|
|
1552
|
+
value: data
|
|
1553
|
+
}
|
|
1554
|
+
]
|
|
1555
|
+
};
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
function createValidator(schema) {
|
|
1559
|
+
const compiler = getCompiledValidator(schema);
|
|
1560
|
+
return (data) => {
|
|
1561
|
+
if (compiler.Check(data)) {
|
|
1562
|
+
return { success: true, data };
|
|
1563
|
+
}
|
|
1564
|
+
const errors = [];
|
|
1565
|
+
for (const error of compiler.Errors(data)) {
|
|
1566
|
+
errors.push({
|
|
1567
|
+
path: error.path,
|
|
1568
|
+
message: error.message,
|
|
1569
|
+
code: "VALIDATION_FAILED",
|
|
1570
|
+
value: error.value,
|
|
1571
|
+
schema: error.schema
|
|
1572
|
+
});
|
|
1573
|
+
}
|
|
1574
|
+
return { success: false, errors };
|
|
1575
|
+
};
|
|
1576
|
+
}
|
|
1577
|
+
function validateFast(schema, data) {
|
|
1578
|
+
const compiler = getCompiledValidator(schema);
|
|
1579
|
+
return compiler.Check(data);
|
|
1580
|
+
}
|
|
1581
|
+
function getValidatorCacheStats() {
|
|
1582
|
+
return {
|
|
1583
|
+
cacheType: "WeakMap",
|
|
1584
|
+
note: "WeakMap \u4E0D\u652F\u6301 size \u5C5E\u6027\uFF0C\u7F13\u5B58\u4F1A\u968F Schema \u5BF9\u8C61\u81EA\u52A8\u6E05\u7406"
|
|
1585
|
+
};
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
// src/middleware/authMiddleware.ts
|
|
1589
|
+
var requireAuth = async (req, next) => {
|
|
1590
|
+
const token = getCookie2(req, "auth");
|
|
1591
|
+
if (!token || token !== "valid-token") {
|
|
1592
|
+
throw new VafastError("Unauthorized", {
|
|
1593
|
+
status: 401,
|
|
1594
|
+
type: "unauthorized",
|
|
1595
|
+
expose: true
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1598
|
+
return next();
|
|
1599
|
+
};
|
|
1600
|
+
|
|
1601
|
+
// src/middleware/rateLimit.ts
|
|
1602
|
+
var store = /* @__PURE__ */ new Map();
|
|
1603
|
+
function rateLimit(options = {}) {
|
|
1604
|
+
const windowMs = options.windowMs ?? 6e4;
|
|
1605
|
+
const max = options.max ?? 30;
|
|
1606
|
+
const keyFn = options.keyFn ?? getIP;
|
|
1607
|
+
return async (req, next) => {
|
|
1608
|
+
const key = keyFn(req);
|
|
1609
|
+
const now = Date.now();
|
|
1610
|
+
const entry = store.get(key);
|
|
1611
|
+
if (entry && entry.expires > now) {
|
|
1612
|
+
if (entry.count >= max) {
|
|
1613
|
+
throw new VafastError("Too many requests", {
|
|
1614
|
+
status: 429,
|
|
1615
|
+
type: "rate_limit",
|
|
1616
|
+
expose: true
|
|
1617
|
+
});
|
|
1618
|
+
}
|
|
1619
|
+
entry.count += 1;
|
|
1620
|
+
} else {
|
|
1621
|
+
store.set(key, { count: 1, expires: now + windowMs });
|
|
1622
|
+
}
|
|
1623
|
+
return next();
|
|
1624
|
+
};
|
|
1625
|
+
}
|
|
1626
|
+
function getIP(req) {
|
|
1627
|
+
return req.headers.get("cf-connecting-ip") || // Cloudflare
|
|
1628
|
+
req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || // Vercel
|
|
1629
|
+
"unknown";
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
// src/middleware/cors.ts
|
|
1633
|
+
function createCORS(options = {}) {
|
|
1634
|
+
const {
|
|
1635
|
+
origin = [],
|
|
1636
|
+
methods = ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
1637
|
+
headers = [],
|
|
1638
|
+
credentials = false,
|
|
1639
|
+
maxAge
|
|
1640
|
+
} = options;
|
|
1641
|
+
return async (req, next) => {
|
|
1642
|
+
const reqOrigin = req.headers.get("Origin") || "";
|
|
1643
|
+
const isAllowedOrigin = origin === "*" || origin.includes(reqOrigin);
|
|
1644
|
+
if (req.method === "OPTIONS") {
|
|
1645
|
+
const resHeaders = new Headers();
|
|
1646
|
+
if (isAllowedOrigin) {
|
|
1647
|
+
resHeaders.set(
|
|
1648
|
+
"Access-Control-Allow-Origin",
|
|
1649
|
+
origin === "*" ? "*" : reqOrigin
|
|
1650
|
+
);
|
|
1651
|
+
resHeaders.set("Access-Control-Allow-Methods", methods.join(","));
|
|
1652
|
+
resHeaders.set("Access-Control-Allow-Headers", headers.join(","));
|
|
1653
|
+
if (credentials)
|
|
1654
|
+
resHeaders.set("Access-Control-Allow-Credentials", "true");
|
|
1655
|
+
if (maxAge) resHeaders.set("Access-Control-Max-Age", maxAge.toString());
|
|
1656
|
+
}
|
|
1657
|
+
return new Response(null, { status: 204, headers: resHeaders });
|
|
1658
|
+
}
|
|
1659
|
+
const res = await next();
|
|
1660
|
+
if (isAllowedOrigin) {
|
|
1661
|
+
res.headers.set(
|
|
1662
|
+
"Access-Control-Allow-Origin",
|
|
1663
|
+
origin === "*" ? "*" : reqOrigin
|
|
1664
|
+
);
|
|
1665
|
+
if (credentials)
|
|
1666
|
+
res.headers.set("Access-Control-Allow-Credentials", "true");
|
|
1667
|
+
}
|
|
1668
|
+
return res;
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
// src/auth/token.ts
|
|
1673
|
+
var TokenError = class extends Error {
|
|
1674
|
+
constructor(message, code) {
|
|
1675
|
+
super(message);
|
|
1676
|
+
this.code = code;
|
|
1677
|
+
this.name = "TokenError";
|
|
1678
|
+
}
|
|
1679
|
+
};
|
|
1680
|
+
var encoder = new TextEncoder();
|
|
1681
|
+
async function sign(data, secret) {
|
|
1682
|
+
const key = await crypto.subtle.importKey(
|
|
1683
|
+
"raw",
|
|
1684
|
+
encoder.encode(secret),
|
|
1685
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
1686
|
+
false,
|
|
1687
|
+
["sign"]
|
|
1688
|
+
);
|
|
1689
|
+
const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(data));
|
|
1690
|
+
return btoa(
|
|
1691
|
+
String.fromCharCode.apply(null, Array.from(new Uint8Array(signature)))
|
|
1692
|
+
);
|
|
1693
|
+
}
|
|
1694
|
+
async function generateToken(payload, secret, options = {}) {
|
|
1695
|
+
const { expiresIn = 3600, issuer, audience, subject } = options;
|
|
1696
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
1697
|
+
const tokenPayload = {
|
|
1698
|
+
...payload,
|
|
1699
|
+
iat: now,
|
|
1700
|
+
exp: now + expiresIn
|
|
1701
|
+
};
|
|
1702
|
+
if (issuer) tokenPayload.iss = issuer;
|
|
1703
|
+
if (audience) tokenPayload.aud = audience;
|
|
1704
|
+
if (subject) tokenPayload.sub = subject;
|
|
1705
|
+
const data = base64urlEncode(JSON.stringify(tokenPayload));
|
|
1706
|
+
const sig = await sign(data, secret);
|
|
1707
|
+
const token = `${data}.${base64urlEncode(sig)}`;
|
|
1708
|
+
return {
|
|
1709
|
+
payload: tokenPayload,
|
|
1710
|
+
token,
|
|
1711
|
+
expiresAt: tokenPayload.exp * 1e3
|
|
1712
|
+
// 转换为毫秒
|
|
1713
|
+
};
|
|
1714
|
+
}
|
|
1715
|
+
async function verifyToken(token, secret) {
|
|
1716
|
+
try {
|
|
1717
|
+
const [data, sig] = token.split(".");
|
|
1718
|
+
if (!data || !sig) {
|
|
1719
|
+
throw new TokenError("\u4EE4\u724C\u683C\u5F0F\u65E0\u6548", "MALFORMED_TOKEN");
|
|
1720
|
+
}
|
|
1721
|
+
const expectedSig = await sign(data, secret);
|
|
1722
|
+
const expected = base64urlEncode(expectedSig);
|
|
1723
|
+
if (sig !== expected) {
|
|
1724
|
+
throw new TokenError("\u4EE4\u724C\u7B7E\u540D\u65E0\u6548", "INVALID_SIGNATURE");
|
|
1725
|
+
}
|
|
1726
|
+
const payload = JSON.parse(base64urlDecode(data));
|
|
1727
|
+
if (payload.exp && Date.now() / 1e3 > payload.exp) {
|
|
1728
|
+
throw new TokenError("\u4EE4\u724C\u5DF2\u8FC7\u671F", "EXPIRED_TOKEN");
|
|
1729
|
+
}
|
|
1730
|
+
return payload;
|
|
1731
|
+
} catch (error) {
|
|
1732
|
+
if (error instanceof TokenError) {
|
|
1733
|
+
throw error;
|
|
1734
|
+
}
|
|
1735
|
+
throw new TokenError("\u4EE4\u724C\u9A8C\u8BC1\u5931\u8D25", "INVALID_TOKEN");
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
function parseToken(token) {
|
|
1739
|
+
try {
|
|
1740
|
+
const [data] = token.split(".");
|
|
1741
|
+
if (!data) return null;
|
|
1742
|
+
return JSON.parse(base64urlDecode(data));
|
|
1743
|
+
} catch {
|
|
1744
|
+
return null;
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
function isTokenExpired(token) {
|
|
1748
|
+
const payload = parseToken(token);
|
|
1749
|
+
if (!payload || !payload.exp) return true;
|
|
1750
|
+
return Date.now() / 1e3 > payload.exp;
|
|
1751
|
+
}
|
|
1752
|
+
function getTokenTimeRemaining(token) {
|
|
1753
|
+
const payload = parseToken(token);
|
|
1754
|
+
if (!payload || !payload.exp) return 0;
|
|
1755
|
+
const remaining = payload.exp - Date.now() / 1e3;
|
|
1756
|
+
return Math.max(0, Math.floor(remaining));
|
|
1757
|
+
}
|
|
1758
|
+
async function refreshToken(token, secret, options = {}) {
|
|
1759
|
+
try {
|
|
1760
|
+
const payload = await verifyToken(token, secret);
|
|
1761
|
+
if (!payload) return null;
|
|
1762
|
+
const { exp, iat, ...cleanPayload } = payload;
|
|
1763
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
1764
|
+
return await generateToken(cleanPayload, secret, options);
|
|
1765
|
+
} catch {
|
|
1766
|
+
return null;
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
async function createTokenPair(payload, secret, options = {}) {
|
|
1770
|
+
const accessToken = await generateToken(payload, secret, {
|
|
1771
|
+
...options,
|
|
1772
|
+
expiresIn: options.expiresIn || 3600
|
|
1773
|
+
// 1小时
|
|
1774
|
+
});
|
|
1775
|
+
const refreshToken2 = await generateToken(payload, secret, {
|
|
1776
|
+
...options,
|
|
1777
|
+
expiresIn: 7 * 24 * 3600
|
|
1778
|
+
// 7天
|
|
1779
|
+
});
|
|
1780
|
+
return { accessToken, refreshToken: refreshToken2 };
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
// src/middleware/auth.ts
|
|
1784
|
+
function createAuth(options) {
|
|
1785
|
+
const {
|
|
1786
|
+
secret,
|
|
1787
|
+
cookieName = "auth",
|
|
1788
|
+
headerName = "authorization",
|
|
1789
|
+
required = true,
|
|
1790
|
+
roles = [],
|
|
1791
|
+
permissions = []
|
|
1792
|
+
} = options;
|
|
1793
|
+
return async (req, next) => {
|
|
1794
|
+
const token = getCookie2(req, cookieName) || req.headers.get(headerName)?.replace("Bearer ", "") || "";
|
|
1795
|
+
if (!token && required) {
|
|
1796
|
+
throw new VafastError("\u7F3A\u5C11\u8BA4\u8BC1\u4EE4\u724C", {
|
|
1797
|
+
status: 401,
|
|
1798
|
+
type: "unauthorized",
|
|
1799
|
+
expose: true
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
if (!token && !required) {
|
|
1803
|
+
return next();
|
|
1804
|
+
}
|
|
1805
|
+
try {
|
|
1806
|
+
const user = await verifyToken(token, secret);
|
|
1807
|
+
if (!user) {
|
|
1808
|
+
throw new VafastError("\u4EE4\u724C\u9A8C\u8BC1\u5931\u8D25", {
|
|
1809
|
+
status: 401,
|
|
1810
|
+
type: "unauthorized",
|
|
1811
|
+
expose: true
|
|
1812
|
+
});
|
|
1813
|
+
}
|
|
1814
|
+
if (roles.length > 0 && user.role && !roles.includes(user.role)) {
|
|
1815
|
+
throw new VafastError("\u6743\u9650\u4E0D\u8DB3", {
|
|
1816
|
+
status: 403,
|
|
1817
|
+
type: "forbidden",
|
|
1818
|
+
expose: true
|
|
1819
|
+
});
|
|
1820
|
+
}
|
|
1821
|
+
if (permissions.length > 0 && user.permissions) {
|
|
1822
|
+
const userPermissions = Array.isArray(user.permissions) ? user.permissions : [user.permissions];
|
|
1823
|
+
const hasPermission = permissions.some(
|
|
1824
|
+
(permission) => userPermissions.includes(permission)
|
|
1825
|
+
);
|
|
1826
|
+
if (!hasPermission) {
|
|
1827
|
+
throw new VafastError("\u6743\u9650\u4E0D\u8DB3", {
|
|
1828
|
+
status: 403,
|
|
1829
|
+
type: "forbidden",
|
|
1830
|
+
expose: true
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
req.user = user;
|
|
1835
|
+
req.token = token;
|
|
1836
|
+
return next();
|
|
1837
|
+
} catch (error) {
|
|
1838
|
+
if (error instanceof TokenError) {
|
|
1839
|
+
let status = 401;
|
|
1840
|
+
let message = "\u8BA4\u8BC1\u5931\u8D25";
|
|
1841
|
+
switch (error.code) {
|
|
1842
|
+
case "EXPIRED_TOKEN":
|
|
1843
|
+
status = 401;
|
|
1844
|
+
message = "\u4EE4\u724C\u5DF2\u8FC7\u671F";
|
|
1845
|
+
break;
|
|
1846
|
+
case "INVALID_SIGNATURE":
|
|
1847
|
+
status = 401;
|
|
1848
|
+
message = "\u4EE4\u724C\u7B7E\u540D\u65E0\u6548";
|
|
1849
|
+
break;
|
|
1850
|
+
case "MALFORMED_TOKEN":
|
|
1851
|
+
status = 400;
|
|
1852
|
+
message = "\u4EE4\u724C\u683C\u5F0F\u9519\u8BEF";
|
|
1853
|
+
break;
|
|
1854
|
+
default:
|
|
1855
|
+
status = 401;
|
|
1856
|
+
message = "\u4EE4\u724C\u9A8C\u8BC1\u5931\u8D25";
|
|
1857
|
+
}
|
|
1858
|
+
throw new VafastError(message, {
|
|
1859
|
+
status,
|
|
1860
|
+
type: "unauthorized",
|
|
1861
|
+
expose: true
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
if (error instanceof VafastError) {
|
|
1865
|
+
throw error;
|
|
1866
|
+
}
|
|
1867
|
+
throw new VafastError("\u8BA4\u8BC1\u8FC7\u7A0B\u4E2D\u53D1\u751F\u9519\u8BEF", {
|
|
1868
|
+
status: 500,
|
|
1869
|
+
type: "internal_error",
|
|
1870
|
+
expose: false
|
|
1871
|
+
});
|
|
1872
|
+
}
|
|
1873
|
+
};
|
|
1874
|
+
}
|
|
1875
|
+
function createOptionalAuth(options) {
|
|
1876
|
+
return createAuth({ ...options, required: false });
|
|
1877
|
+
}
|
|
1878
|
+
function createRoleAuth(roles, options) {
|
|
1879
|
+
return createAuth({ ...options, roles });
|
|
1880
|
+
}
|
|
1881
|
+
function createPermissionAuth(permissions, options) {
|
|
1882
|
+
return createAuth({ ...options, permissions });
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
// src/defineRoute.ts
|
|
1886
|
+
function defineRoutes(routes) {
|
|
1887
|
+
return routes;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
// src/types/route.ts
|
|
1891
|
+
function createTypedRoute(config) {
|
|
1892
|
+
return config;
|
|
1893
|
+
}
|
|
1894
|
+
function isTypedRoute(route) {
|
|
1895
|
+
return route && typeof route === "object" && "method" in route && "path" in route && "handler" in route;
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
// src/node-server/serve.ts
|
|
1899
|
+
import {
|
|
1900
|
+
createServer
|
|
1901
|
+
} from "http";
|
|
1902
|
+
|
|
1903
|
+
// src/node-server/request.ts
|
|
1904
|
+
import { Readable } from "stream";
|
|
1905
|
+
var requestCache = /* @__PURE__ */ Symbol("requestCache");
|
|
1906
|
+
var incomingKey = /* @__PURE__ */ Symbol("incoming");
|
|
1907
|
+
var urlKey = /* @__PURE__ */ Symbol("url");
|
|
1908
|
+
var headersKey = /* @__PURE__ */ Symbol("headers");
|
|
1909
|
+
function parseHeaders2(rawHeaders) {
|
|
1910
|
+
const headers = new Headers();
|
|
1911
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
1912
|
+
const key = rawHeaders[i];
|
|
1913
|
+
const value = rawHeaders[i + 1];
|
|
1914
|
+
if (key.charCodeAt(0) !== 58) {
|
|
1915
|
+
headers.append(key, value);
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
return headers;
|
|
1919
|
+
}
|
|
1920
|
+
function toWebStream(nodeStream) {
|
|
1921
|
+
return nodeStream;
|
|
1922
|
+
}
|
|
1923
|
+
function createRealRequest(proxy) {
|
|
1924
|
+
const incoming = proxy[incomingKey];
|
|
1925
|
+
const method = incoming.method || "GET";
|
|
1926
|
+
const init = {
|
|
1927
|
+
method,
|
|
1928
|
+
headers: proxy[headersKey] || parseHeaders2(incoming.rawHeaders)
|
|
1929
|
+
};
|
|
1930
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
1931
|
+
const nodeWebStream = Readable.toWeb(
|
|
1932
|
+
incoming
|
|
1933
|
+
);
|
|
1934
|
+
init.body = toWebStream(nodeWebStream);
|
|
1935
|
+
init.duplex = "half";
|
|
1936
|
+
}
|
|
1937
|
+
return new Request(proxy[urlKey], init);
|
|
1938
|
+
}
|
|
1939
|
+
var requestPrototype = {};
|
|
1940
|
+
Object.defineProperty(requestPrototype, "method", {
|
|
1941
|
+
get() {
|
|
1942
|
+
const self = this;
|
|
1943
|
+
return self[incomingKey].method || "GET";
|
|
1944
|
+
},
|
|
1945
|
+
enumerable: true
|
|
1946
|
+
});
|
|
1947
|
+
Object.defineProperty(requestPrototype, "url", {
|
|
1948
|
+
get() {
|
|
1949
|
+
const self = this;
|
|
1950
|
+
return self[urlKey];
|
|
1951
|
+
},
|
|
1952
|
+
enumerable: true
|
|
1953
|
+
});
|
|
1954
|
+
Object.defineProperty(requestPrototype, "headers", {
|
|
1955
|
+
get() {
|
|
1956
|
+
const self = this;
|
|
1957
|
+
if (!self[headersKey]) {
|
|
1958
|
+
self[headersKey] = parseHeaders2(self[incomingKey].rawHeaders);
|
|
1959
|
+
}
|
|
1960
|
+
return self[headersKey];
|
|
1961
|
+
},
|
|
1962
|
+
enumerable: true
|
|
1963
|
+
});
|
|
1964
|
+
Object.defineProperty(requestPrototype, "_getRequest", {
|
|
1965
|
+
value: function() {
|
|
1966
|
+
const self = this;
|
|
1967
|
+
if (!self[requestCache]) {
|
|
1968
|
+
self[requestCache] = createRealRequest(self);
|
|
1969
|
+
}
|
|
1970
|
+
return self[requestCache];
|
|
1971
|
+
},
|
|
1972
|
+
enumerable: false
|
|
1973
|
+
});
|
|
1974
|
+
var proxyGetters = [
|
|
1975
|
+
"body",
|
|
1976
|
+
"bodyUsed",
|
|
1977
|
+
"signal",
|
|
1978
|
+
"cache",
|
|
1979
|
+
"credentials",
|
|
1980
|
+
"destination",
|
|
1981
|
+
"integrity",
|
|
1982
|
+
"mode",
|
|
1983
|
+
"redirect",
|
|
1984
|
+
"referrer",
|
|
1985
|
+
"referrerPolicy",
|
|
1986
|
+
"keepalive"
|
|
1987
|
+
];
|
|
1988
|
+
proxyGetters.forEach((key) => {
|
|
1989
|
+
Object.defineProperty(requestPrototype, key, {
|
|
1990
|
+
get() {
|
|
1991
|
+
const self = this;
|
|
1992
|
+
return self._getRequest()[key];
|
|
1993
|
+
},
|
|
1994
|
+
enumerable: true
|
|
1995
|
+
});
|
|
1996
|
+
});
|
|
1997
|
+
var proxyMethods = [
|
|
1998
|
+
"arrayBuffer",
|
|
1999
|
+
"blob",
|
|
2000
|
+
"clone",
|
|
2001
|
+
"formData",
|
|
2002
|
+
"json",
|
|
2003
|
+
"text"
|
|
2004
|
+
];
|
|
2005
|
+
proxyMethods.forEach((key) => {
|
|
2006
|
+
Object.defineProperty(requestPrototype, key, {
|
|
2007
|
+
value: function() {
|
|
2008
|
+
const self = this;
|
|
2009
|
+
const req = self._getRequest();
|
|
2010
|
+
return req[key].call(req);
|
|
2011
|
+
},
|
|
2012
|
+
enumerable: true
|
|
2013
|
+
});
|
|
2014
|
+
});
|
|
2015
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
2016
|
+
function createProxyRequest(incoming, defaultHost) {
|
|
2017
|
+
const req = Object.create(requestPrototype);
|
|
2018
|
+
req[incomingKey] = incoming;
|
|
2019
|
+
const host = incoming.headers.host || defaultHost;
|
|
2020
|
+
const protocol = incoming.socket.encrypted ? "https" : "http";
|
|
2021
|
+
req[urlKey] = `${protocol}://${host}${incoming.url || "/"}`;
|
|
2022
|
+
return req;
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
// src/node-server/response.ts
|
|
2026
|
+
function buildOutgoingHeaders(headers) {
|
|
2027
|
+
const result = {};
|
|
2028
|
+
const cookies = [];
|
|
2029
|
+
headers.forEach((value, key) => {
|
|
2030
|
+
if (key === "set-cookie") {
|
|
2031
|
+
cookies.push(value);
|
|
2032
|
+
} else {
|
|
2033
|
+
result[key] = value;
|
|
2034
|
+
}
|
|
2035
|
+
});
|
|
2036
|
+
if (cookies.length > 0) {
|
|
2037
|
+
result["set-cookie"] = cookies;
|
|
2038
|
+
}
|
|
2039
|
+
return result;
|
|
2040
|
+
}
|
|
2041
|
+
async function writeBodyStream(body, outgoing) {
|
|
2042
|
+
const reader = body.getReader();
|
|
2043
|
+
try {
|
|
2044
|
+
while (true) {
|
|
2045
|
+
const { done, value } = await reader.read();
|
|
2046
|
+
if (done) {
|
|
2047
|
+
break;
|
|
2048
|
+
}
|
|
2049
|
+
const canContinue = outgoing.write(value);
|
|
2050
|
+
if (!canContinue) {
|
|
2051
|
+
await new Promise((resolve) => {
|
|
2052
|
+
outgoing.once("drain", resolve);
|
|
2053
|
+
});
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
} finally {
|
|
2057
|
+
reader.releaseLock();
|
|
2058
|
+
}
|
|
2059
|
+
}
|
|
2060
|
+
async function writeResponse(response, outgoing) {
|
|
2061
|
+
outgoing.statusCode = response.status;
|
|
2062
|
+
const headers = buildOutgoingHeaders(response.headers);
|
|
2063
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
2064
|
+
outgoing.setHeader(key, value);
|
|
2065
|
+
}
|
|
2066
|
+
const body = response.body;
|
|
2067
|
+
if (!body) {
|
|
2068
|
+
outgoing.end();
|
|
2069
|
+
return;
|
|
2070
|
+
}
|
|
2071
|
+
try {
|
|
2072
|
+
await writeBodyStream(body, outgoing);
|
|
2073
|
+
outgoing.end();
|
|
2074
|
+
} catch (error) {
|
|
2075
|
+
if (!outgoing.destroyed) {
|
|
2076
|
+
outgoing.destroy(
|
|
2077
|
+
error instanceof Error ? error : new Error(String(error))
|
|
2078
|
+
);
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
|
|
2083
|
+
// src/node-server/serve.ts
|
|
2084
|
+
function createRequestHandler(fetch, defaultHost, onError) {
|
|
2085
|
+
return async (incoming, outgoing) => {
|
|
2086
|
+
try {
|
|
2087
|
+
const request = createProxyRequest(incoming, defaultHost);
|
|
2088
|
+
const response = await fetch(request);
|
|
2089
|
+
await writeResponse(response, outgoing);
|
|
2090
|
+
} catch (error) {
|
|
2091
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
2092
|
+
if (onError) {
|
|
2093
|
+
try {
|
|
2094
|
+
const errorResponse = await onError(err);
|
|
2095
|
+
await writeResponse(errorResponse, outgoing);
|
|
2096
|
+
return;
|
|
2097
|
+
} catch {
|
|
2098
|
+
}
|
|
2099
|
+
}
|
|
2100
|
+
if (!outgoing.headersSent) {
|
|
2101
|
+
outgoing.statusCode = 500;
|
|
2102
|
+
outgoing.setHeader("Content-Type", "text/plain");
|
|
2103
|
+
outgoing.end("Internal Server Error");
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
};
|
|
2107
|
+
}
|
|
2108
|
+
function serve(options, callback) {
|
|
2109
|
+
const { fetch, port = 3e3, hostname = "0.0.0.0", onError } = options;
|
|
2110
|
+
const defaultHost = `${hostname === "0.0.0.0" ? "localhost" : hostname}:${port}`;
|
|
2111
|
+
const handler = createRequestHandler(fetch, defaultHost, onError);
|
|
2112
|
+
const server = createServer(handler);
|
|
2113
|
+
server.listen(port, hostname, callback);
|
|
2114
|
+
return {
|
|
2115
|
+
server,
|
|
2116
|
+
port,
|
|
2117
|
+
hostname,
|
|
2118
|
+
stop: () => new Promise((resolve, reject) => {
|
|
2119
|
+
server.close((err) => {
|
|
2120
|
+
if (err) reject(err);
|
|
2121
|
+
else resolve();
|
|
2122
|
+
});
|
|
2123
|
+
})
|
|
2124
|
+
};
|
|
2125
|
+
}
|
|
2126
|
+
|
|
2127
|
+
// src/index.ts
|
|
2128
|
+
import { Type as Type2 } from "@sinclair/typebox";
|
|
2129
|
+
export {
|
|
2130
|
+
BaseServer,
|
|
2131
|
+
ComponentServer,
|
|
2132
|
+
DependencyManager,
|
|
2133
|
+
HtmlRenderer,
|
|
2134
|
+
Server,
|
|
2135
|
+
ServerFactory,
|
|
2136
|
+
TokenError,
|
|
2137
|
+
Type2 as Type,
|
|
2138
|
+
VafastError,
|
|
2139
|
+
base64urlDecode,
|
|
2140
|
+
base64urlEncode,
|
|
2141
|
+
composeMiddleware,
|
|
2142
|
+
createAuth,
|
|
2143
|
+
createCORS,
|
|
2144
|
+
createHandler,
|
|
2145
|
+
createHandlerWithExtra,
|
|
2146
|
+
createOptionalAuth,
|
|
2147
|
+
createPermissionAuth,
|
|
2148
|
+
createRequestValidator,
|
|
2149
|
+
createRoleAuth,
|
|
2150
|
+
createTokenPair,
|
|
2151
|
+
createTypedRoute,
|
|
2152
|
+
createValidator,
|
|
2153
|
+
defineRoutes,
|
|
2154
|
+
empty,
|
|
2155
|
+
flattenNestedRoutes,
|
|
2156
|
+
generateToken,
|
|
2157
|
+
getCookie,
|
|
2158
|
+
getHeader,
|
|
2159
|
+
getLocals,
|
|
2160
|
+
getTokenTimeRemaining,
|
|
2161
|
+
getValidatorCacheStats,
|
|
2162
|
+
goAwait,
|
|
2163
|
+
html,
|
|
2164
|
+
isTokenExpired,
|
|
2165
|
+
isTypedRoute,
|
|
2166
|
+
json,
|
|
2167
|
+
normalizePath,
|
|
2168
|
+
parseAndValidateRequest,
|
|
2169
|
+
parseBody,
|
|
2170
|
+
parseCookies,
|
|
2171
|
+
parseCookiesFast,
|
|
2172
|
+
parseHeaders,
|
|
2173
|
+
parseQuery,
|
|
2174
|
+
parseQueryFast,
|
|
2175
|
+
parseRequest,
|
|
2176
|
+
parseToken,
|
|
2177
|
+
precompileSchemas,
|
|
2178
|
+
rateLimit,
|
|
2179
|
+
redirect,
|
|
2180
|
+
refreshToken,
|
|
2181
|
+
requireAuth,
|
|
2182
|
+
serve,
|
|
2183
|
+
setLocals,
|
|
2184
|
+
simpleHandler,
|
|
2185
|
+
stream,
|
|
2186
|
+
text,
|
|
2187
|
+
validateFast,
|
|
2188
|
+
validateRequest,
|
|
2189
|
+
validateSchema,
|
|
2190
|
+
verifyToken
|
|
2191
|
+
};
|
|
2192
|
+
/**
|
|
2193
|
+
* Go 风格的错误处理工具
|
|
2194
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
2195
|
+
*
|
|
2196
|
+
* @author Framework Team
|
|
2197
|
+
* @version 1.0.0
|
|
2198
|
+
* @license MIT
|
|
2199
|
+
*/
|
|
2200
|
+
/**
|
|
2201
|
+
* 超优化版Schema验证器 v5.0.0
|
|
2202
|
+
*
|
|
2203
|
+
* 使用经过验证的优化技术,确保极致性能
|
|
2204
|
+
* - 内联函数调用
|
|
2205
|
+
* - 预编译缓存优化
|
|
2206
|
+
* - 内存池优化
|
|
2207
|
+
* - 位运算优化
|
|
2208
|
+
* - 类型特化优化
|
|
2209
|
+
* - 循环展开优化
|
|
2210
|
+
* - 位掩码优化
|
|
2211
|
+
* - 字符串池优化
|
|
2212
|
+
* - 内联缓存优化
|
|
2213
|
+
* - 内存对齐优化
|
|
2214
|
+
*
|
|
2215
|
+
* @author Framework Team
|
|
2216
|
+
* @version 5.0.0
|
|
2217
|
+
* @license MIT
|
|
2218
|
+
*/
|
|
2219
|
+
/**
|
|
2220
|
+
* 类型安全的路由处理器工厂
|
|
2221
|
+
*
|
|
2222
|
+
* 非柯里化设计,API 更简洁
|
|
2223
|
+
*
|
|
2224
|
+
* @author Framework Team
|
|
2225
|
+
* @version 3.0.0
|
|
2226
|
+
* @license MIT
|
|
2227
|
+
*/
|
|
2228
|
+
/**
|
|
2229
|
+
* 请求解析和验证器
|
|
2230
|
+
*
|
|
2231
|
+
* 解析handler的req参数,使用Ultra验证器进行验证,
|
|
2232
|
+
* 并类型安全地返回解析出来的值
|
|
2233
|
+
*
|
|
2234
|
+
* @author Framework Team
|
|
2235
|
+
* @version 1.0.0
|
|
2236
|
+
* @license MIT
|
|
2237
|
+
*/
|
|
2238
|
+
/**
|
|
2239
|
+
* Schema 类型定义
|
|
2240
|
+
*
|
|
2241
|
+
* 使用 TSchema 约束替代 any,提供完整的类型安全
|
|
2242
|
+
*
|
|
2243
|
+
* @author Framework Team
|
|
2244
|
+
* @version 1.0.0
|
|
2245
|
+
* @license MIT
|
|
2246
|
+
*/
|
|
2247
|
+
//# sourceMappingURL=index.js.map
|