This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
399 lines (369 loc) · 12.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
const http = require('http');
const request = require('request');
const async = require('async');
// Log all uncaught exceptions
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
// Create a new server on port 80
http.createServer(function (req, res) {
try {
// Matches the root of the page, e.g. '', '/', '?a=b', '/?a=b'
if (/^\/?(\?.*)?$/gm.exec(req.url) !== null) {
// Redirect to the latest snapshot
return redirectToLatestRelease(res, 'api');
}
// Matches url which looks like /rest-api/latest-version/[release|build]
let match = /^\/rest\/latest-version\/(release|build)\/?(\?.*)?$/gm.exec(req.url);
if (match !== null) {
let versionType = match[1];
// Show the latest version
return showLatestVersion(res, versionType);
}
// Matches url which looks like /rest-api/versions/[release|build]
match = /^\/rest\/versions\/?(\?.*)?$/gm.exec(req.url);
if (match !== null) {
// Show the release versions
return showVersions(res);
}
// Matches url which look like /api and similar
match = /^\/(api|core)\/?(\?.*)?$/gm.exec(req.url);
if (match !== null) {
let type = match[1];
// Redirect to the latest snapshot
return redirectToLatestRelease(res, type);
}
// Matches url which look like /build/latest and similar
match = /^\/(api|core)\/build\/latest\/?/gm.exec(req.url);
if (match !== null) {
let type = match[1];
// Redirect to the latest snapshot
return redirectToLatestSnapshot(res, type);
}
// Matches url which look like /v/latest and similar
match = /^\/(api|core)\/v\/latest\/?/gm.exec(req.url);
if (match !== null) {
let type = match[1];
// Redirect to the latest snapshot
return redirectToLatestRelease(res, type);
}
// Matches urls which start with /build/1234/ (1234 = any build id)
match = /^\/(api|core)\/build\/(\d+)\/?/gm.exec(req.url);
if (match !== null) {
let type = match[1];
let buildId = match[2];
return proxyJavadocsByBuildId(res, req, buildId, type, 'Javacord_PublishSnapshots');
}
// Like above, but without the slash at the end
match = /^\/(api|core)\/build\/(\d+)/gm.exec(req.url);
if (match !== null) {
let type = match[1];
let buildId = match[2];
// We want an url which ends with a slash
return redirect(res, `/${type}/build/${buildId}/`);
}
// Matches urls which start with /v/1.2.3/ (1.2.3 = any version number)
match = /^\/(api|core)\/v\/([\d\\.]+)\//gm.exec(req.url);
if (match !== null) {
let type = match[1];
let versionNumber = match[2];
return proxyJavadocsByVersionNumber(res, req, versionNumber, type);
}
// Like above, but without the slash at the end
match = /^\/(api|core)\/v\/([\d\\.]+)/gm.exec(req.url);
if (match !== null) {
let type = match[1];
let versionNumber = match[2];
// We want an url which ends with a slash
return redirect(res, `/${type}/v/${versionNumber}/`);
}
// If no type/artifact was given, redirect to api
match = /^\/build\/(\d+|latest)/gm.exec(req.url);
if (match !== null) {
let buildId = match[1];
// We want an url which ends with a slash
return redirect(res, `/api/build/${buildId}/`);
}
return render404Page(res);
} catch (e) {
return renderErrorPage(res, `Error: ${e.message}`);
}
}).listen(80);
/**
* Renders a 500 page.
*
* @param res The response to which the site should be sent.
* @param message The message to display.
*/
function renderErrorPage(res, message) {
// TODO make this fancier
res.writeHead(500);
res.write(message);
res.end();
}
/**
* Renders a rest page.
*
* @param res The response to which the site should be sent.
* @param jsonData The data to display.
*/
function renderRestPage(res, jsonData) {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json; charset=utf-8'
});
res.write(JSON.stringify(jsonData));
res.end();
}
/**
* Renders the 404 page.
*
* @param res The response to which the site should be sent.
*/
function render404Page(res) {
// TODO make this fancier
res.writeHead(404);
res.write('Not found :-(');
res.end();
}
/**
* Redirects to the latest snapshot.
*
* @param res The response to redirect.
* @param type The type, either 'api' or 'core'
*/
function redirectToLatestSnapshot(res, type) {
getLatestBuildId(function (error, buildId) {
if (error) {
return renderErrorPage(res, `Error: ${error.message}`);
}
return redirect(res, `/${type}/build/${buildId}/`);
});
}
/**
* Redirects to the latest version.
*
* @param res The response to redirect.
* @param type The type, either 'api' or 'core'
*/
function redirectToLatestRelease(res, type) {
getLatestRelease(function (error, versionNumber) {
if (error) {
return renderErrorPage(res, `Error: ${error.message}`);
}
return redirect(res, `/${type}/v/${versionNumber}/`);
});
}
/**
* Shows the latest version.
*
* @param res The response to redirect.
* @param versionType The version type, either 'release' or 'build'
*/
function showLatestVersion(res, versionType) {
if (versionType.toLowerCase() === 'build') {
getLatestBuildId(function (error, buildId) {
if (error) {
return renderErrorPage(res, `Error: ${error.message}`);
}
return renderRestPage(res, {
build_id: buildId
});
});
} else {
getLatestRelease(function (error, versionNumber) {
if (error) {
return renderErrorPage(res, `Error: ${error.message}`);
}
return renderRestPage(res, {
version: versionNumber
});
});
}
}
/**
* Shows all release versions.
*
* @param res The response to redirect.
*/
function showVersions(res) {
getAllReleases(function (error, versions) {
if (error) {
return renderErrorPage(res, `Error: ${error.message}`);
}
let versionArray = [];
for (let version in versions) {
if (versions.hasOwnProperty(version)) {
versionArray.push(version);
}
}
return renderRestPage(res, versionArray);
});
}
/**
* Redirects to the given url.
*
* @param res The response to redirect.
* @param url The url to which the redirect should point.
*/
function redirect(res, url) {
res.writeHead(302, {
'Location': url,
'Access-Control-Allow-Origin': '*'
});
res.end();
}
/**
* Proxies sites.
*
* @param res The response to which the site should be sent.
* @param req The request.
* @param versionNumber The version number.
* @param type The type, either 'api' or 'core'
*/
function proxyJavadocsByVersionNumber(res, req, versionNumber, type) {
getAllReleases(function (error, versions) {
if (error) {
return renderErrorPage(res, `Error: ${error.message}`);
}
for (let version in versions) {
if (!versions.hasOwnProperty(version)) {
continue;
}
if (version === versionNumber) {
return proxyJavadocsByBuildId(res, req, versions[version], type, 'Javacord_Release');
}
}
render404Page(res);
});
}
/**
* Proxies sites.
*
* @param res The response to which the site should be sent.
* @param req The request.
* @param buildId The build id.
* @param type The type, either 'api' or 'core'
* @param project Either 'Javacord_UpdateCommitStatus' or 'Javacord_Release'
*/
function proxyJavadocsByBuildId(res, req, buildId, type, project) {
async.waterfall([
function (callback) {
let options = {
url: `https://ci.javacord.org/app/rest/builds/id:${buildId}/artifacts/javacord-${type}?guest=1`,
headers: { 'Accept': 'application/json' }
};
request(options, callback);
},
function (response, body, callback) {
if (response.statusCode < 200 || response.statusCode > 299) {
return callback(null, null);
}
let fileName = null;
let jsonBody = JSON.parse(body);
// Search for the javadoc artifact (ends with -javadoc.jar)
for (let i = 0; i < jsonBody.file.length; i++) {
let file = jsonBody.file[i];
if (file.name.endsWith('-javadoc.jar')) {
fileName = file.name;
}
}
callback(null, fileName);
}
], function (error, fileName) {
if (error) {
return renderErrorPage(res, `Error: ${error.message}`);
}
if (fileName === null) {
return render404Page(res);
}
let urlAppendix = req.url.replace(`/${type}/build/${buildId}`, '').replace(/\/(api|core)\/v\/[\d\\.]+/, '');
urlAppendix = urlAppendix === '' || urlAppendix === '/' ? '/index.html' : urlAppendix;
return proxySite(res, `https://ci.javacord.org/repository/download/${project}/${buildId}:id/javacord-${type}/${fileName}%21${urlAppendix}`);
});
}
/**
* Gets all release versions.
*
* @param callback A object, with fields where the key is the version number and the value the build id.
*/
function getAllReleases(callback) {
let options = {
url: 'https://ci.javacord.org/app/rest/builds/?locator=buildType:(id:Javacord_Release),status:SUCCESS&guest=1',
headers: { 'Accept': 'application/json' }
};
request(options, function (error, response, body) {
if (error) {
return callback(error);
}
try {
let versions = {};
let jsonBody = JSON.parse(body);
for (let i = 0; i < jsonBody.build.length; i++) {
let build = jsonBody.build[i];
versions[build.number.split(' ')[0]] = build.id;
}
return callback(null, versions);
} catch (e) {
return callback(e);
}
});
}
/**
* Gets the latest build id.
*
* @param callback The callback.
*/
function getLatestBuildId(callback) {
let options = {
url: 'https://ci.javacord.org/app/rest/builds/buildType:(id:Javacord_PublishSnapshots),status:SUCCESS,branch:development?guest=1',
headers: { 'Accept': 'application/json' }
};
request(options, function (error, response, body) {
if (error) {
return callback(error);
}
try {
return callback(null, JSON.parse(body).id);
} catch (e) {
return callback(e);
}
});
}
/**
* Gets the latest release build id.
*
* @param callback The callback.
*/
function getLatestRelease(callback) {
let options = {
url: 'https://ci.javacord.org/app/rest/builds/buildType:(id:Javacord_Release),status:SUCCESS?guest=1',
headers: { 'Accept': 'application/json' }
};
request(options, function (error, response, body) {
if (error) {
return callback(error);
}
try {
return callback(null, JSON.parse(body).number.split(' ')[0]);
} catch (e) {
return callback(e);
}
});
}
/**
* Proxies the given site.
*
* @param res The response to which the site should be sent.
* @param url The url which should be proxied.
*/
function proxySite(res, url) {
request({
url: url
}, function (error, response, body) {
response.headers['Access-Control-Allow-Origin'] = '*';
res.writeHead(response.statusCode, response.headers);
res.write(body);
res.end();
});
}