Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
58 lines (55 loc) · 1.95 KB
/
Copy pathvite.config.ts
File metadata and controls
58 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { defineConfig, type Plugin } from "vite";
import react from "@vitejs/plugin-react";
// Runflow's API doesn't return CORS headers, so the browser can't call it
// directly. Vite proxies two things server-side:
// /api/runflow/* → https://api.runflow.io/v1/*
// /api/asset-proxy?url → arbitrary CDN URL (used to download generated
// images into the browser for display + ZIPing)
//
// Both work identically on localhost and Replit.
// Generic asset-proxy plugin: GET /api/asset-proxy?url=<encoded url>
// fetches the URL server-side and streams it back to the browser, side-
// stepping browser CORS on the destination.
function assetProxyPlugin(): Plugin {
return {
name: "dtc-ad-builder-asset-proxy",
configureServer(server) {
server.middlewares.use("/api/asset-proxy", async (req, res) => {
try {
const url = new URL(req.url || "/", "http://x").searchParams.get("url");
if (!url || !/^https?:\/\//.test(url)) {
res.statusCode = 400;
res.end("bad url");
return;
}
const upstream = await fetch(url);
res.statusCode = upstream.status;
const ct = upstream.headers.get("content-type");
if (ct) res.setHeader("content-type", ct);
const buf = Buffer.from(await upstream.arrayBuffer());
res.end(buf);
} catch (e) {
res.statusCode = 502;
res.end(`asset-proxy error: ${(e as Error).message}`);
}
});
},
};
}
export default defineConfig({
plugins: [react(), assetProxyPlugin()],
server: {
// host: true binds 0.0.0.0 so Replit / Codespaces / Docker can proxy the dev server.
host: true,
port: 5173,
allowedHosts: true,
proxy: {
"/api/runflow": {
target: "https://api.runflow.io",
changeOrigin: true,
secure: true,
rewrite: (path) => path.replace(/^\/api\/runflow/, "/v1"),
},
},
},
});