From e1401584a59e7d01f2637f24381c49d4609e1c6d Mon Sep 17 00:00:00 2001 From: Shigma <1700011071@pku.edu.cn> Date: Thu, 27 Aug 2020 03:47:07 +0800 Subject: [PATCH] eval-addons: support debug mode --- packages/plugin-eval-addons/src/index.ts | 1 + packages/plugin-eval-addons/src/worker.ts | 14 +++++++------- packages/plugin-eval/src/internal.ts | 3 ++- packages/plugin-eval/src/vm.ts | 4 ++-- packages/plugin-eval/src/worker.ts | 14 ++++++++++---- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/packages/plugin-eval-addons/src/index.ts b/packages/plugin-eval-addons/src/index.ts index f201bc840a..dd887e8819 100644 --- a/packages/plugin-eval-addons/src/index.ts +++ b/packages/plugin-eval-addons/src/index.ts @@ -102,6 +102,7 @@ export function apply(ctx: Context, config: Config) { const cmd = addon .subcommand(rawName, desc, config) + .option('debug', '启用调试模式', { type: 'boolean' }) .action(async ({ session, command, options }, ...args) => { const { $app, $user, $uuid } = session const { name } = command diff --git a/packages/plugin-eval-addons/src/worker.ts b/packages/plugin-eval-addons/src/worker.ts index c8abb1862e..fdcc660a06 100644 --- a/packages/plugin-eval-addons/src/worker.ts +++ b/packages/plugin-eval-addons/src/worker.ts @@ -1,4 +1,4 @@ -import { config, context, internal, WorkerAPI, createContext, response, mapDirectory } from 'koishi-plugin-eval/dist/worker' +import { config, context, internal, WorkerAPI, Context, response, mapDirectory, formatError } from 'koishi-plugin-eval/dist/worker' import { promises, readFileSync } from 'fs' import { resolve, posix, dirname } from 'path' import { User } from 'koishi-core' @@ -34,9 +34,7 @@ interface AddonArgv { options: Record } -interface AddonContext extends AddonArgv { - user: Partial -} +interface AddonContext extends AddonArgv, Context {} type AddonAction = (ctx: AddonContext) => string | void | Promise const commandMap: Record = {} @@ -44,9 +42,11 @@ const commandMap: Record = {} WorkerAPI.prototype.callAddon = async function (sid, user, argv) { const callback = commandMap[argv.name] try { - return await callback({ ...argv, ...createContext(sid, user) }) + return await callback({ ...argv, ...Context(sid, user) }) } catch (error) { - logger.warn(error) + if (!argv.options.debug) return logger.warn(error) + return formatError(error) + .replace('WorkerAPI.worker_1.WorkerAPI.callAddon', 'WorkerAPI.callAddon') } } @@ -64,7 +64,7 @@ export const modules: Record = {} export function synthetize(identifier: string, namespace: {}) { const module = new SyntheticModule(Object.keys(namespace), function () { for (const key in namespace) { - this.setExport(key, namespace[key]) + this.setExport(key, internal.contextify(namespace[key])) } }, { context, identifier }) modules[identifier] = module diff --git a/packages/plugin-eval/src/internal.ts b/packages/plugin-eval/src/internal.ts index 371313f690..308d40c31a 100644 --- a/packages/plugin-eval/src/internal.ts +++ b/packages/plugin-eval/src/internal.ts @@ -687,7 +687,8 @@ connect(host.Buffer.prototype['inspect'], function inspect() { return `<${this.constructor.name} ${str}>` }) -export const value: (value: T) => T = Decontextify.value.bind(Decontextify) +export const contextify: (value: T) => T = Contextify.value.bind(Contextify) +export const decontextify: (value: T) => T = Decontextify.value.bind(Decontextify) export const sandbox = Decontextify.value(GLOBAL) delete global.console diff --git a/packages/plugin-eval/src/vm.ts b/packages/plugin-eval/src/vm.ts index 2527c562c4..e733b3e50f 100644 --- a/packages/plugin-eval/src/vm.ts +++ b/packages/plugin-eval/src/vm.ts @@ -39,9 +39,9 @@ export class VM { const script = new Script(code, options) try { - return this.internal.value(script.runInContext(this.context, { displayErrors: false })) + return this.internal.decontextify(script.runInContext(this.context, { displayErrors: false })) } catch (e) { - throw this.internal.value(e) + throw this.internal.decontextify(e) } } } diff --git a/packages/plugin-eval/src/worker.ts b/packages/plugin-eval/src/worker.ts index 851c729722..026642eefe 100644 --- a/packages/plugin-eval/src/worker.ts +++ b/packages/plugin-eval/src/worker.ts @@ -47,7 +47,7 @@ function formatResult(...param: [string, ...any[]]) { return formatWithOptions(config.inspect, ...param) } -function formatError(error: Error) { +export function formatError(error: Error) { if (!(error instanceof Error)) return `Uncaught: ${error}` if (error.name === 'SyntaxError') { @@ -61,7 +61,7 @@ function formatError(error: Error) { } return error.stack - .replace(/\s*.+Script[\s\S]*/, '') + .replace(/\s*.+(Script|MessagePort)[\s\S]*/, '') .split('\n') .map((line) => { for (const name in pathMapper) { @@ -74,7 +74,13 @@ function formatError(error: Error) { const main = wrap(parentPort) -export const createContext = (sid: string, user: Partial) => ({ +export interface Context { + user: Partial + send(...param: any[]): Promise + exec(message: string): Promise +} + +export const Context = (sid: string, user: Partial): Context => ({ user, async send(...param: [string, ...any[]]) { return await main.send(sid, formatResult(...param)) @@ -100,7 +106,7 @@ export class WorkerAPI { const { sid, user, source, silent } = options const key = 'koishi-eval-session:' + sid - internal.setGlobal(Symbol.for(key), createContext(sid, user), false, true) + internal.setGlobal(Symbol.for(key), Context(sid, user), false, true) let result: any try {