@kevisual/router 0.0.69 → 0.0.71

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.
@@ -1,5 +1,4 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
- import { z } from 'zod';
3
2
  import * as http from 'node:http';
4
3
  import { IncomingMessage, ServerResponse } from 'node:http';
5
4
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
@@ -33,6 +32,9 @@ type RouteContext<T = {
33
32
  query?: {
34
33
  [key: string]: any;
35
34
  };
35
+ args?: {
36
+ [key: string]: any;
37
+ };
36
38
  /** return body */
37
39
  body?: number | string | Object;
38
40
  forward?: (response: {
@@ -95,6 +97,12 @@ type RouteContext<T = {
95
97
  } & T;
96
98
  type SimpleObject = Record<string, any>;
97
99
  type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
100
+ type RunMessage = {
101
+ path?: string;
102
+ key?: string;
103
+ id?: string;
104
+ payload?: any;
105
+ };
98
106
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
99
107
  type RouteMiddleware = {
100
108
  path: string;
@@ -328,9 +336,7 @@ declare class QueryRouter {
328
336
  * -- .send
329
337
  */
330
338
  wait(params?: {
331
- path?: string;
332
- key?: string;
333
- payload?: any;
339
+ message: RunMessage;
334
340
  }, opts?: {
335
341
  mockProcess?: MockProcess;
336
342
  timeout?: number;
@@ -338,10 +344,8 @@ declare class QueryRouter {
338
344
  force?: boolean;
339
345
  filter?: (route: Route) => boolean;
340
346
  }): Promise<void>;
341
- static toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
342
- static fromJSONSchema: (route: RouteInfo) => {
343
- [key: string]: z.ZodTypeAny;
344
- };
347
+ toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
348
+ fromJSONSchema: (route: RouteInfo) => RouteInfo;
345
349
  }
346
350
  type QueryRouterServerOpts = {
347
351
  handleFn?: HandleFn;
package/dist/opencode.js CHANGED
@@ -1,3 +1,5 @@
1
+ import 'es-toolkit';
2
+
1
3
  // src/utils/path-key.ts
2
4
 
3
5
  // ../../node_modules/.pnpm/@kevisual+load@0.0.6/node_modules/@kevisual/load/dist/load.js
@@ -15,6 +15,22 @@ declare class MockProcess {
15
15
  on(fn: (msg?: any) => any): void;
16
16
  desctroy(): void;
17
17
  }
18
+ type ListenProcessParams = {
19
+ message?: RunMessage;
20
+ context?: any;
21
+ };
22
+ type ListenProcessResponse = {
23
+ success?: boolean;
24
+ data?: {
25
+ code?: number;
26
+ data?: any;
27
+ message?: string;
28
+ [key: string]: any;
29
+ };
30
+ error?: any;
31
+ timestamp?: string;
32
+ [key: string]: any;
33
+ };
18
34
 
19
35
  type RouterContextT = {
20
36
  code?: number;
@@ -31,6 +47,9 @@ type RouteContext<T = {
31
47
  query?: {
32
48
  [key: string]: any;
33
49
  };
50
+ args?: {
51
+ [key: string]: any;
52
+ };
34
53
  /** return body */
35
54
  body?: number | string | Object;
36
55
  forward?: (response: {
@@ -93,6 +112,12 @@ type RouteContext<T = {
93
112
  } & T;
94
113
  type SimpleObject$1 = Record<string, any>;
95
114
  type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
115
+ type RunMessage = {
116
+ path?: string;
117
+ key?: string;
118
+ id?: string;
119
+ payload?: any;
120
+ };
96
121
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
97
122
  type RouteMiddleware = {
98
123
  path: string;
@@ -182,9 +207,7 @@ declare class Route<U = {
182
207
  throw(code?: number | string, message?: string, tips?: string): void;
183
208
  }
184
209
  declare const toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
185
- declare const fromJSONSchema: (route: RouteInfo) => {
186
- [key: string]: z.ZodTypeAny;
187
- };
210
+ declare const fromJSONSchema: (route: RouteInfo) => RouteInfo;
188
211
  /**
189
212
  * @parmas overwrite 是否覆盖已存在的route,默认true
190
213
  */
@@ -343,9 +366,7 @@ declare class QueryRouter {
343
366
  * -- .send
344
367
  */
345
368
  wait(params?: {
346
- path?: string;
347
- key?: string;
348
- payload?: any;
369
+ message: RunMessage;
349
370
  }, opts?: {
350
371
  mockProcess?: MockProcess;
351
372
  timeout?: number;
@@ -353,10 +374,8 @@ declare class QueryRouter {
353
374
  force?: boolean;
354
375
  filter?: (route: Route) => boolean;
355
376
  }): Promise<void>;
356
- static toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
357
- static fromJSONSchema: (route: RouteInfo) => {
358
- [key: string]: z.ZodTypeAny;
359
- };
377
+ toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
378
+ fromJSONSchema: (route: RouteInfo) => RouteInfo;
360
379
  }
361
380
  type QueryRouterServerOpts = {
362
381
  handleFn?: HandleFn;
@@ -546,4 +565,4 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
546
565
  declare const App: typeof QueryRouterServer;
547
566
 
548
567
  export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, createSkill, define, fromJSONSchema, toJSONSchema, tool, util };
549
- export type { RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run, Schema, Skill };
568
+ export type { ListenProcessParams, ListenProcessResponse, RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run, Schema, Skill };
@@ -1,3 +1,5 @@
1
+ import { merge as merge$1 } from 'es-toolkit';
2
+
1
3
  const urlAlphabet =
2
4
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';
3
5
 
@@ -490,7 +492,7 @@ class MockProcess {
490
492
  this.process = undefined;
491
493
  }
492
494
  }
493
- const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60 * 1000 }) => {
495
+ const listenProcess = async ({ app, mockProcess, params = {}, timeout = 10 * 60 * 60 * 1000 }) => {
494
496
  const process = mockProcess || new MockProcess();
495
497
  let isEnd = false;
496
498
  const timer = setTimeout(() => {
@@ -504,12 +506,12 @@ const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60
504
506
  // 监听来自主进程的消息
505
507
  const getParams = async () => {
506
508
  return new Promise((resolve) => {
507
- process.on((msg) => {
509
+ process.on((params) => {
508
510
  if (isEnd)
509
511
  return;
510
512
  isEnd = true;
511
513
  clearTimeout(timer);
512
- resolve(msg);
514
+ resolve(params || {});
513
515
  });
514
516
  });
515
517
  };
@@ -517,8 +519,10 @@ const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60
517
519
  /**
518
520
  * 如果不提供path,默认是main
519
521
  */
520
- const { payload = {}, ...rest } = await getParams();
521
- const msg = { ...params, ...rest, payload: { ...params?.payload, ...payload } };
522
+ const _params = await getParams();
523
+ const mergeParams = merge$1(params, _params);
524
+ const msg = mergeParams?.message || {};
525
+ const ctx = mergeParams?.context || {};
522
526
  /**
523
527
  * 如果没有提供path和id,默认取第一个路由, 而且路由path不是router的
524
528
  */
@@ -527,7 +531,7 @@ const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60
527
531
  msg.id = route?.id;
528
532
  }
529
533
  // 执行主要逻辑
530
- const result = await app.run(msg);
534
+ const result = await app.run(msg, ctx);
531
535
  // 发送结果回主进程
532
536
  const response = {
533
537
  success: true,
@@ -539,6 +543,7 @@ const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60
539
543
  });
540
544
  }
541
545
  catch (error) {
546
+ console.error('Error in listenProcess:', error);
542
547
  process.send?.({
543
548
  success: false,
544
549
  error: error.message
@@ -15064,14 +15069,17 @@ const toJSONSchema = (route) => {
15064
15069
  return pickValues;
15065
15070
  };
15066
15071
  const fromJSONSchema = (route) => {
15067
- const args = route?.metadata?.args || {};
15072
+ const args = route?.metadata?.args;
15073
+ if (!args)
15074
+ return route;
15068
15075
  const keys = Object.keys(args);
15069
15076
  const newArgs = {};
15070
15077
  for (let key of keys) {
15071
15078
  const item = args[key];
15072
15079
  newArgs[key] = fromJSONSchema$1(item);
15073
15080
  }
15074
- return newArgs;
15081
+ route.metadata.args = newArgs;
15082
+ return route;
15075
15083
  };
15076
15084
  class QueryRouter {
15077
15085
  appId = '';
@@ -15204,7 +15212,6 @@ class QueryRouter {
15204
15212
  console.error('=====debug====:middlerware error');
15205
15213
  console.error('=====debug====:', e);
15206
15214
  console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
15207
- console.error('=====debug====:', e.message);
15208
15215
  }
15209
15216
  if (e instanceof CustomError || e?.code) {
15210
15217
  ctx.code = e.code;
@@ -15212,8 +15219,9 @@ class QueryRouter {
15212
15219
  ctx.body = null;
15213
15220
  }
15214
15221
  else {
15215
- console.error(`fn:${route.path}-${route.key}:${route.id}`);
15216
- console.error(`middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
15222
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
15223
+ console.error(`[router error] middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
15224
+ console.error(e);
15217
15225
  ctx.code = 500;
15218
15226
  ctx.message = 'Internal Server Error';
15219
15227
  ctx.body = null;
@@ -15232,15 +15240,17 @@ class QueryRouter {
15232
15240
  }
15233
15241
  catch (e) {
15234
15242
  if (route?.isDebug) {
15235
- console.error('=====debug====:', 'router run error:', e.message);
15243
+ console.error('=====debug====:route error');
15244
+ console.error('=====debug====:', e);
15245
+ console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
15236
15246
  }
15237
15247
  if (e instanceof CustomError) {
15238
15248
  ctx.code = e.code;
15239
15249
  ctx.message = e.message;
15240
15250
  }
15241
15251
  else {
15242
- console.error(`[error]fn:${route.path}-${route.key}:${route.id}`);
15243
- console.error('error', e.message);
15252
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
15253
+ console.error(`[router error] error`, e);
15244
15254
  ctx.code = 500;
15245
15255
  ctx.message = 'Internal Server Error';
15246
15256
  }
@@ -15271,6 +15281,7 @@ class QueryRouter {
15271
15281
  return ctx;
15272
15282
  }
15273
15283
  ctx.query = { ...ctx.query, ...ctx.nextQuery };
15284
+ ctx.args = ctx.query;
15274
15285
  ctx.nextQuery = {};
15275
15286
  return await this.runRoute(path, key, ctx);
15276
15287
  }
@@ -15300,6 +15311,7 @@ class QueryRouter {
15300
15311
  const { path, key = '', payload = {}, ...query } = message;
15301
15312
  ctx = ctx || {};
15302
15313
  ctx.query = { ...ctx.query, ...query, ...payload };
15314
+ ctx.args = ctx.query;
15303
15315
  ctx.state = { ...ctx?.state };
15304
15316
  ctx.throw = this.throw;
15305
15317
  ctx.app = this;
@@ -15494,8 +15506,8 @@ class QueryRouter {
15494
15506
  }
15495
15507
  return listenProcess({ app: this, params, ...opts });
15496
15508
  }
15497
- static toJSONSchema = toJSONSchema;
15498
- static fromJSONSchema = fromJSONSchema;
15509
+ toJSONSchema = toJSONSchema;
15510
+ fromJSONSchema = fromJSONSchema;
15499
15511
  }
15500
15512
  /**
15501
15513
  * QueryRouterServer
package/dist/router.d.ts CHANGED
@@ -21,6 +21,22 @@ declare class MockProcess {
21
21
  on(fn: (msg?: any) => any): void;
22
22
  desctroy(): void;
23
23
  }
24
+ type ListenProcessParams = {
25
+ message?: RunMessage;
26
+ context?: any;
27
+ };
28
+ type ListenProcessResponse = {
29
+ success?: boolean;
30
+ data?: {
31
+ code?: number;
32
+ data?: any;
33
+ message?: string;
34
+ [key: string]: any;
35
+ };
36
+ error?: any;
37
+ timestamp?: string;
38
+ [key: string]: any;
39
+ };
24
40
 
25
41
  type RouterContextT = {
26
42
  code?: number;
@@ -37,6 +53,9 @@ type RouteContext<T = {
37
53
  query?: {
38
54
  [key: string]: any;
39
55
  };
56
+ args?: {
57
+ [key: string]: any;
58
+ };
40
59
  /** return body */
41
60
  body?: number | string | Object;
42
61
  forward?: (response: {
@@ -99,6 +118,12 @@ type RouteContext<T = {
99
118
  } & T;
100
119
  type SimpleObject$1 = Record<string, any>;
101
120
  type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
121
+ type RunMessage = {
122
+ path?: string;
123
+ key?: string;
124
+ id?: string;
125
+ payload?: any;
126
+ };
102
127
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
103
128
  type RouteMiddleware = {
104
129
  path: string;
@@ -188,9 +213,7 @@ declare class Route<U = {
188
213
  throw(code?: number | string, message?: string, tips?: string): void;
189
214
  }
190
215
  declare const toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
191
- declare const fromJSONSchema: (route: RouteInfo) => {
192
- [key: string]: z.ZodTypeAny;
193
- };
216
+ declare const fromJSONSchema: (route: RouteInfo) => RouteInfo;
194
217
  /**
195
218
  * @parmas overwrite 是否覆盖已存在的route,默认true
196
219
  */
@@ -349,9 +372,7 @@ declare class QueryRouter {
349
372
  * -- .send
350
373
  */
351
374
  wait(params?: {
352
- path?: string;
353
- key?: string;
354
- payload?: any;
375
+ message: RunMessage;
355
376
  }, opts?: {
356
377
  mockProcess?: MockProcess;
357
378
  timeout?: number;
@@ -359,10 +380,8 @@ declare class QueryRouter {
359
380
  force?: boolean;
360
381
  filter?: (route: Route) => boolean;
361
382
  }): Promise<void>;
362
- static toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
363
- static fromJSONSchema: (route: RouteInfo) => {
364
- [key: string]: z.ZodTypeAny;
365
- };
383
+ toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
384
+ fromJSONSchema: (route: RouteInfo) => RouteInfo;
366
385
  }
367
386
  type QueryRouterServerOpts = {
368
387
  handleFn?: HandleFn;
@@ -959,4 +978,4 @@ declare class App<U = {}> extends QueryRouter {
959
978
  }
960
979
 
961
980
  export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, ServerNode, createSchema, createSkill, define, fromJSONSchema, handleServer, toJSONSchema, tool, util };
962
- export type { HttpListenerFun, Listener, OnListener, OnWebSocketFn, RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, RouterReq, RouterRes, Rule, Run, Schema, Skill, WS, WebSocketListenerFun, WebSocketReq, WebSocketRes };
981
+ export type { HttpListenerFun, ListenProcessParams, ListenProcessResponse, Listener, OnListener, OnWebSocketFn, RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, RouterReq, RouterRes, Rule, Run, Schema, Skill, WS, WebSocketListenerFun, WebSocketReq, WebSocketRes };
package/dist/router.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import require$$1, { webcrypto } from 'node:crypto';
2
+ import { merge as merge$1 } from 'es-toolkit';
2
3
  import require$$2 from 'node:http';
3
4
  import require$$1$1 from 'node:https';
4
5
  import http2 from 'node:http2';
@@ -514,7 +515,7 @@ class MockProcess {
514
515
  this.process = undefined;
515
516
  }
516
517
  }
517
- const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60 * 1000 }) => {
518
+ const listenProcess = async ({ app, mockProcess, params = {}, timeout = 10 * 60 * 60 * 1000 }) => {
518
519
  const process = mockProcess || new MockProcess();
519
520
  let isEnd = false;
520
521
  const timer = setTimeout(() => {
@@ -528,12 +529,12 @@ const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60
528
529
  // 监听来自主进程的消息
529
530
  const getParams = async () => {
530
531
  return new Promise((resolve) => {
531
- process.on((msg) => {
532
+ process.on((params) => {
532
533
  if (isEnd)
533
534
  return;
534
535
  isEnd = true;
535
536
  clearTimeout(timer);
536
- resolve(msg);
537
+ resolve(params || {});
537
538
  });
538
539
  });
539
540
  };
@@ -541,8 +542,10 @@ const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60
541
542
  /**
542
543
  * 如果不提供path,默认是main
543
544
  */
544
- const { payload = {}, ...rest } = await getParams();
545
- const msg = { ...params, ...rest, payload: { ...params?.payload, ...payload } };
545
+ const _params = await getParams();
546
+ const mergeParams = merge$1(params, _params);
547
+ const msg = mergeParams?.message || {};
548
+ const ctx = mergeParams?.context || {};
546
549
  /**
547
550
  * 如果没有提供path和id,默认取第一个路由, 而且路由path不是router的
548
551
  */
@@ -551,7 +554,7 @@ const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60
551
554
  msg.id = route?.id;
552
555
  }
553
556
  // 执行主要逻辑
554
- const result = await app.run(msg);
557
+ const result = await app.run(msg, ctx);
555
558
  // 发送结果回主进程
556
559
  const response = {
557
560
  success: true,
@@ -563,6 +566,7 @@ const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60
563
566
  });
564
567
  }
565
568
  catch (error) {
569
+ console.error('Error in listenProcess:', error);
566
570
  process.send?.({
567
571
  success: false,
568
572
  error: error.message
@@ -15088,14 +15092,17 @@ const toJSONSchema = (route) => {
15088
15092
  return pickValues;
15089
15093
  };
15090
15094
  const fromJSONSchema = (route) => {
15091
- const args = route?.metadata?.args || {};
15095
+ const args = route?.metadata?.args;
15096
+ if (!args)
15097
+ return route;
15092
15098
  const keys = Object.keys(args);
15093
15099
  const newArgs = {};
15094
15100
  for (let key of keys) {
15095
15101
  const item = args[key];
15096
15102
  newArgs[key] = fromJSONSchema$1(item);
15097
15103
  }
15098
- return newArgs;
15104
+ route.metadata.args = newArgs;
15105
+ return route;
15099
15106
  };
15100
15107
  class QueryRouter {
15101
15108
  appId = '';
@@ -15228,7 +15235,6 @@ class QueryRouter {
15228
15235
  console.error('=====debug====:middlerware error');
15229
15236
  console.error('=====debug====:', e);
15230
15237
  console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
15231
- console.error('=====debug====:', e.message);
15232
15238
  }
15233
15239
  if (e instanceof CustomError || e?.code) {
15234
15240
  ctx.code = e.code;
@@ -15236,8 +15242,9 @@ class QueryRouter {
15236
15242
  ctx.body = null;
15237
15243
  }
15238
15244
  else {
15239
- console.error(`fn:${route.path}-${route.key}:${route.id}`);
15240
- console.error(`middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
15245
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
15246
+ console.error(`[router error] middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
15247
+ console.error(e);
15241
15248
  ctx.code = 500;
15242
15249
  ctx.message = 'Internal Server Error';
15243
15250
  ctx.body = null;
@@ -15256,15 +15263,17 @@ class QueryRouter {
15256
15263
  }
15257
15264
  catch (e) {
15258
15265
  if (route?.isDebug) {
15259
- console.error('=====debug====:', 'router run error:', e.message);
15266
+ console.error('=====debug====:route error');
15267
+ console.error('=====debug====:', e);
15268
+ console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
15260
15269
  }
15261
15270
  if (e instanceof CustomError) {
15262
15271
  ctx.code = e.code;
15263
15272
  ctx.message = e.message;
15264
15273
  }
15265
15274
  else {
15266
- console.error(`[error]fn:${route.path}-${route.key}:${route.id}`);
15267
- console.error('error', e.message);
15275
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
15276
+ console.error(`[router error] error`, e);
15268
15277
  ctx.code = 500;
15269
15278
  ctx.message = 'Internal Server Error';
15270
15279
  }
@@ -15295,6 +15304,7 @@ class QueryRouter {
15295
15304
  return ctx;
15296
15305
  }
15297
15306
  ctx.query = { ...ctx.query, ...ctx.nextQuery };
15307
+ ctx.args = ctx.query;
15298
15308
  ctx.nextQuery = {};
15299
15309
  return await this.runRoute(path, key, ctx);
15300
15310
  }
@@ -15324,6 +15334,7 @@ class QueryRouter {
15324
15334
  const { path, key = '', payload = {}, ...query } = message;
15325
15335
  ctx = ctx || {};
15326
15336
  ctx.query = { ...ctx.query, ...query, ...payload };
15337
+ ctx.args = ctx.query;
15327
15338
  ctx.state = { ...ctx?.state };
15328
15339
  ctx.throw = this.throw;
15329
15340
  ctx.app = this;
@@ -15518,8 +15529,8 @@ class QueryRouter {
15518
15529
  }
15519
15530
  return listenProcess({ app: this, params, ...opts });
15520
15531
  }
15521
- static toJSONSchema = toJSONSchema;
15522
- static fromJSONSchema = fromJSONSchema;
15532
+ toJSONSchema = toJSONSchema;
15533
+ fromJSONSchema = fromJSONSchema;
15523
15534
  }
15524
15535
  /**
15525
15536
  * QueryRouterServer
package/dist/ws.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
- import { z } from 'zod';
3
2
  import * as http from 'node:http';
4
3
  import { IncomingMessage, ServerResponse } from 'node:http';
5
4
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
@@ -80,6 +79,9 @@ type RouteContext<T = {
80
79
  query?: {
81
80
  [key: string]: any;
82
81
  };
82
+ args?: {
83
+ [key: string]: any;
84
+ };
83
85
  /** return body */
84
86
  body?: number | string | Object;
85
87
  forward?: (response: {
@@ -142,6 +144,12 @@ type RouteContext<T = {
142
144
  } & T;
143
145
  type SimpleObject = Record<string, any>;
144
146
  type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
147
+ type RunMessage = {
148
+ path?: string;
149
+ key?: string;
150
+ id?: string;
151
+ payload?: any;
152
+ };
145
153
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
146
154
  type RouteMiddleware = {
147
155
  path: string;
@@ -375,9 +383,7 @@ declare class QueryRouter {
375
383
  * -- .send
376
384
  */
377
385
  wait(params?: {
378
- path?: string;
379
- key?: string;
380
- payload?: any;
386
+ message: RunMessage;
381
387
  }, opts?: {
382
388
  mockProcess?: MockProcess;
383
389
  timeout?: number;
@@ -385,10 +391,8 @@ declare class QueryRouter {
385
391
  force?: boolean;
386
392
  filter?: (route: Route) => boolean;
387
393
  }): Promise<void>;
388
- static toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "key" | "path" | "id" | "description" | "type" | "middleware" | "metadata">;
389
- static fromJSONSchema: (route: RouteInfo) => {
390
- [key: string]: z.ZodTypeAny;
391
- };
394
+ toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
395
+ fromJSONSchema: (route: RouteInfo) => RouteInfo;
392
396
  }
393
397
  interface HandleFn<T = any> {
394
398
  (msg: {
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.69",
4
+ "version": "0.0.71",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -29,7 +29,7 @@
29
29
  "@kevisual/local-proxy": "^0.0.8",
30
30
  "@kevisual/query": "^0.0.39",
31
31
  "@kevisual/use-config": "^1.0.30",
32
- "@opencode-ai/plugin": "^1.1.48",
32
+ "@opencode-ai/plugin": "^1.1.49",
33
33
  "@rollup/plugin-alias": "^6.0.0",
34
34
  "@rollup/plugin-commonjs": "29.0.0",
35
35
  "@rollup/plugin-node-resolve": "^16.0.3",
@@ -41,6 +41,7 @@
41
41
  "@types/xml2js": "^0.4.14",
42
42
  "eventemitter3": "^5.0.4",
43
43
  "fast-glob": "^3.3.3",
44
+ "hono": "^4.11.7",
44
45
  "nanoid": "^5.1.6",
45
46
  "path-to-regexp": "^8.3.0",
46
47
  "rollup": "^4.57.1",
@@ -53,14 +54,15 @@
53
54
  "typescript": "^5.9.3",
54
55
  "ws": "npm:@kevisual/ws",
55
56
  "xml2js": "^0.6.2",
56
- "zod": "^4.3.6",
57
- "hono": "^4.11.7"
57
+ "zod": "^4.3.6"
58
58
  },
59
59
  "repository": {
60
60
  "type": "git",
61
61
  "url": "git+https://github.com/abearxiong/kevisual-router.git"
62
62
  },
63
- "dependencies": {},
63
+ "dependencies": {
64
+ "es-toolkit": "^1.44.0"
65
+ },
64
66
  "publishConfig": {
65
67
  "access": "public"
66
68
  },
package/src/browser.ts CHANGED
@@ -14,5 +14,5 @@ export { CustomError } from './result/error.ts';
14
14
 
15
15
  export * from './router-define.ts';
16
16
 
17
- export { MockProcess } from './utils/listen-process.ts'
17
+ export { MockProcess, type ListenProcessParams, type ListenProcessResponse } from './utils/listen-process.ts'
18
18
  // --- 以上同步更新至 browser.ts ---
package/src/index.ts CHANGED
@@ -14,7 +14,7 @@ export { CustomError } from './result/error.ts';
14
14
 
15
15
  export * from './router-define.ts';
16
16
 
17
- export { MockProcess } from './utils/listen-process.ts'
17
+ export { MockProcess, type ListenProcessParams, type ListenProcessResponse } from './utils/listen-process.ts'
18
18
  // --- 以上同步更新至 browser.ts ---
19
19
 
20
20
  export { ServerNode, handleServer } from './server/index.ts';
package/src/route.ts CHANGED
@@ -3,7 +3,6 @@ import { CustomError } from './result/error.ts';
3
3
  import { pick } from './utils/pick.ts';
4
4
  import { listenProcess, MockProcess } from './utils/listen-process.ts';
5
5
  import { z } from 'zod';
6
- import { filter } from '@kevisual/js-filter'
7
6
 
8
7
  export type RouterContextT = { code?: number;[key: string]: any };
9
8
  export type RouteContext<T = { code?: number }, S = any> = {
@@ -14,6 +13,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
14
13
  appId?: string;
15
14
  // run first
16
15
  query?: { [key: string]: any };
16
+ args?: { [key: string]: any };
17
17
  // response body
18
18
  /** return body */
19
19
  body?: number | string | Object;
@@ -61,7 +61,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
61
61
  } & T;
62
62
  export type SimpleObject = Record<string, any>;
63
63
  export type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
64
-
64
+ export type RunMessage = { path?: string; key?: string; id?: string; payload?: any; };
65
65
  export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
66
66
  export type RouteMiddleware =
67
67
  | {
@@ -262,17 +262,17 @@ export const toJSONSchema = (route: RouteInfo) => {
262
262
  return pickValues;
263
263
  }
264
264
 
265
- export const fromJSONSchema = (route: RouteInfo): {
266
- [key: string]: z.ZodTypeAny
267
- } => {
268
- const args = route?.metadata?.args || {};
265
+ export const fromJSONSchema = (route: RouteInfo): RouteInfo => {
266
+ const args = route?.metadata?.args;
267
+ if (!args) return route;
269
268
  const keys = Object.keys(args);
270
269
  const newArgs: { [key: string]: any } = {};
271
270
  for (let key of keys) {
272
271
  const item = args[key];
273
272
  newArgs[key] = z.fromJSONSchema(item);
274
273
  }
275
- return newArgs;
274
+ route.metadata.args = newArgs;
275
+ return route;
276
276
  }
277
277
 
278
278
  /**
@@ -405,15 +405,15 @@ export class QueryRouter {
405
405
  console.error('=====debug====:middlerware error');
406
406
  console.error('=====debug====:', e);
407
407
  console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
408
- console.error('=====debug====:', e.message);
409
408
  }
410
409
  if (e instanceof CustomError || e?.code) {
411
410
  ctx.code = e.code;
412
411
  ctx.message = e.message;
413
412
  ctx.body = null;
414
413
  } else {
415
- console.error(`fn:${route.path}-${route.key}:${route.id}`);
416
- console.error(`middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
414
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
415
+ console.error(`[router error] middleware:${middleware.path}-${middleware.key}:${middleware.id}`);
416
+ console.error(e)
417
417
  ctx.code = 500;
418
418
  ctx.message = 'Internal Server Error';
419
419
  ctx.body = null;
@@ -432,14 +432,16 @@ export class QueryRouter {
432
432
  await route.run(ctx as Required<RouteContext>);
433
433
  } catch (e) {
434
434
  if (route?.isDebug) {
435
- console.error('=====debug====:', 'router run error:', e.message);
435
+ console.error('=====debug====:route error');
436
+ console.error('=====debug====:', e);
437
+ console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
436
438
  }
437
439
  if (e instanceof CustomError) {
438
440
  ctx.code = e.code;
439
441
  ctx.message = e.message;
440
442
  } else {
441
- console.error(`[error]fn:${route.path}-${route.key}:${route.id}`);
442
- console.error('error', e.message);
443
+ console.error(`[router error] fn:${route.path}-${route.key}:${route.id}`);
444
+ console.error(`[router error] error`, e);
443
445
  ctx.code = 500;
444
446
  ctx.message = 'Internal Server Error';
445
447
  }
@@ -469,6 +471,7 @@ export class QueryRouter {
469
471
  return ctx;
470
472
  }
471
473
  ctx.query = { ...ctx.query, ...ctx.nextQuery };
474
+ ctx.args = ctx.query;
472
475
  ctx.nextQuery = {};
473
476
  return await this.runRoute(path, key, ctx);
474
477
  }
@@ -496,6 +499,7 @@ export class QueryRouter {
496
499
  const { path, key = '', payload = {}, ...query } = message;
497
500
  ctx = ctx || {};
498
501
  ctx.query = { ...ctx.query, ...query, ...payload };
502
+ ctx.args = ctx.query;
499
503
  ctx.state = { ...ctx?.state };
500
504
  ctx.throw = this.throw;
501
505
  ctx.app = this;
@@ -680,7 +684,7 @@ export class QueryRouter {
680
684
  * -- .on
681
685
  * -- .send
682
686
  */
683
- wait(params?: { path?: string; key?: string; payload?: any }, opts?: {
687
+ wait(params?: { message: RunMessage }, opts?: {
684
688
  mockProcess?: MockProcess,
685
689
  timeout?: number,
686
690
  getList?: boolean
@@ -693,8 +697,8 @@ export class QueryRouter {
693
697
  }
694
698
  return listenProcess({ app: this as any, params, ...opts });
695
699
  }
696
- static toJSONSchema = toJSONSchema;
697
- static fromJSONSchema = fromJSONSchema;
700
+ toJSONSchema = toJSONSchema;
701
+ fromJSONSchema = fromJSONSchema;
698
702
  }
699
703
 
700
704
  type QueryRouterServerOpts = {
@@ -1,25 +1,7 @@
1
1
  import { fork } from 'child_process'
2
-
3
- export type RunCodeParams = {
4
- path?: string;
5
- key?: string;
6
- payload?: string;
7
- [key: string]: any
8
- }
9
- type RunCode = {
10
- // 调用进程的功能
11
- success?: boolean
12
- data?: {
13
- // 调用router的结果
14
- code?: number
15
- data?: any
16
- message?: string
17
- [key: string]: any
18
- };
19
- error?: any
20
- timestamp?: string
21
- [key: string]: any
22
- }
2
+ import { ListenProcessParams, ListenProcessResponse } from '@/utils/listen-process.ts';
3
+ export type RunCodeParams = ListenProcessParams
4
+ export type RunCode = ListenProcessResponse
23
5
  export const runCode = async (tsPath: string, params: RunCodeParams = {}): Promise<RunCode> => {
24
6
  return new Promise((resolve, reject) => {
25
7
  // 使用 Bun 的 fork 模式启动子进程
@@ -52,8 +34,11 @@ import path from 'node:path'
52
34
  const res = await runCode(path.join(process.cwd(), './src/test/mini.ts'), {
53
35
  // path: 'main'
54
36
  // id: 'abc'
55
- path: 'router',
56
- key: 'list'
37
+ message: {
38
+ path: 'router',
39
+ key: 'list'
40
+ }
57
41
  })
58
42
 
59
- console.log('res', res.data.data.list)
43
+ console.log('success', res)
44
+ console.log('res', res.data?.data?.list)
package/src/test/ws.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { App } from "../app.ts";
2
2
 
3
3
  const app = new App({
4
- io: true
5
4
  });
6
5
 
7
6
  app
@@ -1,5 +1,6 @@
1
1
  import { EventEmitter } from "eventemitter3";
2
- import { QueryRouterServer } from "../route.ts"
2
+ import { QueryRouterServer, RouterContextT, RunMessage } from "../route.ts"
3
+ import { merge } from 'es-toolkit'
3
4
  export class MockProcess {
4
5
  emitter?: EventEmitter
5
6
  process?: NodeJS.Process;
@@ -37,13 +38,31 @@ export class MockProcess {
37
38
  this.process = undefined;
38
39
  }
39
40
  }
41
+ export type ListenProcessParams = {
42
+ message?: RunMessage,
43
+ context?: any
44
+ }
45
+ export type ListenProcessResponse = {
46
+ // 调用进程的功能
47
+ success?: boolean
48
+ data?: {
49
+ // 调用router的结果
50
+ code?: number
51
+ data?: any
52
+ message?: string
53
+ [key: string]: any
54
+ };
55
+ error?: any
56
+ timestamp?: string
57
+ [key: string]: any
58
+ }
40
59
  export type ListenProcessOptions = {
41
60
  app?: QueryRouterServer; // 传入的应用实例
42
61
  mockProcess?: MockProcess; // 可选的事件发射器
43
- params?: any; // 可选的参数
62
+ params?: ListenProcessParams; // 可选的参数
44
63
  timeout?: number; // 可选的超时时间 (单位: 毫秒) 默认 10 分钟
45
64
  };
46
- export const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60 * 1000 }: ListenProcessOptions) => {
65
+ export const listenProcess = async ({ app, mockProcess, params = {}, timeout = 10 * 60 * 60 * 1000 }: ListenProcessOptions) => {
47
66
  const process = mockProcess || new MockProcess();
48
67
  let isEnd = false;
49
68
  const timer = setTimeout(() => {
@@ -57,11 +76,11 @@ export const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 6
57
76
  // 监听来自主进程的消息
58
77
  const getParams = async (): Promise<any> => {
59
78
  return new Promise((resolve) => {
60
- process.on((msg) => {
79
+ process.on((params) => {
61
80
  if (isEnd) return;
62
81
  isEnd = true;
63
82
  clearTimeout(timer);
64
- resolve(msg)
83
+ resolve(params || {})
65
84
  })
66
85
  })
67
86
  }
@@ -70,11 +89,11 @@ export const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 6
70
89
  /**
71
90
  * 如果不提供path,默认是main
72
91
  */
73
- const {
74
- payload = {},
75
- ...rest
76
- } = await getParams()
77
- const msg = { ...params, ...rest, payload: { ...params?.payload, ...payload } }
92
+ const _params = await getParams()
93
+ const mergeParams = merge(params, _params)
94
+
95
+ const msg = mergeParams?.message || {};
96
+ const ctx: RouterContextT = mergeParams?.context || {}
78
97
  /**
79
98
  * 如果没有提供path和id,默认取第一个路由, 而且路由path不是router的
80
99
  */
@@ -83,7 +102,7 @@ export const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 6
83
102
  msg.id = route?.id
84
103
  }
85
104
  // 执行主要逻辑
86
- const result = await app.run(msg)
105
+ const result = await app.run(msg, ctx);
87
106
  // 发送结果回主进程
88
107
  const response = {
89
108
  success: true,
@@ -95,6 +114,7 @@ export const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 6
95
114
  process.exit?.(0)
96
115
  })
97
116
  } catch (error) {
117
+ console.error('Error in listenProcess:', error);
98
118
  process.send?.({
99
119
  success: false,
100
120
  error: error.message