feat!: migrate to TypeScript and implement emulate using Miniflare

Signed-off-by: Pascal Vorwerk <info@fossores.de>
This commit is contained in:
2024-01-28 22:16:18 +01:00
commit d4d1742487
10 changed files with 2254 additions and 0 deletions

75
src/worker.ts Normal file
View File

@@ -0,0 +1,75 @@
import { Server } from "SERVER";
import { manifest, prerendered, app_path } from "MANIFEST";
import * as Cache from "worktop/cfw.cache";
import type { Module } from "worktop/cfw";
import type { Durable } from "worktop/cfw.durable";
const server = new Server(manifest);
const immutable = `/${app_path}/immutable/`;
const version_file = `/${app_path}/version.json`;
const worker: Module.Worker<{ ASSETS: Durable.Object }> = {
async fetch(req: Request, env, context): Promise<Response> {
// @ts-ignore
await server.init({ env });
// skip cache if "cache-control: no-cache" in request
let pragma = req.headers.get("cache-control") || "";
let res = !pragma.includes("no-cache") && (await Cache.lookup(req));
if (res) return res;
let { pathname, search } = new URL(req.url);
try {
pathname = decodeURIComponent(pathname);
} catch {
// ignore invalid URI
}
const stripped_pathname = pathname.replace(/\/$/, "");
// prerendered pages and /static files
let is_static_asset = false;
const filename = stripped_pathname.substring(1);
if (filename) {
is_static_asset =
manifest.assets.has(filename) ||
manifest.assets.has(filename + "/index.html");
}
let location =
pathname.at(-1) === "/" ? stripped_pathname : pathname + "/";
if (
is_static_asset ||
prerendered.has(pathname) ||
pathname === version_file ||
pathname.startsWith(immutable)
) {
res = await env.ASSETS.fetch(req);
} else if (location && prerendered.has(location)) {
if (search) location += search;
res = new Response("", {
status: 308,
headers: {
location,
},
});
} else {
// dynamically-generated pages
res = await server.respond(req, {
// @ts-ignore
platform: { env, context, caches, cf: req.cf },
getClientAddress() {
return req.headers.get("cf-connecting-ip")!;
},
});
}
// write to `Cache` only if response is not an error,
// let `Cache.save` handle the Cache-Control and Vary headers
pragma = res.headers.get("cache-control") || "";
return pragma && res.status < 400 ? Cache.save(req, res, context) : res;
},
};
export default worker;