lint like rest of codebase

This commit is contained in:
Thomas Hobson 2021-05-08 12:30:40 +12:00
parent 1b7504a191
commit b3be57e0b4
No known key found for this signature in database
GPG Key ID: 9F1FD9D87950DB6F
10 changed files with 1893 additions and 1864 deletions

View File

@ -1 +1,3 @@
singleQuote: true singleQuote: true
tabWidth: 4
arrowParens: avoid

View File

@ -136,7 +136,7 @@ router.post('/execute', async (req, res) => {
}); });
router.get('/runtimes', (req, res) => { router.get('/runtimes', (req, res) => {
const runtimes = runtime.map((rt) => { const runtimes = runtime.map(rt => {
return { return {
language: rt.language, language: rt.language,
version: rt.version.raw, version: rt.version.raw,
@ -152,7 +152,7 @@ router.get('/packages', async (req, res) => {
logger.debug('Request to list packages'); logger.debug('Request to list packages');
let packages = await package.get_package_list(); let packages = await package.get_package_list();
packages = packages.map((pkg) => { packages = packages.map(pkg => {
return { return {
language: pkg.language, language: pkg.language,
language_version: pkg.version.raw, language_version: pkg.version.raw,

View File

@ -9,7 +9,7 @@ const options = [
default: 'INFO', default: 'INFO',
options: Object.values(Logger.LogLevels), options: Object.values(Logger.LogLevels),
validators: [ validators: [
(x) => x =>
Object.values(Logger.LogLevels).includes(x) || Object.values(Logger.LogLevels).includes(x) ||
`Log level ${x} does not exist`, `Log level ${x} does not exist`,
], ],
@ -24,7 +24,9 @@ const options = [
key: 'data_directory', key: 'data_directory',
desc: 'Absolute path to store all piston related data at', desc: 'Absolute path to store all piston related data at',
default: '/piston', default: '/piston',
validators: [(x) => fss.exists_sync(x) || `Directory ${x} does not exist`], validators: [
x => fss.exists_sync(x) || `Directory ${x} does not exist`,
],
}, },
{ {
key: 'runner_uid_min', key: 'runner_uid_min',
@ -58,8 +60,8 @@ const options = [
key: 'disable_networking', key: 'disable_networking',
desc: 'Set to true to disable networking', desc: 'Set to true to disable networking',
default: true, default: true,
parser: (x) => x === 'true', parser: x => x === 'true',
validators: [(x) => typeof x === 'boolean' || `${x} is not a boolean`], validators: [x => typeof x === 'boolean' || `${x} is not a boolean`],
}, },
{ {
key: 'output_max_size', key: 'output_max_size',
@ -99,7 +101,8 @@ const options = [
}, },
{ {
key: 'run_memory_limit', key: 'run_memory_limit',
desc: 'Max memory usage for run stage in bytes (set to -1 for no limit)', desc:
'Max memory usage for run stage in bytes (set to -1 for no limit)',
default: -1, // no limit default: -1, // no limit
parser: parse_int, parser: parse_int,
validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`], validators: [(x, raw) => !is_nan(x) || `${raw} is not a number`],
@ -119,10 +122,10 @@ let errored = false;
let config = {}; let config = {};
options.forEach((option) => { options.forEach(option => {
const env_key = 'PISTON_' + option.key.to_upper_case(); const env_key = 'PISTON_' + option.key.to_upper_case();
const parser = option.parser || ((x) => x); const parser = option.parser || (x => x);
const env_val = process.env[env_key]; const env_val = process.env[env_key];
@ -130,14 +133,17 @@ options.forEach((option) => {
const value = env_val || option.default; const value = env_val || option.default;
option.validators.for_each((validator) => { option.validators.for_each(validator => {
let response = null; let response = null;
if (env_val) response = validator(parsed_val, env_val); if (env_val) response = validator(parsed_val, env_val);
else response = validator(value, value); else response = validator(value, value);
if (response !== true) { if (response !== true) {
errored = true; errored = true;
logger.error(`Config option ${option.key} failed validation:`, response); logger.error(
`Config option ${option.key} failed validation:`,
response
);
return; return;
} }
}); });

View File

@ -5,7 +5,7 @@ const platform = `${is_docker() ? 'docker' : 'baremetal'}-${fs
.read_file_sync('/etc/os-release') .read_file_sync('/etc/os-release')
.toString() .toString()
.split('\n') .split('\n')
.find((x) => x.startsWith('ID')) .find(x => x.startsWith('ID'))
.replace('ID=', '')}`; .replace('ID=', '')}`;
module.exports = { module.exports = {

View File

@ -18,7 +18,7 @@ const app = express();
Logger.setLogLevel(config.log_level); Logger.setLogLevel(config.log_level);
logger.debug('Ensuring data directories exist'); logger.debug('Ensuring data directories exist');
Object.values(globals.data_directories).for_each((dir) => { Object.values(globals.data_directories).for_each(dir => {
let data_path = path.join(config.data_directory, dir); let data_path = path.join(config.data_directory, dir);
logger.debug(`Ensuring ${data_path} exists`); logger.debug(`Ensuring ${data_path} exists`);
@ -43,20 +43,20 @@ const app = express();
const pkglist = await fs.readdir(pkgdir); const pkglist = await fs.readdir(pkgdir);
const languages = await Promise.all( const languages = await Promise.all(
pkglist.map((lang) => { pkglist.map(lang => {
return fs.readdir(path.join(pkgdir, lang)).then((x) => { return fs.readdir(path.join(pkgdir, lang)).then(x => {
return x.map((y) => path.join(pkgdir, lang, y)); return x.map(y => path.join(pkgdir, lang, y));
}); });
}) })
); );
const installed_languages = languages const installed_languages = languages
.flat() .flat()
.filter((pkg) => .filter(pkg =>
fss.exists_sync(path.join(pkg, globals.pkg_installed_file)) fss.exists_sync(path.join(pkg, globals.pkg_installed_file))
); );
installed_languages.for_each((pkg) => runtime.load_package(pkg)); installed_languages.for_each(pkg => runtime.load_package(pkg));
logger.info('Starting API Server'); logger.info('Starting API Server');
logger.debug('Constructing Express App'); logger.debug('Constructing Express App');

View File

@ -106,9 +106,12 @@ class Job {
proc.stdin.end(); proc.stdin.end();
proc.stdin.destroy(); proc.stdin.destroy();
const kill_timeout = set_timeout((_) => proc.kill('SIGKILL'), timeout); const kill_timeout = set_timeout(
_ => proc.kill('SIGKILL'),
timeout
);
proc.stderr.on('data', (data) => { proc.stderr.on('data', data => {
if (stderr.length > config.output_max_size) { if (stderr.length > config.output_max_size) {
proc.kill('SIGKILL'); proc.kill('SIGKILL');
} else { } else {
@ -117,7 +120,7 @@ class Job {
} }
}); });
proc.stdout.on('data', (data) => { proc.stdout.on('data', data => {
if (stdout.length > config.output_max_size) { if (stdout.length > config.output_max_size) {
proc.kill('SIGKILL'); proc.kill('SIGKILL');
} else { } else {
@ -139,7 +142,7 @@ class Job {
resolve({ stdout, stderr, code, signal, output }); resolve({ stdout, stderr, code, signal, output });
}); });
proc.on('error', (err) => { proc.on('error', err => {
exit_cleanup(); exit_cleanup();
reject({ error: err, stdout, stderr, output }); reject({ error: err, stdout, stderr, output });
@ -150,7 +153,8 @@ class Job {
async execute() { async execute() {
if (this.state !== job_states.PRIMED) { if (this.state !== job_states.PRIMED) {
throw new Error( throw new Error(
'Job must be in primed state, current state: ' + this.state.toString() 'Job must be in primed state, current state: ' +
this.state.toString()
); );
} }
@ -167,7 +171,7 @@ class Job {
if (this.runtime.compiled) { if (this.runtime.compiled) {
compile = await this.safe_call( compile = await this.safe_call(
path.join(this.runtime.pkgdir, 'compile'), path.join(this.runtime.pkgdir, 'compile'),
this.files.map((x) => x.name), this.files.map(x => x.name),
this.timeouts.compile, this.timeouts.compile,
this.memory_limits.compile this.memory_limits.compile
); );
@ -200,11 +204,11 @@ class Job {
cp.execFile('ps', ['awwxo', 'pid,ruid'], (err, stdout) => { cp.execFile('ps', ['awwxo', 'pid,ruid'], (err, stdout) => {
if (err === null) { if (err === null) {
const lines = stdout.split('\n').slice(1); //Remove header with slice const lines = stdout.split('\n').slice(1); //Remove header with slice
const procs = lines.map((line) => { const procs = lines.map(line => {
const [pid, ruid] = line const [pid, ruid] = line
.trim() .trim()
.split(/\s+/) .split(/\s+/)
.map((n) => parseInt(n)); .map(n => parseInt(n));
return { pid, ruid }; return { pid, ruid };
}); });
@ -216,7 +220,7 @@ class Job {
}) })
); );
processes = processes.filter((proc) => proc.ruid === this.uid); processes = processes.filter(proc => proc.ruid === this.uid);
for (const proc of processes) { for (const proc of processes) {
// First stop the processes, but keep their resources allocated so they cant re-fork // First stop the processes, but keep their resources allocated so they cant re-fork
@ -251,7 +255,10 @@ class Job {
const stat = await fs.stat(file_path); const stat = await fs.stat(file_path);
if (stat.uid === this.uid) { if (stat.uid === this.uid) {
await fs.rm(file_path, { recursive: true, force: true }); await fs.rm(file_path, {
recursive: true,
force: true,
});
} }
} catch (e) { } catch (e) {
// File was somehow deleted in the time that we read the dir to when we checked the file // File was somehow deleted in the time that we read the dir to when we checked the file
@ -266,7 +273,10 @@ class Job {
async cleanup() { async cleanup() {
logger.info(`Cleaning up job uuid=${this.uuid}`); logger.info(`Cleaning up job uuid=${this.uuid}`);
await Promise.all([this.cleanup_processes(), this.cleanup_filesystem()]); await Promise.all([
this.cleanup_processes(),
this.cleanup_filesystem(),
]);
} }
} }

View File

@ -105,15 +105,19 @@ class Package {
const envout = await new Promise((resolve, reject) => { const envout = await new Promise((resolve, reject) => {
let stdout = ''; let stdout = '';
const proc = cp.spawn('env', ['-i', 'bash', '-c', `${get_env_command}`], { const proc = cp.spawn(
'env',
['-i', 'bash', '-c', `${get_env_command}`],
{
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
}); }
);
proc.once('exit', (code, _) => { proc.once('exit', (code, _) => {
code === 0 ? resolve(stdout) : reject(); code === 0 ? resolve(stdout) : reject();
}); });
proc.stdout.on('data', (data) => { proc.stdout.on('data', data => {
stdout += data; stdout += data;
}); });
@ -123,7 +127,10 @@ class Package {
const filtered_env = envout const filtered_env = envout
.split('\n') .split('\n')
.filter( .filter(
(l) => !['PWD', 'OLDPWD', '_', 'SHLVL'].includes(l.split('=', 2)[0]) l =>
!['PWD', 'OLDPWD', '_', 'SHLVL'].includes(
l.split('=', 2)[0]
)
) )
.join('\n'); .join('\n');
@ -159,7 +166,9 @@ class Package {
logger.error( logger.error(
`Uninstalling ${this.language}-${this.version.raw} failed: Not installed` `Uninstalling ${this.language}-${this.version.raw} failed: Not installed`
); );
throw new Error(`${this.language}-${this.version.raw} is not installed`); throw new Error(
`${this.language}-${this.version.raw} is not installed`
);
} }
logger.debug('Unregistering runtime'); logger.debug('Unregistering runtime');
@ -177,11 +186,11 @@ class Package {
} }
static async get_package_list() { static async get_package_list() {
const repo_content = await fetch(config.repo_url).then((x) => x.text()); const repo_content = await fetch(config.repo_url).then(x => x.text());
const entries = repo_content.split('\n').filter((x) => x.length > 0); const entries = repo_content.split('\n').filter(x => x.length > 0);
return entries.map((line) => { return entries.map(line => {
const [language, version, checksum, download] = line.split(',', 4); const [language, version, checksum, download] = line.split(',', 4);
return new Package({ return new Package({
@ -196,8 +205,10 @@ class Package {
static async get_package(lang, version) { static async get_package(lang, version) {
const packages = await Package.get_package_list(); const packages = await Package.get_package_list();
const candidates = packages.filter((pkg) => { const candidates = packages.filter(pkg => {
return pkg.language == lang && semver.satisfies(pkg.version, version); return (
pkg.language == lang && semver.satisfies(pkg.version, version)
);
}); });
candidates.sort((a, b) => semver.rcompare(a.version, b.version)); candidates.sort((a, b) => semver.rcompare(a.version, b.version));

View File

@ -33,7 +33,7 @@ class Runtime {
if (provides) { if (provides) {
// Multiple languages in 1 package // Multiple languages in 1 package
provides.forEach((lang) => { provides.forEach(lang => {
runtimes.push( runtimes.push(
new Runtime({ new Runtime({
language: lang.language, language: lang.language,
@ -76,7 +76,7 @@ class Runtime {
env_content env_content
.trim() .trim()
.split('\n') .split('\n')
.map((line) => line.split('=', 2)) .map(line => line.split('=', 2))
.forEach(([key, val]) => { .forEach(([key, val]) => {
this._env_vars[key.trim()] = val.trim(); this._env_vars[key.trim()] = val.trim();
}); });
@ -99,7 +99,7 @@ module.exports = runtimes;
module.exports.Runtime = Runtime; module.exports.Runtime = Runtime;
module.exports.get_runtimes_matching_language_version = function (lang, ver) { module.exports.get_runtimes_matching_language_version = function (lang, ver) {
return runtimes.filter( return runtimes.filter(
(rt) => rt =>
(rt.language == lang || rt.aliases.includes(lang)) && (rt.language == lang || rt.aliases.includes(lang)) &&
semver.satisfies(rt.version, ver) semver.satisfies(rt.version, ver)
); );
@ -115,7 +115,7 @@ module.exports.get_latest_runtime_matching_language_version = function (
module.exports.get_runtime_by_name_and_version = function (runtime, ver) { module.exports.get_runtime_by_name_and_version = function (runtime, ver) {
return runtimes.find( return runtimes.find(
(rt) => rt =>
(rt.runtime == runtime || (rt.runtime == runtime ||
(rt.runtime === undefined && rt.language == runtime)) && (rt.runtime === undefined && rt.language == runtime)) &&
semver.satisfies(rt.version, ver) semver.satisfies(rt.version, ver)