piston/api/src/runtime.js

111 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-02-20 23:39:03 +01:00
const logger = require('logplease').create('runtime');
const semver = require('semver');
const config = require('./config');
const globals = require('./globals');
const fss = require('fs');
const path = require('path');
2021-02-20 15:13:56 +01:00
2021-02-20 23:39:03 +01:00
const runtimes = [];
2021-02-20 15:13:56 +01:00
class Runtime {
2021-03-13 06:01:04 +01:00
constructor({language, version, aliases, pkgdir, runtime}){
this.language = language;
this.version = version;
2021-04-25 06:37:05 +02:00
this.aliases = aliases || [];
this.pkgdir = pkgdir;
this.runtime = runtime;
}
static load_package(package_dir){
2021-03-13 06:01:04 +01:00
let info = JSON.parse(
2021-02-20 23:39:03 +01:00
fss.read_file_sync(path.join(package_dir, 'pkg-info.json'))
);
2021-02-20 15:13:56 +01:00
let { language, version, build_platform, aliases, provides } = info;
version = semver.parse(version);
2021-02-20 15:13:56 +01:00
2021-03-13 06:01:04 +01:00
if (build_platform !== globals.platform) {
logger.warn(
`Package ${language}-${version} was built for platform ${build_platform}, ` +
`but our platform is ${globals.platform}`
);
2021-02-20 15:13:56 +01:00
}
2021-03-13 06:01:04 +01:00
if(provides){
// Multiple languages in 1 package
provides.forEach(lang => {
runtimes.push(new Runtime({
language: lang.language,
aliases: lang.aliases,
version,
pkgdir: package_dir,
runtime: language
}));
});
}else{
runtimes.push(new Runtime({
language,
version,
aliases,
pkgdir: package_dir
}))
}
2021-02-20 23:39:03 +01:00
logger.debug(`Package ${language}-${version} was loaded`);
2021-03-13 06:01:04 +01:00
2021-02-20 15:13:56 +01:00
}
2021-03-13 06:01:04 +01:00
get compiled() {
2021-03-13 11:09:13 +01:00
if (this._compiled === undefined) {
this._compiled = fss.exists_sync(path.join(this.pkgdir, 'compile'));
2021-03-13 06:01:04 +01:00
}
2021-02-20 15:13:56 +01:00
2021-03-13 11:09:13 +01:00
return this._compiled;
2021-02-20 15:13:56 +01:00
}
2021-03-13 06:01:04 +01:00
get env_vars() {
2021-03-13 11:09:13 +01:00
if (!this._env_vars) {
2021-02-20 23:39:03 +01:00
const env_file = path.join(this.pkgdir, '.env');
const env_content = fss.read_file_sync(env_file).toString();
2021-03-13 06:01:04 +01:00
2021-03-13 11:09:13 +01:00
this._env_vars = {};
2021-03-13 06:01:04 +01:00
2021-02-20 15:13:56 +01:00
env_content
2021-02-21 00:57:20 +01:00
.trim()
2021-02-20 23:39:03 +01:00
.split('\n')
.map(line => line.split('=',2))
2021-02-20 15:13:56 +01:00
.forEach(([key,val]) => {
2021-03-13 11:09:13 +01:00
this._env_vars[key.trim()] = val.trim();
2021-02-20 23:39:03 +01:00
});
2021-02-20 15:13:56 +01:00
}
2021-03-13 06:01:04 +01:00
2021-03-13 11:09:13 +01:00
return this._env_vars;
2021-02-20 15:13:56 +01:00
}
2021-03-13 06:01:04 +01:00
toString() {
2021-02-20 23:39:03 +01:00
return `${this.language}-${this.version.raw}`;
2021-02-20 15:13:56 +01:00
}
2021-04-10 06:10:18 +02:00
unregister() {
const index = runtimes.indexOf(this);
runtimes.splice(index, 1); //Remove from runtimes list
}
2021-02-20 15:13:56 +01:00
}
2021-02-20 23:39:03 +01:00
module.exports = runtimes;
module.exports.Runtime = Runtime;
2021-02-20 15:13:56 +01:00
module.exports.get_runtimes_matching_language_version = function(lang, ver){
2021-03-06 07:17:56 +01:00
return runtimes.filter(rt => (rt.language == lang || rt.aliases.includes(lang)) && semver.satisfies(rt.version, ver));
2021-02-20 23:39:03 +01:00
};
2021-02-20 15:13:56 +01:00
module.exports.get_latest_runtime_matching_language_version = function(lang, ver){
return module.exports.get_runtimes_matching_language_version(lang, ver)
2021-02-20 23:39:03 +01:00
.sort((a,b) => semver.rcompare(a.version, b.version))[0];
};
module.exports.get_runtime_by_name_and_version = function(runtime, ver){
2021-04-25 05:11:32 +02:00
return runtimes.find(rt => (rt.runtime == runtime || (rt.runtime === undefined && rt.language == runtime)) && semver.satisfies(rt.version, ver));
}
module.exports.load_package = Runtime.load_package;