| 223 | } |
| 224 | |
| 225 | async function proxyToVite(req: Request, viteUrl: string): Promise<Response> { |
| 226 | const target = new URL(req.url); |
| 227 | target.protocol = "http:"; |
| 228 | target.host = new URL(viteUrl).host; |
| 229 | // Strip hop-by-hop headers that confuse the upstream |
| 230 | const headers = new Headers(req.headers); |
| 231 | headers.delete("host"); |
| 232 | headers.set("host", target.host); |
| 233 | const hasBody = req.method !== "GET" && req.method !== "HEAD"; |
| 234 | return fetch(target, { |
| 235 | method: req.method, |
| 236 | headers, |
| 237 | body: hasBody ? req.body : undefined, |
| 238 | redirect: "manual", |
| 239 | // @ts-expect-error — Bun/undici extension required for streamed bodies |
| 240 | duplex: hasBody ? "half" : undefined, |
| 241 | }); |
| 242 | } |
| 243 | |
| 244 | // --------------------------------------------------------------------------- |
| 245 | // Server |