-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
85 lines (78 loc) · 2.86 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
const fs = require('fs');
const archiver = require('archiver');
const express = require('express');
const session = require('express-session');
const parser = require('body-parser');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const code = fs.readFileSync('./public/server.js', 'utf8');
const shared = fs.existsSync('./public/shared.js') ? fs.readFileSync('./public/shared.js', 'utf8') : '';
const storage = require('./lib/storage');
let packageSize = 0;
function createSandbox() {
const sandbox = {
console,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
storage: storage.interface,
io: io
};
Object.defineProperty(sandbox, 'module', {
enumerable: true,
configurable: false,
writable: false,
value: Object.create(null)
});
sandbox.module.exports = Object.create(null);
sandbox.exports = sandbox.module.exports;
return sandbox;
};
function createZip() {
const archive = archiver('zip', {zlib: { level: 9 }});
const output = fs.createWriteStream('dist.zip');
output.on('close', () => {
packageSize = archive.pointer();
});
archive.pipe(output);
archive.directory('public/', '');
archive.finalize();
};
app.set('port', (process.env.PORT || 3000))
.set('storage', process.env.DATABASE_URL || 'sqlite:storage.sqlite')
.get('/server-info', (req, res) => {
let limit = 13312,
storageSize = storage.interface.size();
res.set('Content-Type', 'text/plain').send([
`Package: ${packageSize} byte / ${(packageSize ? packageSize / limit * 100 : 0).toFixed(2)}%`,
`Storage: ${storageSize} byte / ${(storageSize ? storageSize / limit * 100 : 0).toFixed(2)}%`
].join("\n"));
})
.use(express.static('public'))
.use(session({ secret: 'js13kserver', saveUninitialized: false, resave: false }));
storage.init(app.get('storage')).then(() => {
const sandbox = createSandbox();
require('vm').runInNewContext(shared + '\n' + code, sandbox);
if (typeof sandbox.module.exports == 'function') {
io.on('connection', sandbox.module.exports);
} else if (typeof sandbox.module.exports == 'object') {
app.use(parser.urlencoded({ extended: true }))
.use(parser.json());
for (let route in sandbox.module.exports) {
if (route == 'io') {
io.on('connection', sandbox.module.exports[route]);
} else {
app.all('/' + route, sandbox.module.exports[route])
}
}
}
server.listen(app.get('port'), () => {
console.log('Server started at port: ' + app.get('port'));
// createZip(); // Not clear what this adds, since the files are not minified
});
}).catch(err => {
console.error(err);
});