Skip to content

Commit

Permalink
eval: fix eval without database
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 26, 2020
1 parent e61eeb6 commit 4988436
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
10 changes: 2 additions & 8 deletions packages/plugin-eval/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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),
},
Expand Down Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-eval/src/trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export namespace UserTrap {
}

export function get($user: {}, fields: string[]) {
if (!$user) return
const result: Partial<User> = {}
for (const field of fields) {
const trap = traps[field]
Expand Down
9 changes: 4 additions & 5 deletions packages/plugin-eval/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down

0 comments on commit 4988436

Please sign in to comment.