MCPcopy Create free account
hub / github.com/donghaxkim/react-rewrite / createProxyServer

Function createProxyServer

packages/cli/src/inject.ts:20–164  ·  view source on GitHub ↗
(
  options: ProxyServerOptions
)

Source from the content-addressed store, hash-verified

18}
19
20export function createProxyServer(
21 options: ProxyServerOptions
22): http.Server {
23 const { targetPort, targetHost, proxyPort, wsPort, getActiveClient } = options;
24
25 const proxy = httpProxy.createProxyServer({
26 target: `http://${targetHost}:${targetPort}`,
27 ws: true,
28 selfHandleResponse: true,
29 });
30
31 const bundledOverlayPath = path.join(__dirname, "overlay.js");
32 const workspaceOverlayPath = path.resolve(__dirname, "../../overlay/dist/overlay.js");
33 const overlayPath = fs.existsSync(workspaceOverlayPath)
34 ? workspaceOverlayPath
35 : bundledOverlayPath;
36 let upstreamDown = false;
37
38 const fontsDir = path.join(__dirname, "fonts");
39
40 const server = http.createServer((req, res) => {
41 // Normalize URL to prevent path traversal
42 const normalizedUrl = new URL(req.url || "/", "http://localhost").pathname;
43
44 // Serve overlay bundle
45 if (normalizedUrl === "/__react-rewrite/overlay.js") {
46 res.writeHead(200, { "Content-Type": "application/javascript" });
47 fs.createReadStream(overlayPath).pipe(res);
48 return;
49 }
50
51 // Serve font files
52 if (normalizedUrl === "/__react-rewrite/inter-regular.woff2") {
53 res.writeHead(200, {
54 "Content-Type": "font/woff2",
55 "Cache-Control": "public, max-age=31536000, immutable",
56 });
57 fs.createReadStream(path.join(fontsDir, "inter-regular.woff2")).pipe(res);
58 return;
59 }
60 if (normalizedUrl === "/__react-rewrite/inter-semibold.woff2") {
61 res.writeHead(200, {
62 "Content-Type": "font/woff2",
63 "Cache-Control": "public, max-age=31536000, immutable",
64 });
65 fs.createReadStream(path.join(fontsDir, "inter-semibold.woff2")).pipe(res);
66 return;
67 }
68
69 // For HTML requests: disable caching so we always get the full response
70 // to inject into (prevents 304 Not Modified with empty body)
71 if (req.headers.accept?.includes("text/html")) {
72 req.headers["accept-encoding"] = "identity";
73 delete req.headers["if-none-match"];
74 delete req.headers["if-modified-since"];
75 }
76
77 proxy.web(req, res);

Callers 1

index.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected