|
| 1 | +const zlib = require("zlib"); |
| 2 | +const fs = require("fs"); |
| 3 | + |
| 4 | +const MIME_TYPES: Record<string, string> = { |
| 5 | + ".css": "text/css", ".js": "application/javascript", ".html": "text/html", |
| 6 | + ".woff2": "font/woff2", ".svg": "image/svg+xml", ".webp": "image/webp", ".json": "application/json", |
| 7 | +}; |
| 8 | + |
| 9 | +// Pre-load dataset |
| 10 | +const data = JSON.parse(fs.readFileSync("/data/dataset.json", "utf8")); |
| 11 | +const items = data.map((d: any) => ({ |
| 12 | + id: d.id, name: d.name, category: d.category, |
| 13 | + price: d.price, quantity: d.quantity, active: d.active, |
| 14 | + tags: d.tags, rating: d.rating, |
| 15 | + total: Math.round(d.price * d.quantity * 100) / 100, |
| 16 | +})); |
| 17 | +const jsonResponseBuf = Buffer.from(JSON.stringify({ items, count: items.length })); |
| 18 | + |
| 19 | +// Pre-load large dataset for /compression endpoint (compressed per-request) |
| 20 | +const largeData = JSON.parse(fs.readFileSync("/data/dataset-large.json", "utf8")); |
| 21 | +const largeItems = largeData.map((d: any) => ({ |
| 22 | + id: d.id, name: d.name, category: d.category, |
| 23 | + price: d.price, quantity: d.quantity, active: d.active, |
| 24 | + tags: d.tags, rating: d.rating, |
| 25 | + total: Math.round(d.price * d.quantity * 100) / 100, |
| 26 | +})); |
| 27 | +const largeJsonBuf = Buffer.from(JSON.stringify({ items: largeItems, count: largeItems.length })); |
| 28 | + |
| 29 | +// Pre-load static files |
| 30 | +const staticFiles: Record<string, { buf: Buffer; ct: string }> = {}; |
| 31 | +try { |
| 32 | + for (const name of fs.readdirSync("/data/static")) { |
| 33 | + const buf = fs.readFileSync(`/data/static/${name}`); |
| 34 | + const ext = name.slice(name.lastIndexOf(".")); |
| 35 | + staticFiles[name] = { buf: Buffer.from(buf), ct: MIME_TYPES[ext] || "application/octet-stream" }; |
| 36 | + } |
| 37 | +} catch (_) {} |
| 38 | + |
| 39 | +function sumQuery(url: string): number { |
| 40 | + const q = url.indexOf("?"); |
| 41 | + if (q === -1) return 0; |
| 42 | + let sum = 0; |
| 43 | + const qs = url.slice(q + 1); |
| 44 | + let i = 0; |
| 45 | + while (i < qs.length) { |
| 46 | + const eq = qs.indexOf("=", i); |
| 47 | + if (eq === -1) break; |
| 48 | + let amp = qs.indexOf("&", eq); |
| 49 | + if (amp === -1) amp = qs.length; |
| 50 | + const n = parseInt(qs.slice(eq + 1, amp), 10); |
| 51 | + if (!isNaN(n)) sum += n; |
| 52 | + i = amp + 1; |
| 53 | + } |
| 54 | + return sum; |
| 55 | +} |
| 56 | + |
| 57 | +function handleRequest(req: Request): Response | Promise<Response> { |
| 58 | + const url = req.url; |
| 59 | + const protoEnd = url.indexOf("//") + 2; |
| 60 | + const pathStart = url.indexOf("/", protoEnd); |
| 61 | + const qIdx = url.indexOf("?", pathStart); |
| 62 | + const path = qIdx === -1 ? url.slice(pathStart) : url.slice(pathStart, qIdx); |
| 63 | + |
| 64 | + if (path === "/pipeline") { |
| 65 | + return new Response("ok", { headers: { "content-type": "text/plain" } }); |
| 66 | + } |
| 67 | + |
| 68 | + if (path === "/json") { |
| 69 | + return new Response(jsonResponseBuf, { |
| 70 | + headers: { "content-type": "application/json", "content-length": String(jsonResponseBuf.length) }, |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + if (path === "/compression") { |
| 75 | + const compressed = Bun.gzipSync(largeJsonBuf, { level: 1 }); |
| 76 | + return new Response(compressed, { |
| 77 | + headers: { |
| 78 | + "content-type": "application/json", |
| 79 | + "content-encoding": "gzip", |
| 80 | + "content-length": String(compressed.length), |
| 81 | + }, |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + if (path === "/baseline2") { |
| 86 | + const body = String(sumQuery(url)); |
| 87 | + return new Response(body, { headers: { "content-type": "text/plain" } }); |
| 88 | + } |
| 89 | + |
| 90 | + if (path.startsWith("/static/")) { |
| 91 | + const name = path.slice(8); |
| 92 | + const sf = staticFiles[name]; |
| 93 | + if (sf) { |
| 94 | + return new Response(sf.buf, { |
| 95 | + headers: { "content-type": sf.ct, "content-length": String(sf.buf.length) }, |
| 96 | + }); |
| 97 | + } |
| 98 | + return new Response("Not found", { status: 404 }); |
| 99 | + } |
| 100 | + |
| 101 | + if (path === "/upload" && req.method === "POST") { |
| 102 | + return req.arrayBuffer().then((ab) => { |
| 103 | + const buf = Buffer.from(ab); |
| 104 | + const c = zlib.crc32(buf); |
| 105 | + return new Response((c >>> 0).toString(16).padStart(8, "0"), { |
| 106 | + headers: { "content-type": "text/plain" }, |
| 107 | + }); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + // /baseline11 — GET or POST |
| 112 | + const querySum = sumQuery(url); |
| 113 | + if (req.method === "POST") { |
| 114 | + return req.text().then((body) => { |
| 115 | + let total = querySum; |
| 116 | + const n = parseInt(body.trim(), 10); |
| 117 | + if (!isNaN(n)) total += n; |
| 118 | + return new Response(String(total), { headers: { "content-type": "text/plain" } }); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + return new Response(String(querySum), { headers: { "content-type": "text/plain" } }); |
| 123 | +} |
| 124 | + |
| 125 | +// Read TLS certs |
| 126 | +let tlsOptions: { cert: string; key: string } | undefined; |
| 127 | +try { |
| 128 | + tlsOptions = { |
| 129 | + cert: fs.readFileSync("/certs/server.crt", "utf8"), |
| 130 | + key: fs.readFileSync("/certs/server.key", "utf8"), |
| 131 | + }; |
| 132 | +} catch (_) {} |
| 133 | + |
| 134 | +// HTTP server on port 8080 |
| 135 | +Bun.serve({ |
| 136 | + port: 8080, |
| 137 | + fetch: handleRequest, |
| 138 | + reusePort: true, |
| 139 | +}); |
| 140 | + |
| 141 | +// HTTPS/H2 server on port 8443 |
| 142 | +if (tlsOptions) { |
| 143 | + Bun.serve({ |
| 144 | + port: 8443, |
| 145 | + tls: tlsOptions, |
| 146 | + fetch: handleRequest, |
| 147 | + reusePort: true, |
| 148 | + }); |
| 149 | +} |
0 commit comments