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