-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
71 lines (56 loc) · 2.01 KB
/
worker.js
File metadata and controls
71 lines (56 loc) · 2.01 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
export default {
async fetch(request) {
const url = new URL(request.url);
const path = url.pathname;
const TARGETS = {
"/root": "https://api.genius.com/",
"/public": "https://genius.com/api/",
"/web": "https://genius.com/"
};
let targetKey = null;
const sortedKeys = Object.keys(TARGETS).sort((a, b) => b.length - a.length);
for (const key of sortedKeys) {
if (path.startsWith(key)) {
targetKey = key;
break;
}
}
if (!targetKey) {
return new Response("Not Found", { status: 404 });
}
const upstreamBase = TARGETS[targetKey];
const remainingPath = path.slice(targetKey.length);
const cleanBase = upstreamBase.replace(/\/+$/, "");
const cleanPath = remainingPath.replace(/^\/+/, "");
const destinationUrl = new URL(`${cleanBase}/${cleanPath}${url.search}`);
const proxyHeaders = new Headers(request.headers);
proxyHeaders.set("Host", destinationUrl.hostname);
const hopByHopHeaders = [
"keep-alive", "transfer-encoding", "te", "connection", "trailer", "upgrade",
"proxy-authorization", "proxy-authenticate"
];
hopByHopHeaders.forEach(h => proxyHeaders.delete(h));
const method = request.method.toUpperCase();
const hasBody = !["GET", "HEAD"].includes(method);
const proxyRequest = new Request(destinationUrl, {
method: method,
headers: proxyHeaders,
body: hasBody ? request.body : null,
redirect: "manual"
});
try {
const response = await fetch(proxyRequest);
const responseHeaders = new Headers(response.headers);
responseHeaders.delete("content-encoding");
responseHeaders.delete("content-length");
hopByHopHeaders.forEach(h => responseHeaders.delete(h));
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: responseHeaders
});
} catch (e) {
return new Response("Proxy Error: Bad Gateway", { status: 502 });
}
},
};