forked from jsr-io/jsr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
executable file
·158 lines (145 loc) · 4.69 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env -S deno run -A --watch
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
const FRONTEND_SERVER = "http://localhost:8000";
const API_SERVER = "http://localhost:8001";
const GCS_ENDPOINT = "http://localhost:4080";
const MODULES_BUCKET = "modules";
const NPM_BUCKET = "npm";
const DOMAIN = "jsr.test";
const API_DOMAIN = "api.jsr.test";
const NPM_DOMAIN = "npm.jsr.test";
const PORT = 80;
async function createBucket(name: string) {
try {
const resp = await fetch("http://localhost:4080/storage/v1/b", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name }),
});
await resp.arrayBuffer();
return resp.ok || resp.status === 409;
} catch {
return false;
}
}
const bucketCreationInterval = setInterval(async () => {
let allBucketsCreated = true;
for (const bucket of [MODULES_BUCKET, "docs", "publishing", NPM_BUCKET]) {
allBucketsCreated &&= await createBucket(bucket);
}
if (allBucketsCreated) {
console.log("All buckets ready.");
clearInterval(bucketCreationInterval);
}
}, 5000);
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
function redirectRoot() {
url.hostname = DOMAIN;
url.port = String(PORT);
return Response.redirect(url.href, 307);
}
switch (url.hostname) {
case DOMAIN: {
if (isCDNRequest(req, url)) {
const file = encodeURIComponent(url.pathname.slice(1));
const res = await fetch(
`${GCS_ENDPOINT}/storage/v1/b/${MODULES_BUCKET}/o/${file}?alt=media`,
{ redirect: "manual", method: req.method, headers: req.headers },
);
return res;
}
if (
url.pathname.startsWith("/api/") ||
url.pathname === "/sitemap.xml" ||
url.pathname === "/sitemap-scopes.xml" ||
url.pathname === "/sitemap-packages.xml" ||
url.pathname === "/login" ||
url.pathname === "/login/callback" ||
url.pathname === "/logout"
) {
const apiUrl = `${API_SERVER}${url.pathname}${url.search}`;
const apiRes = await proxy(req, apiUrl);
return apiRes;
}
const frontendUrl = `${FRONTEND_SERVER}${url.pathname}${url.search}`;
const frontendRes = await proxy(req, frontendUrl);
return frontendRes;
}
case API_DOMAIN: {
const apiUrl = `${API_SERVER}/api${url.pathname}${url.search}`;
const apiRes = await proxy(req, apiUrl);
return apiRes;
}
case NPM_DOMAIN: {
const file = encodeURIComponent(
decodeURIComponent(url.pathname.slice(1)),
);
const res = await fetch(
`${GCS_ENDPOINT}/storage/v1/b/${NPM_BUCKET}/o/${file}?alt=media`,
{ redirect: "manual" },
);
return res;
}
default:
return redirectRoot();
}
}
export function isCDNRequest(req: Request, url: URL): boolean {
return (req.method === "HEAD" || req.method === "GET") &&
url.pathname.startsWith("/@") &&
!req.headers.get("Accept")?.startsWith("text/html") &&
(!req.headers.has("Sec-Fetch-Dest") ||
req.headers.get("Sec-Fetch-Dest") === "empty" ||
((req.headers.get("Sec-Fetch-Dest") === "image" ||
req.headers.get("Sec-Fetch-Dest") === "video") &&
req.headers.get("Sec-Fetch-Site") === "same-origin"));
}
export async function proxy(req: Request, newUrl: string): Promise<Response> {
// If the request is WebSocket, proxy it using WebSocket, relaying all
// messages and closing the connection when one side closes it.
if (req.headers.get("Upgrade") === "websocket") {
const { socket, response } = Deno.upgradeWebSocket(req);
const ws = new WebSocket(newUrl);
const externalOpen = new Promise<void>((resolve) => {
ws.onopen = () => resolve();
});
const internalOpen = new Promise<void>((resolve) => {
socket.onopen = () => resolve();
});
socket.onmessage = (e) => {
externalOpen.then(() => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(e.data);
} else {
ws.close();
socket.close();
}
});
};
ws.onmessage = (e) => {
internalOpen.then(() => {
if (socket.readyState === WebSocket.OPEN) {
socket.send(e.data);
} else {
ws.close();
socket.close();
}
});
};
ws.onclose = () => socket.close();
socket.onclose = () => ws.close();
return response;
} else {
// Otherwise, proxy it using fetch.
return await fetch(newUrl, {
method: req.method,
headers: req.headers,
body: req.body,
redirect: "manual",
});
}
}
if (import.meta.main) {
Deno.serve({ port: PORT, hostname: "0.0.0.0" }, handler);
}