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

Function createProxyHandlers

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

Source from the content-addressed store, hash-verified

55}
56
57export function createProxyHandlers({
58 label = "proxy",
59 timeout = DEFAULT_PROXY_TIMEOUT_MS,
60 proxyTimeout = DEFAULT_PROXY_TIMEOUT_MS,
61} = {}) {
62 const proxy = createProxyServer({
63 ws: true,
64 changeOrigin: true,
65 xfwd: true,
66 timeout,
67 proxyTimeout,
68 });
69 const metrics = {
70 activeHttpRequests: 0,
71 activeWebSockets: 0,
72 totalHttpRequests: 0,
73 totalWebSockets: 0,
74 totalErrors: 0,
75 };
76
77 proxy.on("error", (err, _req, resOrSocket, target) => {
78 metrics.totalErrors += 1;
79 const targetText = target ? ` -> ${target}` : "";
80 if (!isBenignSocketError(err)) {
81 console.error(`[${label}] Proxy error${targetText}: ${err.message}`);
82 }
83 if (resOrSocket && typeof resOrSocket.writeHead === "function") {
84 writeProxyError(resOrSocket, err.message);
85 } else if (resOrSocket && typeof resOrSocket.destroy === "function") {
86 resOrSocket.destroy();
87 }
88 });
89
90 function proxyHttp(req, res, target) {
91 metrics.activeHttpRequests += 1;
92 metrics.totalHttpRequests += 1;
93 const finish = once(() => {
94 metrics.activeHttpRequests = Math.max(0, metrics.activeHttpRequests - 1);
95 });
96 res.on("close", finish);
97 res.on("finish", finish);
98 res.on("error", finish);
99
100 proxy.web(req, res, { target }).catch((err) => {
101 metrics.totalErrors += 1;
102 if (!isBenignSocketError(err)) {
103 console.error(
104 `[${label}] Proxy error for ${req.url} -> ${target}:`,
105 err,
106 );
107 }
108 writeProxyError(res, err instanceof Error ? err.message : String(err));
109 finish();
110 });
111 }
112
113 function proxyWebSocket(req, socket, head, target) {
114 metrics.activeWebSockets += 1;

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