Skip to content

Instantly share code, notes, and snippets.

@kt3k
Last active May 26, 2021 08:01
Show Gist options
  • Save kt3k/16531691a8533a0f965582a48b3b1922 to your computer and use it in GitHub Desktop.
Save kt3k/16531691a8533a0f965582a48b3b1922 to your computer and use it in GitHub Desktop.
const dirData: Record<string, Uint8Array> = {};
dirData["/bar.ts"] = [Uint8Array.from(atob("Y29uc29sZS5sb2coImJhciIpOwo="), (c) => c.charCodeAt(0)), "text/typescript"];
dirData["/foo.txt"] = [Uint8Array.from(atob("Zm9vCg=="), (c) => c.charCodeAt(0)), "text/plain"];
addEventListener("fetch", (e) => {
const { pathname } = new URL(e.request.url);
const data = dirData[pathname];
if (data) {
const [bytes, mediaType] = data;
e.respondWith(new Response(bytes, { headers: { "content-type": mediaType } }));
return;
}
e.respondWith(new Response("404 Not Found", { status: 404 }));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment