Created
November 22, 2023 06:56
-
-
Save iameli/233950fd8d67769c52d15aa2a3195c6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
// The fetch handler is invoked when this worker receives a HTTP(S) request | |
// and should return a Response (optionally wrapped in a Promise) | |
async fetch(request, env, ctx) { | |
// You'll find it helpful to parse the request.url string into a URL object. Learn more at https://developer.mozilla.org/en-US/docs/Web/API/URL | |
const url = new URL(request.url); | |
const corsHeaders = { | |
"access-control-allow-origin": request.headers.get("origin"), | |
"access-control-allow-headers": "custom-header", | |
} | |
if (request.method === "OPTIONS") { | |
return new Response(null, { | |
headers: corsHeaders, | |
}); | |
} | |
if (!url.pathname.endsWith("redirect")) { | |
return new Response(website, { | |
headers: { | |
...corsHeaders, | |
"content-type": "text/html" | |
} | |
}) | |
} | |
if (url.host === "header-redirect-test.livepeer.workers.dev") { | |
return new Response("Redirect one...", { | |
status: 307, | |
headers: { | |
...corsHeaders, | |
location: "https://redirect-1.livepeer-ac.live/redirect" | |
} | |
}); | |
} | |
if (url.host === "redirect-1.livepeer-ac.live") { | |
return new Response("Redirect two...", { | |
status: 307, | |
headers: { | |
...corsHeaders, | |
location: "https://redirect-2.livepeer.fish/redirect" | |
} | |
}); | |
} | |
const headers = {} | |
for (const [key, value] of [...request.headers]) { | |
headers[key] = value; | |
} | |
const body = JSON.stringify({ | |
"message": "you made it! here are my headers", | |
"headers": headers | |
}, null, 2); | |
return new Response(body, { | |
headers: corsHeaders, | |
}); | |
// return new Response( | |
// `Try making requests to: | |
// <ul> | |
// <li><code><a href="/redirect?redirectUrl=https://example.com/">/redirect?redirectUrl=https://example.com/</a></code>,</li> | |
// <li><code><a href="/proxy?modify&proxyUrl=https://example.com/">/proxy?modify&proxyUrl=https://example.com/</a></code>, or</li> | |
// <li><code><a href="/api/todos">/api/todos</a></code></li>`, | |
// { headers: { "Content-Type": "text/html" } } | |
// ); | |
}, | |
}; | |
const website = ` | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Redirect Thing</title> | |
</head> | |
<body> | |
<h1>Redirect Thing</h1> | |
<button>Activate</button> | |
<pre><code></code></pre> | |
</body> | |
<script> | |
document.querySelector("button").addEventListener("click", async () => { | |
document.querySelector("code").innerHTML = ""; | |
const res = await fetch( | |
"https://header-redirect-test.livepeer.workers.dev/redirect", | |
{ | |
headers: { | |
"custom-header": "custom-value", | |
Cookie: "CustomCookie=ChocolateChip", | |
}, | |
} | |
); | |
const data = await res.json(); | |
console.log(data); | |
document.querySelector("code").innerHTML = JSON.stringify(data, null, 2); | |
}); | |
</script> | |
</html> | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment