-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
185 lines (150 loc) · 4.58 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const fs = require('fs')
const express = require('express');
const app = express();
var http = require('http').Server(app);
var http_get = require('http').get;
var io = require('socket.io')(http);
const port = 8123;
const npm = require('npm');
const browserify = require('browserify');
var path = require('path');
var busboy = require("connect-busboy");
app.use(busboy());
var pkg = require('bc19/package');
var versions = {
curr: pkg.version,
newest: '',
available: []
};
console.log('BC19 Version: ' + versions.curr);
npm.load(function(err) {
npm.commands.view(['bc19', 'versions'], function(er, data){
var n = Object.keys(data)[0];
versions.newest = n;
versions.available = data[n]['versions'].reverse();
console.log('Newest: ' + versions.newest);
});
});
app.get('/main.js', function(req, res) {
var b = browserify();
b.add('./site/main.js');
b.bundle(function(err, buf) {
res.type('text/javascript').send(buf);
});
});
app.use(express.static('./site'));
var file_path = '';
var url_path = '';
var used_path = '';
function watch_file(fpath) {
delay = false;
fs.watch(fpath, (event, filename) => {
if (filename) {
if (delay) return;
setTimeout(function() {
delay = false;
}, 100);
delay = true;
console.log('file updated!');
// broadcast file update news
io.emit('file_update', { for: 'everyone' });
}
});
}
app.get('/replay', function(req, res) {
if (used_path == 'file') {
if (file_path == '') {
res.sendStatus(404);
} else {
res.sendFile(file_path);
}
} else if (used_path == 'url') {
if (url_path == '') {
res.sendStatus(404);
} else {
http_get(url_path, function(resp) {
resp.pipe(res);
});
}
} else if (used_path == 'upload') {
res.sendFile(path.join(__dirname, '.tmp_replay'), {dotfiles: 'allow'});
} else {
res.sendStatus(404);
}
});
app.get('/replay_path', function(req, res) {
if (used_path == 'file') {
res.send(file_path);
} else if (used_path == 'url') {
res.send(url_path);
} else if (used_path == 'upload') {
res.send(file_path);
} else {
res.sendStatus(404);
}
});
app.get('/set_replay', function(req, res) {
var fpath = (new Buffer(req.url.split('?')[1], 'base64')).toString('binary');
if (fs.existsSync(fpath)) {
file_path = fpath;
used_path = 'file';
res.sendStatus(200);
watch_file(file_path);
} else {
res.sendStatus(404);
}
});
app.get('/set_replay_url', function(req, res) {
var upath = (new Buffer(req.url.split('?')[1], 'base64')).toString('binary');
// very basic sanitisation.
if (upath.startsWith("https://")) {
// this is a somewhat dodgy way of doing it,
// someone feel free to make it less dodgy
// in the future
url_path = upath.substring(0, 4) + upath.substring(5);
used_path = 'url';
res.sendStatus(200);
} else if (upath.startsWith("http://")) {
url_path = upath;
used_path = 'url';
res.sendStatus(200);
} else {
res.sendStatus(404);
}
})
app.post('/upload_replay', function(req, res) {
if(req.busboy) {
req.pipe(req.busboy);
req.busboy.on("file", function(fieldName, fileStream, fileName, encoding, mimeType) {
file_path = fileName;
used_path = 'upload';
// save to disk at: .tmp_replay
var fstr = fs.createWriteStream(path.join(__dirname, '.tmp_replay'));
fileStream.pipe(fstr);
res.redirect('/')
});
} else {
res.redirect('/settings');
}
});
app.get('/version', function(req, res) {
res.send(JSON.stringify(versions));
});
app.get('/set_version', function(req, res) {
var version = req.url.split('?')[1];
console.log('switching to version: ' + version);
npm.load(function(err) {
npm.commands.install(['bc19@' + version], function(er, data){
if (er) {
res.sendStatus(404);
} else {
delete require.cache[require.resolve('bc19/package')]
var pkg = require('bc19/package');
versions.curr = pkg.version;
res.sendStatus(200);
}
});
});
});
io.on('connection', function(socket){ });
http.listen(port, () => console.log(`Running on port ${port}!`))