piston/cli/commands/execute.js

267 lines
6.9 KiB
JavaScript
Raw Permalink Normal View History

2021-02-22 11:38:11 +01:00
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
2021-07-16 14:23:45 +02:00
const WebSocket = require('ws');
2021-10-08 15:16:57 +02:00
const SIGNALS = [
'SIGABRT',
'SIGALRM',
'SIGBUS',
'SIGCHLD',
'SIGCLD',
'SIGCONT',
'SIGEMT',
'SIGFPE',
'SIGHUP',
'SIGILL',
'SIGINFO',
'SIGINT',
'SIGIO',
'SIGIOT',
'SIGLOST',
'SIGPIPE',
'SIGPOLL',
'SIGPROF',
'SIGPWR',
'SIGQUIT',
'SIGSEGV',
'SIGSTKFLT',
'SIGTSTP',
'SIGSYS',
'SIGTERM',
'SIGTRAP',
'SIGTTIN',
'SIGTTOU',
'SIGUNUSED',
'SIGURG',
'SIGUSR1',
'SIGUSR2',
'SIGVTALRM',
'SIGXCPU',
'SIGXFSZ',
'SIGWINCH',
];
2021-02-22 11:38:11 +01:00
2021-04-23 05:27:50 +02:00
exports.command = ['execute <language> <file> [args..]'];
exports.aliases = ['run'];
exports.describe = 'Executes file with the specified runner';
2021-02-22 11:38:11 +01:00
exports.builder = {
2021-05-08 02:57:37 +02:00
language_version: {
2021-04-23 01:59:54 +02:00
string: true,
desc: 'Set the version of the language to use',
alias: ['l'],
2021-10-08 15:16:57 +02:00
default: '*',
2021-04-23 01:59:54 +02:00
},
2021-02-22 11:38:11 +01:00
stdin: {
boolean: true,
desc: 'Read input from stdin and pass to executor',
2021-10-08 15:16:57 +02:00
alias: ['i'],
2021-02-22 11:38:11 +01:00
},
run_timeout: {
alias: ['rt', 'r'],
number: true,
desc: 'Milliseconds before killing run process',
2021-10-08 15:16:57 +02:00
default: 3000,
2021-02-22 11:38:11 +01:00
},
compile_timeout: {
alias: ['ct', 'c'],
number: true,
desc: 'Milliseconds before killing compile process',
default: 10000,
},
files: {
alias: ['f'],
array: true,
desc: 'Additional files to add',
2021-07-16 14:23:45 +02:00
},
interactive: {
boolean: true,
alias: ['t'],
2021-10-08 15:16:57 +02:00
desc: 'Run interactively using WebSocket transport',
2021-07-16 14:23:45 +02:00
},
status: {
boolean: true,
alias: ['s'],
2021-10-08 15:16:57 +02:00
desc: 'Output additional status to stderr',
},
2021-04-23 05:27:50 +02:00
};
2021-02-22 11:38:11 +01:00
2021-10-08 15:16:57 +02:00
async function handle_interactive(files, argv) {
const ws = new WebSocket(
argv.pistonUrl.replace('http', 'ws') + '/api/v2/connect'
);
2021-07-16 14:23:45 +02:00
2021-10-08 15:16:57 +02:00
const log_message =
process.stderr.isTTY && argv.status ? console.error : () => {};
2021-07-16 14:23:45 +02:00
2021-10-08 15:16:57 +02:00
process.on('exit', () => {
2021-07-16 14:23:45 +02:00
ws.close();
process.stdin.end();
process.stdin.destroy();
2021-10-08 15:16:57 +02:00
process.exit();
});
2021-07-16 14:23:45 +02:00
2021-10-08 15:16:57 +02:00
for (const signal of SIGNALS) {
process.on(signal, () => {
ws.send(JSON.stringify({ type: 'signal', signal }));
});
2021-07-16 14:23:45 +02:00
}
2021-10-08 15:16:57 +02:00
ws.on('open', () => {
2021-07-16 14:23:45 +02:00
const request = {
2021-10-08 15:16:57 +02:00
type: 'init',
2021-07-16 14:23:45 +02:00
language: argv.language,
version: argv['language_version'],
files: files,
args: argv.args,
compile_timeout: argv.ct,
2021-10-08 15:16:57 +02:00
run_timeout: argv.rt,
};
2021-07-16 14:23:45 +02:00
2021-10-08 15:16:57 +02:00
ws.send(JSON.stringify(request));
log_message(chalk.white.bold('Connected'));
2021-07-16 14:23:45 +02:00
process.stdin.resume();
2021-10-08 15:16:57 +02:00
process.stdin.on('data', data => {
ws.send(
JSON.stringify({
type: 'data',
stream: 'stdin',
data: data.toString(),
})
);
});
});
2021-07-16 14:23:45 +02:00
2021-10-08 15:16:57 +02:00
ws.on('close', (code, reason) => {
2021-07-16 14:23:45 +02:00
log_message(
2021-10-08 15:16:57 +02:00
chalk.white.bold('Disconnected: '),
chalk.white.bold('Reason: '),
2021-07-16 14:23:45 +02:00
chalk.yellow(`"${reason}"`),
2021-10-08 15:16:57 +02:00
chalk.white.bold('Code: '),
chalk.yellow(`"${code}"`)
);
process.stdin.pause();
});
2021-07-16 14:23:45 +02:00
2021-10-08 15:16:57 +02:00
ws.on('message', function (data) {
2021-07-16 14:23:45 +02:00
const msg = JSON.parse(data);
2021-10-08 15:16:57 +02:00
switch (msg.type) {
case 'runtime':
log_message(
chalk.bold.white('Runtime:'),
chalk.yellow(`${msg.language} ${msg.version}`)
);
2021-07-16 14:23:45 +02:00
break;
2021-10-08 15:16:57 +02:00
case 'stage':
log_message(
chalk.bold.white('Stage:'),
chalk.yellow(msg.stage)
);
2021-07-16 14:23:45 +02:00
break;
2021-10-08 15:16:57 +02:00
case 'data':
if (msg.stream == 'stdout') process.stdout.write(msg.data);
else if (msg.stream == 'stderr') process.stderr.write(msg.data);
else log_message(chalk.bold.red(`(${msg.stream}) `), msg.data);
2021-07-16 14:23:45 +02:00
break;
2021-10-08 15:16:57 +02:00
case 'exit':
if (msg.signal === null)
2021-07-16 14:23:45 +02:00
log_message(
2021-10-08 15:16:57 +02:00
chalk.white.bold('Stage'),
2021-07-16 14:23:45 +02:00
chalk.yellow(msg.stage),
2021-10-08 15:16:57 +02:00
chalk.white.bold('exited with code'),
2021-07-16 14:23:45 +02:00
chalk.yellow(msg.code)
2021-10-08 15:16:57 +02:00
);
2021-07-16 14:23:45 +02:00
else
log_message(
2021-10-08 15:16:57 +02:00
chalk.white.bold('Stage'),
2021-07-16 14:23:45 +02:00
chalk.yellow(msg.stage),
2021-10-08 15:16:57 +02:00
chalk.white.bold('exited with signal'),
2021-07-16 14:23:45 +02:00
chalk.yellow(msg.signal)
2021-10-08 15:16:57 +02:00
);
break;
2021-07-16 14:23:45 +02:00
default:
2021-10-08 15:16:57 +02:00
log_message(chalk.red.bold('Unknown message:'), msg);
2021-07-16 14:23:45 +02:00
}
2021-10-08 15:16:57 +02:00
});
2021-07-16 14:23:45 +02:00
}
async function run_non_interactively(files, argv) {
2021-10-08 15:16:57 +02:00
const stdin =
(argv.stdin &&
(await new Promise((resolve, _) => {
let data = '';
process.stdin.on('data', d => (data += d));
process.stdin.on('end', _ => resolve(data));
}))) ||
'';
2021-02-22 11:38:11 +01:00
2021-03-13 04:08:35 +01:00
const request = {
2021-03-05 07:40:47 +01:00
language: argv.language,
2021-05-08 02:57:37 +02:00
version: argv['language_version'],
2021-03-05 07:40:47 +01:00
files: files,
2021-03-05 12:44:35 +01:00
args: argv.args,
2021-02-22 11:38:11 +01:00
stdin,
2021-03-05 07:40:47 +01:00
compile_timeout: argv.ct,
2021-10-08 15:16:57 +02:00
run_timeout: argv.rt,
2021-03-13 04:08:35 +01:00
};
let { data: response } = await argv.axios.post('/api/v2/execute', request);
2021-02-22 11:38:11 +01:00
2021-04-23 05:27:50 +02:00
const step = (name, ctx) => {
console.log(chalk.bold(`== ${name} ==`));
if (ctx.stdout) {
2021-10-08 15:16:57 +02:00
console.log(chalk.bold(`STDOUT`));
console.log(ctx.stdout.replace(/\n/g, '\n '));
2021-02-22 11:38:11 +01:00
}
2021-04-23 05:27:50 +02:00
if (ctx.stderr) {
2021-10-08 15:16:57 +02:00
console.log(chalk.bold(`STDERR`));
console.log(ctx.stderr.replace(/\n/g, '\n '));
2021-02-22 11:38:11 +01:00
}
2021-04-23 05:27:50 +02:00
if (ctx.code) {
2021-02-22 11:38:11 +01:00
console.log(
chalk.bold(`Exit Code:`),
chalk.bold[ctx.code > 0 ? 'red' : 'green'](ctx.code)
2021-04-23 05:27:50 +02:00
);
}
if (ctx.signal) {
2021-10-08 15:16:57 +02:00
console.log(chalk.bold(`Signal:`), chalk.bold.yellow(ctx.signal));
2021-04-23 05:27:50 +02:00
}
2021-10-08 15:16:57 +02:00
};
2021-03-14 07:36:12 +01:00
2021-04-23 05:27:50 +02:00
if (response.compile) {
step('Compile', response.compile);
}
step('Run', response.run);
}
2021-07-16 14:23:45 +02:00
2021-10-08 15:16:57 +02:00
exports.handler = async argv => {
const files = [...(argv.files || []), argv.file].map(file_path => {
2021-10-16 11:55:22 +02:00
const buffer = fs.readFileSync(file_path);
// Checks for <20> (the replacement character) after encoding the buffer to uf8
2021-10-15 16:51:11 +02:00
const encoding =
2021-10-16 11:55:22 +02:00
(buffer
2021-10-15 16:51:11 +02:00
.toString()
.split('')
.some(x => x.charCodeAt(0) === 65533) &&
2021-10-15 16:51:11 +02:00
'base64') ||
'utf8';
2021-10-08 15:16:57 +02:00
return {
name: path.basename(file_path),
2021-10-16 11:55:22 +02:00
content: buffer.toString(encoding),
2021-10-15 16:51:11 +02:00
encoding,
2021-10-08 15:16:57 +02:00
};
});
2021-07-16 14:23:45 +02:00
2021-10-08 15:16:57 +02:00
if (argv.interactive) await handle_interactive(files, argv);
2021-07-16 14:23:45 +02:00
else await run_non_interactively(files, argv);
2021-10-08 15:16:57 +02:00
};