Merge pull request #58 from Vrganj/fix-stdin-lines

Fix stdin lines
This commit is contained in:
Brian Seymour 2021-01-25 12:28:42 -06:00 committed by GitHub
commit f9b5fe652f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 41 deletions

View file

@ -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);