@kevisual/router 0.0.28 → 0.0.29
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-browser.d.ts +16 -0
- package/dist/router-browser.js +56 -0
- package/dist/router.d.ts +16 -0
- package/dist/router.js +56 -0
- package/package.json +2 -2
- package/src/route.ts +22 -10
- package/src/utils/listen-process.ts +50 -0
package/dist/router-browser.d.ts
CHANGED
|
@@ -360,6 +360,22 @@ declare class QueryRouter {
|
|
|
360
360
|
hasRoute(path: string, key?: string): Route<{
|
|
361
361
|
[key: string]: any;
|
|
362
362
|
}>;
|
|
363
|
+
/**
|
|
364
|
+
* 等待程序运行, 获取到message的数据,就执行
|
|
365
|
+
*
|
|
366
|
+
* emitter = process
|
|
367
|
+
* -- .exit
|
|
368
|
+
* -- .on
|
|
369
|
+
* -- .send
|
|
370
|
+
*/
|
|
371
|
+
wait(params?: {
|
|
372
|
+
path?: string;
|
|
373
|
+
key?: string;
|
|
374
|
+
payload?: any;
|
|
375
|
+
}, opts?: {
|
|
376
|
+
emitter?: any;
|
|
377
|
+
timeout?: number;
|
|
378
|
+
}): Promise<void>;
|
|
363
379
|
}
|
|
364
380
|
type QueryRouterServerOpts = {
|
|
365
381
|
handleFn?: HandleFn;
|
package/dist/router-browser.js
CHANGED
|
@@ -4743,6 +4743,51 @@ function get(object, path, defaultValue) {
|
|
|
4743
4743
|
return result === undefined ? defaultValue : result;
|
|
4744
4744
|
}
|
|
4745
4745
|
|
|
4746
|
+
const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 1000 }) => {
|
|
4747
|
+
const process = emitter || globalThis.process;
|
|
4748
|
+
let isEnd = false;
|
|
4749
|
+
const timer = setTimeout(() => {
|
|
4750
|
+
if (isEnd)
|
|
4751
|
+
return;
|
|
4752
|
+
isEnd = true;
|
|
4753
|
+
process.send?.({ success: false, error: 'Timeout' });
|
|
4754
|
+
process.exit?.(1);
|
|
4755
|
+
}, timeout);
|
|
4756
|
+
// 监听来自主进程的消息
|
|
4757
|
+
const getParams = async () => {
|
|
4758
|
+
return new Promise((resolve) => {
|
|
4759
|
+
process.on('message', (msg) => {
|
|
4760
|
+
if (isEnd)
|
|
4761
|
+
return;
|
|
4762
|
+
isEnd = true;
|
|
4763
|
+
clearTimeout(timer);
|
|
4764
|
+
resolve(msg);
|
|
4765
|
+
});
|
|
4766
|
+
});
|
|
4767
|
+
};
|
|
4768
|
+
try {
|
|
4769
|
+
const { path = 'main', ...rest } = await getParams();
|
|
4770
|
+
// 执行主要逻辑
|
|
4771
|
+
const result = await app.queryRoute({ path, ...rest, ...params });
|
|
4772
|
+
// 发送结果回主进程
|
|
4773
|
+
const response = {
|
|
4774
|
+
success: true,
|
|
4775
|
+
data: result,
|
|
4776
|
+
timestamp: new Date().toISOString()
|
|
4777
|
+
};
|
|
4778
|
+
process.send?.(response, (error) => {
|
|
4779
|
+
process.exit?.(0);
|
|
4780
|
+
});
|
|
4781
|
+
}
|
|
4782
|
+
catch (error) {
|
|
4783
|
+
process.send?.({
|
|
4784
|
+
success: false,
|
|
4785
|
+
error: error.message
|
|
4786
|
+
});
|
|
4787
|
+
process.exit?.(1);
|
|
4788
|
+
}
|
|
4789
|
+
};
|
|
4790
|
+
|
|
4746
4791
|
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
|
|
4747
4792
|
class Route {
|
|
4748
4793
|
/**
|
|
@@ -5304,6 +5349,17 @@ class QueryRouter {
|
|
|
5304
5349
|
hasRoute(path, key = '') {
|
|
5305
5350
|
return this.routes.find((r) => r.path === path && r.key === key);
|
|
5306
5351
|
}
|
|
5352
|
+
/**
|
|
5353
|
+
* 等待程序运行, 获取到message的数据,就执行
|
|
5354
|
+
*
|
|
5355
|
+
* emitter = process
|
|
5356
|
+
* -- .exit
|
|
5357
|
+
* -- .on
|
|
5358
|
+
* -- .send
|
|
5359
|
+
*/
|
|
5360
|
+
wait(params, opts) {
|
|
5361
|
+
return listenProcess({ app: this, params, ...opts });
|
|
5362
|
+
}
|
|
5307
5363
|
}
|
|
5308
5364
|
/**
|
|
5309
5365
|
* QueryRouterServer
|
package/dist/router.d.ts
CHANGED
|
@@ -364,6 +364,22 @@ declare class QueryRouter {
|
|
|
364
364
|
hasRoute(path: string, key?: string): Route<{
|
|
365
365
|
[key: string]: any;
|
|
366
366
|
}>;
|
|
367
|
+
/**
|
|
368
|
+
* 等待程序运行, 获取到message的数据,就执行
|
|
369
|
+
*
|
|
370
|
+
* emitter = process
|
|
371
|
+
* -- .exit
|
|
372
|
+
* -- .on
|
|
373
|
+
* -- .send
|
|
374
|
+
*/
|
|
375
|
+
wait(params?: {
|
|
376
|
+
path?: string;
|
|
377
|
+
key?: string;
|
|
378
|
+
payload?: any;
|
|
379
|
+
}, opts?: {
|
|
380
|
+
emitter?: any;
|
|
381
|
+
timeout?: number;
|
|
382
|
+
}): Promise<void>;
|
|
367
383
|
}
|
|
368
384
|
type QueryRouterServerOpts = {
|
|
369
385
|
handleFn?: HandleFn;
|
package/dist/router.js
CHANGED
|
@@ -4765,6 +4765,51 @@ function get(object, path, defaultValue) {
|
|
|
4765
4765
|
return result === undefined ? defaultValue : result;
|
|
4766
4766
|
}
|
|
4767
4767
|
|
|
4768
|
+
const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 1000 }) => {
|
|
4769
|
+
const process = emitter || globalThis.process;
|
|
4770
|
+
let isEnd = false;
|
|
4771
|
+
const timer = setTimeout(() => {
|
|
4772
|
+
if (isEnd)
|
|
4773
|
+
return;
|
|
4774
|
+
isEnd = true;
|
|
4775
|
+
process.send?.({ success: false, error: 'Timeout' });
|
|
4776
|
+
process.exit?.(1);
|
|
4777
|
+
}, timeout);
|
|
4778
|
+
// 监听来自主进程的消息
|
|
4779
|
+
const getParams = async () => {
|
|
4780
|
+
return new Promise((resolve) => {
|
|
4781
|
+
process.on('message', (msg) => {
|
|
4782
|
+
if (isEnd)
|
|
4783
|
+
return;
|
|
4784
|
+
isEnd = true;
|
|
4785
|
+
clearTimeout(timer);
|
|
4786
|
+
resolve(msg);
|
|
4787
|
+
});
|
|
4788
|
+
});
|
|
4789
|
+
};
|
|
4790
|
+
try {
|
|
4791
|
+
const { path = 'main', ...rest } = await getParams();
|
|
4792
|
+
// 执行主要逻辑
|
|
4793
|
+
const result = await app.queryRoute({ path, ...rest, ...params });
|
|
4794
|
+
// 发送结果回主进程
|
|
4795
|
+
const response = {
|
|
4796
|
+
success: true,
|
|
4797
|
+
data: result,
|
|
4798
|
+
timestamp: new Date().toISOString()
|
|
4799
|
+
};
|
|
4800
|
+
process.send?.(response, (error) => {
|
|
4801
|
+
process.exit?.(0);
|
|
4802
|
+
});
|
|
4803
|
+
}
|
|
4804
|
+
catch (error) {
|
|
4805
|
+
process.send?.({
|
|
4806
|
+
success: false,
|
|
4807
|
+
error: error.message
|
|
4808
|
+
});
|
|
4809
|
+
process.exit?.(1);
|
|
4810
|
+
}
|
|
4811
|
+
};
|
|
4812
|
+
|
|
4768
4813
|
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
|
|
4769
4814
|
class Route {
|
|
4770
4815
|
/**
|
|
@@ -5326,6 +5371,17 @@ class QueryRouter {
|
|
|
5326
5371
|
hasRoute(path, key = '') {
|
|
5327
5372
|
return this.routes.find((r) => r.path === path && r.key === key);
|
|
5328
5373
|
}
|
|
5374
|
+
/**
|
|
5375
|
+
* 等待程序运行, 获取到message的数据,就执行
|
|
5376
|
+
*
|
|
5377
|
+
* emitter = process
|
|
5378
|
+
* -- .exit
|
|
5379
|
+
* -- .on
|
|
5380
|
+
* -- .send
|
|
5381
|
+
*/
|
|
5382
|
+
wait(params, opts) {
|
|
5383
|
+
return listenProcess({ app: this, params, ...opts });
|
|
5384
|
+
}
|
|
5329
5385
|
}
|
|
5330
5386
|
/**
|
|
5331
5387
|
* QueryRouterServer
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@kevisual/router",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.29",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/router.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"@kevisual/local-proxy": "^0.0.6",
|
|
25
25
|
"@kevisual/query": "^0.0.29",
|
|
26
26
|
"@rollup/plugin-alias": "^5.1.1",
|
|
27
|
-
"@rollup/plugin-commonjs": "
|
|
27
|
+
"@rollup/plugin-commonjs": "28.0.6",
|
|
28
28
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
29
29
|
"@rollup/plugin-typescript": "^12.1.4",
|
|
30
30
|
"@types/lodash-es": "^4.17.12",
|
package/src/route.ts
CHANGED
|
@@ -3,8 +3,9 @@ import { CustomError } from './result/error.ts';
|
|
|
3
3
|
import { Schema, Rule, createSchema } from './validator/index.ts';
|
|
4
4
|
import { pick } from './utils/pick.ts';
|
|
5
5
|
import { get } from 'lodash-es';
|
|
6
|
+
import { listenProcess } from './utils/listen-process.ts';
|
|
6
7
|
|
|
7
|
-
export type RouterContextT = { code?: number;
|
|
8
|
+
export type RouterContextT = { code?: number;[key: string]: any };
|
|
8
9
|
export type RouteContext<T = { code?: number }, S = any> = {
|
|
9
10
|
// run first
|
|
10
11
|
query?: { [key: string]: any };
|
|
@@ -47,7 +48,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
|
|
|
47
48
|
error?: any;
|
|
48
49
|
/** 请求 route的返回结果,包函ctx */
|
|
49
50
|
call?: (
|
|
50
|
-
message: { path: string; key?: string; payload?: any;
|
|
51
|
+
message: { path: string; key?: string; payload?: any;[key: string]: any } | { id: string; apyload?: any;[key: string]: any },
|
|
51
52
|
ctx?: RouteContext & { [key: string]: any },
|
|
52
53
|
) => Promise<any>;
|
|
53
54
|
/** 请求 route的返回结果,不包函ctx */
|
|
@@ -63,10 +64,10 @@ export type Run<T extends SimpleObject = {}> = (ctx: RouteContext<T>) => Promise
|
|
|
63
64
|
export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
64
65
|
export type RouteMiddleware =
|
|
65
66
|
| {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
path: string;
|
|
68
|
+
key?: string;
|
|
69
|
+
id?: string;
|
|
70
|
+
}
|
|
70
71
|
| string;
|
|
71
72
|
export type RouteOpts = {
|
|
72
73
|
path?: string;
|
|
@@ -303,7 +304,7 @@ export class Route<U = { [key: string]: any }> {
|
|
|
303
304
|
}
|
|
304
305
|
return this;
|
|
305
306
|
}
|
|
306
|
-
addTo(router: QueryRouter | { add: (route: Route) => void;
|
|
307
|
+
addTo(router: QueryRouter | { add: (route: Route) => void;[key: string]: any }) {
|
|
307
308
|
router.add(this);
|
|
308
309
|
}
|
|
309
310
|
setData(data: any) {
|
|
@@ -608,7 +609,7 @@ export class QueryRouter {
|
|
|
608
609
|
* @description 这里的上下文是为了在handle函数中使用
|
|
609
610
|
* @param ctx
|
|
610
611
|
*/
|
|
611
|
-
|
|
612
|
+
setContext(ctx: RouteContext) {
|
|
612
613
|
this.context = ctx;
|
|
613
614
|
}
|
|
614
615
|
getList(): RouteInfo[] {
|
|
@@ -620,7 +621,7 @@ export class QueryRouter {
|
|
|
620
621
|
* 获取handle函数, 这里会去执行parse函数
|
|
621
622
|
*/
|
|
622
623
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
|
|
623
|
-
return async (msg: { path: string; key?: string;
|
|
624
|
+
return async (msg: { path: string; key?: string;[key: string]: any }, handleContext?: RouteContext) => {
|
|
624
625
|
try {
|
|
625
626
|
const context = { ...ctx, ...handleContext };
|
|
626
627
|
const res = await router.parse(msg, context);
|
|
@@ -655,6 +656,17 @@ export class QueryRouter {
|
|
|
655
656
|
hasRoute(path: string, key: string = '') {
|
|
656
657
|
return this.routes.find((r) => r.path === path && r.key === key);
|
|
657
658
|
}
|
|
659
|
+
/**
|
|
660
|
+
* 等待程序运行, 获取到message的数据,就执行
|
|
661
|
+
*
|
|
662
|
+
* emitter = process
|
|
663
|
+
* -- .exit
|
|
664
|
+
* -- .on
|
|
665
|
+
* -- .send
|
|
666
|
+
*/
|
|
667
|
+
wait(params?: { path?: string; key?: string; payload?: any }, opts?: { emitter?: any, timeout?: number }) {
|
|
668
|
+
return listenProcess({ app: this, params, ...opts });
|
|
669
|
+
}
|
|
658
670
|
}
|
|
659
671
|
|
|
660
672
|
type QueryRouterServerOpts = {
|
|
@@ -662,7 +674,7 @@ type QueryRouterServerOpts = {
|
|
|
662
674
|
context?: RouteContext;
|
|
663
675
|
};
|
|
664
676
|
interface HandleFn<T = any> {
|
|
665
|
-
(msg: { path: string;
|
|
677
|
+
(msg: { path: string;[key: string]: any }, ctx?: any): { code: string; data?: any; message?: string;[key: string]: any };
|
|
666
678
|
(res: RouteContext<T>): any;
|
|
667
679
|
}
|
|
668
680
|
/**
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type ListenProcessOptions = {
|
|
2
|
+
app?: any; // 传入的应用实例
|
|
3
|
+
emitter?: any; // 可选的事件发射器
|
|
4
|
+
params?: any; // 可选的参数
|
|
5
|
+
timeout?: number; // 可选的超时时间 (单位: 毫秒)
|
|
6
|
+
};
|
|
7
|
+
export const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 1000 }: ListenProcessOptions) => {
|
|
8
|
+
const process = emitter || globalThis.process;
|
|
9
|
+
let isEnd = false;
|
|
10
|
+
const timer = setTimeout(() => {
|
|
11
|
+
if (isEnd) return;
|
|
12
|
+
isEnd = true;
|
|
13
|
+
process.send?.({ success: false, error: 'Timeout' });
|
|
14
|
+
process.exit?.(1);
|
|
15
|
+
}, timeout);
|
|
16
|
+
|
|
17
|
+
// 监听来自主进程的消息
|
|
18
|
+
const getParams = async (): Promise<any> => {
|
|
19
|
+
return new Promise((resolve) => {
|
|
20
|
+
process.on('message', (msg) => {
|
|
21
|
+
if (isEnd) return;
|
|
22
|
+
isEnd = true;
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
resolve(msg)
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const { path = 'main', ...rest } = await getParams()
|
|
31
|
+
// 执行主要逻辑
|
|
32
|
+
const result = await app.queryRoute({ path, ...rest, ...params })
|
|
33
|
+
// 发送结果回主进程
|
|
34
|
+
const response = {
|
|
35
|
+
success: true,
|
|
36
|
+
data: result,
|
|
37
|
+
timestamp: new Date().toISOString()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
process.send?.(response, (error) => {
|
|
41
|
+
process.exit?.(0)
|
|
42
|
+
})
|
|
43
|
+
} catch (error) {
|
|
44
|
+
process.send?.({
|
|
45
|
+
success: false,
|
|
46
|
+
error: error.message
|
|
47
|
+
})
|
|
48
|
+
process.exit?.(1)
|
|
49
|
+
}
|
|
50
|
+
}
|