fix mini modules
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/package",
|
"$schema": "https://json.schemastore.org/package",
|
||||||
"name": "@kevisual/router",
|
"name": "@kevisual/router",
|
||||||
"version": "0.0.60",
|
"version": "0.0.61",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/router.js",
|
"main": "./dist/router.js",
|
||||||
|
|||||||
@@ -13,4 +13,6 @@ export { createSkill, tool } from './route.ts';
|
|||||||
export { CustomError } from './result/error.ts';
|
export { CustomError } from './result/error.ts';
|
||||||
|
|
||||||
export * from './router-define.ts';
|
export * from './router-define.ts';
|
||||||
|
|
||||||
|
export { MockProcess } from './utils/listen-process.ts'
|
||||||
// --- 以上同步更新至 browser.ts ---
|
// --- 以上同步更新至 browser.ts ---
|
||||||
@@ -13,6 +13,8 @@ export { createSkill, tool } from './route.ts';
|
|||||||
export { CustomError } from './result/error.ts';
|
export { CustomError } from './result/error.ts';
|
||||||
|
|
||||||
export * from './router-define.ts';
|
export * from './router-define.ts';
|
||||||
|
|
||||||
|
export { MockProcess } from './utils/listen-process.ts'
|
||||||
// --- 以上同步更新至 browser.ts ---
|
// --- 以上同步更新至 browser.ts ---
|
||||||
|
|
||||||
export { ServerNode, handleServer } from './server/index.ts';
|
export { ServerNode, handleServer } from './server/index.ts';
|
||||||
|
|||||||
@@ -632,7 +632,7 @@ export class QueryRouter {
|
|||||||
if (getList) {
|
if (getList) {
|
||||||
this.createRouteList(opts?.force ?? false, opts?.filter);
|
this.createRouteList(opts?.force ?? false, opts?.filter);
|
||||||
}
|
}
|
||||||
return listenProcess({ app: this, params, ...opts });
|
return listenProcess({ app: this as any, params, ...opts });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
14
src/test/mini.ts
Normal file
14
src/test/mini.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { Mini } from "../route.ts";
|
||||||
|
|
||||||
|
const app = new Mini();
|
||||||
|
|
||||||
|
app.route({
|
||||||
|
path: 'main'
|
||||||
|
}).define(async (ctx) => {
|
||||||
|
ctx.body = {
|
||||||
|
a: '123'
|
||||||
|
}
|
||||||
|
}).addTo(app)
|
||||||
|
|
||||||
|
|
||||||
|
app.wait()
|
||||||
56
src/test/run-mini.ts
Normal file
56
src/test/run-mini.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import { fork } from 'child_process'
|
||||||
|
|
||||||
|
export type RunCodeParams = {
|
||||||
|
path?: string;
|
||||||
|
key?: string;
|
||||||
|
payload?: string;
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
type RunCode = {
|
||||||
|
// 调用进程的功能
|
||||||
|
success?: boolean
|
||||||
|
data?: {
|
||||||
|
// 调用router的结果
|
||||||
|
code?: number
|
||||||
|
data?: any
|
||||||
|
message?: string
|
||||||
|
[key: string]: any
|
||||||
|
};
|
||||||
|
error?: any
|
||||||
|
timestamp?: string
|
||||||
|
[key: string]: any
|
||||||
|
}
|
||||||
|
export const runCode = async (tsPath: string, params: RunCodeParams = {}): Promise<RunCode> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// 使用 Bun 的 fork 模式启动子进程
|
||||||
|
const child = fork(tsPath)
|
||||||
|
|
||||||
|
// 监听来自子进程的消息
|
||||||
|
child.on('message', (msg: RunCode) => {
|
||||||
|
resolve(msg)
|
||||||
|
})
|
||||||
|
|
||||||
|
// child.on('exit', (code, signal) => {
|
||||||
|
// console.log('子进程已退出,退出码:', code, '信号:', signal)
|
||||||
|
// })
|
||||||
|
|
||||||
|
// child.on('close', (code, signal) => {
|
||||||
|
// console.log('子进程已关闭,退出码:', code, '信号:', signal)
|
||||||
|
// })
|
||||||
|
|
||||||
|
child.on('error', (error) => {
|
||||||
|
resolve({
|
||||||
|
success: false, error: error?.message
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// 向子进程发送消息
|
||||||
|
child.send(params)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
import path from 'node:path'
|
||||||
|
const res =await runCode(path.join(process.cwd(), './src/test/mini.ts'), {
|
||||||
|
// path: 'main'
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('res', res)
|
||||||
@@ -1,23 +1,63 @@
|
|||||||
|
import { EventEmitter } from "eventemitter3";
|
||||||
|
import { QueryRouterServer } from "../route.ts"
|
||||||
|
export class MockProcess {
|
||||||
|
emitter?: EventEmitter
|
||||||
|
process?: NodeJS.Process;
|
||||||
|
constructor(opts?: { emitter?: EventEmitter, isNode?: boolean }) {
|
||||||
|
this.emitter = opts?.emitter || new EventEmitter();
|
||||||
|
const isNode = opts?.isNode ?? true;
|
||||||
|
if (isNode) {
|
||||||
|
this.process = globalThis?.process;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
send(data?: any, callback?: (err?: Error) => void) {
|
||||||
|
if (this.process) {
|
||||||
|
this.process?.send?.(data, (err?: Error) => {
|
||||||
|
callback(err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
this.emitter.emit('send', data)
|
||||||
|
}
|
||||||
|
exit(flag: number = 0) {
|
||||||
|
if (this.process) {
|
||||||
|
this.process?.exit?.(flag)
|
||||||
|
}
|
||||||
|
this.emitter.emit('exit', flag)
|
||||||
|
}
|
||||||
|
on(fn: (msg?: any) => any) {
|
||||||
|
if (this.process) {
|
||||||
|
this.process.on('message', fn)
|
||||||
|
}
|
||||||
|
this.emitter.on('message', fn)
|
||||||
|
}
|
||||||
|
desctroy() {
|
||||||
|
if (this.emitter) {
|
||||||
|
this.emitter = undefined;
|
||||||
|
}
|
||||||
|
this.process = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
export type ListenProcessOptions = {
|
export type ListenProcessOptions = {
|
||||||
app?: any; // 传入的应用实例
|
app?: QueryRouterServer; // 传入的应用实例
|
||||||
emitter?: any; // 可选的事件发射器
|
mockProcess?: MockProcess; // 可选的事件发射器
|
||||||
params?: any; // 可选的参数
|
params?: any; // 可选的参数
|
||||||
timeout?: number; // 可选的超时时间 (单位: 毫秒) 默认 10 分钟
|
timeout?: number; // 可选的超时时间 (单位: 毫秒) 默认 10 分钟
|
||||||
};
|
};
|
||||||
export const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 1000 }: ListenProcessOptions) => {
|
export const listenProcess = async ({ app, mockProcess, params, timeout = 10 * 60 * 60 * 1000 }: ListenProcessOptions) => {
|
||||||
const process = emitter || globalThis.process;
|
const process = mockProcess || new MockProcess();
|
||||||
let isEnd = false;
|
let isEnd = false;
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
if (isEnd) return;
|
if (isEnd) return;
|
||||||
isEnd = true;
|
isEnd = true;
|
||||||
process.send?.({ success: false, error: 'Timeout' });
|
process.send?.({ success: false, error: 'Timeout' }, () => {
|
||||||
process.exit?.(1);
|
process.exit?.(1);
|
||||||
|
});
|
||||||
}, timeout);
|
}, timeout);
|
||||||
|
|
||||||
// 监听来自主进程的消息
|
// 监听来自主进程的消息
|
||||||
const getParams = async (): Promise<any> => {
|
const getParams = async (): Promise<any> => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
process.on('message', (msg) => {
|
process.on((msg) => {
|
||||||
if (isEnd) return;
|
if (isEnd) return;
|
||||||
isEnd = true;
|
isEnd = true;
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
@@ -27,10 +67,16 @@ export const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 *
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { path = 'main', payload = {}, ...rest
|
/**
|
||||||
|
* 如果不提供path,默认是main
|
||||||
|
*/
|
||||||
|
const {
|
||||||
|
path = 'main',
|
||||||
|
payload = {},
|
||||||
|
...rest
|
||||||
} = await getParams()
|
} = await getParams()
|
||||||
// 执行主要逻辑
|
// 执行主要逻辑
|
||||||
const result = await app.queryRoute({ path, ...params, ...rest, payload: { ...params.payload, ...payload } })
|
const result = await app.run({ path, ...params, ...rest, payload: { ...params?.payload, ...payload } })
|
||||||
// 发送结果回主进程
|
// 发送结果回主进程
|
||||||
const response = {
|
const response = {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -38,14 +84,15 @@ export const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 *
|
|||||||
timestamp: new Date().toISOString()
|
timestamp: new Date().toISOString()
|
||||||
}
|
}
|
||||||
|
|
||||||
process.send?.(response, (error) => {
|
process.send?.(response, () => {
|
||||||
process.exit?.(0)
|
process.exit?.(0)
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
process.send?.({
|
process.send?.({
|
||||||
success: false,
|
success: false,
|
||||||
error: error.message
|
error: error.message
|
||||||
|
}, () => {
|
||||||
|
process.exit?.(1)
|
||||||
})
|
})
|
||||||
process.exit?.(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user