-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
130 lines (122 loc) · 4.4 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"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');
const limit = 13 * 1024;
const chalk = require('chalk');
const runMode = process.env.NODE_ENV || 'development'
const isDevelopment = !!(runMode === 'development')
const isProduction = !!(runMode === 'production')
const isTest = !!(runMode === 'test')
let packageSize = 0;
function createSandbox() {
const sandbox = {
console,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
Buffer,
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() {
return new Promise((resolve, reject) => {
const archive = archiver('zip', {zlib: { level: 9 }});
const output = fs.createWriteStream('dist.zip');
output.on('close', () => {
packageSize = archive.pointer();
resolve(packageSize);
});
output.on('error', (error) => {
packageSize = -1;
reject(error);
})
archive.on('error', (error) => {
packageSize = -1;
reject(error);
})
archive.pipe(output);
archive.directory('public/', '');
archive.finalize();
})
};
function recurrentLogStorageSize() {
let lastStoragePct = -1;
function logStorageSize() {
let storageSize = storage.interface.size();
let pct = storageSize ? storageSize / limit * 100 : 0;
if (lastStoragePct !== pct) {
let color = pct <= 100 ? chalk.green : chalk.red;
console.log(color(`Storage: ${storageSize} byte / ${pct.toFixed(2)}%`));
lastStoragePct = pct;
}
}
logStorageSize();
setInterval(logStorageSize, 2000);
};
app.set('port', (process.env.PORT || 3000))
.set('storage', process.env.DATABASE_URL || 'sqlite:storage.sqlite')
.get('/server-info', (req, res) => {
let 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();
console.log('🤖 Running server in VM');
require('vm').runInNewContext(shared + '\n' + code, sandbox);
console.log('🤖 Server ready');
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(chalk.blue(`Server started in ${runMode} mode at port ${app.get('port')}`));
// createZip()
// .then(()=> {
// if (isDevelopment) {
// let pct = packageSize / limit * 100;
// let color = pct <= 100 ? chalk.green : chalk.red;
// console.log(color(`Package: ${packageSize} byte / ${pct.toFixed(2)}%`));
// recurrentLogStorageSize();
// }
// })
// .catch((err)=> {
// console.error(err.message)
// });
});
}).catch(err => {
console.error(err);
});