| 190 | } |
| 191 | |
| 192 | async function proxyToVite(req: Request, viteUrl: string): Promise<Response> { |
| 193 | const target = new URL(req.url); |
| 194 | target.protocol = "http:"; |
| 195 | target.host = new URL(viteUrl).host; |
| 196 | // Strip hop-by-hop headers that confuse the upstream |
| 197 | const headers = new Headers(req.headers); |
| 198 | headers.delete("host"); |
| 199 | headers.set("host", target.host); |
| 200 | const hasBody = req.method !== "GET" && req.method !== "HEAD"; |
| 201 | return fetch(target, { |
| 202 | method: req.method, |
| 203 | headers, |
| 204 | body: hasBody ? req.body : undefined, |
| 205 | redirect: "manual", |
| 206 | // @ts-expect-error — Bun/undici extension required for streamed bodies |
| 207 | duplex: hasBody ? "half" : undefined, |
| 208 | }); |
| 209 | } |
| 210 | |
| 211 | // --------------------------------------------------------------------------- |
| 212 | // Server |