-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
30 lines (27 loc) · 839 Bytes
/
proxy.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
/**
* An object with different URLs to fetch
* @param {Object} ORIGINS
*/
const ORIGINS = {
"api.tweeter.workers.dev": "api.twitter.com",
// "root.tweeter.workers.dev": "twitter.com"
}
async function handleRequest(request) {
const url = new URL(request.url)
// Check if incoming hostname is a key in the ORIGINS object
if (url.hostname in ORIGINS) {
const target = ORIGINS[url.hostname]
url.hostname = target
request = new Request(url, request)
request.headers.set("Origin", new URL(url).origin)
// If it is, proxy request to that third party origin
return await fetch(url.toString(), request)
}
else {
url.hostname = url.searchParams.get("host")
return await fetch(url.toString(), request)
}
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})