forked from HOOLC/zork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
128 lines (125 loc) · 5.19 KB
/
Copy pathvite.config.ts
File metadata and controls
128 lines (125 loc) · 5.19 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { spawn } from "node:child_process";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vite-plus";
const adminApiProxyOrigin = process.env.ADMIN_API_PROXY_ORIGIN;
const adminApiProxyCookie = process.env.ADMIN_API_PROXY_COOKIE;
export default defineConfig({
root: "src/admin-ui",
base: "/admin/",
plugins: [
{
name: "admin-base-redirect",
configureServer(server) {
server.middlewares.use((request, response, next) => {
if (request.url === "/admin") {
response.statusCode = 302;
response.setHeader("location", "/admin/");
response.end();
return;
}
next();
});
if (adminApiProxyOrigin) {
server.middlewares.use("/admin/api/events", (request, response) => {
response.writeHead(200, {
"content-type": "text/event-stream; charset=utf-8",
"cache-control": "no-store",
connection: "keep-alive",
});
response.write(": local preview realtime disabled\n\n");
const timer = setInterval(() => response.write(": keepalive\n\n"), 15_000);
request.on("close", () => clearInterval(timer));
});
server.middlewares.use("/admin/api", (request, response) => {
const requestUrl = request.url || "/";
const target = new URL(`/admin/api${requestUrl}`, adminApiProxyOrigin);
const chunks: Buffer[] = [];
request.on("data", (chunk: Buffer) => chunks.push(chunk));
request.on("end", () => {
const body = Buffer.concat(chunks);
const args = ["--silent", "--show-error", "--max-time", "60", "--request", request.method || "GET", "--dump-header", "-", "--output", "-", "--header", `accept: ${request.headers.accept || "application/json"}`];
if (adminApiProxyCookie) {
args.push("--header", `cookie: ${adminApiProxyCookie}`);
}
if (request.headers["content-type"]) {
args.push("--header", `content-type: ${request.headers["content-type"]}`);
}
if (body.length > 0) {
args.push("--data-binary", "@-");
}
args.push(target.toString());
const child = spawn("curl", args, {
stdio: ["pipe", "pipe", "pipe"],
});
const stdout: Buffer[] = [];
const stderr: Buffer[] = [];
child.stdout.on("data", (chunk: Buffer) => stdout.push(chunk));
child.stderr.on("data", (chunk: Buffer) => stderr.push(chunk));
child.on("error", (error) => {
response.writeHead(502, { "content-type": "application/json" });
response.end(JSON.stringify({ ok: false, error: error.message }));
});
child.on("close", (code) => {
const output = Buffer.concat(stdout);
const headerEnd = output.indexOf(Buffer.from("\r\n\r\n"));
if (code !== 0 || headerEnd < 0) {
response.writeHead(502, { "content-type": "application/json" });
response.end(
JSON.stringify({
ok: false,
error: Buffer.concat(stderr).toString("utf8") || `curl exited with status ${code}`,
}),
);
return;
}
const rawHeaders = output.subarray(0, headerEnd).toString("utf8").split("\r\n");
const status = Number(rawHeaders[0]?.match(/\s(\d{3})\s/)?.[1] || 502);
const headers: Record<string, string> = {
"cache-control": "no-store",
};
for (const line of rawHeaders.slice(1)) {
const index = line.indexOf(":");
if (index <= 0) continue;
const key = line.slice(0, index).trim().toLowerCase();
if (["content-length", "transfer-encoding", "connection", "set-cookie"].includes(key)) continue;
headers[key] = line.slice(index + 1).trim();
}
response.writeHead(status, headers);
response.end(output.subarray(headerEnd + 4));
});
if (body.length > 0) {
child.stdin.end(body);
} else {
child.stdin.end();
}
});
request.resume();
});
}
},
},
react(),
],
server: {
host: "127.0.0.1",
port: Number(process.env.ADMIN_UI_DEV_PORT || 5173),
strictPort: true,
cors: true,
},
build: {
outDir: "../../dist/admin-ui",
emptyOutDir: true,
rollupOptions: {
input: "index.html",
output: {
entryFileNames: "assets/admin-ui.js",
chunkFileNames: "assets/[name].js",
assetFileNames: (assetInfo) => {
const names = "names" in assetInfo && Array.isArray(assetInfo.names) ? assetInfo.names : [];
const name = assetInfo.name ?? names[0] ?? "";
return name.endsWith(".css") ? "assets/admin-ui.css" : "assets/[name][extname]";
},
},
},
},
});