chore: update version to 0.0.49 and refactor WebSocket handling and type definitions
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.48",
|
||||
"version": "0.0.49",
|
||||
"description": "",
|
||||
"type": "module",
|
||||
"main": "./dist/router.js",
|
||||
@@ -84,11 +84,6 @@
|
||||
"require": "./dist/router-define.js",
|
||||
"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": {
|
||||
"import": "./mod.ts",
|
||||
"require": "./mod.ts",
|
||||
|
||||
@@ -149,27 +149,4 @@ export default [
|
||||
},
|
||||
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()],
|
||||
},
|
||||
];
|
||||
|
||||
@@ -134,7 +134,7 @@ export class App<U = {}> {
|
||||
}
|
||||
this.server.on({
|
||||
id: 'app-request-listener',
|
||||
fun: fn as any,
|
||||
func: fn as any,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,22 +198,29 @@ export class ServerBase implements ServerType {
|
||||
}
|
||||
async onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions) {
|
||||
const listener = this.listeners.find((item) => item.path === pathname && item.io);
|
||||
const data: any = parseIfJson(message);
|
||||
|
||||
if (listener) {
|
||||
const end = (data: any) => {
|
||||
ws.send(JSON.stringify(data));
|
||||
}
|
||||
let data: any = {};
|
||||
const isJson = listener.json !== false;
|
||||
if (isJson) {
|
||||
data = parseIfJson(message);
|
||||
}
|
||||
(listener.func as WebSocketListenerFun)({
|
||||
emitter: this.emitter,
|
||||
data,
|
||||
token,
|
||||
message,
|
||||
pathname,
|
||||
id,
|
||||
ws,
|
||||
}, { end });
|
||||
return;
|
||||
}
|
||||
|
||||
// 默认处理方案,直接调用 handle 方法
|
||||
const data: any = parseIfJson(message);
|
||||
if (typeof data === 'string') {
|
||||
const cleanMessage = data.trim().replace(/^["']|["']$/g, '');
|
||||
if (cleanMessage === 'close') {
|
||||
|
||||
@@ -40,11 +40,17 @@ export interface ServerType {
|
||||
*/
|
||||
on(listener: OnListener): void;
|
||||
onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
|
||||
onWsClose(ws: WS): void;
|
||||
sendConnected(ws: WS): void;
|
||||
onWsClose<T = {}>(ws: WS<T>): 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 WS<T = {}> = {
|
||||
send: (data: any) => void;
|
||||
@@ -65,15 +71,20 @@ export type Listener = {
|
||||
io?: boolean;
|
||||
path?: string;
|
||||
func: WebSocketListenerFun | HttpListenerFun;
|
||||
/**
|
||||
* @description 是否默认解析为 JSON,如果为 true,则 message 会被 JSON.parse 处理,默认是 true
|
||||
*/
|
||||
json?: boolean,
|
||||
}
|
||||
|
||||
export type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => 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;
|
||||
ws: WS;
|
||||
data: any;
|
||||
ws: WS<T>;
|
||||
data?: U;
|
||||
message?: string | Buffer;
|
||||
pathname?: string;
|
||||
token?: string;
|
||||
id?: string;
|
||||
|
||||
Reference in New Issue
Block a user