@kevisual/router 0.0.47 → 0.0.48

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 CHANGED
@@ -405,6 +405,7 @@ type ServerOpts<T = {}> = {
405
405
  }) => any;
406
406
  cors?: Cors$2;
407
407
  io?: boolean;
408
+ showConnected?: boolean;
408
409
  } & T;
409
410
  interface ServerType {
410
411
  path?: string;
@@ -428,6 +429,7 @@ interface ServerType {
428
429
  on(listener: OnListener): void;
429
430
  onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
430
431
  onWsClose(ws: WS): void;
432
+ sendConnected(ws: WS): void;
431
433
  }
432
434
  type OnWebSocketOptions = {
433
435
  ws: WS;
@@ -437,7 +439,7 @@ type OnWebSocketOptions = {
437
439
  id?: string;
438
440
  };
439
441
  type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void;
440
- type WS = {
442
+ type WS<T = {}> = {
441
443
  send: (data: any) => void;
442
444
  close: (code?: number, reason?: string) => void;
443
445
  data?: {
@@ -449,7 +451,7 @@ type WS = {
449
451
  * 鉴权后的获取的信息
450
452
  */
451
453
  userApp?: string;
452
- };
454
+ } & T;
453
455
  };
454
456
  type Listener = {
455
457
  id?: string;
@@ -480,6 +482,7 @@ type RouterReq<T = {}> = {
480
482
  remoteAddress?: string;
481
483
  remotePort?: number;
482
484
  };
485
+ body?: string;
483
486
  cookies?: Record<string, string>;
484
487
  } & T;
485
488
  type RouterRes<T = {}> = {
@@ -617,6 +620,7 @@ declare class ServerBase implements ServerType {
617
620
  cors: Cors$1;
618
621
  listeners: Listener[];
619
622
  emitter: EventEmitter$1<any>;
623
+ showConnected: boolean;
620
624
  constructor(opts?: ServerOpts);
621
625
  listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
622
626
  listen(port: number, hostname?: string, listeningListener?: () => void): void;
@@ -644,6 +648,7 @@ declare class ServerBase implements ServerType {
644
648
  on(listener: OnListener): void;
645
649
  onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): Promise<void>;
646
650
  onWsClose(ws: WS): Promise<void>;
651
+ sendConnected(ws: WS): Promise<void>;
647
652
  }
648
653
 
649
654
  type WsServerBaseOpts = {
package/dist/router.js CHANGED
@@ -1215,10 +1215,12 @@ class ServerBase {
1215
1215
  cors;
1216
1216
  listeners = [];
1217
1217
  emitter = new EventEmitter();
1218
+ showConnected = true;
1218
1219
  constructor(opts) {
1219
1220
  this.path = opts?.path || '/api/router';
1220
1221
  this.handle = opts?.handle;
1221
1222
  this.cors = opts?.cors;
1223
+ this.showConnected = opts?.showConnected !== false;
1222
1224
  }
1223
1225
  listen(...args) {
1224
1226
  this.customListen(...args);
@@ -1419,6 +1421,10 @@ class ServerBase {
1419
1421
  }, 5000);
1420
1422
  }
1421
1423
  }
1424
+ async sendConnected(ws) {
1425
+ if (this.showConnected)
1426
+ ws.send(JSON.stringify({ type: 'connected' }));
1427
+ }
1422
1428
  }
1423
1429
 
1424
1430
  function getDefaultExportFromCjs (x) {
@@ -6373,7 +6379,7 @@ class WsServerBase {
6373
6379
  ws.on('message', async (message) => {
6374
6380
  await this.server.onWebSocket({ ws, message, pathname, token, id });
6375
6381
  });
6376
- ws.send(JSON.stringify({ type: 'connected' }));
6382
+ this.server.sendConnected(ws);
6377
6383
  this.wss.on('close', () => {
6378
6384
  this.server.onWsClose(ws);
6379
6385
  });
@@ -6538,7 +6544,7 @@ class BunServer extends ServerBase {
6538
6544
  return new Response('WebSocket upgrade failed', { status: 400 });
6539
6545
  }
6540
6546
  // 将 Bun 的 Request 转换为 Node.js 风格的 req/res
6541
- return new Promise((resolve) => {
6547
+ return new Promise(async (resolve) => {
6542
6548
  const req = {
6543
6549
  url: url.pathname + url.search,
6544
6550
  method: request.method,
@@ -6547,6 +6553,11 @@ class BunServer extends ServerBase {
6547
6553
  // @ts-ignore
6548
6554
  remoteAddress: request?.remoteAddress || request?.ip || clientInfo?.address || '',
6549
6555
  remotePort: clientInfo?.port || 0,
6556
+ },
6557
+ // @ts-ignore
6558
+ bun: {
6559
+ request, // 原始请求对象
6560
+ server, // 原始服务器对象
6550
6561
  }
6551
6562
  };
6552
6563
  const res = {
@@ -6672,10 +6683,27 @@ class BunServer extends ServerBase {
6672
6683
  };
6673
6684
  // 处理请求体
6674
6685
  if (request.method !== 'GET' && request.method !== 'HEAD') {
6675
- request.text().then((body) => {
6676
- req.body = body;
6686
+ const contentType = request.headers.get('content-type') || '';
6687
+ if (contentType.includes('application/json')) {
6688
+ const text = await request.text();
6689
+ req.body = text;
6677
6690
  requestCallback(req, res);
6678
- });
6691
+ return;
6692
+ }
6693
+ else if (contentType.includes('application/x-www-form-urlencoded')) {
6694
+ const formData = await request.formData();
6695
+ const body = {};
6696
+ for (const [key, value] of formData.entries()) {
6697
+ body[key] = value;
6698
+ }
6699
+ req.body = JSON.stringify(body);
6700
+ requestCallback(req, res);
6701
+ return;
6702
+ }
6703
+ else {
6704
+ requestCallback(req, res);
6705
+ return;
6706
+ }
6679
6707
  }
6680
6708
  else {
6681
6709
  requestCallback(req, res);
@@ -6684,7 +6712,7 @@ class BunServer extends ServerBase {
6684
6712
  },
6685
6713
  websocket: {
6686
6714
  open: (ws) => {
6687
- ws.send(JSON.stringify({ type: 'connected' }));
6715
+ this.sendConnected(ws);
6688
6716
  },
6689
6717
  message: async (ws, message) => {
6690
6718
  const pathname = ws.data.pathname || '';
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.47",
4
+ "version": "0.0.48",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -1,7 +1,7 @@
1
1
  import type { IncomingMessage, ServerResponse } from 'node:http';
2
2
  import { handleServer } from './handle-server.ts';
3
3
  import * as cookie from './cookie.ts';
4
- import { ServerType, Listener, OnListener, ServerOpts, OnWebSocketOptions, OnWebSocketFn, WebScoketListenerFun, ListenerFun, HttpListenerFun, WS } from './server-type.ts';
4
+ import { ServerType, Listener, OnListener, ServerOpts, OnWebSocketOptions, OnWebSocketFn, WebSocketListenerFun, ListenerFun, HttpListenerFun, WS } from './server-type.ts';
5
5
  import { parseIfJson } from '../utils/parse.ts';
6
6
  import { EventEmitter } from 'events';
7
7
  type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
@@ -64,11 +64,12 @@ export class ServerBase implements ServerType {
64
64
  cors: Cors;
65
65
  listeners: Listener[] = [];
66
66
  emitter = new EventEmitter();
67
+ showConnected = true;
67
68
  constructor(opts?: ServerOpts) {
68
69
  this.path = opts?.path || '/api/router';
69
70
  this.handle = opts?.handle;
70
71
  this.cors = opts?.cors;
71
-
72
+ this.showConnected = opts?.showConnected !== false;
72
73
  }
73
74
  listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
74
75
  listen(port: number, hostname?: string, listeningListener?: () => void): void;
@@ -202,7 +203,7 @@ export class ServerBase implements ServerType {
202
203
  const end = (data: any) => {
203
204
  ws.send(JSON.stringify(data));
204
205
  }
205
- (listener.func as WebScoketListenerFun)({
206
+ (listener.func as WebSocketListenerFun)({
206
207
  emitter: this.emitter,
207
208
  data,
208
209
  token,
@@ -275,4 +276,8 @@ export class ServerBase implements ServerType {
275
276
  }, 5000);
276
277
  }
277
278
  }
279
+ async sendConnected(ws: WS) {
280
+ if (this.showConnected)
281
+ ws.send(JSON.stringify({ type: 'connected' }));
282
+ }
278
283
  }
@@ -71,7 +71,7 @@ export class BunServer extends ServerBase implements ServerType {
71
71
  }
72
72
 
73
73
  // 将 Bun 的 Request 转换为 Node.js 风格的 req/res
74
- return new Promise((resolve) => {
74
+ return new Promise(async (resolve) => {
75
75
  const req: RouterReq = {
76
76
  url: url.pathname + url.search,
77
77
  method: request.method,
@@ -80,6 +80,11 @@ export class BunServer extends ServerBase implements ServerType {
80
80
  // @ts-ignore
81
81
  remoteAddress: request?.remoteAddress || request?.ip || clientInfo?.address || '',
82
82
  remotePort: clientInfo?.port || 0,
83
+ },
84
+ // @ts-ignore
85
+ bun: {
86
+ request, // 原始请求对象
87
+ server, // 原始服务器对象
83
88
  }
84
89
  };
85
90
 
@@ -212,10 +217,25 @@ export class BunServer extends ServerBase implements ServerType {
212
217
  };
213
218
  // 处理请求体
214
219
  if (request.method !== 'GET' && request.method !== 'HEAD') {
215
- request.text().then((body) => {
216
- (req as any).body = body;
220
+ const contentType = request.headers.get('content-type') || '';
221
+ if (contentType.includes('application/json')) {
222
+ const text = await request.text();
223
+ req.body = text;
217
224
  requestCallback(req, res);
218
- });
225
+ return;
226
+ } else if (contentType.includes('application/x-www-form-urlencoded')) {
227
+ const formData = await request.formData();
228
+ const body: Record<string, any> = {};
229
+ for (const [key, value] of formData.entries()) {
230
+ body[key] = value;
231
+ }
232
+ req.body = JSON.stringify(body);
233
+ requestCallback(req, res);
234
+ return;
235
+ } else {
236
+ requestCallback(req, res);
237
+ return;
238
+ }
219
239
  } else {
220
240
  requestCallback(req, res);
221
241
  }
@@ -223,7 +243,7 @@ export class BunServer extends ServerBase implements ServerType {
223
243
  },
224
244
  websocket: {
225
245
  open: (ws: any) => {
226
- ws.send(JSON.stringify({ type: 'connected' }));
246
+ this.sendConnected(ws);
227
247
  },
228
248
  message: async (ws: any, message: string | Buffer) => {
229
249
  const pathname = ws.data.pathname || '';
@@ -16,6 +16,7 @@ export type ServerOpts<T = {}> = {
16
16
  handle?: (msg?: { path: string; key?: string;[key: string]: any }, ctx?: { req: http.IncomingMessage; res: http.ServerResponse }) => any;
17
17
  cors?: Cors;
18
18
  io?: boolean;
19
+ showConnected?: boolean;
19
20
  } & T;
20
21
 
21
22
  export interface ServerType {
@@ -40,11 +41,12 @@ export interface ServerType {
40
41
  on(listener: OnListener): void;
41
42
  onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
42
43
  onWsClose(ws: WS): void;
44
+ sendConnected(ws: WS): void;
43
45
  }
44
46
 
45
47
  export type OnWebSocketOptions = { ws: WS; message: string | Buffer; pathname: string, token?: string, id?: string }
46
48
  export type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void;
47
- export type WS = {
49
+ export type WS<T = {}> = {
48
50
  send: (data: any) => void;
49
51
  close: (code?: number, reason?: string) => void;
50
52
  data?: {
@@ -56,7 +58,7 @@ export type WS = {
56
58
  * 鉴权后的获取的信息
57
59
  */
58
60
  userApp?: string;
59
- }
61
+ } & T;
60
62
  }
61
63
  export type Listener = {
62
64
  id?: string;
@@ -89,6 +91,7 @@ export type RouterReq<T = {}> = {
89
91
  remoteAddress?: string;
90
92
  remotePort?: number;
91
93
  };
94
+ body?: string;
92
95
  cookies?: Record<string, string>;
93
96
  } & T;
94
97
 
@@ -60,7 +60,7 @@ export class WsServerBase {
60
60
  ws.on('message', async (message: string | Buffer) => {
61
61
  await this.server.onWebSocket({ ws, message, pathname, token, id });
62
62
  });
63
- ws.send(JSON.stringify({ type: 'connected' }));
63
+ this.server.sendConnected(ws);
64
64
  this.wss.on('close', () => {
65
65
  this.server.onWsClose(ws);
66
66
  });