view benchmark/bun-http-framework-benchmark/src/bun/bun.ts @ 279:b3b547563ec7

Add Google connector service and agent wiki Implement the C/Seobeo Google Drive and Gmail connector with encrypted OAuth storage, Zenbu authentication, browser testing, AI tool discovery, chunked HTTP decoding, and Bazel coverage. Consolidate repository guidance into progressive wiki documentation and enforce arena-first allocation for new first-party C code. Co-authored-by: Copilot <[email protected]> Copilot-Session: 84c338fd-0939-4bb3-b7f3-1062eb213e5d
author MrJuneJune <me@mrjunejune.com>
date Mon, 17 Aug 2026 22:22:36 -0700
parents a8976a008a9d
children
line wrap: on
line source

const jsonHeaders = { headers: [['Content-Type', 'application/json']] },
	queryHeaders = { headers: [['X-Powered-By', 'benchmark']] },
	notFound = new Response(null, { status: 404 }),
	hiRes = new Response('Hi')

function toResponse(json: unknown) {
	return new Response(JSON.stringify(json), jsonHeaders)
}

// json -> [106, 115, 111, 110]
// id/ -> [105, 100, 47]
// Simulate the maximum performance you can get with Bun.serve
// We should appreciate how frameworks make all these stuff easier :) - Reve
Bun.serve({
	static: {
		'/': new Response('Hi', {
			headers: { 'content-type': 'text/plain;charset=utf8' }
		})
	},
	routes: {
		'/id/:id': {
			GET(request: Request) {
				const url = request.url,
					s = url.indexOf('/', 11),
					qi = url.indexOf('?', s + 1)

				const nameIdx = url.indexOf('name=', qi + 1)
				const nameEndIdx = url.indexOf('&', nameIdx + 1)
				return new Response(
					`${request.params.id} ${
						nameEndIdx === -1
							? url.substring(nameIdx + 5)
							: url.substring(nameIdx + 5, nameEndIdx)
					}`,
					queryHeaders
				)
			}
		}
	},
	fetch(req): Response | Promise<Response> {
		const url = req.url

		const pathIndex = url.indexOf('/', 12) + 1
		const queryIndex = url.indexOf('?', pathIndex)
		const path =
			queryIndex === -1
				? url.substring(pathIndex)
				: url.substring(pathIndex, queryIndex)

		if (path.length === 0)
			return req.method === 'GET' ? hiRes.clone() : notFound

		switch (path.charCodeAt(0)) {
			case 105:
				if (
					path.charCodeAt(1) === 100 &&
					path.charCodeAt(2) === 47 &&
					req.method === 'GET'
				) {
					// Shouldn't include a slash and should have query
					if (queryIndex === -1 || path.indexOf('/', 3) !== -1)
						return notFound

					const nameQueryIdx = url.indexOf('name=', queryIndex + 1)
					if (nameQueryIdx === -1) return notFound

					const nameQueryEndIdx = url.indexOf('&', nameQueryIdx + 1)
					return new Response(
						`${path.substring(3, queryIndex)} ${
							nameQueryEndIdx === -1
								? url.substring(nameQueryIdx + 5)
								: url.substring(
										nameQueryIdx + 5,
										nameQueryEndIdx
									)
						}`,
						queryHeaders
					)
				}

				return notFound

			case 106:
				return path.charCodeAt(1) === 115 &&
					path.charCodeAt(2) === 111 &&
					path.charCodeAt(3) === 110 &&
					req.method === 'POST'
					? req.json().then(toResponse)
					: notFound

			default:
				return notFound
		}
	}
})