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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import { parseAuthorizationHeader } from "https://esm.town/v/pomdtr/parseAuthorizationHeader";
export default async function(req: Request) {
const url = new URL(req.url);
const parts = url.pathname.slice(1).split("/");
if (parts.length != 3) {
return new Response("Not Found", { status: 404 });
}
const author = parts[0];
const [name, version] = parts[1].split("@");
const filename = parts[2];
const headers = {};
if (req.headers.get("Authorization")) {
const auth = parseAuthorizationHeader(
req.headers.get("Authorization"),
);
if (auth.type == "basic") {
headers["Authorization"] = `Bearer ${auth.username}`;
}
else {
headers["Authorization"] = req.headers.get("Authorization");
}
}
const resp = await fetch(`https://api.val.town/v1/alias/${author}/${name}`, {
headers,
});
if (!resp.ok) {
return resp;
}
let val = await resp.json();
if (filename == "README.md") {
return new Response(val.readme, {
headers: { "content-type": "text/markdown" },
});
}
if (filename == "mod.js") {
return new Response(val.code, {
headers: {
"Content-Type": "text/javascript",
},
});
}
if (filename == "mod.ts") {
return new Response(val.code, {
headers: {
"Content-Type": "text/typescript",
},
});
}
if (filename == "mod.jsx") {
return new Response(val.code, {
headers: {
"Content-Type": "text/jsx",
},
});
}
if (filename == "mod.tsx") {
return new Response(val.code, {
headers: {
"Content-Type": "text/tsx",
},
});
}
return new Response("Not Found", { status: 404 });
}