update: add listen process
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.28",
|
||||
"version": "0.0.29",
|
||||
"description": "",
|
||||
"type": "module",
|
||||
"main": "./dist/router.js",
|
||||
@@ -24,7 +24,7 @@
|
||||
"@kevisual/local-proxy": "^0.0.6",
|
||||
"@kevisual/query": "^0.0.29",
|
||||
"@rollup/plugin-alias": "^5.1.1",
|
||||
"@rollup/plugin-commonjs": "^28.0.6",
|
||||
"@rollup/plugin-commonjs": "28.0.6",
|
||||
"@rollup/plugin-node-resolve": "^16.0.3",
|
||||
"@rollup/plugin-typescript": "^12.1.4",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
|
||||
12
src/route.ts
12
src/route.ts
@@ -3,6 +3,7 @@ import { CustomError } from './result/error.ts';
|
||||
import { Schema, Rule, createSchema } from './validator/index.ts';
|
||||
import { pick } from './utils/pick.ts';
|
||||
import { get } from 'lodash-es';
|
||||
import { listenProcess } from './utils/listen-process.ts';
|
||||
|
||||
export type RouterContextT = { code?: number;[key: string]: any };
|
||||
export type RouteContext<T = { code?: number }, S = any> = {
|
||||
@@ -655,6 +656,17 @@ export class QueryRouter {
|
||||
hasRoute(path: string, key: string = '') {
|
||||
return this.routes.find((r) => r.path === path && r.key === key);
|
||||
}
|
||||
/**
|
||||
* 等待程序运行, 获取到message的数据,就执行
|
||||
*
|
||||
* emitter = process
|
||||
* -- .exit
|
||||
* -- .on
|
||||
* -- .send
|
||||
*/
|
||||
wait(params?: { path?: string; key?: string; payload?: any }, opts?: { emitter?: any, timeout?: number }) {
|
||||
return listenProcess({ app: this, params, ...opts });
|
||||
}
|
||||
}
|
||||
|
||||
type QueryRouterServerOpts = {
|
||||
|
||||
50
src/utils/listen-process.ts
Normal file
50
src/utils/listen-process.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
export type ListenProcessOptions = {
|
||||
app?: any; // 传入的应用实例
|
||||
emitter?: any; // 可选的事件发射器
|
||||
params?: any; // 可选的参数
|
||||
timeout?: number; // 可选的超时时间 (单位: 毫秒)
|
||||
};
|
||||
export const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 1000 }: ListenProcessOptions) => {
|
||||
const process = emitter || globalThis.process;
|
||||
let isEnd = false;
|
||||
const timer = setTimeout(() => {
|
||||
if (isEnd) return;
|
||||
isEnd = true;
|
||||
process.send?.({ success: false, error: 'Timeout' });
|
||||
process.exit?.(1);
|
||||
}, timeout);
|
||||
|
||||
// 监听来自主进程的消息
|
||||
const getParams = async (): Promise<any> => {
|
||||
return new Promise((resolve) => {
|
||||
process.on('message', (msg) => {
|
||||
if (isEnd) return;
|
||||
isEnd = true;
|
||||
clearTimeout(timer);
|
||||
resolve(msg)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
const { path = 'main', ...rest } = await getParams()
|
||||
// 执行主要逻辑
|
||||
const result = await app.queryRoute({ path, ...rest, ...params })
|
||||
// 发送结果回主进程
|
||||
const response = {
|
||||
success: true,
|
||||
data: result,
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
|
||||
process.send?.(response, (error) => {
|
||||
process.exit?.(0)
|
||||
})
|
||||
} catch (error) {
|
||||
process.send?.({
|
||||
success: false,
|
||||
error: error.message
|
||||
})
|
||||
process.exit?.(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user