MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / startStaticProxy

Function startStaticProxy

graph-ui/tools/lib/static-proxy.mjs:73–174  ·  view source on GitHub ↗
({ distDir, upstreamPort, port })

Source from the content-addressed store, hash-verified

71 * @returns {Promise<{port: number, log: {apiRoutes: Record<string, number>, rpcTools: Record<string, number>, errors: string[]}, close: () => Promise<void>}>}
72 */
73export async function startStaticProxy({ distDir, upstreamPort, port }) {
74 const log = { apiRoutes: {}, rpcTools: {}, errors: [] };
75
76 const forward = async (req, res, body) => {
77 const route = req.url.split('?')[0];
78 if (route === '/rpc' && body.length > 0) {
79 try {
80 const parsed = JSON.parse(body.toString('utf8'));
81 const tool = parsed?.params?.name;
82 if (typeof tool === 'string') {
83 log.rpcTools[tool] = (log.rpcTools[tool] ?? 0) + 1;
84 }
85 } catch {
86 // Ein unlesbarer Rumpf ist die Sache des Servers, nicht des Proxys.
87 }
88 } else if (route.startsWith('/api')) {
89 log.apiRoutes[route] = (log.apiRoutes[route] ?? 0) + 1;
90 }
91
92 const headers = { ...req.headers };
93 // Die beiden Koepfe, um die es geht. Alles andere geht unveraendert durch.
94 headers.host = `127.0.0.1:${upstreamPort}`;
95 delete headers.origin;
96 delete headers.referer;
97 delete headers['accept-encoding'];
98
99 const upstream = http.request(
100 {
101 host: '127.0.0.1',
102 port: upstreamPort,
103 method: req.method,
104 path: req.url,
105 headers,
106 },
107 (upstreamRes) => {
108 res.writeHead(upstreamRes.statusCode ?? 502, upstreamRes.headers);
109 upstreamRes.pipe(res);
110 },
111 );
112 upstream.on('error', (error) => {
113 log.errors.push(`${req.method} ${req.url}: ${error.message}`);
114 res.writeHead(502, { 'Content-Type': 'application/json' });
115 res.end(JSON.stringify({ error: `upstream unreachable: ${error.message}` }));
116 });
117 if (body.length > 0) {
118 upstream.write(body);
119 }
120 upstream.end();
121 };
122
123 const server = http.createServer((req, res) => {
124 const route = (req.url ?? '/').split('?')[0];
125 if (route === '/rpc' || route.startsWith('/api')) {
126 readBody(req)
127 .then((body) => forward(req, res, body))
128 .catch((error) => {
129 log.errors.push(String(error));
130 res.writeHead(500);

Callers

nothing calls this directly

Calls 6

readBodyFunction · 0.85
safeJoinFunction · 0.85
forwardFunction · 0.70
statClass · 0.50
pushMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected