piston/shared/execute.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

const { writeFileSync } = require('fs');
2021-01-16 13:14:17 +01:00
const { spawn } = require('child_process');
function execute(language, source, stdin = '', args = []) {
return new Promise(resolve => {
2021-01-16 13:14:17 +01:00
const stamp = new Date().getTime();
const sourceFile = `/tmp/${stamp}.code`;
writeFileSync(sourceFile, source);
2021-01-16 13:14:17 +01:00
const process = spawn(__dirname + '/../lxc/execute', [
language.name,
sourceFile,
stdin,
args.join('\n'),
2021-01-16 13:14:17 +01:00
]);
let stdout = '';
let stderr = '';
let output = '';
process.stderr.on('data', chunk => {
stderr += chunk;
output += chunk;
});
process.stdout.on('data', chunk => {
stdout += chunk;
output += chunk;
});
2021-01-16 14:19:29 +01:00
process.on('exit', code => {
2021-01-16 13:14:17 +01:00
stderr = stderr.trim().substring(0, 65535);
stdout = stdout.trim().substring(0, 65535);
output = output.trim().substring(0, 65535);
2021-01-16 14:19:29 +01:00
resolve({
stdout,
stderr,
output,
ran: code === 0,
});
2021-01-16 13:14:17 +01:00
});
});
}
module.exports = {
execute,
};