@kevisual/router 0.2.4 → 0.2.6

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/ws.d.ts CHANGED
@@ -218,7 +218,7 @@ declare class Route<M extends SimpleObject = SimpleObject, U extends SimpleObjec
218
218
  description?: string;
219
219
  metadata?: M;
220
220
  middleware?: RouteMiddleware[];
221
- type?: string | undefined;
221
+ type?: string;
222
222
  /**
223
223
  * 是否开启debug,开启后会打印错误信息
224
224
  */
@@ -294,6 +294,7 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
294
294
  path: string;
295
295
  key?: string;
296
296
  payload?: any;
297
+ args?: any;
297
298
  }, ctx?: RouteContext<T> & {
298
299
  [key: string]: any;
299
300
  }): Promise<RouteContext<T, {}, {
@@ -310,13 +311,14 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
310
311
  path?: string;
311
312
  key?: string;
312
313
  payload?: any;
314
+ args?: any;
313
315
  }, ctx?: RouteContext<T> & {
314
316
  [key: string]: any;
315
317
  }): Promise<RouteContext<T, {}, {
316
318
  [key: string]: any;
317
319
  }> | {
318
320
  code: number;
319
- body: null;
321
+ body: any;
320
322
  message: string;
321
323
  }>;
322
324
  /**
@@ -331,12 +333,13 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
331
333
  path: string;
332
334
  key?: string;
333
335
  payload?: any;
336
+ args?: any;
334
337
  }, ctx?: RouteContext & {
335
338
  [key: string]: any;
336
339
  }): Promise<{
337
- code: number | undefined;
338
- data: string | number | Object | null | undefined;
339
- message: string | undefined;
340
+ code: number;
341
+ data: any;
342
+ message: string;
340
343
  }>;
341
344
  /**
342
345
  * Router Run获取数据
@@ -349,12 +352,13 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
349
352
  path?: string;
350
353
  key?: string;
351
354
  payload?: any;
355
+ args?: any;
352
356
  }, ctx?: RouteContext<T> & {
353
357
  [key: string]: any;
354
358
  }): Promise<{
355
- code: number | undefined;
356
- data: string | number | Object | null | undefined;
357
- message: string | undefined;
359
+ code: number;
360
+ data: any;
361
+ message: string;
358
362
  }>;
359
363
  /**
360
364
  * 设置上下文
@@ -389,12 +393,12 @@ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements thro
389
393
  importRoutes(routes: Route[]): void;
390
394
  importRouter(router: QueryRouter): void;
391
395
  throw(...args: any[]): void;
392
- hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject> | undefined;
396
+ hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject>;
393
397
  findRoute(opts?: {
394
398
  path?: string;
395
399
  key?: string;
396
400
  rid?: string;
397
- }): Route<SimpleObject, SimpleObject> | undefined;
401
+ }): Route<SimpleObject, SimpleObject>;
398
402
  createRouteList(opts?: {
399
403
  force?: boolean;
400
404
  filter?: (route: Route) => boolean;
@@ -488,6 +492,7 @@ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Q
488
492
  path?: string;
489
493
  key?: string;
490
494
  payload?: any;
495
+ args?: any;
491
496
  token?: string;
492
497
  data?: any;
493
498
  }, ctx?: Partial<RouteContext<C>>): Promise<any>;
@@ -503,7 +508,9 @@ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Q
503
508
  * 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
504
509
  * @param fun 认证函数,接收 RouteContext 和认证类型
505
510
  */
506
- createAuth(fun: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any): Promise<void>;
511
+ createAuth(fun?: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any, opts?: {
512
+ overwrite?: boolean;
513
+ }): Promise<void>;
507
514
  }
508
515
  /** JSON Schema 基本类型映射到 TypeScript 类型 */
509
516
  type JsonSchemaTypeToTS<T> = T extends {
@@ -838,7 +845,10 @@ declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
838
845
  listen(handle: any, backlog?: number, listeningListener?: () => void): void;
839
846
  listen(handle: any, listeningListener?: () => void): void;
840
847
  Route: typeof Route;
841
- static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<any>;
848
+ static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
849
+ cookies: Record<string, string>;
850
+ token: string;
851
+ }>;
842
852
  onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
843
853
  }
844
854
 
package/dist/ws.js CHANGED
@@ -32,7 +32,7 @@ var __toESM = (mod, isNodeMode, target) => {
32
32
  var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
33
33
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
34
34
 
35
- // node_modules/ws/lib/constants.js
35
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/constants.js
36
36
  var require_constants = __commonJS((exports, module) => {
37
37
  var BINARY_TYPES = ["nodebuffer", "arraybuffer", "fragments"];
38
38
  var hasBlob = typeof Blob !== "undefined";
@@ -40,6 +40,7 @@ var require_constants = __commonJS((exports, module) => {
40
40
  BINARY_TYPES.push("blob");
41
41
  module.exports = {
42
42
  BINARY_TYPES,
43
+ CLOSE_TIMEOUT: 30000,
43
44
  EMPTY_BUFFER: Buffer.alloc(0),
44
45
  GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
45
46
  hasBlob,
@@ -51,7 +52,7 @@ var require_constants = __commonJS((exports, module) => {
51
52
  };
52
53
  });
53
54
 
54
- // node_modules/ws/lib/buffer-util.js
55
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/buffer-util.js
55
56
  var require_buffer_util = __commonJS((exports, module) => {
56
57
  var { EMPTY_BUFFER } = require_constants();
57
58
  var FastBuffer = Buffer[Symbol.species];
@@ -112,7 +113,7 @@ var require_buffer_util = __commonJS((exports, module) => {
112
113
  };
113
114
  });
114
115
 
115
- // node_modules/ws/lib/limiter.js
116
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/limiter.js
116
117
  var require_limiter = __commonJS((exports, module) => {
117
118
  var kDone = Symbol("kDone");
118
119
  var kRun = Symbol("kRun");
@@ -144,7 +145,7 @@ var require_limiter = __commonJS((exports, module) => {
144
145
  module.exports = Limiter;
145
146
  });
146
147
 
147
- // node_modules/ws/lib/permessage-deflate.js
148
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/permessage-deflate.js
148
149
  var require_permessage_deflate = __commonJS((exports, module) => {
149
150
  var zlib = __require("zlib");
150
151
  var bufferUtil = require_buffer_util();
@@ -399,12 +400,16 @@ var require_permessage_deflate = __commonJS((exports, module) => {
399
400
  }
400
401
  function inflateOnError(err) {
401
402
  this[kPerMessageDeflate]._inflate = null;
403
+ if (this[kError]) {
404
+ this[kCallback](this[kError]);
405
+ return;
406
+ }
402
407
  err[kStatusCode] = 1007;
403
408
  this[kCallback](err);
404
409
  }
405
410
  });
406
411
 
407
- // node_modules/ws/lib/validation.js
412
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/validation.js
408
413
  var require_validation = __commonJS((exports, module) => {
409
414
  var { isUtf8 } = __require("buffer");
410
415
  var { hasBlob } = require_constants();
@@ -584,7 +589,7 @@ var require_validation = __commonJS((exports, module) => {
584
589
  }
585
590
  });
586
591
 
587
- // node_modules/ws/lib/receiver.js
592
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/receiver.js
588
593
  var require_receiver = __commonJS((exports, module) => {
589
594
  var { Writable } = __require("stream");
590
595
  var PerMessageDeflate = require_permessage_deflate();
@@ -965,7 +970,7 @@ var require_receiver = __commonJS((exports, module) => {
965
970
  module.exports = Receiver;
966
971
  });
967
972
 
968
- // node_modules/ws/lib/sender.js
973
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/sender.js
969
974
  var require_sender = __commonJS((exports, module) => {
970
975
  var { Duplex } = __require("stream");
971
976
  var { randomFillSync } = __require("crypto");
@@ -1319,7 +1324,7 @@ var require_sender = __commonJS((exports, module) => {
1319
1324
  }
1320
1325
  });
1321
1326
 
1322
- // node_modules/ws/lib/event-target.js
1327
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/event-target.js
1323
1328
  var require_event_target = __commonJS((exports, module) => {
1324
1329
  var { kForOnEventAttribute, kListener } = require_constants();
1325
1330
  var kCode = Symbol("kCode");
@@ -1470,7 +1475,7 @@ var require_event_target = __commonJS((exports, module) => {
1470
1475
  }
1471
1476
  });
1472
1477
 
1473
- // node_modules/ws/lib/extension.js
1478
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/extension.js
1474
1479
  var require_extension = __commonJS((exports, module) => {
1475
1480
  var { tokenChars } = require_validation();
1476
1481
  function push(dest, name, elem) {
@@ -1635,7 +1640,7 @@ var require_extension = __commonJS((exports, module) => {
1635
1640
  module.exports = { format, parse };
1636
1641
  });
1637
1642
 
1638
- // node_modules/ws/lib/websocket.js
1643
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/websocket.js
1639
1644
  var require_websocket = __commonJS((exports, module) => {
1640
1645
  var EventEmitter = __require("events");
1641
1646
  var https = __require("https");
@@ -1651,6 +1656,7 @@ var require_websocket = __commonJS((exports, module) => {
1651
1656
  var { isBlob } = require_validation();
1652
1657
  var {
1653
1658
  BINARY_TYPES,
1659
+ CLOSE_TIMEOUT,
1654
1660
  EMPTY_BUFFER,
1655
1661
  GUID,
1656
1662
  kForOnEventAttribute,
@@ -1664,7 +1670,6 @@ var require_websocket = __commonJS((exports, module) => {
1664
1670
  } = require_event_target();
1665
1671
  var { format, parse } = require_extension();
1666
1672
  var { toBuffer } = require_buffer_util();
1667
- var closeTimeout = 30 * 1000;
1668
1673
  var kAborted = Symbol("kAborted");
1669
1674
  var protocolVersions = [8, 13];
1670
1675
  var readyStates = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"];
@@ -1704,6 +1709,7 @@ var require_websocket = __commonJS((exports, module) => {
1704
1709
  initAsClient(this, address, protocols, options);
1705
1710
  } else {
1706
1711
  this._autoPong = options.autoPong;
1712
+ this._closeTimeout = options.closeTimeout;
1707
1713
  this._isServer = true;
1708
1714
  }
1709
1715
  }
@@ -1995,6 +2001,7 @@ var require_websocket = __commonJS((exports, module) => {
1995
2001
  const opts = {
1996
2002
  allowSynchronousEvents: true,
1997
2003
  autoPong: true,
2004
+ closeTimeout: CLOSE_TIMEOUT,
1998
2005
  protocolVersion: protocolVersions[1],
1999
2006
  maxPayload: 100 * 1024 * 1024,
2000
2007
  skipUTF8Validation: false,
@@ -2012,6 +2019,7 @@ var require_websocket = __commonJS((exports, module) => {
2012
2019
  port: undefined
2013
2020
  };
2014
2021
  websocket._autoPong = opts.autoPong;
2022
+ websocket._closeTimeout = opts.closeTimeout;
2015
2023
  if (!protocolVersions.includes(opts.protocolVersion)) {
2016
2024
  throw new RangeError(`Unsupported protocol version: ${opts.protocolVersion} ` + `(supported versions: ${protocolVersions.join(", ")})`);
2017
2025
  }
@@ -2350,7 +2358,7 @@ var require_websocket = __commonJS((exports, module) => {
2350
2358
  }
2351
2359
  }
2352
2360
  function setCloseTimer(websocket) {
2353
- websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), closeTimeout);
2361
+ websocket._closeTimer = setTimeout(websocket._socket.destroy.bind(websocket._socket), websocket._closeTimeout);
2354
2362
  }
2355
2363
  function socketOnClose() {
2356
2364
  const websocket = this[kWebSocket];
@@ -2358,8 +2366,8 @@ var require_websocket = __commonJS((exports, module) => {
2358
2366
  this.removeListener("data", socketOnData);
2359
2367
  this.removeListener("end", socketOnEnd);
2360
2368
  websocket._readyState = WebSocket.CLOSING;
2361
- let chunk;
2362
- if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && (chunk = websocket._socket.read()) !== null) {
2369
+ if (!this._readableState.endEmitted && !websocket._closeFrameReceived && !websocket._receiver._writableState.errorEmitted && this._readableState.length !== 0) {
2370
+ const chunk = this.read(this._readableState.length);
2363
2371
  websocket._receiver.write(chunk);
2364
2372
  }
2365
2373
  websocket._receiver.end();
@@ -2394,7 +2402,7 @@ var require_websocket = __commonJS((exports, module) => {
2394
2402
  }
2395
2403
  });
2396
2404
 
2397
- // node_modules/ws/lib/stream.js
2405
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/stream.js
2398
2406
  var require_stream = __commonJS((exports, module) => {
2399
2407
  var WebSocket = require_websocket();
2400
2408
  var { Duplex } = __require("stream");
@@ -2497,7 +2505,7 @@ var require_stream = __commonJS((exports, module) => {
2497
2505
  module.exports = createWebSocketStream;
2498
2506
  });
2499
2507
 
2500
- // node_modules/ws/lib/subprotocol.js
2508
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/subprotocol.js
2501
2509
  var require_subprotocol = __commonJS((exports, module) => {
2502
2510
  var { tokenChars } = require_validation();
2503
2511
  function parse(header) {
@@ -2542,7 +2550,7 @@ var require_subprotocol = __commonJS((exports, module) => {
2542
2550
  module.exports = { parse };
2543
2551
  });
2544
2552
 
2545
- // node_modules/ws/lib/websocket-server.js
2553
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/lib/websocket-server.js
2546
2554
  var require_websocket_server = __commonJS((exports, module) => {
2547
2555
  var EventEmitter = __require("events");
2548
2556
  var http = __require("http");
@@ -2552,7 +2560,7 @@ var require_websocket_server = __commonJS((exports, module) => {
2552
2560
  var PerMessageDeflate = require_permessage_deflate();
2553
2561
  var subprotocol = require_subprotocol();
2554
2562
  var WebSocket = require_websocket();
2555
- var { GUID, kWebSocket } = require_constants();
2563
+ var { CLOSE_TIMEOUT, GUID, kWebSocket } = require_constants();
2556
2564
  var keyRegex = /^[+/0-9A-Za-z]{22}==$/;
2557
2565
  var RUNNING = 0;
2558
2566
  var CLOSING = 1;
@@ -2569,6 +2577,7 @@ var require_websocket_server = __commonJS((exports, module) => {
2569
2577
  perMessageDeflate: false,
2570
2578
  handleProtocols: null,
2571
2579
  clientTracking: true,
2580
+ closeTimeout: CLOSE_TIMEOUT,
2572
2581
  verifyClient: null,
2573
2582
  noServer: false,
2574
2583
  backlog: null,
@@ -2689,9 +2698,11 @@ var require_websocket_server = __commonJS((exports, module) => {
2689
2698
  abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
2690
2699
  return;
2691
2700
  }
2692
- if (version !== 8 && version !== 13) {
2701
+ if (version !== 13 && version !== 8) {
2693
2702
  const message = "Missing or invalid Sec-WebSocket-Version header";
2694
- abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
2703
+ abortHandshakeOrEmitwsClientError(this, req, socket, 400, message, {
2704
+ "Sec-WebSocket-Version": "13, 8"
2705
+ });
2695
2706
  return;
2696
2707
  }
2697
2708
  if (!this.shouldHandle(req)) {
@@ -2830,18 +2841,18 @@ var require_websocket_server = __commonJS((exports, module) => {
2830
2841
  \r
2831
2842
  ` + message);
2832
2843
  }
2833
- function abortHandshakeOrEmitwsClientError(server, req, socket, code, message) {
2844
+ function abortHandshakeOrEmitwsClientError(server, req, socket, code, message, headers) {
2834
2845
  if (server.listenerCount("wsClientError")) {
2835
2846
  const err = new Error(message);
2836
2847
  Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
2837
2848
  server.emit("wsClientError", err, socket, req);
2838
2849
  } else {
2839
- abortHandshake(socket, code, message);
2850
+ abortHandshake(socket, code, message, headers);
2840
2851
  }
2841
2852
  }
2842
2853
  });
2843
2854
 
2844
- // node_modules/ws/wrapper.mjs
2855
+ // ../../node_modules/.pnpm/@kevisual+ws@8.19.0/node_modules/@kevisual/ws/wrapper.mjs
2845
2856
  var import_stream = __toESM(require_stream(), 1);
2846
2857
  var import_receiver = __toESM(require_receiver(), 1);
2847
2858
  var import_sender = __toESM(require_sender(), 1);
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.2.4",
4
+ "version": "0.2.6",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -22,18 +22,18 @@
22
22
  "author": "abearxiong",
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
- "@kevisual/code-builder": "^0.0.6",
26
- "@kevisual/context": "^0.0.8",
25
+ "@kevisual/code-builder": "^0.0.7",
26
+ "@kevisual/context": "^0.0.10",
27
27
  "@kevisual/dts": "^0.0.4",
28
28
  "@kevisual/js-filter": "^0.0.6",
29
29
  "@kevisual/local-proxy": "^0.0.8",
30
30
  "@kevisual/query": "^0.0.55",
31
31
  "@kevisual/remote-app": "^0.0.7",
32
32
  "@kevisual/use-config": "^1.0.30",
33
- "@opencode-ai/plugin": "^1.3.2",
34
- "@types/bun": "^1.3.11",
33
+ "@opencode-ai/plugin": "^1.4.6",
34
+ "@types/bun": "^1.3.12",
35
35
  "@types/crypto-js": "^4.2.2",
36
- "@types/node": "^25.5.0",
36
+ "@types/node": "^25.6.0",
37
37
  "@types/send": "^1.2.1",
38
38
  "@types/ws": "^8.18.1",
39
39
  "@types/xml2js": "^0.4.14",
@@ -43,7 +43,7 @@
43
43
  "eventemitter3": "^5.0.4",
44
44
  "fast-glob": "^3.3.3",
45
45
  "nanoid": "^5.1.7",
46
- "path-to-regexp": "^8.3.0",
46
+ "path-to-regexp": "^8.4.2",
47
47
  "send": "^1.2.1",
48
48
  "typescript": "^6.0.2",
49
49
  "ws": "npm:@kevisual/ws",
@@ -23,7 +23,7 @@ export class ServerTimer {
23
23
  clearInterval(that.timer);
24
24
  that.timer = null;
25
25
  }
26
- }, that.interval);
26
+ }, that.interval).unref?.();
27
27
  }
28
28
 
29
29
  run(): number {
package/src/browser.ts CHANGED
@@ -13,6 +13,4 @@ export { createSkill, tool, fromJSONSchema, toJSONSchema } from './route.ts';
13
13
  export { CustomError } from './result/error.ts';
14
14
 
15
15
  export * from './router-define.ts';
16
-
17
- export { MockProcess, type ListenProcessParams, type ListenProcessResponse } from './utils/listen-process.ts'
18
16
  // --- 以上同步更新至 browser.ts ---
package/src/commander.ts CHANGED
@@ -2,6 +2,7 @@ import { Command, program } from 'commander';
2
2
  import { App } from './app.ts';
3
3
  import { RemoteApp } from '@kevisual/remote-app'
4
4
  import z from 'zod';
5
+ import { create } from 'node:domain';
5
6
  export const groupByPath = (routes: App['routes']) => {
6
7
  return routes.reduce((acc, route) => {
7
8
  const path = route.path || 'default';
@@ -68,7 +69,7 @@ export const parseDescription = (route: App['routes'][number]) => {
68
69
  }
69
70
  return desc;
70
71
  }
71
- export const createCommand = (opts: { app: any, program: Command }) => {
72
+ export const createCommandList = (opts: { app: any, program: Command }) => {
72
73
  const { program } = opts;
73
74
  const app = opts.app as App;
74
75
  const routes = app.routes;
@@ -76,45 +77,53 @@ export const createCommand = (opts: { app: any, program: Command }) => {
76
77
  const groupRoutes = groupByPath(routes);
77
78
  for (const path in groupRoutes) {
78
79
  const routeList = groupRoutes[path];
79
- const keys = routeList.map(route => route.key).filter(Boolean);
80
+ if (!routeList) continue;
81
+ const keys = routeList.map(route => route.key)
80
82
  const subProgram = program.command(path).description(`路由[${path}] ${keys.length > 0 ? ': ' + keys.join(', ') : ''}`);
81
83
  routeList.forEach(route => {
82
- if (!route.key) return;
83
84
  const description = parseDescription(route);
84
- subProgram.command(route.key)
85
- .description(description || '')
86
- .option('--args <args>', '命令参数,支持 JSON 格式或 key=value 形式,例如: --args \'{"a":1}\' 或 --args \'a=1 b=2\'')
87
- .argument('[args...]', '位置参数(推荐通过 -- 分隔传入),支持 JSON 或 key=value 格式,例如: -- a=1 b=2 或 -- \'{"a":1}\'')
88
- .action(async (passedArgs: string[], options, _command) => {
89
- const output = (data: any) => {
90
- if (typeof data === 'object') {
91
- process.stdout.write(JSON.stringify(data, null, 2) + '\n');
92
- } else {
93
- process.stdout.write(String(data) + '\n');
94
- }
95
- }
96
- try {
97
- let args: Record<string, any> = {};
98
- if (options.args) {
99
- args = parseArgs(options.args);
100
- } else if (passedArgs.length > 0) {
101
- args = parseArgs(passedArgs.join(' '));
102
- }
103
- // 这里可以添加实际的命令执行逻辑,例如调用对应的路由处理函数
104
- const res = await app.run({ path, key: route.key, payload: args }, { appId: app.appId });
105
- if (res.code === 200) {
106
- output(res.data);
107
- } else {
108
- output(`Error: ${res.message}`);
109
- }
110
- } catch (error) {
111
- output(`Execution error: ${error instanceof Error ? error.message : String(error)}`);
112
- }
113
- });
85
+ if (!route.key) {
86
+ createCommand(subProgram, { description, app, route });
87
+ return;
88
+ }
89
+ const _sumCommandy = subProgram.command(route.key)
90
+ createCommand(_sumCommandy, { description, app, route });
114
91
  });
115
92
  }
116
93
  }
117
-
94
+ export const createCommand = (command: Command, opts: { description?: string, app: App, route: any }) => {
95
+ const { description, app, route } = opts;
96
+ const path = route.path;
97
+ command.description(description || '')
98
+ .option('--args <args>', '命令参数,支持 JSON 格式或 key=value 形式,例如: --args \'{"a":1}\' 或 --args \'a=1 b=2\'')
99
+ .argument('[args...]', '位置参数(推荐通过 -- 分隔传入),支持 JSON 或 key=value 格式,例如: -- a=1 b=2 或 -- \'{"a":1}\'')
100
+ .action(async (passedArgs: string[], options, _command) => {
101
+ const output = (data: any) => {
102
+ if (typeof data === 'object') {
103
+ process.stdout.write(JSON.stringify(data, null, 2) + '\n');
104
+ } else {
105
+ process.stdout.write(String(data) + '\n');
106
+ }
107
+ }
108
+ try {
109
+ let args: Record<string, any> = {};
110
+ if (options.args) {
111
+ args = parseArgs(options.args);
112
+ } else if (passedArgs.length > 0) {
113
+ args = parseArgs(passedArgs.join(' '));
114
+ }
115
+ // 这里可以添加实际的命令执行逻辑,例如调用对应的路由处理函数
116
+ const res = await app.run({ path, key: route.key, payload: args }, { appId: app.appId });
117
+ if (res.code === 200) {
118
+ output(res.data);
119
+ } else {
120
+ output(`Error: ${res.message}`);
121
+ }
122
+ } catch (error) {
123
+ output(`Execution error: ${error instanceof Error ? error.message : String(error)}`);
124
+ }
125
+ });
126
+ }
118
127
  export const parse = async (opts: {
119
128
  app: any,
120
129
  description?: string,
@@ -125,10 +134,12 @@ export const parse = async (opts: {
125
134
  token?: string,
126
135
  username?: string,
127
136
  id?: string,
137
+ /** https://kevisual.cn/ws/proxy */
138
+ url?: string,
128
139
  },
129
140
  exitOnEnd?: boolean,
130
141
  }) => {
131
- const { description, parse = true, version, exitOnEnd = true } = opts;
142
+ const { description, parse = true, version, exitOnEnd = false } = opts;
132
143
  const app = opts.app as App;
133
144
  const _program = opts.program || program;
134
145
  _program.description(description || 'Router 命令行工具');
@@ -138,15 +149,16 @@ export const parse = async (opts: {
138
149
  app.createRouteList();
139
150
 
140
151
  createCliList(app);
141
- createCommand({ app: app as App, program: _program });
152
+ createCommandList({ app: app as App, program: _program });
142
153
 
143
154
  if (opts.remote) {
144
- const { token, username, id } = opts.remote;
155
+ const { token, username, id, url } = opts.remote;
145
156
  const remoteApp = new RemoteApp({
146
157
  app,
147
158
  token,
148
159
  username,
149
160
  id,
161
+ url,
150
162
  });
151
163
  const isConnect = await remoteApp.isConnect();
152
164
  if (isConnect) {
package/src/index.ts CHANGED
@@ -13,8 +13,6 @@ export { createSkill, tool, fromJSONSchema, toJSONSchema } from './route.ts';
13
13
  export { CustomError } from './result/error.ts';
14
14
 
15
15
  export * from './router-define.ts';
16
-
17
- export { MockProcess, type ListenProcessParams, type ListenProcessResponse } from './utils/listen-process.ts'
18
16
  // --- 以上同步更新至 browser.ts ---
19
17
 
20
18
  export { ServerNode, handleServer } from './server/index.ts';
package/src/route.ts CHANGED
@@ -490,13 +490,13 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
490
490
  * @param ctx
491
491
  * @returns
492
492
  */
493
- async parse(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
493
+ async parse(message: { path: string; key?: string; payload?: any, args?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
494
494
  if (!message?.path) {
495
495
  return Promise.resolve({ code: 404, body: null, message: 'Not found path' } as RouteContext<T>);
496
496
  }
497
- const { path, key = '', payload = {}, ...query } = message;
497
+ const { path, key = '', payload = {}, args = {}, ...query } = message;
498
498
  ctx = ctx || {} as RouteContext<T>;
499
- ctx.query = { ...ctx.query, ...query, ...payload };
499
+ ctx.query = { ...ctx.query, ...query, ...payload, ...args };
500
500
  ctx.args = ctx.query;
501
501
  ctx.state = { ...ctx?.state };
502
502
  ctx.throw = this.throw;
@@ -529,7 +529,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
529
529
  * @param ctx
530
530
  * @returns
531
531
  */
532
- async call(message: { rid?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
532
+ async call(message: { rid?: string; path?: string; key?: string; payload?: any, args?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
533
533
  let path = message.path;
534
534
  let key = message.key;
535
535
  // 优先 path + key
@@ -556,7 +556,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
556
556
  * @deprecated use run or call instead
557
557
  * @returns
558
558
  */
559
- async queryRoute(message: { id?: string; path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
559
+ async queryRoute(message: { id?: string; path: string; key?: string; payload?: any, args?: any }, ctx?: RouteContext & { [key: string]: any }) {
560
560
  const res = await this.call(message, { ...this.context, ...ctx });
561
561
  return {
562
562
  code: res.code,
@@ -570,7 +570,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
570
570
  * @param ctx
571
571
  * @returns
572
572
  */
573
- async run(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
573
+ async run(message: { id?: string; path?: string; key?: string; payload?: any, args?: any }, ctx?: RouteContext<T> & { [key: string]: any }) {
574
574
  const res = await this.call(message, { ...this.context, ...ctx });
575
575
  return {
576
576
  code: res.code,
@@ -766,7 +766,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
766
766
  * @param param0
767
767
  * @returns
768
768
  */
769
- async run(msg: { rid?: string; path?: string; key?: string; payload?: any, token?: string, data?: any }, ctx?: Partial<RouteContext<C>>) {
769
+ async run(msg: { rid?: string; path?: string; key?: string; payload?: any, args?: any, token?: string, data?: any }, ctx?: Partial<RouteContext<C>>) {
770
770
  const handle = this.handle;
771
771
  if (handle) {
772
772
  return handle(msg, ctx);
@@ -786,7 +786,8 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
786
786
  * 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
787
787
  * @param fun 认证函数,接收 RouteContext 和认证类型
788
788
  */
789
- async createAuth(fun: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any) {
789
+ async createAuth(fun?: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any, opts?: { overwrite?: boolean }) {
790
+ const overwrite = opts?.overwrite ?? false;
790
791
  this.route({
791
792
  path: 'auth',
792
793
  key: 'auth',
@@ -796,7 +797,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
796
797
  if (fun) {
797
798
  await fun(ctx, 'auth');
798
799
  }
799
- }).addTo(this, { overwrite: false });
800
+ }).addTo(this, { overwrite: overwrite });
800
801
 
801
802
  this.route({
802
803
  path: 'auth-admin',
@@ -808,7 +809,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
808
809
  if (fun) {
809
810
  await fun(ctx, 'auth-admin');
810
811
  }
811
- }).addTo(this, { overwrite: false });
812
+ }).addTo(this, { overwrite: overwrite });
812
813
 
813
814
  this.route({
814
815
  path: 'auth-can',
@@ -819,7 +820,7 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
819
820
  if (fun) {
820
821
  await fun(ctx, 'auth-can');
821
822
  }
822
- }).addTo(this, { overwrite: false });
823
+ }).addTo(this, { overwrite: overwrite });
823
824
  }
824
825
  }
825
826
 
@@ -277,7 +277,7 @@ export class HttpChain {
277
277
  // 每隔 2 秒发送一个空行,保持连接
278
278
  setInterval(() => {
279
279
  res.write('\n'); // 发送一个空行,保持连接
280
- }, 3000);
280
+ }, 3000).unref?.();
281
281
  // 客户端断开连接时清理
282
282
  req.on('close', () => {
283
283
  clearInterval(intervalId);