-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
54 lines (49 loc) · 1.64 KB
/
server.ts
File metadata and controls
54 lines (49 loc) · 1.64 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
import { serve } from "bun";
import datastar from "./pages/datastar.html";
import datastarReplace from "./pages/datastar-replace.html";
import htmx from "./pages/htmx.html";
import idiomorph from "./pages/idiomorph.html";
import jquery from "./pages/jquery.html";
import vanilla from "./pages/vanilla.html";
function fixture(size: "small" | "big" | "small-replace" | "big-replace") {
const path = `./fixtures/${size}.html`;
// Cached in memory by Bun
return Bun.file(path).text();
}
serve({
development: true,
routes: {
"/jquery": jquery,
"/htmx": htmx,
"/datastar": datastar,
"/datastar-replace": datastarReplace,
"/idiomorph": idiomorph,
"/vanilla": vanilla,
"/common.js": () => new Response(Bun.file("./pages/common.js"), { headers: { "content-type": "text/javascript" } }),
// Endpoint the pages will call
"/snippet/:size": async (req) => {
const { size } = req.params; // "small" | "big"
const html = await fixture(size as any);
return new Response(html, {
headers: {
"content-type": "text/html",
"datastar-selector": "#target",
"datastar-mode": "inner",
}
});
},
// Endpoint the pages will call
"/snippet-replace/:size": async (req) => {
const { size } = req.params; // "small" | "big"
const html = await fixture(`${size}-replace`);
return new Response(html, {
headers: {
"content-type": "text/html",
"datastar-selector": "#target",
"datastar-mode": "replace",
}
});
}
}
});
console.log("Bench server listening:", Bun.env.BUN_ORIGIN ?? "http://localhost:3000");