diff --git a/.vscode/import_map.json b/.vscode/import_map.json index 86b8a314..88037a45 100644 --- a/.vscode/import_map.json +++ b/.vscode/import_map.json @@ -7,6 +7,8 @@ "preact": "https://esm.sh/preact@10.6.6", "preact/": "https://esm.sh/preact@10.6.6/", - "preact-render-to-string": "https://esm.sh/preact-render-to-string@5.1.20?deps=preact@10.6.6" + "preact-render-to-string": "https://esm.sh/preact-render-to-string@5.1.20?deps=preact@10.6.6", + + "$semver/": "https://deno.land/x/semver@v1.4.0/" } } diff --git a/www/fresh.gen.ts b/www/fresh.gen.ts index 6424093f..dace9fbb 100644 --- a/www/fresh.gen.ts +++ b/www/fresh.gen.ts @@ -5,6 +5,7 @@ import * as $0 from "./routes/docs/[...slug].tsx"; import * as $1 from "./routes/gfm.css.ts"; import * as $2 from "./routes/index.tsx"; +import * as $3 from "./routes/raw.ts"; import * as $$0 from "./islands/Counter.tsx"; import * as $$1 from "./islands/LemonDrop.tsx"; @@ -13,6 +14,7 @@ const manifest = { "./routes/docs/[...slug].tsx": $0, "./routes/gfm.css.ts": $1, "./routes/index.tsx": $2, + "./routes/raw.ts": $3, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/www/import_map.json b/www/import_map.json index 7701760b..7a3b507f 100644 --- a/www/import_map.json +++ b/www/import_map.json @@ -5,6 +5,7 @@ "$twind/": "https://esm.sh/twind@0.16.16/", "preact": "https://esm.sh/preact@10.6.6", "preact/": "https://esm.sh/preact@10.6.6/", - "preact-render-to-string": "https://esm.sh/preact-render-to-string@5.1.20?deps=preact@10.6.6" + "preact-render-to-string": "https://esm.sh/preact-render-to-string@5.1.20?deps=preact@10.6.6", + "$semver/": "https://deno.land/x/semver@v1.4.0/" } } diff --git a/www/routes/raw.ts b/www/routes/raw.ts new file mode 100644 index 00000000..fbc6f6c4 --- /dev/null +++ b/www/routes/raw.ts @@ -0,0 +1,63 @@ +import { RouteConfig } from "$fresh/server.ts"; +import { Handlers } from "$fresh/server.ts"; +import { parse } from "$semver/mod.ts"; +import VERSIONS from "../../versions.json" assert { type: "json" }; + +const BASE_URL = "https://raw.githubusercontent.com/lucacasonato/fresh/"; + +export const handler: Handlers = { + async GET(req, ctx) { + const accept = req.headers.get("Accept"); + const isHTML = accept?.includes("text/html"); + const { version, path } = ctx.params; + + const semver = parse(version, { includePrerelease: true, loose: false }); + if (!semver) { + return new Response("Invalid version", { status: 400 }); + } + + if (!VERSIONS.includes(semver.version)) { + return new Response("Version not found", { status: 404 }); + } + + const url = `${BASE_URL}${semver.version}/${path}`; + const r = await fetch(url, { redirect: "manual" }); + const response = new Response(r.body, r); + response.headers.delete("content-encoding"); + + if (response.status === 404) { + return new Response( + "404: Not Found. The requested fresh release or file do not exist.", + { status: 404 }, + ); + } + + if (!response.ok) { + response.headers.set("Content-Type", "text/plain"); + return response; + } + + if (isHTML) { + response.headers.set("Content-Type", "text/plain"); + } else if (path.endsWith(".ts")) { + response.headers.set("Content-Type", "application/typescript"); + } else if (path.endsWith(".js")) { + response.headers.set("Content-Type", "application/javascript"); + } else if (path.endsWith(".tsx")) { + response.headers.set("Content-Type", "text/tsx"); + } else if (path.endsWith(".jsx")) { + response.headers.set("Content-Type", "text/jsx"); + } else if (path.endsWith(".json")) { + response.headers.set("Content-Type", "application/json"); + } else if (path.endsWith(".wasm")) { + response.headers.set("Content-Type", "application/wasm"); + } else { + response.headers.set("Content-Type", "text/plain"); + } + return response; + }, +}; + +export const config: RouteConfig = { + routeOverride: "/@:version/:path*", +};