diff --git a/packages/plugin-eval/src/index.ts b/packages/plugin-eval/src/index.ts index 871c9b5787..d3b3ef884a 100644 --- a/packages/plugin-eval/src/index.ts +++ b/packages/plugin-eval/src/index.ts @@ -90,11 +90,7 @@ export class MainAPI { } } -process.env.KOISHI_WORKER_ENTRY = resolve(__dirname, 'worker.js') - -process.env.KOISHI_WORKER_LOADER = `\ -const { workerData } = require('worker_threads'); -require(workerData.entry);` +export const workerScript = `require(${JSON.stringify(resolve(__dirname, 'worker.js'))});` export function apply(ctx: Context, config: Config = {}) { const { prefix } = config = { ...defaultConfig, ...config } @@ -109,10 +105,9 @@ export function apply(ctx: Context, config: Config = {}) { async function createWorker() { await app.parallel('worker/start') - const worker = app.evalWorker = new Worker(process.env.KOISHI_WORKER_LOADER, { + const worker = app.evalWorker = new Worker(workerScript, { eval: true, workerData: { - entry: process.env.KOISHI_WORKER_ENTRY, logLevels: Logger.levels, ...pick(config, config.dataKeys), }, @@ -160,7 +155,6 @@ export function apply(ctx: Context, config: Config = {}) { const cmd = ctx.command('evaluate [expr...]', '执行 JavaScript 脚本', { noEval: true }) .alias('eval') .userFields(['authority']) - .userFields(config.userFields) .option('slient', '-s 不输出最后的结果') .option('restart', '-r 重启子线程', { authority: 3 }) .before((session) => { diff --git a/packages/plugin-eval/src/trap.ts b/packages/plugin-eval/src/trap.ts index 54fabc0d38..7d8560a41f 100644 --- a/packages/plugin-eval/src/trap.ts +++ b/packages/plugin-eval/src/trap.ts @@ -20,6 +20,7 @@ export namespace UserTrap { } export function get($user: {}, fields: string[]) { + if (!$user) return const result: Partial = {} for (const field of fields) { const trap = traps[field] diff --git a/packages/plugin-eval/tests/index.spec.ts b/packages/plugin-eval/tests/index.spec.ts index 6cb4087e89..71ba193881 100644 --- a/packages/plugin-eval/tests/index.spec.ts +++ b/packages/plugin-eval/tests/index.spec.ts @@ -1,15 +1,14 @@ +import { defineProperty } from 'koishi-utils' import { App } from 'koishi-test-utils' import { inspect } from 'util' import { resolve } from 'path' import * as _eval from 'koishi-plugin-eval' -process.env.KOISHI_WORKER_ENTRY = resolve(__dirname, '../src/worker.ts') - -process.env.KOISHI_WORKER_LOADER = [ +defineProperty(_eval, 'workerScript', [ 'require("ts-node/register/transpile-only");', 'require("tsconfig-paths/register");', - process.env.KOISHI_WORKER_LOADER, -].join('\n') + `require(${JSON.stringify(resolve(__dirname, '../src/worker.ts'))})`, +].join('\n')) const app = new App()