forked from BaliBalo/ScoreSaber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (154 loc) · 5.83 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
const fs = require('fs');
const path = require('path');
const express = require('express');
const rateLimit = require('express-rate-limit');
const cookieParser = require('cookie-parser');
const request = require('request');
const CronJob = require('cron').CronJob;
const { timetag } = require('./utils');
const auth = require('./utils/auth');
const ranked = require('./utils/ranked');
const rankedUpdate = require('./utils/ranked/update');
const checkNew = require('./utils/ranked/check-new');
const removeUnranks = require('./utils/ranked/remove-unranks');
const removeDupes = require('./utils/ranked/remove-dupes');
const removePartial = require('./utils/ranked/remove-partial');
const updateScoresaberValues = require('./utils/ranked/update-scoresaber-values');
const updateStats = require('./utils/ranked/update-stats');
const Playlist = require('./utils/playlist');
const top200 = require('./utils/top200');
const app = express();
const port = 2148;
fs.mkdirSync(path.resolve(__dirname, 'data'), { recursive: true });
app.use(cookieParser());
app.set('etag', 'strong');
// Enable only on specific endpoints for which we want cross-origin requests allowed (none for now)
// const cors = require('cors');
// app.use(cors());
let serveStaticFiles = express.static('client', { extensions: ['html'], cacheControl: false });
app.use(['/client'], serveStaticFiles);
app.get(['/favicon.ico', '/robots.txt'], serveStaticFiles);
// Proxy requests (to avoid front-end cross-origin requests issues)
app.use('/proxy', (req, res) => {
let srcStream = req.pipe(request('https://scoresaber.com' + req.url));
srcStream.on('error', error => {
res.status(502).send(error.message);
});
return srcStream.pipe(res);
});
// Custom urls - if string, makes a client file available at root
// e.g. 'thing' makes `client/thing.html` available at `/thing`
[
{ url: '/', file: 'pages/index.html' },
'peepee',
'overlay',
'playlist-maker',
].forEach(data => {
if (typeof data === 'string') {
data = { url: '/' + data, file: 'pages/' + data + '.html' };
}
if (!data.url || !data.file) {
return;
}
if (process.argv.includes('--dev')) {
app.get(data.url, async (req, res) => {
try {
let content = await fs.promises.readFile(path.resolve(__dirname, data.file), 'utf8');
content = content.replace(/\/client\/([^"' /]+)\.min\.js/g, '/dev/pages/$1.js');
res.send(content);
} catch(e) {
console.error(e);
res.sendStatus(500);
}
});
} else {
app.get(data.url, (req, res) => res.sendFile(path.resolve(__dirname, data.file)));
}
});
app.get('/ranked', async (req, res) => {
// caching already handled by nginx
let timestamp = await rankedUpdate.getTime();
let after = req.get('If-Modified-Since');
if (after && timestamp <= +new Date(after)) {
return res.status(304).end();
}
res.append('Last-Modified', new Date(timestamp).toUTCString());
let list = await ranked.find({});
res.send({ list, timestamp });
});
top200.autoUpdate();
app.get(/^\/top-?200(\.bplist)?/, async (req, res) => {
let { at, data } = await top200.get();
res.append('Last-Modified', at);
res.append('Content-Disposition', 'attachment; filename="top200.bplist"');
res.send(data);
});
const customPlaylist = new Playlist({ author: 'Peepee' });
let customPlaylistImage = customPlaylist.setImageFromFile(path.resolve(__dirname, 'client/scoresaber.png'));
app.get('/custom-playlist/:filename', async (req, res) => {
let list = [];
if (req.query.s) {
list = req.query.s.split('.').filter(e => e);
} else if (req.query.i) {
let uids = req.query.i.split('.').map(e => +e).filter(e => e);
let fromDb = await ranked.find({ uid: { $in: uids } });
let maps = fromDb.reduce((o, e) => {
o[e.uid] = e.id;
return o;
}, {});
list = uids.map(e => maps[e]).filter(e => e);
}
if (!list || !list.length) {
return res.status(400).end();
}
await customPlaylistImage;
// Make a copy to ensure no weird race conditions
let pl = new Playlist(customPlaylist);
if (req.query.t) {
pl.title = req.query.t;
}
pl.setSongsFromHashes(list);
res.append('Content-Disposition', 'attachment; filename="' + req.params.filename.replace(/"/g, '_') + '"');
res.set('Cache-Control', 'no-store');
res.send(pl.toJSON());
});
const execTask = (fn, ...args) => async (req, res) => {
try {
console.log(timetag(), 'Manually executing task from ' + req.baseUrl + req.path);
let result = await fn(...args);
console.log(timetag(), 'Finished executing ' + req.baseUrl + req.path);
let message = 'Done';
if (result !== undefined) {
message += ' - ' + result;
}
res.send(message);
} catch(e) {
console.log(timetag(), 'Error during task', e);
res.status(500).send('Error');
}
};
const execLongTask = (fn, ...args) => execTask(() => {
// do not await the long task
fn(...args);
});
const limiter = rateLimit({
windowMs: 5 * 60 * 1000,
max: 100
});
app.all('/admin/check-new', limiter, auth.check, execTask(checkNew, true));
app.all('/admin/check-new/full', limiter, auth.check, execTask(checkNew.full, true));
app.all('/admin/remove-unranks', limiter, auth.check, execTask(removeUnranks));
app.all('/admin/remove-dupes', limiter, auth.check, execTask(removeDupes));
app.all('/admin/remove-partial', limiter, auth.check, execTask(removePartial));
app.all('/admin/update-scoresaber-values', limiter, auth.check, execTask(updateScoresaberValues));
app.all('/admin/update-stats', limiter, auth.check, execLongTask(updateStats, true));
if (process.argv.includes('--dev')) {
app.use(['/dev/pages'], express.static('pages', { cacheControl: false }));
} else {
new CronJob('0 */5 * * * *', checkNew, null, true);
new CronJob('0 6 23 * * *', checkNew.full, null, true);
new CronJob('0 1 */6 * * *', removeUnranks, null, true);
new CronJob('0 7 0 * * *', updateStats, null, true);
new CronJob('0 12 0 * * *', updateScoresaberValues, null, true);
}
app.listen(port, () => console.log(timetag(), 'Scoresaber server listening (port ' + port + ')'));