-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathlibrary.ts
More file actions
121 lines (108 loc) · 3.44 KB
/
library.ts
File metadata and controls
121 lines (108 loc) · 3.44 KB
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
import {isValidSrcUrl} from "./cd-rom";
import {type RequestInit as CloudflareWorkerRequestInit} from "@cloudflare/workers-types/2021-11-03";
export async function handleRequest(request: Request) {
if (request.method !== "GET") {
return errorResponse("Method not allowed", 405);
}
const url = new URL(request.url);
const path = url.pathname.split("/").slice(2).join("/");
switch (path) {
case "proxy":
return await handleProxy(url);
case "details":
return await handleDetails(url);
default:
return errorResponse("Unknown library path", 404);
}
}
async function handleDetails(url: URL) {
const id = url.searchParams.get("id");
if (!id) {
return errorResponse("id parameter is required");
}
const type = url.searchParams.get("type");
if (!type) {
return errorResponse("id parameter is required");
}
if (!details) {
// eslint-disable-next-line require-atomic-updates
details = await loadLibraryDetails();
}
return new Response(JSON.stringify(details[type][id]), {
headers: {
"Content-Type": "application/json",
},
});
}
let details: {[type: string]: {[id: string]: LibraryDetailsItem}};
type LibraryDetailsItem = {[key: string]: any};
async function loadLibraryDetails() {
console.time("Load library details");
const {default: libraryDetailsExport} = (await import(
"./Library-details.json"
)) as {default: string | object};
const libraryDetails = typeof libraryDetailsExport === "object" ?
libraryDetailsExport :
JSON.parse(libraryDetailsExport);
const unpacked = Object.fromEntries(
Object.entries(libraryDetails).map(([type, items]) => [
type,
unpackDetailsItems(items as any[]),
])
);
console.timeEnd("Load library details");
return unpacked;
}
function unpackDetailsItems(detailsItems: any[]) {
return Object.fromEntries(
detailsItems.map((detailItem, i) => {
const [
url,
files,
description,
screenshots,
manuals,
composers,
externalDownloadUrl,
] = detailItem;
return [
i,
{
url,
files: files ?? {},
description,
screenshots: screenshots ?? [],
manuals: manuals ?? {},
composers: composers ?? [],
externalDownloadUrl,
},
];
})
);
}
async function handleProxy(url: URL) {
const srcUrl = url.searchParams.get("url");
if (!srcUrl) {
return errorResponse("url parameter is required");
}
if (!isValidSrcUrl(srcUrl)) {
return errorResponse("Unexpected Library src URL: " + srcUrl);
}
const requestInit: CloudflareWorkerRequestInit = {
headers: {
"User-Agent": "Infinite Mac (+https://infinitemac.org)",
},
cf: {
cacheEverything: true,
cacheTtl: 30 * 24 * 60 * 60,
},
};
return fetch(srcUrl, requestInit as RequestInit);
}
function errorResponse(message: string, status: number = 400): Response {
return new Response(message, {
status,
statusText: message,
headers: {"Content-Type": "text/plain"},
});
}