douyinWEB/desktop/electron/uiServer.ts
Eric 960a268fa2 Make desktop the sole Windows client with unmanned live and boot/poll hardening.
Consolidate Douyin→YouTube RTMPS into /live, parallel local service boot, poll-only realtime, slimmer packs/charts, and deprecate youtube-live-win.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 14:10:07 +01:00

107 lines
3.2 KiB
TypeScript

import http from "node:http";
import fs from "node:fs";
import path from "node:path";
import { URL } from "node:url";
const MIME: Record<string, string> = {
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".json": "application/json",
".svg": "image/svg+xml",
".png": "image/png",
".jpg": "image/jpeg",
".ico": "image/x-icon",
".woff2": "font/woff2",
".map": "application/json",
};
function proxy(req: http.IncomingMessage, res: http.ServerResponse, apiBase: string) {
const target = new URL(req.url || "/", apiBase);
const headers = { ...req.headers, host: target.host };
const preq = http.request(
{
protocol: target.protocol,
hostname: target.hostname,
port: target.port,
path: target.pathname + target.search,
method: req.method,
headers,
},
(pres) => {
res.writeHead(pres.statusCode || 502, pres.headers);
pres.pipe(res);
},
);
preq.on("error", (err) => {
res.writeHead(502, { "Content-Type": "text/plain" });
res.end(`proxy error: ${err.message}`);
});
req.pipe(preq);
}
function cacheHeaders(urlPath: string, ext: string): Record<string, string> {
// Vite hashed assets under /assets/
if (urlPath.startsWith("/assets/") || /\.[a-f0-9]{8,}\.(js|css)$/i.test(urlPath)) {
return { "Cache-Control": "public, max-age=31536000, immutable" };
}
if (ext === ".html") {
return { "Cache-Control": "no-cache" };
}
if (ext === ".woff2" || ext === ".png" || ext === ".jpg" || ext === ".svg" || ext === ".ico") {
return { "Cache-Control": "public, max-age=86400" };
}
return { "Cache-Control": "public, max-age=3600" };
}
export function startUiServer(opts: {
distDir: string;
apiBase: string;
port?: number;
}): Promise<{ port: number; url: string; close: () => void }> {
const port = opts.port ?? 18574;
const distDir = path.resolve(opts.distDir);
const server = http.createServer((req, res) => {
const url = req.url || "/";
// Renderer talks to API directly; only proxy media that may be relative.
if (url.startsWith("/covers")) {
proxy(req, res, opts.apiBase);
return;
}
const rawPath = decodeURIComponent(url.split("?")[0] || "/");
let filePath = path.join(distDir, rawPath === "/" ? "index.html" : rawPath);
if (!filePath.startsWith(distDir)) {
res.writeHead(403);
res.end("forbidden");
return;
}
let st: fs.Stats | null = null;
try {
st = fs.statSync(filePath);
} catch {
st = null;
}
if (!st || st.isDirectory()) {
filePath = path.join(distDir, "index.html");
}
const ext = path.extname(filePath).toLowerCase();
const headers: Record<string, string> = {
"Content-Type": MIME[ext] || "application/octet-stream",
...cacheHeaders(rawPath, ext),
};
res.writeHead(200, headers);
fs.createReadStream(filePath).pipe(res);
});
return new Promise((resolve, reject) => {
server.listen(port, "127.0.0.1", () => {
resolve({
port,
url: `http://127.0.0.1:${port}`,
close: () => server.close(),
});
});
server.on("error", reject);
});
}