-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
67 lines (57 loc) · 2.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import get from "./lib/get.js";
import post from "./lib/post.js";
import or from "./lib/or.js";
const defaultServer = "https://emkc.org";
export const piston = (opts = {}) => {
const server = String(opts.server || defaultServer).replace(/\/$/, '');
const store = {};
const api = {
async runtimes() {
if (store.runtimes) {
return store.runtimes;
}
const suffix = (server === defaultServer)
? '/api/v2/piston/runtimes'
: '/api/v2/runtimes';
const url = `${server}${suffix}`;
const runtimes = await get(url);
if (runtimes && runtimes.success !== false) {
store.runtimes = runtimes;
}
return runtimes;
},
async execute(argA, argB, argC) {
const runtimes = await api.runtimes();
if (runtimes.success === false) {
return runtimes;
}
const config = typeof argA === 'object' ? argA : typeof argB === 'object' ? argB : argC || {};
let language = (typeof argA === 'string') ? argA : undefined;
language = language || config.language;
const code = typeof argB === 'string' ? argB : undefined;
const latestVersion = (runtimes.filter(n => n.language === language).sort((a, b) => {
return a.version > b.version ? -1 : b.version > a.version ? 1 : 0;
})[0] || {}).version;
const boilerplate = {
"language": language,
"version": config.version || latestVersion,
"files": or(config.files, [{
"content": code
}]),
"stdin": or(config.stdin, ""),
"args": or(config.args, ["1", "2", "3"]),
"compile_timeout": or(config.compileTimeout, 10000),
"run_timeout": or(config.runTimeout, 3000),
"compile_memory_limit": or(config.compileMemoryLimit, -1),
"run_memory_limit": or(config.runMemoryLimit, -1)
}
const suffix = (server === defaultServer)
? '/api/v2/piston/execute'
: '/api/v2/execute';
const url = `${server}${suffix}`;
return await post(url, boilerplate);
}
}
return api;
}
export default piston;