diff --git a/packages/hydrojudge/src/cases.ts b/packages/hydrojudge/src/cases.ts index 420636784..b8ec6c1d2 100644 --- a/packages/hydrojudge/src/cases.ts +++ b/packages/hydrojudge/src/cases.ts @@ -49,7 +49,7 @@ export async function processTestdata(folder: string) { } const ini = files.filter((i) => i.toLowerCase() === 'config.ini')[0]; if (!ini) return; - const t = fs.readFileSync(path.resolve(folder, ini), 'utf8'); + const t = await fs.readFile(path.resolve(folder, ini), 'utf8'); await fs.writeFile(path.resolve(folder, 'config.ini'), t.toLowerCase()); for (const i of files) { if (i.toLowerCase() === 'input') await fs.rename(`${folder}/${i}`, `${folder}/input`); @@ -84,11 +84,11 @@ export default async function readCases(folder: string, cfg: ProblemConfigFile = }; try { if (fs.existsSync(yamlConfig)) { - Object.assign(config, yaml.load(fs.readFileSync(yamlConfig).toString())); + Object.assign(config, yaml.load(await fs.readFile(yamlConfig, 'utf-8'))); } else if (fs.existsSync(ymlConfig)) { - Object.assign(config, yaml.load(fs.readFileSync(ymlConfig).toString())); + Object.assign(config, yaml.load(await fs.readFile(ymlConfig, 'utf-8'))); } else if (fs.existsSync(iniConfig)) { - Object.assign(config, convertIniConfig(fs.readFileSync(iniConfig).toString())); + Object.assign(config, convertIniConfig(await fs.readFile(iniConfig, 'utf-8'))); } } catch (e) { throw changeErrorType(e, FormatError); diff --git a/packages/hydrojudge/src/hosts/builtin.ts b/packages/hydrojudge/src/hosts/builtin.ts index a27ab0344..87e32b677 100644 --- a/packages/hydrojudge/src/hosts/builtin.ts +++ b/packages/hydrojudge/src/hosts/builtin.ts @@ -53,7 +53,7 @@ const session = { if (!files?.length) throw new FormatError('Problem data not found.'); let etags: Record = {}; try { - etags = JSON.parse(fs.readFileSync(path.join(filePath, 'etags')).toString()); + etags = JSON.parse(await fs.readFile(path.join(filePath, 'etags'), 'utf-8')); } catch (e) { /* ignore */ } const version = {}; const filenames = new Set(); @@ -67,8 +67,10 @@ const session = { for (const name in etags) { if (!filenames.has(name) && fs.existsSync(path.join(filePath, name))) await fs.remove(path.join(filePath, name)); } - fs.writeFileSync(path.join(filePath, 'etags'), JSON.stringify(version)); - fs.writeFileSync(path.join(filePath, 'lastUsage'), Date.now().toString()); + await Promise.all([ + fs.writeFile(path.join(filePath, 'etags'), JSON.stringify(version)), + fs.writeFile(path.join(filePath, 'lastUsage'), Date.now().toString()), + ]); await processTestdata(filePath); return filePath; }, diff --git a/packages/hydrojudge/src/hosts/hydro.ts b/packages/hydrojudge/src/hosts/hydro.ts index b88b7a15c..67b1b5f01 100644 --- a/packages/hydrojudge/src/hosts/hydro.ts +++ b/packages/hydrojudge/src/hosts/hydro.ts @@ -64,7 +64,7 @@ export default class Hydro { if (!files?.length) throw new FormatError('Problem data not found.'); let etags: Record = {}; try { - etags = JSON.parse(fs.readFileSync(path.join(filePath, 'etags')).toString()); + etags = JSON.parse(await fs.readFile(path.join(filePath, 'etags'), 'utf-8')); } catch (e) { /* ignore */ } const version = {}; const filenames = []; @@ -102,10 +102,10 @@ export default class Hydro { } queue.start(); await Promise.all(tasks); - fs.writeFileSync(path.join(filePath, 'etags'), JSON.stringify(version)); + await fs.writeFile(path.join(filePath, 'etags'), JSON.stringify(version)); await processTestdata(filePath); } - fs.writeFileSync(path.join(filePath, 'lastUsage'), new Date().getTime().toString()); + await fs.writeFile(path.join(filePath, 'lastUsage'), new Date().getTime().toString()); return filePath; } diff --git a/packages/hydrojudge/src/judge/run.ts b/packages/hydrojudge/src/judge/run.ts index 495ae963c..029665ca9 100644 --- a/packages/hydrojudge/src/judge/run.ts +++ b/packages/hydrojudge/src/judge/run.ts @@ -68,8 +68,7 @@ export const judge = async (ctx: Context) => { if (code < 32) message.push(`ExitCode: ${code} (${signals[code]})`); else message.push(`ExitCode: ${code}`); } - message.push(res.stdout); - message.push(res.stderr); + message.push(res.stdout, res.stderr); ctx.next({ status, case: { diff --git a/packages/hydrojudge/src/sandbox.ts b/packages/hydrojudge/src/sandbox.ts index f1fe501b2..b2679e54d 100644 --- a/packages/hydrojudge/src/sandbox.ts +++ b/packages/hydrojudge/src/sandbox.ts @@ -1,5 +1,4 @@ import cac from 'cac'; -import fs from 'fs-extra'; import PQueue from 'p-queue'; import { ParseEntry } from 'shell-quote'; import { STATUS } from '@hydrooj/utils/lib/status'; @@ -31,8 +30,6 @@ const statusMap: Map = new Map([ interface Parameter { time?: number; stdin?: CopyInFile; - stdout?: string; - stderr?: string; execute?: string; memory?: number; processLimit?: number; @@ -78,7 +75,7 @@ function proc(params: Parameter): Cmd { const size = parseMemoryMB(getConfig('stdio_size')); const rate = getConfig('rate'); const copyOutCached = [...(params.copyOutCached || [])]; - if (params.cacheStdoutAndStderr) copyOutCached.push(params.filename ? `${params.filename}.out` : 'stdout', 'stderr'); + if (params.cacheStdoutAndStderr) copyOutCached.push(params.filename ? `${params.filename}.out?` : 'stdout', 'stderr'); const copyIn = { ...(params.copyIn || {}) }; const stdin = params.stdin || { content: '' }; if (params.filename) copyIn[`${params.filename}.in`] = stdin; @@ -121,10 +118,8 @@ async function adaptResult(result: SandboxResult, params: Parameter): Promise { - return superagent.post(`${this.baseURL}/run`).send(req).then((res) => res.body); - } - - public getFile(fileId: string): Promise { - return superagent.get(`${this.baseURL}/file/${fileId}`).then((res) => res.body); - } - - public deleteFile(fileId: string): Promise { - return superagent.delete(`${this.baseURL}/file/${fileId}`).then((res) => res.body); - } - - public listFiles(): Promise> { - return superagent.get(`${this.baseURL}/file`).then((res) => res.body); - } - - public version(): Promise { - return superagent.get(`${this.baseURL}/version`).then((res) => res.body); - } -} - -const client = new Proxy({} as SandboxClient, { +const client = new Proxy({ + run(req: SandboxRequest): Promise { + return superagent.post(`${url}/run`).send(req).then((res) => res.body); + }, + getFile(fileId: string): Promise { + return superagent.get(`${url}/file/${fileId}`).then((res) => res.body); + }, + deleteFile(fileId: string): Promise { + return superagent.delete(`${url}/file/${fileId}`).then((res) => res.body); + }, + listFiles(): Promise> { + return superagent.get(`${url}/file`).then((res) => res.body); + }, + version(): Promise { + return superagent.get(`${url}/version`).then((res) => res.body); + }, +}, { get(self, key) { - let url = getConfig('sandbox_host'); + url = getConfig('sandbox_host'); if (url.endsWith('/')) url = url.substring(0, url.length - 1); - const c = new SandboxClient(url); - return c[key].bind(c); + return self[key]; }, });