@kevisual/router 0.0.48 → 0.0.49
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/router.d.ts +12 -7
- package/dist/router.js +13 -4
- package/package.json +1 -6
- package/src/app.ts +1 -1
- package/src/server/server-base.ts +12 -4
- package/src/server/server-type.ts +17 -6
- package/dist/router-simple-lib.d.ts +0 -3
- package/dist/router-simple-lib.js +0 -35
package/dist/router.d.ts
CHANGED
|
@@ -428,11 +428,11 @@ interface ServerType {
|
|
|
428
428
|
*/
|
|
429
429
|
on(listener: OnListener): void;
|
|
430
430
|
onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
|
|
431
|
-
onWsClose(ws: WS): void;
|
|
432
|
-
sendConnected(ws: WS): void;
|
|
431
|
+
onWsClose<T = {}>(ws: WS<T>): void;
|
|
432
|
+
sendConnected<T = {}>(ws: WS<T>): void;
|
|
433
433
|
}
|
|
434
|
-
type OnWebSocketOptions = {
|
|
435
|
-
ws: WS
|
|
434
|
+
type OnWebSocketOptions<T = {}> = {
|
|
435
|
+
ws: WS<T>;
|
|
436
436
|
message: string | Buffer;
|
|
437
437
|
pathname: string;
|
|
438
438
|
token?: string;
|
|
@@ -458,13 +458,18 @@ type Listener = {
|
|
|
458
458
|
io?: boolean;
|
|
459
459
|
path?: string;
|
|
460
460
|
func: WebSocketListenerFun | HttpListenerFun;
|
|
461
|
+
/**
|
|
462
|
+
* @description 是否默认解析为 JSON,如果为 true,则 message 会被 JSON.parse 处理,默认是 true
|
|
463
|
+
*/
|
|
464
|
+
json?: boolean;
|
|
461
465
|
};
|
|
462
466
|
type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => Promise<void> | void;
|
|
463
467
|
type HttpListenerFun = (req: RouterReq, res: RouterRes) => Promise<void> | void;
|
|
464
|
-
type WebSocketReq = {
|
|
468
|
+
type WebSocketReq<T = {}, U = Record<string, any>> = {
|
|
465
469
|
emitter?: EventEmitter;
|
|
466
|
-
ws: WS
|
|
467
|
-
data
|
|
470
|
+
ws: WS<T>;
|
|
471
|
+
data?: U;
|
|
472
|
+
message?: string | Buffer;
|
|
468
473
|
pathname?: string;
|
|
469
474
|
token?: string;
|
|
470
475
|
id?: string;
|
package/dist/router.js
CHANGED
|
@@ -1325,14 +1325,15 @@ class ServerBase {
|
|
|
1325
1325
|
}
|
|
1326
1326
|
on(listener) {
|
|
1327
1327
|
this.listeners = [];
|
|
1328
|
+
const randomId = Math.random().toString(36).substring(2, 15);
|
|
1328
1329
|
if (typeof listener === 'function') {
|
|
1329
|
-
this.listeners.push({ func: listener });
|
|
1330
|
+
this.listeners.push({ func: listener, id: 'all-' + randomId });
|
|
1330
1331
|
return;
|
|
1331
1332
|
}
|
|
1332
1333
|
if (Array.isArray(listener)) {
|
|
1333
1334
|
for (const item of listener) {
|
|
1334
1335
|
if (typeof item === 'function') {
|
|
1335
|
-
this.listeners.push({ func: item });
|
|
1336
|
+
this.listeners.push({ func: item, id: 'all-' + randomId });
|
|
1336
1337
|
}
|
|
1337
1338
|
else {
|
|
1338
1339
|
this.listeners.push(item);
|
|
@@ -1345,20 +1346,28 @@ class ServerBase {
|
|
|
1345
1346
|
}
|
|
1346
1347
|
async onWebSocket({ ws, message, pathname, token, id }) {
|
|
1347
1348
|
const listener = this.listeners.find((item) => item.path === pathname && item.io);
|
|
1348
|
-
const data = parseIfJson(message);
|
|
1349
1349
|
if (listener) {
|
|
1350
1350
|
const end = (data) => {
|
|
1351
1351
|
ws.send(JSON.stringify(data));
|
|
1352
1352
|
};
|
|
1353
|
+
let data = {};
|
|
1354
|
+
const isJson = listener.json !== false;
|
|
1355
|
+
if (isJson) {
|
|
1356
|
+
data = parseIfJson(message);
|
|
1357
|
+
}
|
|
1353
1358
|
listener.func({
|
|
1354
1359
|
emitter: this.emitter,
|
|
1355
1360
|
data,
|
|
1356
1361
|
token,
|
|
1362
|
+
message,
|
|
1363
|
+
pathname,
|
|
1357
1364
|
id,
|
|
1358
1365
|
ws,
|
|
1359
1366
|
}, { end });
|
|
1360
1367
|
return;
|
|
1361
1368
|
}
|
|
1369
|
+
// 默认处理方案,直接调用 handle 方法
|
|
1370
|
+
const data = parseIfJson(message);
|
|
1362
1371
|
if (typeof data === 'string') {
|
|
1363
1372
|
const cleanMessage = data.trim().replace(/^["']|["']$/g, '');
|
|
1364
1373
|
if (cleanMessage === 'close') {
|
|
@@ -11209,7 +11218,7 @@ class App {
|
|
|
11209
11218
|
}
|
|
11210
11219
|
this.server.on({
|
|
11211
11220
|
id: 'app-request-listener',
|
|
11212
|
-
|
|
11221
|
+
func: fn,
|
|
11213
11222
|
});
|
|
11214
11223
|
}
|
|
11215
11224
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@kevisual/router",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.49",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/router.js",
|
|
@@ -84,11 +84,6 @@
|
|
|
84
84
|
"require": "./dist/router-define.js",
|
|
85
85
|
"types": "./dist/router-define.d.ts"
|
|
86
86
|
},
|
|
87
|
-
"./simple-lib": {
|
|
88
|
-
"import": "./dist/router-simple-lib.js",
|
|
89
|
-
"require": "./dist/router-simple-lib.js",
|
|
90
|
-
"types": "./dist/router-simple-lib.d.ts"
|
|
91
|
-
},
|
|
92
87
|
"./mod.ts": {
|
|
93
88
|
"import": "./mod.ts",
|
|
94
89
|
"require": "./mod.ts",
|
package/src/app.ts
CHANGED
|
@@ -179,14 +179,15 @@ export class ServerBase implements ServerType {
|
|
|
179
179
|
}
|
|
180
180
|
on(listener: OnListener) {
|
|
181
181
|
this.listeners = [];
|
|
182
|
+
const randomId = Math.random().toString(36).substring(2, 15);
|
|
182
183
|
if (typeof listener === 'function') {
|
|
183
|
-
this.listeners.push({ func: listener });
|
|
184
|
+
this.listeners.push({ func: listener, id: 'all-' + randomId });
|
|
184
185
|
return;
|
|
185
186
|
}
|
|
186
187
|
if (Array.isArray(listener)) {
|
|
187
188
|
for (const item of listener) {
|
|
188
189
|
if (typeof item === 'function') {
|
|
189
|
-
this.listeners.push({ func: item });
|
|
190
|
+
this.listeners.push({ func: item, id: 'all-' + randomId });
|
|
190
191
|
} else {
|
|
191
192
|
this.listeners.push(item);
|
|
192
193
|
}
|
|
@@ -197,22 +198,29 @@ export class ServerBase implements ServerType {
|
|
|
197
198
|
}
|
|
198
199
|
async onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions) {
|
|
199
200
|
const listener = this.listeners.find((item) => item.path === pathname && item.io);
|
|
200
|
-
const data: any = parseIfJson(message);
|
|
201
201
|
|
|
202
202
|
if (listener) {
|
|
203
203
|
const end = (data: any) => {
|
|
204
204
|
ws.send(JSON.stringify(data));
|
|
205
205
|
}
|
|
206
|
+
let data: any = {};
|
|
207
|
+
const isJson = listener.json !== false;
|
|
208
|
+
if (isJson) {
|
|
209
|
+
data = parseIfJson(message);
|
|
210
|
+
}
|
|
206
211
|
(listener.func as WebSocketListenerFun)({
|
|
207
212
|
emitter: this.emitter,
|
|
208
213
|
data,
|
|
209
214
|
token,
|
|
215
|
+
message,
|
|
216
|
+
pathname,
|
|
210
217
|
id,
|
|
211
218
|
ws,
|
|
212
219
|
}, { end });
|
|
213
220
|
return;
|
|
214
221
|
}
|
|
215
|
-
|
|
222
|
+
// 默认处理方案,直接调用 handle 方法
|
|
223
|
+
const data: any = parseIfJson(message);
|
|
216
224
|
if (typeof data === 'string') {
|
|
217
225
|
const cleanMessage = data.trim().replace(/^["']|["']$/g, '');
|
|
218
226
|
if (cleanMessage === 'close') {
|
|
@@ -40,11 +40,17 @@ export interface ServerType {
|
|
|
40
40
|
*/
|
|
41
41
|
on(listener: OnListener): void;
|
|
42
42
|
onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
|
|
43
|
-
onWsClose(ws: WS): void;
|
|
44
|
-
sendConnected(ws: WS): void;
|
|
43
|
+
onWsClose<T = {}>(ws: WS<T>): void;
|
|
44
|
+
sendConnected<T = {}>(ws: WS<T>): void;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export type OnWebSocketOptions = {
|
|
47
|
+
export type OnWebSocketOptions<T = {}> = {
|
|
48
|
+
ws: WS<T>;
|
|
49
|
+
message: string | Buffer;
|
|
50
|
+
pathname: string,
|
|
51
|
+
token?: string,
|
|
52
|
+
id?: string,
|
|
53
|
+
}
|
|
48
54
|
export type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void;
|
|
49
55
|
export type WS<T = {}> = {
|
|
50
56
|
send: (data: any) => void;
|
|
@@ -65,15 +71,20 @@ export type Listener = {
|
|
|
65
71
|
io?: boolean;
|
|
66
72
|
path?: string;
|
|
67
73
|
func: WebSocketListenerFun | HttpListenerFun;
|
|
74
|
+
/**
|
|
75
|
+
* @description 是否默认解析为 JSON,如果为 true,则 message 会被 JSON.parse 处理,默认是 true
|
|
76
|
+
*/
|
|
77
|
+
json?: boolean,
|
|
68
78
|
}
|
|
69
79
|
|
|
70
80
|
export type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => Promise<void> | void;
|
|
71
81
|
export type HttpListenerFun = (req: RouterReq, res: RouterRes) => Promise<void> | void;
|
|
72
82
|
|
|
73
|
-
export type WebSocketReq = {
|
|
83
|
+
export type WebSocketReq<T = {}, U = Record<string, any>> = {
|
|
74
84
|
emitter?: EventEmitter;
|
|
75
|
-
ws: WS
|
|
76
|
-
data
|
|
85
|
+
ws: WS<T>;
|
|
86
|
+
data?: U;
|
|
87
|
+
message?: string | Buffer;
|
|
77
88
|
pathname?: string;
|
|
78
89
|
token?: string;
|
|
79
90
|
id?: string;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import xml2js from 'xml2js';
|
|
2
|
-
|
|
3
|
-
const parseXml = async (req) => {
|
|
4
|
-
return await new Promise((resolve) => {
|
|
5
|
-
// 读取请求数据
|
|
6
|
-
let data = '';
|
|
7
|
-
req.setEncoding('utf8');
|
|
8
|
-
// 监听data事件,接收数据片段
|
|
9
|
-
req.on('data', (chunk) => {
|
|
10
|
-
data += chunk;
|
|
11
|
-
});
|
|
12
|
-
// 当请求结束时处理数据
|
|
13
|
-
req.on('end', () => {
|
|
14
|
-
try {
|
|
15
|
-
// 使用xml2js解析XML
|
|
16
|
-
xml2js.parseString(data, function (err, result) {
|
|
17
|
-
if (err) {
|
|
18
|
-
console.error('XML解析错误:', err);
|
|
19
|
-
resolve(null);
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
const jsonString = JSON.stringify(result);
|
|
23
|
-
resolve(jsonString);
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
catch (error) {
|
|
28
|
-
console.error('处理请求时出错:', error);
|
|
29
|
-
resolve(null);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export { parseXml };
|