-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
executable file
·57 lines (42 loc) · 1.6 KB
/
server.ts
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env -S deno run -A --unstable --no-check --no-config
import { serve } from "https://deno.land/[email protected]/http/server.ts";
const EMPTY_PATHS = ["", "/"];
const QUACKWARE_NO_MOD_REDIRECT = !!Deno.env.get("QUACKWARE_NO_MOD_REDIRECT");
const QUACKWARE_MOD_REDIRECT_FILENAME = "mod.ts";
const indexFile = await Deno.readTextFile(new URL("./public/index.html", import.meta.url));
const favicon = await Deno.readFile(new URL("./public/favicon.ico", import.meta.url));
serve((req) => {
const url = new URL(req.url);
const { pathname } = url;
if (pathname === "/favicon.ico") {
return new Response(favicon);
}
if (pathname === "/") {
return new Response(indexFile, { headers: { "Content-Type": "text/html" } });
}
const [, repo, ...rest] = pathname.split("/");
if (EMPTY_PATHS.includes(repo)) {
console.log(`Returning error for no repo`);
return new Response(null, { status: 404 });
}
let path = rest.join("/");
if (EMPTY_PATHS.includes(path)) {
if (QUACKWARE_NO_MOD_REDIRECT) {
console.log(
`Returning error for path due to QUACKWARE_NO_MOD_REDIRECT: '${path}'`,
);
return new Response(null, { status: 404 });
} else {
console.log(
`Path '${path}' for repo '${repo}' force redirect to '${QUACKWARE_MOD_REDIRECT_FILENAME}'`,
);
path = QUACKWARE_MOD_REDIRECT_FILENAME;
}
}
console.log(`Serving Path: '${path}'`);
url.hostname = "raw.githubusercontent.com";
url.pathname = `/quackware/${repo}/master/${path}`;
url.port = "";
console.log("Redirecting to:", url.href);
return Response.redirect(url);
});