MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / createProxyHandlers

Function createProxyHandlers

scripts/proxy-utils.mjs:206–326  ·  view source on GitHub ↗
({
  label = "proxy",
  timeout = DEFAULT_PROXY_TIMEOUT_MS,
  proxyTimeout = DEFAULT_PROXY_TIMEOUT_MS,
} = {})

Source from the content-addressed store, hash-verified

204}
205
206export function createProxyHandlers({
207 label = "proxy",
208 timeout = DEFAULT_PROXY_TIMEOUT_MS,
209 proxyTimeout = DEFAULT_PROXY_TIMEOUT_MS,
210} = {}) {
211 const proxy = createProxyServer({
212 ws: true,
213 changeOrigin: true,
214 xfwd: true,
215 timeout,
216 proxyTimeout,
217 });
218 const metrics = {
219 activeHttpRequests: 0,
220 activeWebSockets: 0,
221 totalHttpRequests: 0,
222 totalWebSockets: 0,
223 totalErrors: 0,
224 };
225
226 proxy.on("error", (err, _req, resOrSocket, target) => {
227 metrics.totalErrors += 1;
228 const targetText = target ? ` -> ${target}` : "";
229 if (!isBenignSocketError(err)) {
230 console.error(`[${label}] Proxy error${targetText}: ${err.message}`);
231 }
232 if (resOrSocket && typeof resOrSocket.writeHead === "function") {
233 writeProxyError(resOrSocket, err.message);
234 } else if (resOrSocket && typeof resOrSocket.destroy === "function") {
235 resOrSocket.destroy();
236 }
237 });
238
239 function proxyHttp(req, res, target) {
240 metrics.activeHttpRequests += 1;
241 metrics.totalHttpRequests += 1;
242 const finish = once(() => {
243 metrics.activeHttpRequests = Math.max(0, metrics.activeHttpRequests - 1);
244 });
245 res.on("close", finish);
246 res.on("finish", finish);
247 res.on("error", finish);
248
249 const handleProxyError = (err) => {
250 metrics.totalErrors += 1;
251 if (!isBenignSocketError(err)) {
252 console.error(
253 `[${label}] Proxy error for ${req.url} -> ${target}:`,
254 err,
255 );
256 }
257 writeProxyError(res, err instanceof Error ? err.message : String(err));
258 finish();
259 };
260
261 try {
262 proxy.web(req, res, { target }).catch(handleProxyError);
263 } catch (err) {

Callers 2

startStaticServerFunction · 0.90
startIngressFunction · 0.90

Calls 3

isBenignSocketErrorFunction · 0.85
writeProxyErrorFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected