Skip to content

Commit

Permalink
eval: use evalConfig.dataKeys to narrow down worker payload size
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 26, 2020
1 parent fbca5dc commit 3bfc694
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
18 changes: 12 additions & 6 deletions packages/plugin-eval-addons/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Context, CommandConfig, OptionConfig, User } from 'koishi-core'
import { resolve } from 'path'
import {} from 'koishi-plugin-eval'
import { assertProperty, Logger, noop } from 'koishi-utils'
import { resolve } from 'path'
import { safeLoad } from 'js-yaml'
import { promises as fs } from 'fs'
import {} from 'koishi-plugin-eval'
import Git, { CheckRepoActions } from 'simple-git'

const logger = new Logger('addon')

type AddonConfig = Config

export interface Config {
gitRemote?: string
moduleRoot?: string
exclude?: RegExp
}

declare module 'koishi-plugin-eval' {
interface MainConfig extends AddonConfig {}
}

interface OptionManifest extends OptionConfig {
name: string
desc: string
Expand All @@ -39,10 +44,11 @@ interface Manifest {
export function apply(ctx: Context, config: Config) {
const { evalConfig } = ctx.app
Object.assign(evalConfig, config)
const moduleRoot = assertProperty(evalConfig, 'moduleRoot')
const root = resolve(process.cwd(), assertProperty(evalConfig, 'moduleRoot'))
evalConfig.moduleRoot = root
evalConfig.dataKeys.push('addonNames', 'moduleRoot')
evalConfig.setupFiles['koishi/addons.ts'] = resolve(__dirname, 'worker.js')

const root = resolve(process.cwd(), moduleRoot)
const git = Git(root)

const addon = ctx.command('addon', '扩展功能')
Expand All @@ -59,7 +65,7 @@ export function apply(ctx: Context, config: Config) {

ctx.on('before-connect', async () => {
const isRepo = await git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT)
if (!isRepo) throw new Error(`moduleRoot "${moduleRoot}" is not git repository`)
if (!isRepo) throw new Error(`moduleRoot "${root}" is not git repository`)
})

let manifests: Record<string, Promise<Manifest>>
Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-eval-addons/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { promises, readFileSync } from 'fs'
import { resolve, posix, dirname } from 'path'
import { User } from 'koishi-core'
import { Logger, Time, CQCode, Random } from 'koishi-utils'
import { Config } from '.'
import json5 from 'json5'
import ts from 'typescript'

Expand All @@ -12,7 +11,9 @@ const logger = new Logger('addon')
const { SourceTextModule, SyntheticModule } = require('vm')

declare module 'koishi-plugin-eval/dist/worker' {
interface WorkerConfig extends Config {}
interface WorkerConfig {
moduleRoot?: string
}

interface WorkerData {
addonNames: string[]
Expand Down Expand Up @@ -58,7 +59,6 @@ interface Module {
evaluate(): Promise<void>
}

const root = resolve(process.cwd(), config.moduleRoot)
export const modules: Record<string, Module> = {}

export function synthetize(identifier: string, namespace: {}) {
Expand Down Expand Up @@ -111,14 +111,14 @@ async function linker(specifier: string, { identifier }: Module) {
throw new Error(`Unable to resolve dependency "${specifier}" in "${identifier}"`)
}

const json = json5.parse(readFileSync(resolve(root, 'tsconfig.json'), 'utf8'))
const { options: compilerOptions } = ts.parseJsonConfigFileContent(json, ts.sys, root)
const json = json5.parse(readFileSync(resolve(config.moduleRoot, 'tsconfig.json'), 'utf8'))
const { options: compilerOptions } = ts.parseJsonConfigFileContent(json, ts.sys, config.moduleRoot)

async function loadSource(path: string) {
for (const postfix of suffixes) {
try {
const target = path + postfix
return [await promises.readFile(resolve(root, target), 'utf8'), target]
return [await promises.readFile(resolve(config.moduleRoot, target), 'utf8'), target]
} catch {}
}
throw new Error(`cannot load source file "${path}"`)
Expand Down
18 changes: 10 additions & 8 deletions packages/plugin-eval/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App, Context, Session } from 'koishi-core'
import { CQCode, Logger, defineProperty, omit, Random } from 'koishi-utils'
import { CQCode, Logger, defineProperty, Random, pick } from 'koishi-utils'
import { Worker, ResourceLimits } from 'worker_threads'
import { WorkerAPI, WorkerConfig, WorkerData, Response } from './worker'
import { wrap, expose, Remote } from './transfer'
Expand All @@ -22,9 +22,9 @@ declare module 'koishi-core/dist/command' {

declare module 'koishi-core/dist/context' {
interface EventMap {
'worker/start' (): void | Promise<void>
'worker/ready' (response: Response): void
'worker/exit' (): void
'worker/start'(): void | Promise<void>
'worker/ready'(response: Response): void
'worker/exit'(): void
}
}

Expand All @@ -36,22 +36,24 @@ declare module 'koishi-core/dist/session' {
}
}

interface MainConfig {
export interface MainConfig {
prefix?: string
timeout?: number
maxLogs?: number
resourceLimits?: ResourceLimits
dataKeys?: (keyof WorkerData)[]
}

interface EvalConfig extends MainConfig, WorkerData {}
export interface EvalConfig extends MainConfig, WorkerData {}

export interface Config extends MainConfig, WorkerConfig {}

const defaultConfig: Config = {
const defaultConfig: EvalConfig = {
prefix: '>',
timeout: 1000,
setupFiles: {},
maxLogs: Infinity,
dataKeys: ['inspect', 'setupFiles'],
}

const logger = new Logger('eval')
Expand Down Expand Up @@ -107,7 +109,7 @@ export function apply(ctx: Context, config: Config = {}) {
workerData: {
entry: process.env.KOISHI_WORKER_ENTRY,
logLevels: Logger.levels,
...omit(config, ['maxLogs', 'resourceLimits', 'timeout']),
...pick(config, config.dataKeys),
},
resourceLimits: config.resourceLimits,
})
Expand Down

0 comments on commit 3bfc694

Please sign in to comment.