Files
2026-10-15 10:35:55 +08:00

99 lines
3.0 KiB
TypeScript

import { defineConfig, type ProxyOptions } from "vite";
import react from "@vitejs/plugin-react";
import { backendTarget, rewriteDevCookie } from "./server/backend-proxy.mjs";
import { localApiGuard } from "../shared/local-api-guard.ts";
const proxy: Record<string, ProxyOptions> = {
"/api/robot-model-assets/": {
target: backendTarget,
changeOrigin: true,
rewrite: (path) =>
path.replace(
/^\/api\/robot-model-assets\//,
"/admin-api/component-assets/",
),
timeout: 30000,
proxyTimeout: 30000,
},
"/api/robot-": {
target: backendTarget,
changeOrigin: true,
cookieDomainRewrite: "",
timeout: 240000,
proxyTimeout: 240000,
configure(proxy) {
proxy.on("proxyRes", (response) => {
const cookies = response.headers["set-cookie"];
if (cookies)
response.headers["set-cookie"] = cookies.map(rewriteDevCookie);
response.headers["x-accel-buffering"] = "no";
});
proxy.on("error", (_error, _req, res) => {
if ("writeHead" in res && !res.headersSent) {
res.writeHead(502, { "Content-Type": "application/json" });
res.end(JSON.stringify({ code: "DEMAND_UNAVAILABLE", data: null }));
}
});
},
},
};
export default defineConfig({
plugins: [
react(),
{
name: "private-server-resources",
configurePreviewServer(server) {
server.middlewares.use(localApiGuard);
},
configureServer(server) {
server.middlewares.use(localApiGuard);
server.middlewares.use((req, res, next) => {
let path;
try {
path = decodeURIComponent(
new URL(req.url || "/", "http://localhost").pathname,
);
} catch {
res.statusCode = 400;
res.end();
return;
}
if (
path.startsWith("/api/robot-model-assets/") &&
(path.includes("\\") ||
path.includes("\0") ||
path.split("/").some((part) => part === "." || part === ".."))
) {
res.statusCode = 400;
res.end("Invalid resource path");
return;
}
if (
path.startsWith("/api/robot-model-assets/") &&
!["GET", "HEAD"].includes(req.method || "GET")
) {
res.statusCode = 405;
res.end("Method not allowed");
return;
}
if (
path.startsWith("/api/ai") ||
/(?:^|\/)(?:ai-rules|handoff|server|scripts|tests|docs)(?:\/|$)/.test(
path,
) ||
/(?:^|\/)shared\/requirements(?:\.d)?\.m(?:js|ts)$/.test(path) ||
/(?:^|\/)downloads\/robot-demand-parser-v1\.zip$/.test(path)
) {
res.statusCode = 404;
res.end("Not found");
return;
}
next();
});
},
},
],
server: { proxy },
preview: { proxy },
build: { rollupOptions: { output: { manualChunks: { three: ["three"] } } } },
});