mirror of
https://github.com/engineer-man/piston.git
synced 2025-05-09 05:16:28 +02:00
commit
f9b5fe652f
2 changed files with 27 additions and 41 deletions
|
@ -1,20 +1,21 @@
|
|||
const { writeFileSync, unlinkSync } = require('fs');
|
||||
const { writeFileSync, unlinkSync, mkdirSync } = require('fs');
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
const OUTPUT_LIMIT = 65535;
|
||||
const LXC_ROOT = '/var/lib/lxc/piston/rootfs';
|
||||
|
||||
function execute(language, source, stdin = '', args = []) {
|
||||
return new Promise(resolve => {
|
||||
const stamp = new Date().getTime();
|
||||
const sourceFile = `/tmp/${stamp}.code`;
|
||||
const id = new Date().getTime() + '_' + Math.floor(Math.random() * 10000000);
|
||||
|
||||
writeFileSync(sourceFile, source);
|
||||
mkdirSync(`${LXC_ROOT}/tmp/${id}`);
|
||||
writeFileSync(`${LXC_ROOT}/tmp/${id}/code.code`, source);
|
||||
writeFileSync(`${LXC_ROOT}/tmp/${id}/stdin.stdin`, stdin);
|
||||
writeFileSync(`${LXC_ROOT}/tmp/${id}/args.args`, args.join('\n'));
|
||||
|
||||
const process = spawn(__dirname + '/../lxc/execute', [
|
||||
language.name,
|
||||
sourceFile,
|
||||
stdin,
|
||||
args.join('\n'),
|
||||
id,
|
||||
]);
|
||||
|
||||
let stdout = '';
|
||||
|
@ -36,8 +37,6 @@ function execute(language, source, stdin = '', args = []) {
|
|||
});
|
||||
|
||||
process.on('exit', code => {
|
||||
unlinkSync(sourceFile);
|
||||
|
||||
stderr = stderr.trim().substring(0, OUTPUT_LIMIT);
|
||||
stdout = stdout.trim().substring(0, OUTPUT_LIMIT);
|
||||
output = output.trim().substring(0, OUTPUT_LIMIT);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue