Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
291 lines (257 loc) · 9.71 KB
/
Copy pathserver.ts
File metadata and controls
291 lines (257 loc) · 9.71 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { existsSync, statSync } from "node:fs";
import { join, normalize } from "node:path";
import { anonDistinctId, captureEvent } from "./convex/lib/analytics";
const port = Number(process.env.PORT || 3000);
const distDir = join(process.cwd(), "dist");
const siteCacheControl = "public, max-age=300, s-maxage=86400";
function normalizeBaseDomain(baseDomain: string): string {
return baseDomain
.replace(/^https?:\/\//i, "")
.split("/")[0]
.split(":")[0]
.trim()
.toLowerCase();
}
function configuredAppHostnames(): Set<string> {
const explicit = process.env.APP_HOSTNAMES;
const baseDomain = normalizeBaseDomain(
process.env.SITE_BASE_DOMAIN || process.env.WEBVH_DOMAIN || "boop.ad"
);
const values = explicit
? explicit.split(",")
: [baseDomain, `www.${baseDomain}`, "localhost", "127.0.0.1", "0.0.0.0"];
return new Set(values.map((value) => normalizeBaseDomain(value)).filter(Boolean));
}
const appHostnames = configuredAppHostnames();
function hostnameFromRequest(request: Request): string {
// Cloudflare for SaaS proxies customer-domain traffic to the boop.ad
// Railway origin. Railway routes by Host header, so a Cloudflare Origin
// Rule rewrites Host -> boop.ad and a Transform Rule preserves the
// original customer hostname in X-Original-Host. Prefer that header so
// we know which hosted site to serve.
const original = request.headers.get("x-original-host");
if (original) return original.split(":")[0].toLowerCase();
const host = request.headers.get("host") || "";
return host.split(":")[0].toLowerCase();
}
function isAppHostname(hostname: string): boolean {
return appHostnames.has(hostname);
}
function contentTypeFor(pathname: string): string {
if (pathname.endsWith(".html")) return "text/html; charset=utf-8";
if (pathname.endsWith(".js")) return "application/javascript; charset=utf-8";
if (pathname.endsWith(".css")) return "text/css; charset=utf-8";
if (pathname.endsWith(".json")) return "application/json; charset=utf-8";
if (pathname.endsWith(".xml")) return "application/xml; charset=utf-8";
if (pathname.endsWith(".png")) return "image/png";
if (pathname.endsWith(".svg")) return "image/svg+xml";
if (pathname.endsWith(".ico")) return "image/x-icon";
if (pathname.endsWith(".webmanifest")) return "application/manifest+json";
return "application/octet-stream";
}
function safeDistPath(pathname: string): string {
const requested = pathname === "/" ? "/index.html" : pathname;
const normalized = normalize(requested).replace(/^(\.\.(\/|\\|$))+/, "");
return join(distDir, normalized);
}
async function serveApp(request: Request): Promise<Response> {
const url = new URL(request.url);
let filePath = safeDistPath(url.pathname);
if (existsSync(filePath) && statSync(filePath).isDirectory()) filePath = join(filePath, "index.html");
if (!existsSync(filePath)) {
filePath = join(distDir, "index.html");
}
const file = Bun.file(filePath);
if (!(await file.exists())) {
return new Response("boop has not been built yet. Run `bun run build` first.", {
status: 503,
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
return new Response(file, {
headers: {
"Content-Type": contentTypeFor(filePath),
},
});
}
async function resolveHostedSite(hostname: string) {
const convexHttpUrl = process.env.CONVEX_HTTP_URL || process.env.VITE_CONVEX_HTTP_URL;
if (!convexHttpUrl) {
return null;
}
const response = await fetch(
`${convexHttpUrl}/api/sites/resolve-host?hostname=${encodeURIComponent(hostname)}`
);
if (!response.ok && response.status !== 404) {
throw new Error(`Host resolve failed with ${response.status}`);
}
return response.json() as Promise<{
status: "active" | "missing" | "pending" | "redirect" | "error";
hostname: string;
location?: string;
bucketUrl?: string;
contentType?: string;
didLogJsonl?: string;
siteId?: string;
did?: string;
scid?: string;
}>;
}
function clientIpFromRequest(request: Request): string {
return (
request.headers.get("cf-connecting-ip") ||
request.headers.get("x-real-ip") ||
request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ||
"unknown"
);
}
async function resolveSiteAssetUrl(
hostname: string,
fileName: string
): Promise<{ status: "active" | "missing" | "pending"; url?: string; contentType?: string }> {
const convexHttpUrl = process.env.CONVEX_HTTP_URL || process.env.VITE_CONVEX_HTTP_URL;
if (!convexHttpUrl) return { status: "missing" };
const response = await fetch(
`${convexHttpUrl}/api/sites/resolve-asset?hostname=${encodeURIComponent(hostname)}&fileName=${encodeURIComponent(fileName)}`
);
if (response.status === 404) return { status: "missing" };
if (!response.ok) throw new Error(`Asset resolve failed with ${response.status}`);
return response.json() as Promise<{
status: "active" | "missing" | "pending";
url?: string;
contentType?: string;
siteId?: string;
}>;
}
function missingSitePage(hostname: string): Response {
return new Response(
`<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Not found on boop</title><style>body{font-family:ui-sans-serif,system-ui,sans-serif;margin:0;min-height:100vh;display:grid;place-items:center;background:#fafaf7;color:#292524}.wrap{max-width:34rem;padding:2rem}h1{font-size:clamp(2rem,8vw,4rem);line-height:1;margin:0 0 1rem}p{font-size:1.05rem;color:#57534e;line-height:1.6}</style></head><body><main class="wrap"><h1>Nothing here yet.</h1><p><strong>${hostname}</strong> does not exist on boop, or it is still waking up. Check the link and try again.</p></main></body></html>`,
{
status: 404,
headers: { "Content-Type": "text/html; charset=utf-8" },
}
);
}
async function serveHostedSiteAsset(
request: Request,
hostname: string,
fileName: string
): Promise<Response> {
const asset = await resolveSiteAssetUrl(hostname, fileName);
if (asset.status === "active" && asset.url) {
void trackAssetView(request, hostname, fileName, asset).catch(() => {});
return Response.redirect(asset.url, 302);
}
return missingSitePage(hostname);
}
async function trackSiteView(
request: Request,
hostname: string,
site: { siteId?: string; did?: string; scid?: string; contentType?: string }
): Promise<void> {
const ip = clientIpFromRequest(request);
const ua = request.headers.get("user-agent") ?? "";
const referrer = request.headers.get("referer") ?? "";
const distinctId = await anonDistinctId(ip, ua);
await captureEvent({
event: "site_view",
distinctId,
properties: {
$current_url: `https://${hostname}/`,
$referrer: referrer,
$ip: ip,
$useragent: ua,
site_id: site.siteId,
site_hostname: hostname,
site_did: site.did,
site_scid: site.scid,
content_type: site.contentType,
},
});
}
async function trackAssetView(
request: Request,
hostname: string,
fileName: string,
asset: { siteId?: string; contentType?: string }
): Promise<void> {
const ip = clientIpFromRequest(request);
const ua = request.headers.get("user-agent") ?? "";
const referrer = request.headers.get("referer") ?? "";
const distinctId = await anonDistinctId(ip, ua);
await captureEvent({
event: "site_asset_view",
distinctId,
properties: {
$current_url: `https://${hostname}/_assets/${fileName}`,
$referrer: referrer,
$ip: ip,
$useragent: ua,
site_id: asset.siteId,
site_hostname: hostname,
file_name: fileName,
content_type: asset.contentType,
},
});
}
async function serveHostedSite(request: Request, hostname: string): Promise<Response> {
const url = new URL(request.url);
if (url.pathname.startsWith("/_assets/")) {
const fileName = decodeURIComponent(url.pathname.slice("/_assets/".length));
if (!fileName || fileName.includes("/")) return missingSitePage(hostname);
return serveHostedSiteAsset(request, hostname, fileName);
}
const site = await resolveHostedSite(hostname);
if (!site || site.status === "missing" || site.status === "error") {
return missingSitePage(hostname);
}
if (site.status === "pending") {
return new Response("This boop site is still getting ready.", {
status: 202,
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
if (site.status === "redirect" && site.location) {
return Response.redirect(site.location, 302);
}
if (url.pathname === "/.well-known/did.jsonl") {
return new Response(site.didLogJsonl || "", {
headers: {
"Content-Type": "application/jsonl; charset=utf-8",
"Cache-Control": "public, max-age=60",
},
});
}
if (url.pathname === "/") {
if (!site.bucketUrl) return missingSitePage(hostname);
const bucketResponse = await fetch(site.bucketUrl);
if (!bucketResponse.ok) return missingSitePage(hostname);
void trackSiteView(request, hostname, site).catch(() => {});
return new Response(bucketResponse.body, {
headers: {
"Content-Type": site.contentType || "text/html; charset=utf-8",
"Cache-Control": siteCacheControl,
},
});
}
return missingSitePage(hostname);
}
Bun.serve({
port,
async fetch(request) {
try {
const hostname = hostnameFromRequest(request);
if (isAppHostname(hostname)) {
return serveApp(request);
}
return serveHostedSite(request, hostname);
} catch (error) {
console.error("[server] request failed", error);
return new Response("boop tripped while serving this. Try again in a moment.", {
status: 500,
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
},
});
console.log(`boop server listening on :${port}`);