-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·206 lines (189 loc) · 6.7 KB
/
Copy pathserver.js
File metadata and controls
executable file
·206 lines (189 loc) · 6.7 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 4000;
const HAWK_GRAPH = __dirname;
const MIME = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
};
const REPOS = [
'eyrie', 'graycode-core', 'hawk', 'hawk-cloud', 'hawk-community-skills',
'hawk-core-contracts', 'hawk-mcpkit', 'hawk-sdk-go', 'hawk-sdk-python',
'hawk-sdk-typescript', 'inspect', 'sight', 'tok', 'trace', 'yaad'
];
const DARK_CSS = `
body { background: #0d1117 !important; color: #e6edf3 !important; }
h1, h2, h3, h4, h5, h6 { color: #ff7043 !important; }
a { color: #4fc3f7 !important; }
button, .btn, [class*="button"] {
background: #2d1b2e !important;
color: #ce93d8 !important;
border: 1px solid #ba68c8 !important;
border-radius: 6px !important;
padding: 6px 14px !important;
cursor: pointer !important;
}
button:hover { background: #3d1f2e !important; }
input, select, textarea {
background: #161b22 !important;
color: #e6edf3 !important;
border: 1px solid #30363d !important;
border-radius: 6px !important;
padding: 6px 10px !important;
}
table { background: #161b22 !important; color: #e6edf3 !important; }
th { background: #1a0a2e !important; color: #ce93d8 !important; }
td { border-color: #30363d !important; }
pre, code { background: #161b22 !important; color: #aed581 !important; border: 1px solid #30363d !important; }
#tree-container { background: #0d1117 !important; border-color: #30363d !important; box-shadow: none !important; }
svg { background: #0d1117 !important; }
.node text, .node text tspan { fill: #e6edf3 !important; stroke: none !important; }
`;
function serveFile(res, filePath) {
const ext = path.extname(filePath);
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
return;
}
res.writeHead(200, { 'Content-Type': MIME[ext] || 'application/octet-stream' });
res.end(data);
});
}
function serveGraphFile(res, filePath) {
const ext = path.extname(filePath);
if (ext === '.html') {
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
return;
}
let html = data.toString('utf-8');
html = html.replace('</head>', `<style>${DARK_CSS}</style></head>`);
res.writeHead(200, { 'Content-Type': 'text/html', 'Content-Length': Buffer.byteLength(html) });
res.end(html);
});
} else {
serveFile(res, filePath);
}
}
function graphData() {
const globalGraphPath = path.join(process.env.HOME, '.graphify', 'global-graph.json');
try {
const raw = fs.readFileSync(globalGraphPath, 'utf-8');
const g = JSON.parse(raw);
const nodes = g.nodes || [];
const repoCounts = {};
for (const n of nodes) {
const repo = n.repo || (n.label || '').split(':')[0] || 'unknown';
repoCounts[repo] = (repoCounts[repo] || 0) + 1;
}
return { totalNodes: nodes.length, totalEdges: 0, repoCounts, repos: REPOS };
} catch {
return { totalNodes: 0, totalEdges: 0, repoCounts: {}, repos: REPOS };
}
}
function proxyGitNexus(apiReq, apiRes) {
const targetPath = apiReq.url.replace(/^\/gitnexus/, '') || '/';
const MAX_RETRIES = 2;
// Only retry idempotent methods — retrying POST/PUT/DELETE can cause
// duplicate side effects if the first attempt actually reached GitNexus.
const RETRYABLE = apiReq.method === 'GET' || apiReq.method === 'HEAD';
let attempt = 0;
const doProxy = () => {
attempt++;
const options = {
hostname: 'localhost',
port: 4747,
path: targetPath,
method: apiReq.method,
headers: Object.assign({}, apiReq.headers, {
host: 'localhost:4747',
connection: 'close',
}),
timeout: 30_000,
};
delete options.headers['accept-encoding'];
const proxy = http.request(options, (proxyRes) => {
const { statusCode, headers } = proxyRes;
const cleanHeaders = Object.assign({}, headers);
delete cleanHeaders['transfer-encoding'];
// Handle mid-stream errors (e.g. GitNexus restarts mid-response).
proxyRes.on('error', () => {
if (!apiRes.headersSent) {
apiRes.writeHead(502, { 'Content-Type': 'text/plain' });
}
apiRes.end();
});
apiRes.writeHead(statusCode, cleanHeaders);
proxyRes.pipe(apiRes);
});
proxy.on('timeout', () => {
proxy.destroy(new Error('GitNexus proxy timeout'));
if (!apiRes.headersSent) {
apiRes.writeHead(504, { 'Content-Type': 'text/plain' });
apiRes.end('GitNexus timed out');
}
});
proxy.on('error', (err) => {
// Retry transient connection errors for idempotent requests.
const transient =
err.code === 'ECONNREFUSED' ||
err.code === 'ECONNRESET' ||
err.code === 'ENOTFOUND' ||
err.code === 'EPIPE';
if (RETRYABLE && transient && attempt <= MAX_RETRIES) {
setTimeout(doProxy, 200 * attempt);
return;
}
if (!apiRes.headersSent) {
apiRes.writeHead(502, { 'Content-Type': 'text/plain' });
apiRes.end('GitNexus not running on port 4747');
}
});
// If the client disconnects, stop piping to GitNexus.
apiReq.on('aborted', () => proxy.destroy());
apiReq.on('close', () => {
if (!apiReq.complete) proxy.destroy();
});
apiReq.pipe(proxy);
};
doProxy();
}
const server = http.createServer((req, res) => {
const url = new URL(req.url, `http://localhost:${PORT}`);
const pathname = url.pathname;
if (pathname === '/api/graph-data') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(graphData()));
return;
}
if (pathname.startsWith('/gitnexus')) {
return proxyGitNexus(req, res);
}
if (pathname.startsWith('/repos/') || pathname.startsWith('/global/')) {
return serveGraphFile(res, path.join(HAWK_GRAPH, pathname));
}
if (pathname === '/' || pathname === '/index.html') {
return serveFile(res, path.join(HAWK_GRAPH, 'index.html'));
}
serveFile(res, path.join(HAWK_GRAPH, pathname));
});
server.listen(PORT, () => {
console.log(`\n hawk-graph unified platform`);
console.log(` ─────────────────────────`);
console.log(` Dashboard: http://localhost:${PORT}`);
console.log(` GitNexus: http://localhost:${PORT}/gitnexus/`);
console.log(` API: http://localhost:${PORT}/api/graph-data`);
console.log(`\n (GitNexus server must be running on :4747 separately)`);
console.log(` Start it: gitnexus serve\n`);
});