From 9e1aae22d84cb80f05d3de2cc4d0860fa558262b Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sun, 10 Dec 2023 09:53:37 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20working=20proxy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/vscode/src/activation/proxy.ts | 53 +++++++++++++++-------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/extensions/vscode/src/activation/proxy.ts b/extensions/vscode/src/activation/proxy.ts index a002e4373..c181a16fa 100644 --- a/extensions/vscode/src/activation/proxy.ts +++ b/extensions/vscode/src/activation/proxy.ts @@ -1,33 +1,50 @@ import cors from "cors"; import express from "express"; -import { createProxyMiddleware } from "http-proxy-middleware"; +import http from "http"; const PROXY_PORT = 65433; const app = express(); app.use(cors()); -app.use("/", (req, res, next) => { - // Extract the target from the request URL - // const { protocol, host } = url.parse(req.url); - // const target = `${protocol}//${host}`; - const target = "http://localhost:11434"; - - // Create a new proxy middleware for this request - const proxy = createProxyMiddleware({ - target, - changeOrigin: true, - ws: true, - pathRewrite: { - "^/": "/api/generate", +app.use((req, res, next) => { + // Proxy the request + const { origin, ...headers } = req.headers; + const proxy = http.request( + req.headers["x-continue-url"] as string, + { + method: req.method, + headers: headers, }, - }); + (response) => { + // Pipe the response stream directly to the client + response.pipe(res); + } + ); - // Call the middleware - proxy(req, res, next); + // Pipe the request stream directly to the proxy + req.pipe(proxy); }); +// http-middleware-proxy +// app.use("/", (req, res, next) => { +// // Extract the target from the request URL +// const target = req.headers["x-continue-url"] as string; +// const { origin, ...headers } = req.headers; + +// // Create a new proxy middleware for this request +// const proxy = createProxyMiddleware({ +// target, +// ws: true, +// headers: { +// origin: "", +// }, +// }); + +// // Call the middleware +// proxy(req, res, next); +// }); + export function startProxy() { - console.log("Starting proxy"); app.listen(PROXY_PORT, () => { console.log(`Proxy server is running on port ${PROXY_PORT}`); });