chore: update version to 0.0.49 and refactor WebSocket handling and type definitions

This commit is contained in:
2025-12-21 20:37:50 +08:00
parent caaec4d870
commit a76f1fb5d2
5 changed files with 28 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
{ {
"$schema": "https://json.schemastore.org/package", "$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.0.48", "version": "0.0.49",
"description": "", "description": "",
"type": "module", "type": "module",
"main": "./dist/router.js", "main": "./dist/router.js",
@@ -84,11 +84,6 @@
"require": "./dist/router-define.js", "require": "./dist/router-define.js",
"types": "./dist/router-define.d.ts" "types": "./dist/router-define.d.ts"
}, },
"./simple-lib": {
"import": "./dist/router-simple-lib.js",
"require": "./dist/router-simple-lib.js",
"types": "./dist/router-simple-lib.d.ts"
},
"./mod.ts": { "./mod.ts": {
"import": "./mod.ts", "import": "./mod.ts",
"require": "./mod.ts", "require": "./mod.ts",

View File

@@ -149,27 +149,4 @@ export default [
}, },
plugins: [dts()], plugins: [dts()],
}, },
{
input: 'src/router-simple-lib.ts',
output: {
file: 'dist/router-simple-lib.js',
format: 'es',
},
plugins: [
resolve({
browser: false,
}),
commonjs(),
typescript(),
],
external: ['xml2js'],
},
{
input: 'src/router-simple-lib.ts',
output: {
file: 'dist/router-simple-lib.d.ts',
format: 'es',
},
plugins: [dts()],
},
]; ];

View File

@@ -134,7 +134,7 @@ export class App<U = {}> {
} }
this.server.on({ this.server.on({
id: 'app-request-listener', id: 'app-request-listener',
fun: fn as any, func: fn as any,
}); });
} }
} }

View File

@@ -198,22 +198,29 @@ export class ServerBase implements ServerType {
} }
async onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions) { async onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions) {
const listener = this.listeners.find((item) => item.path === pathname && item.io); const listener = this.listeners.find((item) => item.path === pathname && item.io);
const data: any = parseIfJson(message);
if (listener) { if (listener) {
const end = (data: any) => { const end = (data: any) => {
ws.send(JSON.stringify(data)); ws.send(JSON.stringify(data));
} }
let data: any = {};
const isJson = listener.json !== false;
if (isJson) {
data = parseIfJson(message);
}
(listener.func as WebSocketListenerFun)({ (listener.func as WebSocketListenerFun)({
emitter: this.emitter, emitter: this.emitter,
data, data,
token, token,
message,
pathname,
id, id,
ws, ws,
}, { end }); }, { end });
return; return;
} }
// 默认处理方案,直接调用 handle 方法
const data: any = parseIfJson(message);
if (typeof data === 'string') { if (typeof data === 'string') {
const cleanMessage = data.trim().replace(/^["']|["']$/g, ''); const cleanMessage = data.trim().replace(/^["']|["']$/g, '');
if (cleanMessage === 'close') { if (cleanMessage === 'close') {

View File

@@ -40,11 +40,17 @@ export interface ServerType {
*/ */
on(listener: OnListener): void; on(listener: OnListener): void;
onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void; onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
onWsClose(ws: WS): void; onWsClose<T = {}>(ws: WS<T>): void;
sendConnected(ws: WS): void; sendConnected<T = {}>(ws: WS<T>): void;
} }
export type OnWebSocketOptions = { ws: WS; message: string | Buffer; pathname: string, token?: string, id?: string } export type OnWebSocketOptions<T = {}> = {
ws: WS<T>;
message: string | Buffer;
pathname: string,
token?: string,
id?: string,
}
export type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void; export type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void;
export type WS<T = {}> = { export type WS<T = {}> = {
send: (data: any) => void; send: (data: any) => void;
@@ -65,15 +71,20 @@ export type Listener = {
io?: boolean; io?: boolean;
path?: string; path?: string;
func: WebSocketListenerFun | HttpListenerFun; func: WebSocketListenerFun | HttpListenerFun;
/**
* @description 是否默认解析为 JSON如果为 true则 message 会被 JSON.parse 处理,默认是 true
*/
json?: boolean,
} }
export type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => Promise<void> | void; export type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => Promise<void> | void;
export type HttpListenerFun = (req: RouterReq, res: RouterRes) => Promise<void> | void; export type HttpListenerFun = (req: RouterReq, res: RouterRes) => Promise<void> | void;
export type WebSocketReq = { export type WebSocketReq<T = {}, U = Record<string, any>> = {
emitter?: EventEmitter; emitter?: EventEmitter;
ws: WS; ws: WS<T>;
data: any; data?: U;
message?: string | Buffer;
pathname?: string; pathname?: string;
token?: string; token?: string;
id?: string; id?: string;