-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
60 lines (56 loc) · 1.73 KB
/
config.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
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const $c = {};
$c.sizeMin = 1;
$c.sizeMax = 1 * 1024 * 1024;
$c.sandbox = path.join(__dirname, './sandbox');
$c.logger = console.log;
$c.namer = () => { return new Date().getTime(); };
$c.random = (...args) => {
return new Promise((go, stop) => {
return go(Math.random(...args));
});
};
$c.randomSync = Math.random;
$c.executer = childProcess.execFile;
$c.executerSync = childProcess.execFileSync;
$c.stdoutHandler = (data) => {};
$c.stderrHandler = (data) => {};
$c.god = (entity) => {
return new Promise((go, stop) => {
let filename = path.join($c.sandbox, entity.name);
fs.open(filename, 'w+', 0700, (e, fd) => {
if (e) return stop(e);
fs.write(fd, entity.data, 0, entity.data.length, (e, written, data) => {
if (e) return stop(e);
$c.logger(`WRITTEN ${written} BYTES TO ${filename}`);
fs.fchmod(fd, 0700, (e) => {
if (e) return stop(e);
fs.close(fd, (e) => {
if (e) return stop(e);
$c.logger(`TRYING TO RUN ${filename}`);
$c.executer(filename, [], { cwd: $c.sandbox }, (e, stdout, stderr) => {
if (e) return stop(e);
$c.stdoutHandler(stdout);
$c.stderrHandler(stderr);
return go();
});
});
});
});
});
});
};
$c.godSync = (entity) => {
let filename = path.join($c.sandbox, entity.name);
let fd = fs.openSync(filename, 'w+', 0700);
let written = fs.writeSync(fd, entity.data, 0, entity.data.length);
$c.logger(`WRITTEN ${written} BYTES TO ${filename}`);
fs.fchmodSync(fd, 0700);
fs.closeSync(fd);
$c.logger(`TRYING TO RUN ${filename}`);
return $c.executerSync(filename, [], { cwd: $c.sandbox });
};
module.exports = $c;