diff benchmark/bun-http-framework-benchmark/src/bun/bun-web-standard.ts @ 185:dfdd66825396

Merged in keep alive changes and mrjunejune changes.
author MrJuneJune <me@mrjunejune.com>
date Fri, 23 Jan 2026 22:22:30 -0800
parents a8976a008a9d
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/benchmark/bun-http-framework-benchmark/src/bun/bun-web-standard.ts	Fri Jan 23 22:22:30 2026 -0800
@@ -0,0 +1,51 @@
+// Using Web Standard API to handle
+// This is likely an real-world implementation
+// if you're using pure Bun HTTP server
+Bun.serve({
+	port: 3000,
+	fetch: async (request) => {
+		const url = new URL(request.url)
+
+		switch (request.method) {
+			case 'GET':
+				switch (url.pathname) {
+					case '/':
+						return new Response('Hi')
+				}
+
+				if (url.pathname.startsWith('/id/')) {
+					const [id, rest] = url.pathname.slice(4).split('/')
+
+					if (!rest)
+						return new Response(
+							`${id} ${url.searchParams.get('name')}`,
+							{
+								headers: {
+									'x-powered-by': 'benchmark'
+								}
+							}
+						)
+				}
+
+				return new Response('Not Found', {
+					status: 404
+				})
+
+			case 'POST':
+				switch (url.pathname) {
+					case '/json':
+						return Response.json(await request.json())
+
+					default:
+						return new Response('Not Found', {
+							status: 404
+						})
+				}
+
+			default:
+				return new Response('Not Found', {
+					status: 404
+				})
+		}
+	}
+})