feat: Refactor server implementation to support Bun and Node environments

- Introduced `ServerNode` and `BunServer` classes to handle server logic for Node and Bun respectively.
- Updated `App` class to initialize the appropriate server based on the runtime environment.
- Enhanced `parseBody` function to handle request body parsing for both environments.
- Modified WebSocket handling to support Bun's WebSocket upgrade mechanism.
- Improved error handling and response structure across the server implementation.
- Added support for custom middleware in the server's request handling.
- Refactored server base functionality into `ServerBase` for better code organization.
- Updated type definitions to reflect changes in server options and listener handling.
- Added a new demo for testing the server functionality with various request types.
This commit is contained in:
2025-12-20 05:11:51 +08:00
parent e1a53c01ea
commit a6a7e74559
21 changed files with 1173 additions and 454 deletions

70
src/server/server-type.ts Normal file
View File

@@ -0,0 +1,70 @@
import * as http from 'http';
export type Listener = {
id?: string;
io?: boolean;
path?: string;
fun: (...args: any[]) => Promise<void> | void;
}
export type ListenerFun = (...args: any[]) => Promise<void> | void;
export type OnListener = Listener | Listener[] | ListenerFun | ListenerFun[];
export type Cors = {
/**
* @default '*''
*/
origin?: string | undefined;
};
export type ServerOpts<T = {}> = {
/**path default `/api/router` */
path?: string;
/**handle Fn */
handle?: (msg?: { path: string; key?: string;[key: string]: any }, ctx?: { req: http.IncomingMessage; res: http.ServerResponse }) => any;
cors?: Cors;
io?: boolean;
} & T;
export interface ServerType {
path?: string;
server?: any;
handle: ServerOpts['handle'];
setHandle(handle?: any): void;
listeners: Listener[];
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
listen(port: number, hostname?: string, listeningListener?: () => void): void;
listen(port: number, backlog?: number, listeningListener?: () => void): void;
listen(port: number, listeningListener?: () => void): void;
listen(path: string, backlog?: number, listeningListener?: () => void): void;
listen(path: string, listeningListener?: () => void): void;
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
listen(handle: any, listeningListener?: () => void): void;
/**
* 兜底监听,当除开 `/api/router` 之外的请求框架只监听一个api所以有其他的请求都执行其他的监听
* @description 主要是为了兼容其他的监听
* @param listener
*/
on(listener: OnListener): void;
onWebSocket({ ws, message, pathname, token, id }: { ws: WS; message: string | Buffer; pathname: string, token?: string, id?: string }): void;
}
type WS = {
send: (data: any) => void;
close: () => void;
}
export type CommonReq = {
url: string;
method: string;
headers: Record<string, string>;
[key: string]: any;
}
export type CommonRes = {
statusCode: number;
writableEnded: boolean;
writeHead: (statusCode: number, headers?: Record<string, string>) => void;
setHeader: (name: string, value: string | string[]) => void;
cookie: (name: string, value: string, options?: any) => void;
end: (data?: any) => void;
[key: string]: any;
}