2021-04-25 10:55:20 +02:00
|
|
|
const express = require('express');
|
2021-05-08 02:20:21 +02:00
|
|
|
const router = express.Router();
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-07-16 13:10:44 +02:00
|
|
|
const events = require('events');
|
|
|
|
|
2021-04-25 10:55:20 +02:00
|
|
|
const runtime = require('../runtime');
|
2021-05-07 21:22:25 +02:00
|
|
|
const { Job } = require('../job');
|
2021-05-08 02:20:21 +02:00
|
|
|
const package = require('../package');
|
2021-05-07 21:35:27 +02:00
|
|
|
const logger = require('logplease').create('api/v2');
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-10-08 15:16:57 +02:00
|
|
|
const SIGNALS = [
|
|
|
|
'SIGABRT',
|
|
|
|
'SIGALRM',
|
|
|
|
'SIGBUS',
|
|
|
|
'SIGCHLD',
|
|
|
|
'SIGCLD',
|
|
|
|
'SIGCONT',
|
|
|
|
'SIGEMT',
|
|
|
|
'SIGFPE',
|
|
|
|
'SIGHUP',
|
|
|
|
'SIGILL',
|
|
|
|
'SIGINFO',
|
|
|
|
'SIGINT',
|
|
|
|
'SIGIO',
|
|
|
|
'SIGIOT',
|
|
|
|
'SIGKILL',
|
|
|
|
'SIGLOST',
|
|
|
|
'SIGPIPE',
|
|
|
|
'SIGPOLL',
|
|
|
|
'SIGPROF',
|
|
|
|
'SIGPWR',
|
|
|
|
'SIGQUIT',
|
|
|
|
'SIGSEGV',
|
|
|
|
'SIGSTKFLT',
|
|
|
|
'SIGSTOP',
|
|
|
|
'SIGTSTP',
|
|
|
|
'SIGSYS',
|
|
|
|
'SIGTERM',
|
|
|
|
'SIGTRAP',
|
|
|
|
'SIGTTIN',
|
|
|
|
'SIGTTOU',
|
|
|
|
'SIGUNUSED',
|
|
|
|
'SIGURG',
|
|
|
|
'SIGUSR1',
|
|
|
|
'SIGUSR2',
|
|
|
|
'SIGVTALRM',
|
|
|
|
'SIGXCPU',
|
|
|
|
'SIGXFSZ',
|
|
|
|
'SIGWINCH',
|
|
|
|
];
|
2021-07-16 14:22:55 +02:00
|
|
|
// ref: https://man7.org/linux/man-pages/man7/signal.7.html
|
2021-05-07 09:37:22 +02:00
|
|
|
|
2021-10-08 15:16:57 +02:00
|
|
|
function get_job(body) {
|
2021-10-02 14:08:36 +02:00
|
|
|
let {
|
2021-05-08 02:30:40 +02:00
|
|
|
language,
|
|
|
|
version,
|
|
|
|
args,
|
2021-07-16 13:10:44 +02:00
|
|
|
stdin,
|
|
|
|
files,
|
2021-05-08 02:30:40 +02:00
|
|
|
compile_memory_limit,
|
|
|
|
run_memory_limit,
|
2021-07-16 13:10:44 +02:00
|
|
|
run_timeout,
|
2021-10-08 15:16:57 +02:00
|
|
|
compile_timeout,
|
2021-07-16 13:10:44 +02:00
|
|
|
} = body;
|
2021-05-07 10:21:25 +02:00
|
|
|
|
2021-07-16 13:10:44 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!language || typeof language !== 'string') {
|
|
|
|
return reject({
|
|
|
|
message: 'language is required as a string',
|
2021-05-08 02:30:40 +02:00
|
|
|
});
|
|
|
|
}
|
2021-07-16 13:10:44 +02:00
|
|
|
if (!version || typeof version !== 'string') {
|
|
|
|
return reject({
|
|
|
|
message: 'version is required as a string',
|
2021-05-08 02:30:40 +02:00
|
|
|
});
|
|
|
|
}
|
2021-07-16 13:10:44 +02:00
|
|
|
if (!files || !Array.isArray(files)) {
|
|
|
|
return reject({
|
|
|
|
message: 'files is required as an array',
|
2021-05-08 02:30:40 +02:00
|
|
|
});
|
|
|
|
}
|
2021-07-16 13:10:44 +02:00
|
|
|
for (const [i, file] of files.entries()) {
|
|
|
|
if (typeof file.content !== 'string') {
|
|
|
|
return reject({
|
|
|
|
message: `files[${i}].content is required as a string`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 05:14:06 +02:00
|
|
|
if (
|
|
|
|
files.filter(file => !file.encoding || file.encoding === 'utf8')
|
|
|
|
.length === 0
|
|
|
|
) {
|
|
|
|
return reject({
|
|
|
|
message: 'files must include at least one utf8 encoded file',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-16 13:10:44 +02:00
|
|
|
const rt = runtime.get_latest_runtime_matching_language_version(
|
|
|
|
language,
|
|
|
|
version
|
|
|
|
);
|
|
|
|
if (rt === undefined) {
|
|
|
|
return reject({
|
|
|
|
message: `${language}-${version} runtime is unknown`,
|
2021-05-08 02:30:40 +02:00
|
|
|
});
|
|
|
|
}
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-10-02 14:08:36 +02:00
|
|
|
for (let constraint of ['memory_limit', 'timeout']) {
|
|
|
|
for (let type of ['compile', 'run']) {
|
|
|
|
let constraint_name = `${type}_${constraint}`;
|
|
|
|
let constraint_value = body[constraint_name];
|
|
|
|
let configured_limit = rt[`${constraint}s`][type];
|
|
|
|
if (!constraint_value) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (typeof constraint_value !== 'number') {
|
|
|
|
return reject({
|
2021-10-08 15:16:57 +02:00
|
|
|
message: `If specified, ${constraint_name} must be a number`,
|
2021-10-02 14:08:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (configured_limit <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (constraint_value > configured_limit) {
|
|
|
|
return reject({
|
2021-10-08 15:16:57 +02:00
|
|
|
message: `${constraint_name} cannot exceed the configured limit of ${configured_limit}`,
|
2021-10-02 14:08:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (constraint_value < 0) {
|
|
|
|
return reject({
|
2021-10-08 15:16:57 +02:00
|
|
|
message: `${constraint_name} must be non-negative`,
|
2021-10-02 14:08:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
compile_timeout = compile_timeout || rt.timeouts.compile;
|
|
|
|
run_timeout = run_timeout || rt.timeouts.run;
|
|
|
|
compile_memory_limit = compile_memory_limit || rt.memory_limits.compile;
|
2021-10-10 19:11:16 +02:00
|
|
|
run_memory_limit = run_memory_limit || rt.memory_limits.run;
|
2021-10-08 15:16:57 +02:00
|
|
|
resolve(
|
|
|
|
new Job({
|
|
|
|
runtime: rt,
|
|
|
|
args: args || [],
|
|
|
|
stdin: stdin || '',
|
|
|
|
files,
|
|
|
|
timeouts: {
|
|
|
|
run: run_timeout,
|
|
|
|
compile: compile_timeout,
|
|
|
|
},
|
|
|
|
memory_limits: {
|
|
|
|
run: run_memory_limit,
|
|
|
|
compile: compile_memory_limit,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
2021-10-02 14:08:36 +02:00
|
|
|
});
|
2021-07-16 13:10:44 +02:00
|
|
|
}
|
2021-05-08 02:30:40 +02:00
|
|
|
|
2021-07-16 13:10:44 +02:00
|
|
|
router.use((req, res, next) => {
|
|
|
|
if (['GET', 'HEAD', 'OPTIONS'].includes(req.method)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!req.headers['content-type'].startsWith('application/json')) {
|
|
|
|
return res.status(415).send({
|
|
|
|
message: 'requests must be of type application/json',
|
2021-05-08 02:30:40 +02:00
|
|
|
});
|
|
|
|
}
|
2021-05-07 21:22:25 +02:00
|
|
|
|
2021-07-16 13:10:44 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
router.ws('/connect', async (ws, req) => {
|
|
|
|
let job = null;
|
|
|
|
let eventBus = new events.EventEmitter();
|
|
|
|
|
2021-10-08 15:16:57 +02:00
|
|
|
eventBus.on('stdout', data =>
|
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'data',
|
|
|
|
stream: 'stdout',
|
|
|
|
data: data.toString(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
eventBus.on('stderr', data =>
|
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'data',
|
|
|
|
stream: 'stderr',
|
|
|
|
data: data.toString(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
eventBus.on('stage', stage =>
|
|
|
|
ws.send(JSON.stringify({ type: 'stage', stage }))
|
|
|
|
);
|
|
|
|
eventBus.on('exit', (stage, status) =>
|
|
|
|
ws.send(JSON.stringify({ type: 'exit', stage, ...status }))
|
|
|
|
);
|
|
|
|
|
|
|
|
ws.on('message', async data => {
|
|
|
|
try {
|
2021-07-16 13:10:44 +02:00
|
|
|
const msg = JSON.parse(data);
|
|
|
|
|
2021-10-08 15:16:57 +02:00
|
|
|
switch (msg.type) {
|
|
|
|
case 'init':
|
|
|
|
if (job === null) {
|
2021-07-16 14:22:55 +02:00
|
|
|
job = await get_job(msg);
|
2021-07-16 13:10:44 +02:00
|
|
|
|
2021-07-16 14:22:55 +02:00
|
|
|
await job.prime();
|
2021-07-16 13:10:44 +02:00
|
|
|
|
2021-10-08 15:16:57 +02:00
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'runtime',
|
|
|
|
language: job.runtime.language,
|
|
|
|
version: job.runtime.version.raw,
|
|
|
|
})
|
|
|
|
);
|
2021-07-16 13:10:44 +02:00
|
|
|
|
2021-07-16 14:22:55 +02:00
|
|
|
await job.execute_interactive(eventBus);
|
2021-07-16 13:10:44 +02:00
|
|
|
|
2021-10-08 15:16:57 +02:00
|
|
|
ws.close(4999, 'Job Completed');
|
|
|
|
} else {
|
|
|
|
ws.close(4000, 'Already Initialized');
|
2021-07-16 14:22:55 +02:00
|
|
|
}
|
|
|
|
break;
|
2021-10-08 15:16:57 +02:00
|
|
|
case 'data':
|
|
|
|
if (job !== null) {
|
|
|
|
if (msg.stream === 'stdin') {
|
|
|
|
eventBus.emit('stdin', msg.data);
|
|
|
|
} else {
|
|
|
|
ws.close(4004, 'Can only write to stdin');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ws.close(4003, 'Not yet initialized');
|
2021-07-16 13:10:44 +02:00
|
|
|
}
|
2021-10-08 15:16:57 +02:00
|
|
|
break;
|
|
|
|
case 'signal':
|
|
|
|
if (job !== null) {
|
|
|
|
if (SIGNALS.includes(msg.signal)) {
|
|
|
|
eventBus.emit('signal', msg.signal);
|
|
|
|
} else {
|
|
|
|
ws.close(4005, 'Invalid signal');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ws.close(4003, 'Not yet initialized');
|
2021-07-16 14:22:55 +02:00
|
|
|
}
|
2021-10-08 15:16:57 +02:00
|
|
|
break;
|
2021-07-16 13:10:44 +02:00
|
|
|
}
|
2021-10-08 15:16:57 +02:00
|
|
|
} catch (error) {
|
|
|
|
ws.send(JSON.stringify({ type: 'error', message: error.message }));
|
|
|
|
ws.close(4002, 'Notified Error');
|
2021-07-16 13:10:44 +02:00
|
|
|
// ws.close message is limited to 123 characters, so we notify over WS then close.
|
|
|
|
}
|
2021-10-08 15:16:57 +02:00
|
|
|
});
|
2021-07-16 13:10:44 +02:00
|
|
|
|
2021-10-08 15:16:57 +02:00
|
|
|
ws.on('close', async () => {
|
|
|
|
if (job !== null) {
|
|
|
|
await job.cleanup();
|
2021-07-16 13:10:44 +02:00
|
|
|
}
|
2021-10-08 15:16:57 +02:00
|
|
|
});
|
2021-07-16 13:10:44 +02:00
|
|
|
|
2021-10-08 15:16:57 +02:00
|
|
|
setTimeout(() => {
|
2021-07-16 13:10:44 +02:00
|
|
|
//Terminate the socket after 1 second, if not initialized.
|
2021-10-08 15:16:57 +02:00
|
|
|
if (job === null) ws.close(4001, 'Initialization Timeout');
|
|
|
|
}, 1000);
|
|
|
|
});
|
2021-07-16 13:10:44 +02:00
|
|
|
|
|
|
|
router.post('/execute', async (req, res) => {
|
2021-10-08 15:16:57 +02:00
|
|
|
try {
|
2021-07-16 13:10:44 +02:00
|
|
|
const job = await get_job(req.body);
|
|
|
|
|
|
|
|
await job.prime();
|
|
|
|
|
|
|
|
const result = await job.execute();
|
|
|
|
|
|
|
|
await job.cleanup();
|
|
|
|
|
|
|
|
return res.status(200).send(result);
|
2021-10-08 15:16:57 +02:00
|
|
|
} catch (error) {
|
2021-10-02 14:08:36 +02:00
|
|
|
return res.status(400).json(error);
|
2021-07-16 13:10:44 +02:00
|
|
|
}
|
2021-04-25 10:55:20 +02:00
|
|
|
});
|
|
|
|
|
2021-05-07 21:22:25 +02:00
|
|
|
router.get('/runtimes', (req, res) => {
|
2021-05-08 02:30:40 +02:00
|
|
|
const runtimes = runtime.map(rt => {
|
|
|
|
return {
|
|
|
|
language: rt.language,
|
|
|
|
version: rt.version.raw,
|
|
|
|
aliases: rt.aliases,
|
|
|
|
runtime: rt.runtime,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).send(runtimes);
|
2021-04-25 10:55:20 +02:00
|
|
|
});
|
|
|
|
|
2021-05-07 21:22:25 +02:00
|
|
|
router.get('/packages', async (req, res) => {
|
2021-05-08 02:30:40 +02:00
|
|
|
logger.debug('Request to list packages');
|
|
|
|
let packages = await package.get_package_list();
|
|
|
|
|
|
|
|
packages = packages.map(pkg => {
|
|
|
|
return {
|
|
|
|
language: pkg.language,
|
|
|
|
language_version: pkg.version.raw,
|
|
|
|
installed: pkg.installed,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.status(200).send(packages);
|
2021-04-25 10:55:20 +02:00
|
|
|
});
|
|
|
|
|
2021-05-08 02:41:41 +02:00
|
|
|
router.post('/packages', async (req, res) => {
|
2021-05-08 02:30:40 +02:00
|
|
|
logger.debug('Request to install package');
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:41:41 +02:00
|
|
|
const { language, version } = req.body;
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
const pkg = await package.get_package(language, version);
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
if (pkg == null) {
|
|
|
|
return res.status(404).send({
|
|
|
|
message: `Requested package ${language}-${version} does not exist`,
|
|
|
|
});
|
|
|
|
}
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
try {
|
|
|
|
const response = await pkg.install();
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
return res.status(200).send(response);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(
|
|
|
|
`Error while installing package ${pkg.language}-${pkg.version}:`,
|
|
|
|
e.message
|
|
|
|
);
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
return res.status(500).send({
|
|
|
|
message: e.message,
|
|
|
|
});
|
|
|
|
}
|
2021-04-25 10:55:20 +02:00
|
|
|
});
|
|
|
|
|
2021-05-08 02:41:41 +02:00
|
|
|
router.delete('/packages', async (req, res) => {
|
2021-05-08 02:30:40 +02:00
|
|
|
logger.debug('Request to uninstall package');
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:41:41 +02:00
|
|
|
const { language, version } = req.body;
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
const pkg = await package.get_package(language, version);
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
if (pkg == null) {
|
|
|
|
return res.status(404).send({
|
|
|
|
message: `Requested package ${language}-${version} does not exist`,
|
|
|
|
});
|
|
|
|
}
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
try {
|
|
|
|
const response = await pkg.uninstall();
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
return res.status(200).send(response);
|
|
|
|
} catch (e) {
|
|
|
|
logger.error(
|
|
|
|
`Error while uninstalling package ${pkg.language}-${pkg.version}:`,
|
|
|
|
e.message
|
|
|
|
);
|
2021-04-25 10:55:20 +02:00
|
|
|
|
2021-05-08 02:30:40 +02:00
|
|
|
return res.status(500).send({
|
|
|
|
message: e.message,
|
|
|
|
});
|
|
|
|
}
|
2021-04-25 10:55:20 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|