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