-
Notifications
You must be signed in to change notification settings - Fork 6
/
cloudflareworker.js
50 lines (41 loc) · 1.58 KB
/
cloudflareworker.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
const defaultDestination = "jackbox.tv";
const ecastDestination = "ecast.jackboxgames.com";
const ecastAws = "ecast-prod-28687133.us-east-1.elb.amazonaws.com";
const bundleHost = "bundles.jackbox.tv";
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
if (url.pathname.startsWith("/api")) {
const newHeaders = Object.fromEntries([...request.headers]);
delete newHeaders.host;
delete newHeaders.origin;
const req = await fetch(`http://${ecastAws}${url.pathname}`, {
method: request.method,
headers: {
...newHeaders,
host: ecastDestination,
},
});
let text = await req.text();
text = text.replaceAll(bundleHost, `${url.hostname}/bundles`);
text = text.replaceAll(defaultDestination, url.hostname);
text = text.replaceAll(ecastDestination, url.hostname);
return new Response(text, req);
} else {
const bundle = url.pathname.startsWith("/bundles");
const req = await fetch(
`https://${bundle ? bundleHost : defaultDestination}${url.pathname.slice(bundle ? 8 : 0)}`,
request
);
if (['application/javascript', 'text/javascript', 'text/css'].includes(req.headers.get("content-type"))) {
let text = await req.text();
text = text.replaceAll(bundleHost, `${url.hostname}/bundles`);
text = text.replaceAll(defaultDestination, url.hostname);
text = text.replaceAll(ecastDestination, url.hostname);
return new Response(text, req);
}
return req;
}
}