This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (85 loc) · 2.88 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
'use strict';
const Express = require('express');
const Axios = require('axios');
const NodeCache = require('node-cache');
const LAUNCHER_META_ENDPOINT = 'https://launchermeta.mojang.com';
const VERSION_MANIFEST_URI = `${LAUNCHER_META_ENDPOINT}/mc/game/version_manifest.json`;
const CACHE_MANIFEST = 'versionManifest';
const CACHE_DESCRIPTOR = 'descriptor';
const cache = new NodeCache({
stdTTL: parseInt(process.env.CACHE_TTL || 600)
});
const client = Axios.create();
const app = Express();
async function getVersionManifest() {
const cached = cache.get(CACHE_MANIFEST);
if (cached) {
return cached;
}
const manifest = (await client.get(VERSION_MANIFEST_URI))?.data;
if (!manifest) {
throw new Error('Null manifest!');
}
cache.set(CACHE_MANIFEST, manifest);
return manifest;
}
async function getVersionDescriptor(version) {
const cached = cache.get(`${CACHE_DESCRIPTOR}${version}`);
if (cached) {
return cached;
}
const manifest = await getVersionManifest();
const descriptorUrl = manifest.versions.find(current => current.id === version)?.url;
if (!descriptorUrl) {
throw new Error(`Unknown version ${version}`);
}
const descriptor = (await client.get(descriptorUrl))?.data;
if (!descriptor) {
throw new Error('Null descriptor!');
}
cache.set(`${CACHE_DESCRIPTOR}${version}`, descriptor);
return descriptor;
}
app.get('/minecraft/version/:version', async (request, response) => {
let version;
if (['release', 'snapshot'].includes(request.params.version)) {
const manifest = await getVersionManifest();
if (request.params.version === 'release') {
version = manifest.latest.release;
} else if (request.params.version === 'snapshot') {
version = manifest.latest.release;
}
} else {
version = request.params.version;
}
const descriptor = await getVersionDescriptor(version);
if (!descriptor) {
response.status(404);
response.send(`No such version '${request.params.version}'`);
return;
}
response.json(descriptor);
});
app.get('/minecraft/version/:version/download/:download', async (request, response) => {
const manifest = await getVersionManifest();
let version;
switch (request.params.version) {
case 'release':
version = manifest.latest.release;
break;
case 'snapshot':
version = manifest.latest.release;
break;
default:
version = request.params.version;
}
const descriptor = await getVersionDescriptor(version);
const downloadUrl = descriptor.downloads[request.params.download]?.url;
if (!downloadUrl) {
response.status(404);
response.send(`No such download '${request.params.download}'`);
return;
}
response.redirect(downloadUrl);
});
app.listen(process.env.PORT || 3000);