vafast 0.1.11 → 0.1.13
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 +12 -0
- package/dist/index.js +13 -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/index.d.ts +3 -0
- package/dist/types/index.js +3 -0
- package/dist/types/route.d.ts +39 -0
- package/dist/types/route.js +11 -0
- package/dist/types/types.d.ts +18 -0
- package/dist/types/types.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/index.d.ts +10 -0
- package/dist/utils/index.js +11 -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 +182 -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,70 @@
|
|
|
1
|
+
import { Server } from "../server";
|
|
2
|
+
import { ComponentServer } from "./component-server";
|
|
3
|
+
/**
|
|
4
|
+
* 服务器工厂类
|
|
5
|
+
* 用于创建和管理不同类型的服务器
|
|
6
|
+
*/
|
|
7
|
+
export class ServerFactory {
|
|
8
|
+
servers = new Map();
|
|
9
|
+
/**
|
|
10
|
+
* 创建标准REST API服务器
|
|
11
|
+
*/
|
|
12
|
+
createRestServer(routes) {
|
|
13
|
+
const server = new Server(routes);
|
|
14
|
+
this.servers.set("rest", server);
|
|
15
|
+
return server;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 创建组件服务器
|
|
19
|
+
*/
|
|
20
|
+
createComponentServer(routes) {
|
|
21
|
+
const server = new ComponentServer(routes);
|
|
22
|
+
this.servers.set("component", server);
|
|
23
|
+
return server;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 获取指定类型的服务器
|
|
27
|
+
*/
|
|
28
|
+
getServer(type) {
|
|
29
|
+
return this.servers.get(type);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 获取所有服务器
|
|
33
|
+
*/
|
|
34
|
+
getAllServers() {
|
|
35
|
+
return this.servers;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 移除指定类型的服务器
|
|
39
|
+
*/
|
|
40
|
+
removeServer(type) {
|
|
41
|
+
return this.servers.delete(type);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 清除所有服务器
|
|
45
|
+
*/
|
|
46
|
+
clearServers() {
|
|
47
|
+
this.servers.clear();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 获取服务器状态信息
|
|
51
|
+
*/
|
|
52
|
+
getServerStatus() {
|
|
53
|
+
const status = {};
|
|
54
|
+
for (const [name, server] of this.servers) {
|
|
55
|
+
if (server instanceof Server) {
|
|
56
|
+
status[name] = {
|
|
57
|
+
type: "REST API",
|
|
58
|
+
routes: server.routes?.length || 0,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
else if (server instanceof ComponentServer) {
|
|
62
|
+
status[name] = {
|
|
63
|
+
type: "Component",
|
|
64
|
+
routes: server.routes?.length || 0,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return status;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { matchPath, flattenNestedRoutes } from "../router";
|
|
2
|
+
import { composeMiddleware } from "../middleware";
|
|
3
|
+
import { json } from "../utils/response";
|
|
4
|
+
import { BaseServer } from "./base-server";
|
|
5
|
+
import { PathMatcher } from "../utils/path-matcher";
|
|
6
|
+
export class Server extends BaseServer {
|
|
7
|
+
routes;
|
|
8
|
+
constructor(routes) {
|
|
9
|
+
super();
|
|
10
|
+
// 扁平化嵌套路由,计算完整的中间件链
|
|
11
|
+
this.routes = flattenNestedRoutes(routes);
|
|
12
|
+
// 在构造时按路由"特异性"排序:静态 > 动态(:param) > 通配符(*)
|
|
13
|
+
this.routes = this.routes.sort((a, b) => PathMatcher.calculatePathScore(b.fullPath) -
|
|
14
|
+
PathMatcher.calculatePathScore(a.fullPath));
|
|
15
|
+
// 检测路由冲突
|
|
16
|
+
this.detectRouteConflicts(this.routes);
|
|
17
|
+
// 打印扁平化后的路由信息
|
|
18
|
+
this.logFlattenedRoutes(this.routes);
|
|
19
|
+
}
|
|
20
|
+
fetch = async (req) => {
|
|
21
|
+
const { pathname } = new URL(req.url);
|
|
22
|
+
const method = req.method;
|
|
23
|
+
// 自动处理 OPTIONS 请求
|
|
24
|
+
if (method === "OPTIONS") {
|
|
25
|
+
return this.handleOptions(pathname, this.routes);
|
|
26
|
+
}
|
|
27
|
+
let matched;
|
|
28
|
+
let params = {};
|
|
29
|
+
let availableMethods = [];
|
|
30
|
+
for (const route of this.routes) {
|
|
31
|
+
const result = matchPath(route.fullPath, pathname);
|
|
32
|
+
if (result.matched) {
|
|
33
|
+
if (route.method === method) {
|
|
34
|
+
matched = route;
|
|
35
|
+
params = result.params;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
// 路径匹配但方法不匹配,收集可用方法
|
|
40
|
+
availableMethods.push(route.method);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const handler = async (req) => {
|
|
45
|
+
if (matched) {
|
|
46
|
+
// 将路径参数设置到 req 对象上,以便 TypedRoute 处理器能够访问
|
|
47
|
+
req.params = params;
|
|
48
|
+
return await matched.handler(req);
|
|
49
|
+
}
|
|
50
|
+
else if (availableMethods.length > 0) {
|
|
51
|
+
// 路径存在但方法不匹配,返回 405 Method Not Allowed
|
|
52
|
+
return json({
|
|
53
|
+
success: false,
|
|
54
|
+
error: "Method Not Allowed",
|
|
55
|
+
message: `Method ${method} not allowed for this endpoint`,
|
|
56
|
+
allowedMethods: availableMethods,
|
|
57
|
+
}, 405, {
|
|
58
|
+
Allow: availableMethods.join(", "),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
// 路径不存在,返回 404 Not Found
|
|
63
|
+
return json({ success: false, error: "Not Found" }, 404);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const middlewareChain = matched?.middlewareChain
|
|
67
|
+
? [...this.globalMiddleware, ...matched.middlewareChain]
|
|
68
|
+
: this.globalMiddleware;
|
|
69
|
+
// 使用 composeMiddleware 来确保错误处理中间件被应用
|
|
70
|
+
const composedHandler = composeMiddleware(middlewareChain, handler);
|
|
71
|
+
return await composedHandler(req);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Middleware } from "./route";
|
|
2
|
+
/**
|
|
3
|
+
* 组件路由配置
|
|
4
|
+
* 支持声明式的组件关联
|
|
5
|
+
*/
|
|
6
|
+
export interface ComponentRoute {
|
|
7
|
+
path: string;
|
|
8
|
+
component: () => Promise<any>;
|
|
9
|
+
middleware?: Middleware[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 嵌套组件路由配置
|
|
13
|
+
*/
|
|
14
|
+
export interface NestedComponentRoute {
|
|
15
|
+
path: string;
|
|
16
|
+
middleware?: Middleware[];
|
|
17
|
+
children?: (ComponentRoute | NestedComponentRoute)[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 扁平化后的组件路由
|
|
21
|
+
*/
|
|
22
|
+
export interface FlattenedComponentRoute extends ComponentRoute {
|
|
23
|
+
fullPath: string;
|
|
24
|
+
middlewareChain: Middleware[];
|
|
25
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Route } from "./types";
|
|
2
|
+
export interface Middleware {
|
|
3
|
+
(req: Request, next: () => Promise<Response>): Promise<Response>;
|
|
4
|
+
}
|
|
5
|
+
export interface BaseRouteConfig {
|
|
6
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD";
|
|
7
|
+
path: string;
|
|
8
|
+
handler: (req: Request) => Response | Promise<Response>;
|
|
9
|
+
}
|
|
10
|
+
export interface ExtendedRouteConfig extends BaseRouteConfig {
|
|
11
|
+
middleware?: Middleware[];
|
|
12
|
+
body?: any;
|
|
13
|
+
query?: any;
|
|
14
|
+
params?: any;
|
|
15
|
+
headers?: any;
|
|
16
|
+
cookies?: any;
|
|
17
|
+
docs?: {
|
|
18
|
+
description?: string;
|
|
19
|
+
tags?: string[];
|
|
20
|
+
security?: any[];
|
|
21
|
+
responses?: Record<string, any>;
|
|
22
|
+
};
|
|
23
|
+
timeout?: number;
|
|
24
|
+
maxBodySize?: string;
|
|
25
|
+
[key: string]: any;
|
|
26
|
+
}
|
|
27
|
+
export interface NestedRouteConfig {
|
|
28
|
+
path: string;
|
|
29
|
+
middleware?: Middleware[];
|
|
30
|
+
children?: (NestedRouteConfig | ExtendedRouteConfig)[];
|
|
31
|
+
}
|
|
32
|
+
export type TypedRoute = ExtendedRouteConfig;
|
|
33
|
+
export type CompatibleRoute = Route | TypedRoute;
|
|
34
|
+
export interface FlattenedRoute extends ExtendedRouteConfig {
|
|
35
|
+
fullPath: string;
|
|
36
|
+
middlewareChain: Middleware[];
|
|
37
|
+
}
|
|
38
|
+
export declare function createTypedRoute(config: ExtendedRouteConfig): ExtendedRouteConfig;
|
|
39
|
+
export declare function isTypedRoute(route: any): route is TypedRoute;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// 导出一些实际的函数,确保 JavaScript 代码生成
|
|
2
|
+
export function createTypedRoute(config) {
|
|
3
|
+
return config;
|
|
4
|
+
}
|
|
5
|
+
export function isTypedRoute(route) {
|
|
6
|
+
return (route &&
|
|
7
|
+
typeof route === "object" &&
|
|
8
|
+
"method" in route &&
|
|
9
|
+
"path" in route &&
|
|
10
|
+
"handler" in route);
|
|
11
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD";
|
|
2
|
+
export type Handler = (req: Request, params?: Record<string, string>, user?: Record<string, any>) => Response | Promise<Response>;
|
|
3
|
+
export type Middleware = (req: Request, next: () => Promise<Response>) => Promise<Response>;
|
|
4
|
+
export interface Route {
|
|
5
|
+
method: Method;
|
|
6
|
+
path: string;
|
|
7
|
+
handler: Handler;
|
|
8
|
+
middleware?: Middleware[];
|
|
9
|
+
}
|
|
10
|
+
export interface NestedRoute {
|
|
11
|
+
path: string;
|
|
12
|
+
middleware?: Middleware[];
|
|
13
|
+
children?: (NestedRoute | Route)[];
|
|
14
|
+
}
|
|
15
|
+
export interface FlattenedRoute extends Route {
|
|
16
|
+
fullPath: string;
|
|
17
|
+
middlewareChain: Middleware[];
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD";
|
|
2
|
+
export type Handler = (req: Request, params?: Record<string, string>, user?: Record<string, any>) => Response | Promise<Response>;
|
|
3
|
+
export type Middleware = (req: Request, next: () => Promise<Response>) => Promise<Response>;
|
|
4
|
+
export interface Route {
|
|
5
|
+
method: Method;
|
|
6
|
+
path: string;
|
|
7
|
+
handler: Handler;
|
|
8
|
+
middleware?: Middleware[];
|
|
9
|
+
}
|
|
10
|
+
export interface NestedRoute {
|
|
11
|
+
path: string;
|
|
12
|
+
middleware?: Middleware[];
|
|
13
|
+
children?: (NestedRoute | Route)[];
|
|
14
|
+
}
|
|
15
|
+
export interface FlattenedRoute extends Route {
|
|
16
|
+
fullPath: string;
|
|
17
|
+
middlewareChain: Middleware[];
|
|
18
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function base64urlEncode(str) {
|
|
2
|
+
return btoa(str)
|
|
3
|
+
.replace(/=/g, "") // ✅ 删除填充
|
|
4
|
+
.replace(/\+/g, "-")
|
|
5
|
+
.replace(/\//g, "_");
|
|
6
|
+
}
|
|
7
|
+
export function base64urlDecode(str) {
|
|
8
|
+
const pad = str.length % 4 === 0 ? "" : "=".repeat(4 - (str.length % 4));
|
|
9
|
+
const base64 = str.replace(/-/g, "+").replace(/_/g, "/") + pad;
|
|
10
|
+
return atob(base64);
|
|
11
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 依赖管理器
|
|
3
|
+
* 负责按需加载和管理框架依赖
|
|
4
|
+
*/
|
|
5
|
+
export declare class DependencyManager {
|
|
6
|
+
private dependencyCache;
|
|
7
|
+
/**
|
|
8
|
+
* 按需获取框架依赖
|
|
9
|
+
*/
|
|
10
|
+
getFrameworkDeps(framework: "vue" | "react"): Promise<any>;
|
|
11
|
+
/**
|
|
12
|
+
* 检测组件类型
|
|
13
|
+
*/
|
|
14
|
+
detectComponentType(component: any): "vue" | "react";
|
|
15
|
+
/**
|
|
16
|
+
* 清除缓存
|
|
17
|
+
*/
|
|
18
|
+
clearCache(): void;
|
|
19
|
+
/**
|
|
20
|
+
* 获取缓存状态
|
|
21
|
+
*/
|
|
22
|
+
getCacheStatus(): Record<string, boolean>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 依赖管理器
|
|
3
|
+
* 负责按需加载和管理框架依赖
|
|
4
|
+
*/
|
|
5
|
+
export class DependencyManager {
|
|
6
|
+
dependencyCache = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* 按需获取框架依赖
|
|
9
|
+
*/
|
|
10
|
+
async getFrameworkDeps(framework) {
|
|
11
|
+
if (this.dependencyCache.has(framework)) {
|
|
12
|
+
return this.dependencyCache.get(framework);
|
|
13
|
+
}
|
|
14
|
+
console.log(`📦 按需加载 ${framework} 依赖...`);
|
|
15
|
+
try {
|
|
16
|
+
let deps;
|
|
17
|
+
switch (framework) {
|
|
18
|
+
case "vue":
|
|
19
|
+
deps = await Promise.all([import("vue"), import("@vue/server-renderer")]);
|
|
20
|
+
break;
|
|
21
|
+
case "react":
|
|
22
|
+
deps = await Promise.all([import("react"), import("react-dom/server")]);
|
|
23
|
+
break;
|
|
24
|
+
default:
|
|
25
|
+
throw new Error(`不支持的框架: ${framework}`);
|
|
26
|
+
}
|
|
27
|
+
this.dependencyCache.set(framework, deps);
|
|
28
|
+
console.log(`✅ ${framework} 依赖加载完成`);
|
|
29
|
+
return deps;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error(`❌ ${framework} 依赖加载失败:`, error);
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 检测组件类型
|
|
38
|
+
*/
|
|
39
|
+
detectComponentType(component) {
|
|
40
|
+
// 简单的组件类型检测
|
|
41
|
+
if (component.render && typeof component.render === "function") {
|
|
42
|
+
return "vue";
|
|
43
|
+
}
|
|
44
|
+
if (component.$$typeof) {
|
|
45
|
+
return "react";
|
|
46
|
+
}
|
|
47
|
+
// 默认使用 Vue
|
|
48
|
+
return "vue";
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 清除缓存
|
|
52
|
+
*/
|
|
53
|
+
clearCache() {
|
|
54
|
+
this.dependencyCache.clear();
|
|
55
|
+
console.log("🧹 依赖缓存已清除");
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 获取缓存状态
|
|
59
|
+
*/
|
|
60
|
+
getCacheStatus() {
|
|
61
|
+
const status = {};
|
|
62
|
+
for (const [framework] of this.dependencyCache) {
|
|
63
|
+
status[framework] = true;
|
|
64
|
+
}
|
|
65
|
+
return status;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Go 风格的错误处理工具
|
|
3
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
4
|
+
*
|
|
5
|
+
* @author Framework Team
|
|
6
|
+
* @version 1.0.0
|
|
7
|
+
* @license MIT
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Go 风格的错误处理工具
|
|
11
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
12
|
+
*
|
|
13
|
+
* @param promise 要处理的 Promise
|
|
14
|
+
* @returns [Error | null, T | undefined] 元组,第一个元素是错误,第二个是结果
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const [error, result] = await goAwait(someAsyncFunction());
|
|
19
|
+
* if (error) {
|
|
20
|
+
* console.error("操作失败:", error);
|
|
21
|
+
* } else {
|
|
22
|
+
* console.log("操作成功:", result);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function goAwait<T>(promise: Promise<T>): Promise<[Error | null, T | undefined]>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Go 风格的错误处理工具
|
|
3
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
4
|
+
*
|
|
5
|
+
* @author Framework Team
|
|
6
|
+
* @version 1.0.0
|
|
7
|
+
* @license MIT
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Go 风格的错误处理工具
|
|
11
|
+
* 将 Promise 转换为 [Error | null, T | undefined] 格式
|
|
12
|
+
*
|
|
13
|
+
* @param promise 要处理的 Promise
|
|
14
|
+
* @returns [Error | null, T | undefined] 元组,第一个元素是错误,第二个是结果
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const [error, result] = await goAwait(someAsyncFunction());
|
|
19
|
+
* if (error) {
|
|
20
|
+
* console.error("操作失败:", error);
|
|
21
|
+
* } else {
|
|
22
|
+
* console.log("操作成功:", result);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function goAwait(promise) {
|
|
27
|
+
return promise
|
|
28
|
+
.then((data) => [null, data])
|
|
29
|
+
.catch((err) => [
|
|
30
|
+
err instanceof Error ? err : new Error(String(err)),
|
|
31
|
+
undefined,
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** 获取单个 Cookie 值 */
|
|
2
|
+
export declare function getCookie(req: Request, key: string): string | null;
|
|
3
|
+
/** 生成 Set-Cookie 头 */
|
|
4
|
+
export declare function setCookie(key: string, value: string, options?: {
|
|
5
|
+
path?: string;
|
|
6
|
+
httpOnly?: boolean;
|
|
7
|
+
maxAge?: number;
|
|
8
|
+
secure?: boolean;
|
|
9
|
+
}): string;
|
|
10
|
+
export declare function setLocals<T extends object>(req: Request, extras: T): void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { parseCookies } from "./parsers";
|
|
2
|
+
/** 获取单个 Cookie 值 */
|
|
3
|
+
export function getCookie(req, key) {
|
|
4
|
+
const cookies = parseCookies(req);
|
|
5
|
+
return cookies[key] || null;
|
|
6
|
+
}
|
|
7
|
+
/** 生成 Set-Cookie 头 */
|
|
8
|
+
export function setCookie(key, value, options = {}) {
|
|
9
|
+
let cookie = `${key}=${encodeURIComponent(value)}`;
|
|
10
|
+
if (options.path)
|
|
11
|
+
cookie += `; Path=${options.path}`;
|
|
12
|
+
if (options.httpOnly)
|
|
13
|
+
cookie += `; HttpOnly`;
|
|
14
|
+
if (options.secure)
|
|
15
|
+
cookie += `; Secure`;
|
|
16
|
+
if (options.maxAge)
|
|
17
|
+
cookie += `; Max-Age=${options.maxAge}`;
|
|
18
|
+
return cookie;
|
|
19
|
+
}
|
|
20
|
+
// 提供给中间件写入"局部上下文"的工具函数
|
|
21
|
+
export function setLocals(req, extras) {
|
|
22
|
+
const target = req;
|
|
23
|
+
target.__locals = { ...(target.__locals ?? {}), ...extras };
|
|
24
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML渲染工具类
|
|
3
|
+
* 提供统一的HTML模板生成功能
|
|
4
|
+
*/
|
|
5
|
+
export declare class HtmlRenderer {
|
|
6
|
+
/**
|
|
7
|
+
* 生成基础HTML模板
|
|
8
|
+
*/
|
|
9
|
+
static generateBaseHtml(content: string, context: any, clientScriptPath?: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* 生成Vue组件HTML
|
|
12
|
+
*/
|
|
13
|
+
static generateVueHtml(content: string, context: any, clientScriptPath?: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* 生成React组件HTML
|
|
16
|
+
*/
|
|
17
|
+
static generateReactHtml(content: string, context: any, clientScriptPath?: string): string;
|
|
18
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML渲染工具类
|
|
3
|
+
* 提供统一的HTML模板生成功能
|
|
4
|
+
*/
|
|
5
|
+
export class HtmlRenderer {
|
|
6
|
+
/**
|
|
7
|
+
* 生成基础HTML模板
|
|
8
|
+
*/
|
|
9
|
+
static generateBaseHtml(content, context, clientScriptPath = "/client.js") {
|
|
10
|
+
return `
|
|
11
|
+
<!doctype html>
|
|
12
|
+
<html>
|
|
13
|
+
<head>
|
|
14
|
+
<meta charset="utf-8">
|
|
15
|
+
<title>Vafast SSR App</title>
|
|
16
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
17
|
+
</head>
|
|
18
|
+
<body>
|
|
19
|
+
<div id="app">${content}</div>
|
|
20
|
+
<script>
|
|
21
|
+
window.__ROUTE_INFO__ = {
|
|
22
|
+
params: ${JSON.stringify(context.params || {})},
|
|
23
|
+
query: ${JSON.stringify(context.query || {})},
|
|
24
|
+
pathname: '${context.pathname}'
|
|
25
|
+
};
|
|
26
|
+
</script>
|
|
27
|
+
<script type="module" src="${clientScriptPath}"></script>
|
|
28
|
+
</body>
|
|
29
|
+
</html>
|
|
30
|
+
`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 生成Vue组件HTML
|
|
34
|
+
*/
|
|
35
|
+
static generateVueHtml(content, context, clientScriptPath = "/client.js") {
|
|
36
|
+
return this.generateBaseHtml(content, context, clientScriptPath);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 生成React组件HTML
|
|
40
|
+
*/
|
|
41
|
+
static generateReactHtml(content, context, clientScriptPath = "/client.js") {
|
|
42
|
+
return `
|
|
43
|
+
<!doctype html>
|
|
44
|
+
<html>
|
|
45
|
+
<head>
|
|
46
|
+
<meta charset="utf-8">
|
|
47
|
+
<title>Vafast SSR App</title>
|
|
48
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
49
|
+
</head>
|
|
50
|
+
<body>
|
|
51
|
+
<div id="root">${content}</div>
|
|
52
|
+
<script>
|
|
53
|
+
window.__ROUTE_INFO__ = {
|
|
54
|
+
params: ${JSON.stringify(context.params || {})},
|
|
55
|
+
query: ${JSON.stringify(context.query || {})},
|
|
56
|
+
pathname: '${context.pathname}'
|
|
57
|
+
};
|
|
58
|
+
</script>
|
|
59
|
+
<script type="module" src="${clientScriptPath}"></script>
|
|
60
|
+
</body>
|
|
61
|
+
</html>
|
|
62
|
+
`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./handle";
|
|
2
|
+
export * from "./parsers";
|
|
3
|
+
export * from "./response";
|
|
4
|
+
export * from "./base64url";
|
|
5
|
+
export * from "./go-await";
|
|
6
|
+
export * from "./route-handler-factory";
|
|
7
|
+
export * from "./path-matcher";
|
|
8
|
+
export * from "./html-renderer";
|
|
9
|
+
export * from "./dependency-manager";
|
|
10
|
+
export * from "./request-validator";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// 导出所有工具函数
|
|
2
|
+
export * from "./handle";
|
|
3
|
+
export * from "./parsers";
|
|
4
|
+
export * from "./response";
|
|
5
|
+
export * from "./base64url";
|
|
6
|
+
export * from "./go-await";
|
|
7
|
+
export * from "./route-handler-factory";
|
|
8
|
+
export * from "./path-matcher";
|
|
9
|
+
export * from "./html-renderer";
|
|
10
|
+
export * from "./dependency-manager";
|
|
11
|
+
export * from "./request-validator";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface FileInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
type: string;
|
|
4
|
+
size: number;
|
|
5
|
+
data: ArrayBuffer;
|
|
6
|
+
}
|
|
7
|
+
export interface FormData {
|
|
8
|
+
fields: Record<string, string>;
|
|
9
|
+
files: Record<string, FileInfo>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 简化的请求体解析函数
|
|
13
|
+
* 优先简洁性,处理最常见的场景
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseBody(req: Request): Promise<unknown>;
|
|
16
|
+
/**
|
|
17
|
+
* 解析请求体为特定类型
|
|
18
|
+
* 提供类型安全的解析方法
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseBodyAs<T>(req: Request): Promise<T>;
|
|
21
|
+
/**
|
|
22
|
+
* 解析请求体为表单数据
|
|
23
|
+
* 专门用于处理 multipart/form-data
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseFormData(req: Request): Promise<FormData>;
|
|
26
|
+
/**
|
|
27
|
+
* 解析请求体为文件
|
|
28
|
+
* 专门用于处理文件上传
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseFile(req: Request): Promise<FileInfo>;
|
|
31
|
+
/** 获取查询字符串,直接返回对象 */
|
|
32
|
+
export declare function parseQuery(req: Request): Record<string, any>;
|
|
33
|
+
/** 解析请求头,返回对象 */
|
|
34
|
+
export declare function parseHeaders(req: Request): Record<string, string>;
|
|
35
|
+
/** 使用cookie库解析Cookie,保证可靠性 */
|
|
36
|
+
export declare function parseCookies(req: Request): Record<string, string>;
|