TonyAgent / nvidia-local-proxy.mjs
ayoub5550's picture
Upload nvidia-local-proxy.mjs with huggingface_hub
4cf9ef3 verified
Raw
History Blame Contribute Delete
2.77 kB
/**
* Local NVIDIA API proxy — runs inside the HF Space container.
*
* Listens on http://127.0.0.1:${PORT} and forwards every request to
* the Cloudflare Worker proxy (which in turn reaches integrate.api.nvidia.com).
*
* Why? OpenClaw bundles its own copy of undici whose fetch() rejects
* any origin that doesn't match an internal dispatcher. By keeping the
* LLM provider pointed at localhost we sidestep that entirely; the
* outbound leg uses Node's built-in https which the cloudflare-proxy.js
* --require hook patches successfully.
*/
import http from "node:http";
import https from "node:https";
import { URL } from "node:url";
const PORT = parseInt(process.env.NV_LOCAL_PORT || "8787", 10);
const TARGET = (process.env.NV_PROXY_TARGET || "https://ayoub5550-nvidia-proxy.ayoubteke11.workers.dev").replace(/\/+$/, "");
const DEBUG = process.env.NV_LOCAL_DEBUG === "true";
const targetUrl = new URL(TARGET);
const server = http.createServer((clientReq, clientRes) => {
// Collect body
const chunks = [];
clientReq.on("data", (c) => chunks.push(c));
clientReq.on("end", () => {
const body = Buffer.concat(chunks);
const path = clientReq.url || "/";
if (DEBUG) {
console.error(`[nv-local] ${clientReq.method} ${path} (${body.length} bytes)`);
}
const outHeaders = { ...clientReq.headers };
// Fix host header
outHeaders.host = targetUrl.host;
// Remove hop-by-hop headers that shouldn't be forwarded
delete outHeaders.connection;
delete outHeaders["transfer-encoding"];
// Set content-length from actual body
if (body.length > 0) {
outHeaders["content-length"] = String(body.length);
}
const opts = {
hostname: targetUrl.hostname,
port: targetUrl.port || 443,
path: path,
method: clientReq.method,
headers: outHeaders,
};
const proxyReq = https.request(opts, (proxyRes) => {
if (DEBUG) {
console.error(`[nv-local] <- ${proxyRes.statusCode}`);
}
// Forward status + headers
clientRes.writeHead(proxyRes.statusCode, proxyRes.headers);
// Stream the response (works for SSE too)
proxyRes.pipe(clientRes, { end: true });
});
proxyReq.on("error", (err) => {
console.error(`[nv-local] proxy error: ${err.message}`);
if (!clientRes.headersSent) {
clientRes.writeHead(502, { "content-type": "application/json" });
}
clientRes.end(JSON.stringify({ error: "proxy_error", detail: err.message }));
});
// Send body
if (body.length > 0) {
proxyReq.write(body);
}
proxyReq.end();
});
});
server.listen(PORT, "127.0.0.1", () => {
console.log(`[nv-local] NVIDIA local proxy listening on http://127.0.0.1:${PORT} -> ${TARGET}`);
});