@kevisual/router 0.0.83 → 0.0.85
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/agent/routes/index.ts +16 -10
- package/agent/routes/route-create.ts +3 -3
- package/dist/app.js +103 -64
- package/dist/opencode.d.ts +99 -82
- package/dist/opencode.js +10 -5
- package/dist/router-browser.d.ts +77 -51
- package/dist/router-browser.js +16 -17
- package/dist/router-define.d.ts +77 -51
- package/dist/router.d.ts +101 -82
- package/dist/router.js +33 -57
- package/dist/ws.d.ts +122 -69
- package/package.json +7 -7
- package/readme.md +78 -63
- package/src/app.ts +7 -50
- package/src/opencode.ts +12 -5
- package/src/route.ts +80 -62
- package/src/server/server-base.ts +4 -1
- package/src/server/server-bun.ts +6 -2
- package/src/server/server-type.ts +17 -0
- package/src/server/ws-server.ts +8 -1
- package/src/test/app-type.ts +66 -10
- package/src/test/chat.ts +1 -1
- package/src/test/route-ts.ts +15 -0
package/dist/router.d.ts
CHANGED
|
@@ -80,9 +80,20 @@ type RouterContextT = {
|
|
|
80
80
|
code?: number;
|
|
81
81
|
[key: string]: any;
|
|
82
82
|
};
|
|
83
|
+
type BuildRouteContext<M, U> = M extends {
|
|
84
|
+
args?: infer A;
|
|
85
|
+
} ? A extends z.ZodObject<any> ? RouteContext<{
|
|
86
|
+
args?: z.infer<A>;
|
|
87
|
+
}, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
|
|
88
|
+
args?: {
|
|
89
|
+
[K in keyof A]: z.infer<A[K]>;
|
|
90
|
+
};
|
|
91
|
+
}, U> : RouteContext<U> : RouteContext<U>;
|
|
83
92
|
type RouteContext<T = {
|
|
84
93
|
code?: number;
|
|
85
|
-
},
|
|
94
|
+
}, U extends SimpleObject$1 = {}, S = {
|
|
95
|
+
[key: string]: any;
|
|
96
|
+
}> = {
|
|
86
97
|
/**
|
|
87
98
|
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
|
|
88
99
|
* 或者不需要登录的,直接调用
|
|
@@ -105,7 +116,14 @@ type RouteContext<T = {
|
|
|
105
116
|
code?: number;
|
|
106
117
|
/** return msg */
|
|
107
118
|
message?: string;
|
|
119
|
+
/**
|
|
120
|
+
* 传递状态
|
|
121
|
+
*/
|
|
108
122
|
state?: S;
|
|
123
|
+
/**
|
|
124
|
+
* 当前routerId
|
|
125
|
+
*/
|
|
126
|
+
currentId?: string;
|
|
109
127
|
/**
|
|
110
128
|
* 当前路径
|
|
111
129
|
*/
|
|
@@ -146,14 +164,12 @@ type RouteContext<T = {
|
|
|
146
164
|
path: string;
|
|
147
165
|
key?: string;
|
|
148
166
|
payload?: any;
|
|
149
|
-
}, ctx?: RouteContext
|
|
150
|
-
[key: string]: any;
|
|
151
|
-
}) => Promise<any>;
|
|
167
|
+
}, ctx?: RouteContext) => Promise<any>;
|
|
152
168
|
index?: number;
|
|
153
169
|
throw?: throwError['throw'];
|
|
154
170
|
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
|
|
155
171
|
needSerialize?: boolean;
|
|
156
|
-
} & T;
|
|
172
|
+
} & T & U;
|
|
157
173
|
type SimpleObject$1 = Record<string, any>;
|
|
158
174
|
type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
|
|
159
175
|
type RunMessage = {
|
|
@@ -164,7 +180,7 @@ type RunMessage = {
|
|
|
164
180
|
};
|
|
165
181
|
type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
166
182
|
type RouteMiddleware = {
|
|
167
|
-
path
|
|
183
|
+
path?: string;
|
|
168
184
|
key?: string;
|
|
169
185
|
id?: string;
|
|
170
186
|
} | string;
|
|
@@ -177,7 +193,7 @@ type RouteOpts<U = {}, T = SimpleObject$1> = {
|
|
|
177
193
|
description?: string;
|
|
178
194
|
metadata?: T;
|
|
179
195
|
middleware?: RouteMiddleware[];
|
|
180
|
-
type?: 'route' | 'middleware';
|
|
196
|
+
type?: 'route' | 'middleware' | 'compound';
|
|
181
197
|
/**
|
|
182
198
|
* $#$ will be used to split path and key
|
|
183
199
|
*/
|
|
@@ -205,9 +221,11 @@ declare const tool: {
|
|
|
205
221
|
/** */
|
|
206
222
|
declare const createSkill: <T = SimpleObject$1>(skill: Skill<T>) => Skill<T>;
|
|
207
223
|
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
224
|
+
/**
|
|
225
|
+
* @M 是 route的 metadate的类型,默认是 SimpleObject
|
|
226
|
+
* @U 是 RouteContext 里 state的类型
|
|
227
|
+
*/
|
|
228
|
+
declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
211
229
|
/**
|
|
212
230
|
* 一级路径
|
|
213
231
|
*/
|
|
@@ -217,10 +235,10 @@ declare class Route<U = {
|
|
|
217
235
|
*/
|
|
218
236
|
key?: string;
|
|
219
237
|
id?: string;
|
|
220
|
-
run?: Run
|
|
238
|
+
run?: Run<BuildRouteContext<M, U>>;
|
|
221
239
|
nextRoute?: NextRoute;
|
|
222
240
|
description?: string;
|
|
223
|
-
metadata?:
|
|
241
|
+
metadata?: M;
|
|
224
242
|
middleware?: RouteMiddleware[];
|
|
225
243
|
type?: string;
|
|
226
244
|
/**
|
|
@@ -235,13 +253,13 @@ declare class Route<U = {
|
|
|
235
253
|
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
236
254
|
define<T extends {
|
|
237
255
|
[key: string]: any;
|
|
238
|
-
} = RouterContextT>(fn: Run<T & U
|
|
256
|
+
} = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
239
257
|
define<T extends {
|
|
240
258
|
[key: string]: any;
|
|
241
|
-
} = RouterContextT>(key: string, fn: Run<T & U
|
|
259
|
+
} = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
242
260
|
define<T extends {
|
|
243
261
|
[key: string]: any;
|
|
244
|
-
} = RouterContextT>(path: string, key: string, fn: Run<T & U
|
|
262
|
+
} = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
245
263
|
update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
|
|
246
264
|
addTo(router: QueryRouter | {
|
|
247
265
|
add: (route: Route) => void;
|
|
@@ -272,11 +290,11 @@ declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?:
|
|
|
272
290
|
type AddOpts = {
|
|
273
291
|
overwrite?: boolean;
|
|
274
292
|
};
|
|
275
|
-
declare class QueryRouter implements throwError {
|
|
293
|
+
declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
276
294
|
appId: string;
|
|
277
295
|
routes: Route[];
|
|
278
296
|
maxNextRoute: number;
|
|
279
|
-
context?: RouteContext
|
|
297
|
+
context?: RouteContext<T>;
|
|
280
298
|
constructor();
|
|
281
299
|
/**
|
|
282
300
|
* add route
|
|
@@ -304,7 +322,7 @@ declare class QueryRouter implements throwError {
|
|
|
304
322
|
* @param ctx
|
|
305
323
|
* @returns
|
|
306
324
|
*/
|
|
307
|
-
runRoute(path: string, key: string, ctx?: RouteContext):
|
|
325
|
+
runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
|
|
308
326
|
/**
|
|
309
327
|
* 第一次执行
|
|
310
328
|
* @param message
|
|
@@ -315,9 +333,11 @@ declare class QueryRouter implements throwError {
|
|
|
315
333
|
path: string;
|
|
316
334
|
key?: string;
|
|
317
335
|
payload?: any;
|
|
318
|
-
}, ctx?: RouteContext & {
|
|
336
|
+
}, ctx?: RouteContext<T> & {
|
|
337
|
+
[key: string]: any;
|
|
338
|
+
}): Promise<RouteContext<T, {}, {
|
|
319
339
|
[key: string]: any;
|
|
320
|
-
}
|
|
340
|
+
}>>;
|
|
321
341
|
/**
|
|
322
342
|
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
323
343
|
* @param message
|
|
@@ -329,9 +349,15 @@ declare class QueryRouter implements throwError {
|
|
|
329
349
|
path?: string;
|
|
330
350
|
key?: string;
|
|
331
351
|
payload?: any;
|
|
332
|
-
}, ctx?: RouteContext & {
|
|
352
|
+
}, ctx?: RouteContext<T> & {
|
|
353
|
+
[key: string]: any;
|
|
354
|
+
}): Promise<RouteContext<T, {}, {
|
|
333
355
|
[key: string]: any;
|
|
334
|
-
}
|
|
356
|
+
}> | {
|
|
357
|
+
code: number;
|
|
358
|
+
body: any;
|
|
359
|
+
message: string;
|
|
360
|
+
}>;
|
|
335
361
|
/**
|
|
336
362
|
* 请求 result 的数据
|
|
337
363
|
* @param message
|
|
@@ -347,9 +373,9 @@ declare class QueryRouter implements throwError {
|
|
|
347
373
|
}, ctx?: RouteContext & {
|
|
348
374
|
[key: string]: any;
|
|
349
375
|
}): Promise<{
|
|
350
|
-
code:
|
|
376
|
+
code: number;
|
|
351
377
|
data: any;
|
|
352
|
-
message:
|
|
378
|
+
message: string;
|
|
353
379
|
}>;
|
|
354
380
|
/**
|
|
355
381
|
* Router Run获取数据
|
|
@@ -362,12 +388,12 @@ declare class QueryRouter implements throwError {
|
|
|
362
388
|
path?: string;
|
|
363
389
|
key?: string;
|
|
364
390
|
payload?: any;
|
|
365
|
-
}, ctx?: RouteContext & {
|
|
391
|
+
}, ctx?: RouteContext<T> & {
|
|
366
392
|
[key: string]: any;
|
|
367
393
|
}): Promise<{
|
|
368
|
-
code:
|
|
394
|
+
code: number;
|
|
369
395
|
data: any;
|
|
370
|
-
message:
|
|
396
|
+
message: string;
|
|
371
397
|
}>;
|
|
372
398
|
/**
|
|
373
399
|
* 设置上下文
|
|
@@ -379,12 +405,12 @@ declare class QueryRouter implements throwError {
|
|
|
379
405
|
/**
|
|
380
406
|
* 获取handle函数, 这里会去执行parse函数
|
|
381
407
|
*/
|
|
382
|
-
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn
|
|
408
|
+
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
|
|
383
409
|
id?: string;
|
|
384
410
|
path?: string;
|
|
385
411
|
key?: string;
|
|
386
412
|
[key: string]: any;
|
|
387
|
-
}, handleContext?: RouteContext) => Promise<{
|
|
413
|
+
}, handleContext?: RouteContext<T>) => Promise<{
|
|
388
414
|
[key: string]: any;
|
|
389
415
|
code: string;
|
|
390
416
|
data?: any;
|
|
@@ -398,22 +424,16 @@ declare class QueryRouter implements throwError {
|
|
|
398
424
|
message: any;
|
|
399
425
|
data?: undefined;
|
|
400
426
|
}>;
|
|
401
|
-
exportRoutes(): Route<
|
|
402
|
-
[key: string]: any;
|
|
403
|
-
}, SimpleObject$1>[];
|
|
427
|
+
exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
|
|
404
428
|
importRoutes(routes: Route[]): void;
|
|
405
429
|
importRouter(router: QueryRouter): void;
|
|
406
430
|
throw(...args: any[]): void;
|
|
407
|
-
hasRoute(path: string, key?: string): Route<
|
|
408
|
-
[key: string]: any;
|
|
409
|
-
}, SimpleObject$1>;
|
|
431
|
+
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
410
432
|
findRoute(opts?: {
|
|
411
433
|
path?: string;
|
|
412
434
|
key?: string;
|
|
413
435
|
id?: string;
|
|
414
|
-
}): Route<
|
|
415
|
-
[key: string]: any;
|
|
416
|
-
}, SimpleObject$1>;
|
|
436
|
+
}): Route<SimpleObject$1, SimpleObject$1>;
|
|
417
437
|
createRouteList(opts?: {
|
|
418
438
|
force?: boolean;
|
|
419
439
|
filter?: (route: Route) => boolean;
|
|
@@ -455,10 +475,11 @@ declare class QueryRouter implements throwError {
|
|
|
455
475
|
[key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
456
476
|
};
|
|
457
477
|
}
|
|
458
|
-
type QueryRouterServerOpts = {
|
|
478
|
+
type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
|
|
459
479
|
handleFn?: HandleFn;
|
|
460
|
-
context?: RouteContext
|
|
480
|
+
context?: RouteContext<C>;
|
|
461
481
|
appId?: string;
|
|
482
|
+
initHandle?: boolean;
|
|
462
483
|
};
|
|
463
484
|
interface HandleFn<T = any> {
|
|
464
485
|
(msg: {
|
|
@@ -475,20 +496,27 @@ interface HandleFn<T = any> {
|
|
|
475
496
|
/**
|
|
476
497
|
* QueryRouterServer
|
|
477
498
|
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
499
|
+
* @template C 自定义 RouteContext 类型
|
|
478
500
|
*/
|
|
479
|
-
declare class QueryRouterServer extends QueryRouter {
|
|
501
|
+
declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
|
|
480
502
|
appId: string;
|
|
481
503
|
handle: any;
|
|
482
|
-
|
|
504
|
+
context: RouteContext<C>;
|
|
505
|
+
constructor(opts?: QueryRouterServerOpts<C>);
|
|
483
506
|
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
|
|
484
507
|
addRoute(route: Route, opts?: AddOpts): void;
|
|
485
508
|
Route: typeof Route;
|
|
486
|
-
route(opts: RouteOpts
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
route(path: string,
|
|
490
|
-
|
|
491
|
-
|
|
509
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(opts: RouteOpts & {
|
|
510
|
+
metadata?: M;
|
|
511
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
512
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, opts?: RouteOpts & {
|
|
513
|
+
metadata?: M;
|
|
514
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
515
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
|
|
516
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string, opts?: RouteOpts & {
|
|
517
|
+
metadata?: M;
|
|
518
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
519
|
+
prompt(description: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
492
520
|
/**
|
|
493
521
|
* 调用了handle
|
|
494
522
|
* @param param0
|
|
@@ -499,9 +527,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
499
527
|
path?: string;
|
|
500
528
|
key?: string;
|
|
501
529
|
payload?: any;
|
|
502
|
-
}, ctx?: RouteContext
|
|
503
|
-
[key: string]: any;
|
|
504
|
-
}): Promise<any>;
|
|
530
|
+
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
505
531
|
}
|
|
506
532
|
declare class Mini extends QueryRouterServer {
|
|
507
533
|
}
|
|
@@ -662,16 +688,33 @@ type OnWebSocketOptions<T = {}> = {
|
|
|
662
688
|
message: string | Buffer;
|
|
663
689
|
pathname: string;
|
|
664
690
|
token?: string;
|
|
691
|
+
/** data 的id提取出来 */
|
|
665
692
|
id?: string;
|
|
666
693
|
};
|
|
667
694
|
type OnWebSocketFn = (options: OnWebSocketOptions) => Promise<void> | void;
|
|
668
695
|
type WS<T = {}> = {
|
|
669
696
|
send: (data: any) => void;
|
|
670
697
|
close: (code?: number, reason?: string) => void;
|
|
698
|
+
/**
|
|
699
|
+
* ws 自己生成的一个id,主要是为了区分不同的ws连接,方便在onWebSocket中使用
|
|
700
|
+
*/
|
|
701
|
+
wsId?: string;
|
|
671
702
|
data?: {
|
|
703
|
+
/**
|
|
704
|
+
* ws连接时的url,包含pathname和searchParams
|
|
705
|
+
*/
|
|
672
706
|
url: URL;
|
|
707
|
+
/**
|
|
708
|
+
* ws连接时的pathname
|
|
709
|
+
*/
|
|
673
710
|
pathname: string;
|
|
711
|
+
/**
|
|
712
|
+
* ws连接时的url中的token参数
|
|
713
|
+
*/
|
|
674
714
|
token?: string;
|
|
715
|
+
/**
|
|
716
|
+
* ws连接时的url中的id参数.
|
|
717
|
+
*/
|
|
675
718
|
id?: string;
|
|
676
719
|
/**
|
|
677
720
|
* 鉴权后的获取的信息
|
|
@@ -890,6 +933,7 @@ declare class ServerBase implements ServerType {
|
|
|
890
933
|
*/
|
|
891
934
|
onWsClose(ws: WS): Promise<void>;
|
|
892
935
|
sendConnected(ws: WS): Promise<void>;
|
|
936
|
+
createId(): string;
|
|
893
937
|
}
|
|
894
938
|
|
|
895
939
|
type WsServerBaseOpts = {
|
|
@@ -902,6 +946,7 @@ declare class WsServerBase {
|
|
|
902
946
|
server: ServerType;
|
|
903
947
|
constructor(opts: WsServerBaseOpts);
|
|
904
948
|
listen(): void;
|
|
949
|
+
createId(): string;
|
|
905
950
|
}
|
|
906
951
|
declare class WsServer extends WsServerBase {
|
|
907
952
|
constructor(server: ServerType);
|
|
@@ -957,7 +1002,7 @@ type RouterHandle = (msg: {
|
|
|
957
1002
|
[key: string]: any;
|
|
958
1003
|
};
|
|
959
1004
|
type AppOptions<T = {}> = {
|
|
960
|
-
router?:
|
|
1005
|
+
router?: QueryRouterServer;
|
|
961
1006
|
server?: ServerType;
|
|
962
1007
|
/** handle msg 关联 */
|
|
963
1008
|
routerHandle?: RouterHandle;
|
|
@@ -965,17 +1010,18 @@ type AppOptions<T = {}> = {
|
|
|
965
1010
|
serverOptions?: ServerNodeOpts;
|
|
966
1011
|
appId?: string;
|
|
967
1012
|
};
|
|
968
|
-
type AppRouteContext<T
|
|
1013
|
+
type AppRouteContext<T> = HandleCtx & RouteContext<T> & {
|
|
969
1014
|
app: App<T>;
|
|
970
1015
|
};
|
|
971
1016
|
/**
|
|
972
1017
|
* 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
|
|
973
1018
|
* U - Route Context的扩展类型
|
|
974
1019
|
*/
|
|
975
|
-
declare class App<U = {}> extends
|
|
1020
|
+
declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
|
|
976
1021
|
appId: string;
|
|
977
|
-
router:
|
|
1022
|
+
router: QueryRouterServer;
|
|
978
1023
|
server: ServerType;
|
|
1024
|
+
context: AppRouteContext<U>;
|
|
979
1025
|
constructor(opts?: AppOptions<U>);
|
|
980
1026
|
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
981
1027
|
listen(port: number, hostname?: string, listeningListener?: () => void): void;
|
|
@@ -985,34 +1031,7 @@ declare class App<U = {}> extends QueryRouter {
|
|
|
985
1031
|
listen(path: string, listeningListener?: () => void): void;
|
|
986
1032
|
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
987
1033
|
listen(handle: any, listeningListener?: () => void): void;
|
|
988
|
-
addRoute(route: Route, opts?: AddOpts): void;
|
|
989
1034
|
Route: typeof Route;
|
|
990
|
-
route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
991
|
-
route(path: string, key?: string): Route<AppRouteContext<U>>;
|
|
992
|
-
route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
993
|
-
route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
|
|
994
|
-
prompt(description: string): Route<AppRouteContext<U>>;
|
|
995
|
-
prompt(description: Function): Route<AppRouteContext<U>>;
|
|
996
|
-
call(message: {
|
|
997
|
-
id?: string;
|
|
998
|
-
path?: string;
|
|
999
|
-
key?: string;
|
|
1000
|
-
payload?: any;
|
|
1001
|
-
}, ctx?: AppRouteContext<U> & {
|
|
1002
|
-
[key: string]: any;
|
|
1003
|
-
}): Promise<any>;
|
|
1004
|
-
run(msg: {
|
|
1005
|
-
id?: string;
|
|
1006
|
-
path?: string;
|
|
1007
|
-
key?: string;
|
|
1008
|
-
payload?: any;
|
|
1009
|
-
}, ctx?: Partial<AppRouteContext<U>> & {
|
|
1010
|
-
[key: string]: any;
|
|
1011
|
-
}): Promise<{
|
|
1012
|
-
code: any;
|
|
1013
|
-
data: any;
|
|
1014
|
-
message: any;
|
|
1015
|
-
}>;
|
|
1016
1035
|
static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
|
|
1017
1036
|
cookies: Record<string, string>;
|
|
1018
1037
|
token: string;
|
package/dist/router.js
CHANGED
|
@@ -3091,7 +3091,7 @@ function pick(obj, keys) {
|
|
|
3091
3091
|
// node_modules/.pnpm/eventemitter3@5.0.4/node_modules/eventemitter3/index.mjs
|
|
3092
3092
|
var import__ = __toESM(require_eventemitter3(), 1);
|
|
3093
3093
|
|
|
3094
|
-
// node_modules/.pnpm/es-toolkit@1.
|
|
3094
|
+
// node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/predicate/isPlainObject.mjs
|
|
3095
3095
|
function isPlainObject(value) {
|
|
3096
3096
|
if (!value || typeof value !== "object") {
|
|
3097
3097
|
return false;
|
|
@@ -3104,12 +3104,12 @@ function isPlainObject(value) {
|
|
|
3104
3104
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
3105
3105
|
}
|
|
3106
3106
|
|
|
3107
|
-
// node_modules/.pnpm/es-toolkit@1.
|
|
3107
|
+
// node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/_internal/isUnsafeProperty.mjs
|
|
3108
3108
|
function isUnsafeProperty(key) {
|
|
3109
3109
|
return key === "__proto__";
|
|
3110
3110
|
}
|
|
3111
3111
|
|
|
3112
|
-
// node_modules/.pnpm/es-toolkit@1.
|
|
3112
|
+
// node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/object/merge.mjs
|
|
3113
3113
|
function merge(target, source) {
|
|
3114
3114
|
const sourceKeys = Object.keys(source);
|
|
3115
3115
|
for (let i = 0;i < sourceKeys.length; i++) {
|
|
@@ -16929,7 +16929,7 @@ class Route {
|
|
|
16929
16929
|
if (opts) {
|
|
16930
16930
|
this.id = opts.id || randomId(12, "rand-");
|
|
16931
16931
|
if (!opts.id && opts.idUsePath) {
|
|
16932
|
-
const delimiter = opts.delimiter ?? "
|
|
16932
|
+
const delimiter = opts.delimiter ?? "$$";
|
|
16933
16933
|
this.id = path + delimiter + key;
|
|
16934
16934
|
}
|
|
16935
16935
|
this.run = opts.run;
|
|
@@ -17052,6 +17052,7 @@ class QueryRouter {
|
|
|
17052
17052
|
const maxNextRoute = this.maxNextRoute;
|
|
17053
17053
|
ctx = ctx || {};
|
|
17054
17054
|
ctx.currentPath = path;
|
|
17055
|
+
ctx.currentId = route?.id;
|
|
17055
17056
|
ctx.currentKey = key;
|
|
17056
17057
|
ctx.currentRoute = route;
|
|
17057
17058
|
ctx.index = (ctx.index || 0) + 1;
|
|
@@ -17065,7 +17066,7 @@ class QueryRouter {
|
|
|
17065
17066
|
ctx.code = 500;
|
|
17066
17067
|
ctx.message = "Too many nextRoute";
|
|
17067
17068
|
ctx.body = null;
|
|
17068
|
-
return;
|
|
17069
|
+
return ctx;
|
|
17069
17070
|
}
|
|
17070
17071
|
if (route && route.middleware && route.middleware.length > 0) {
|
|
17071
17072
|
const errorMiddleware = [];
|
|
@@ -17140,7 +17141,9 @@ class QueryRouter {
|
|
|
17140
17141
|
}
|
|
17141
17142
|
return ctx;
|
|
17142
17143
|
}
|
|
17143
|
-
if (ctx.end) {
|
|
17144
|
+
if (ctx.end) {
|
|
17145
|
+
return ctx;
|
|
17146
|
+
}
|
|
17144
17147
|
}
|
|
17145
17148
|
}
|
|
17146
17149
|
}
|
|
@@ -17334,7 +17337,7 @@ class QueryRouter {
|
|
|
17334
17337
|
description: "列出当前应用下的所有的路由信息",
|
|
17335
17338
|
middleware: opts?.middleware || [],
|
|
17336
17339
|
run: async (ctx) => {
|
|
17337
|
-
const tokenUser = ctx.state
|
|
17340
|
+
const tokenUser = ctx.state;
|
|
17338
17341
|
let isUser = !!tokenUser;
|
|
17339
17342
|
const list = this.getList(opts?.filter).filter((item) => {
|
|
17340
17343
|
if (item.id === "auth" || item.id === "auth-can" || item.id === "check-auth-admin" || item.id === "auth-admin") {
|
|
@@ -17369,7 +17372,10 @@ class QueryRouterServer extends QueryRouter {
|
|
|
17369
17372
|
handle;
|
|
17370
17373
|
constructor(opts) {
|
|
17371
17374
|
super();
|
|
17372
|
-
|
|
17375
|
+
const initHandle = opts?.initHandle ?? true;
|
|
17376
|
+
if (initHandle || opts?.handleFn) {
|
|
17377
|
+
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
|
17378
|
+
}
|
|
17373
17379
|
this.setContext({ needSerialize: false, ...opts?.context });
|
|
17374
17380
|
if (opts?.appId) {
|
|
17375
17381
|
this.appId = opts.appId;
|
|
@@ -17400,15 +17406,8 @@ class QueryRouterServer extends QueryRouter {
|
|
|
17400
17406
|
}
|
|
17401
17407
|
return new Route(path, key, opts);
|
|
17402
17408
|
}
|
|
17403
|
-
prompt(
|
|
17404
|
-
|
|
17405
|
-
let description = "";
|
|
17406
|
-
if (typeof desc === "string") {
|
|
17407
|
-
description = desc;
|
|
17408
|
-
} else if (typeof desc === "function") {
|
|
17409
|
-
description = desc() || "";
|
|
17410
|
-
}
|
|
17411
|
-
return new Route("", "", { description });
|
|
17409
|
+
prompt(description) {
|
|
17410
|
+
return new Route(undefined, undefined, { description });
|
|
17412
17411
|
}
|
|
17413
17412
|
async run(msg, ctx) {
|
|
17414
17413
|
const handle = this.handle;
|
|
@@ -18089,7 +18088,7 @@ class ServerBase {
|
|
|
18089
18088
|
}
|
|
18090
18089
|
}
|
|
18091
18090
|
async onWsClose(ws) {
|
|
18092
|
-
const id = ws?.
|
|
18091
|
+
const id = ws?.wsId || "";
|
|
18093
18092
|
if (id) {
|
|
18094
18093
|
this.emitter.emit("close--" + id, { type: "close", ws, id });
|
|
18095
18094
|
setTimeout(() => {
|
|
@@ -18102,6 +18101,9 @@ class ServerBase {
|
|
|
18102
18101
|
if (this.showConnected)
|
|
18103
18102
|
ws.send(JSON.stringify({ type: "connected" }));
|
|
18104
18103
|
}
|
|
18104
|
+
createId() {
|
|
18105
|
+
return Math.random().toString(36).substring(2, 15);
|
|
18106
|
+
}
|
|
18105
18107
|
}
|
|
18106
18108
|
|
|
18107
18109
|
// node_modules/.pnpm/@kevisual+ws@8.0.0/node_modules/@kevisual/ws/wrapper.mjs
|
|
@@ -18142,6 +18144,9 @@ class WsServerBase {
|
|
|
18142
18144
|
token,
|
|
18143
18145
|
id
|
|
18144
18146
|
};
|
|
18147
|
+
if (!ws.wsId) {
|
|
18148
|
+
ws.wsId = this.createId();
|
|
18149
|
+
}
|
|
18145
18150
|
ws.on("message", async (message) => {
|
|
18146
18151
|
await this.server.onWebSocket({ ws, message, pathname, token, id });
|
|
18147
18152
|
});
|
|
@@ -18152,6 +18157,9 @@ class WsServerBase {
|
|
|
18152
18157
|
});
|
|
18153
18158
|
});
|
|
18154
18159
|
}
|
|
18160
|
+
createId() {
|
|
18161
|
+
return Math.random().toString(36).substring(2, 15);
|
|
18162
|
+
}
|
|
18155
18163
|
}
|
|
18156
18164
|
|
|
18157
18165
|
class WsServer extends WsServerBase {
|
|
@@ -18466,10 +18474,14 @@ class BunServer extends ServerBase {
|
|
|
18466
18474
|
open: (ws) => {
|
|
18467
18475
|
this.sendConnected(ws);
|
|
18468
18476
|
},
|
|
18469
|
-
message: async (
|
|
18477
|
+
message: async (bunWs, message) => {
|
|
18478
|
+
const ws = bunWs;
|
|
18470
18479
|
const pathname = ws.data.pathname || "";
|
|
18471
18480
|
const token = ws.data.token || "";
|
|
18472
18481
|
const id = ws.data.id || "";
|
|
18482
|
+
if (!ws.wsId) {
|
|
18483
|
+
ws.wsId = this.createId();
|
|
18484
|
+
}
|
|
18473
18485
|
await this.onWebSocket({ ws, message, pathname, token, id });
|
|
18474
18486
|
},
|
|
18475
18487
|
close: (ws) => {
|
|
@@ -18484,11 +18496,11 @@ class BunServer extends ServerBase {
|
|
|
18484
18496
|
}
|
|
18485
18497
|
}
|
|
18486
18498
|
// src/app.ts
|
|
18487
|
-
class App extends
|
|
18499
|
+
class App extends QueryRouterServer {
|
|
18488
18500
|
router;
|
|
18489
18501
|
server;
|
|
18490
18502
|
constructor(opts) {
|
|
18491
|
-
super();
|
|
18503
|
+
super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } });
|
|
18492
18504
|
const router = this;
|
|
18493
18505
|
let server = opts?.server;
|
|
18494
18506
|
if (!server) {
|
|
@@ -18500,7 +18512,6 @@ class App extends QueryRouter {
|
|
|
18500
18512
|
}
|
|
18501
18513
|
}
|
|
18502
18514
|
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
|
|
18503
|
-
router.setContext({ needSerialize: true, ...opts?.routerContext });
|
|
18504
18515
|
this.router = router;
|
|
18505
18516
|
this.server = server;
|
|
18506
18517
|
if (opts?.appId) {
|
|
@@ -18513,42 +18524,7 @@ class App extends QueryRouter {
|
|
|
18513
18524
|
listen(...args) {
|
|
18514
18525
|
this.server.listen(...args);
|
|
18515
18526
|
}
|
|
18516
|
-
addRoute(route, opts) {
|
|
18517
|
-
super.add(route, opts);
|
|
18518
|
-
}
|
|
18519
18527
|
Route = Route;
|
|
18520
|
-
route(...args) {
|
|
18521
|
-
const [path, key, opts] = args;
|
|
18522
|
-
if (typeof path === "object") {
|
|
18523
|
-
return new Route(path.path, path.key, path);
|
|
18524
|
-
}
|
|
18525
|
-
if (typeof path === "string") {
|
|
18526
|
-
if (opts) {
|
|
18527
|
-
return new Route(path, key, opts);
|
|
18528
|
-
}
|
|
18529
|
-
if (key && typeof key === "object") {
|
|
18530
|
-
return new Route(path, key?.key || "", key);
|
|
18531
|
-
}
|
|
18532
|
-
return new Route(path, key);
|
|
18533
|
-
}
|
|
18534
|
-
return new Route(path, key, opts);
|
|
18535
|
-
}
|
|
18536
|
-
prompt(...args) {
|
|
18537
|
-
const [desc] = args;
|
|
18538
|
-
let description = "";
|
|
18539
|
-
if (typeof desc === "string") {
|
|
18540
|
-
description = desc;
|
|
18541
|
-
} else if (typeof desc === "function") {
|
|
18542
|
-
description = desc() || "";
|
|
18543
|
-
}
|
|
18544
|
-
return new Route("", "", { description });
|
|
18545
|
-
}
|
|
18546
|
-
async call(message, ctx) {
|
|
18547
|
-
return await super.call(message, ctx);
|
|
18548
|
-
}
|
|
18549
|
-
async run(msg, ctx) {
|
|
18550
|
-
return await super.run(msg, ctx);
|
|
18551
|
-
}
|
|
18552
18528
|
static handleRequest(req, res) {
|
|
18553
18529
|
return handleServer(req, res);
|
|
18554
18530
|
}
|