Skip to content

Commit

Permalink
eval-addons: support debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 26, 2020
1 parent 4988436 commit e140158
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/plugin-eval-addons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions packages/plugin-eval-addons/src/worker.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -34,19 +34,19 @@ interface AddonArgv {
options: Record<string, any>
}

interface AddonContext extends AddonArgv {
user: Partial<User>
}
interface AddonContext extends AddonArgv, Context {}

type AddonAction = (ctx: AddonContext) => string | void | Promise<string | void>
const commandMap: Record<string, AddonAction> = {}

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')
}
}

Expand All @@ -64,7 +64,7 @@ export const modules: Record<string, Module> = {}
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
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-eval/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,8 @@ connect(host.Buffer.prototype['inspect'], function inspect() {
return `<${this.constructor.name} ${str}>`
})

export const value: <T>(value: T) => T = Decontextify.value.bind(Decontextify)
export const contextify: <T>(value: T) => T = Contextify.value.bind(Contextify)
export const decontextify: <T>(value: T) => T = Decontextify.value.bind(Decontextify)
export const sandbox = Decontextify.value(GLOBAL)

delete global.console
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-eval/src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions packages/plugin-eval/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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) {
Expand All @@ -74,7 +74,13 @@ function formatError(error: Error) {

const main = wrap<MainAPI>(parentPort)

export const createContext = (sid: string, user: Partial<User>) => ({
export interface Context {
user: Partial<User>
send(...param: any[]): Promise<void>
exec(message: string): Promise<void>
}

export const Context = (sid: string, user: Partial<User>): Context => ({
user,
async send(...param: [string, ...any[]]) {
return await main.send(sid, formatResult(...param))
Expand All @@ -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 {
Expand Down

0 comments on commit e140158

Please sign in to comment.