-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
107 lines (92 loc) · 2.55 KB
/
Copy pathvite.config.js
File metadata and controls
107 lines (92 loc) · 2.55 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
const rootDir = dirname(fileURLToPath(import.meta.url));
function readJsonBody(req) {
return new Promise((resolveBody, rejectBody) => {
let raw = "";
req.on("data", (chunk) => {
raw += chunk;
});
req.on("end", () => {
try {
resolveBody(JSON.parse(raw || "{}"));
} catch (error) {
rejectBody(error);
}
});
req.on("error", rejectBody);
});
}
function runAnalysis(input) {
return new Promise((resolveResult, rejectResult) => {
const child = spawn("python", [resolve(rootDir, "ui_adapter", "analyze.py")], {
cwd: rootDir,
stdio: ["pipe", "pipe", "pipe"],
});
let stdout = "";
let stderr = "";
child.stdout.on("data", (chunk) => {
stdout += chunk.toString();
});
child.stderr.on("data", (chunk) => {
stderr += chunk.toString();
});
child.on("error", rejectResult);
child.on("close", (code) => {
if (code !== 0) {
rejectResult(new Error(stderr || `Analysis process exited with code ${code}.`));
return;
}
try {
resolveResult(JSON.parse(stdout));
} catch (error) {
rejectResult(error);
}
});
child.stdin.write(JSON.stringify({ input }));
child.stdin.end();
});
}
function xrayApiPlugin() {
const handler = async (req, res, next) => {
if (req.method !== "POST" || req.url !== "/api/analyze") {
next();
return;
}
try {
const body = await readJsonBody(req);
const data = await runAnalysis(body.input ?? "");
res.statusCode = 200;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(data));
} catch (error) {
res.statusCode = 500;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ error: error.message || "Analysis failed." }));
}
};
return {
name: "xray-api",
configureServer(server) {
server.middlewares.use(handler);
},
configurePreviewServer(server) {
server.middlewares.use(handler);
},
};
}
export default defineConfig({
plugins: [react(), xrayApiPlugin()],
build: {
rollupOptions: {
output: {
manualChunks: {
recharts: ["recharts"],
},
},
},
},
});