From 03c200b932cb2747fdd04305877c0e0b8b6bbc06 Mon Sep 17 00:00:00 2001 From: Will Chen Date: Thu, 31 Jul 2025 15:18:37 -0700 Subject: [PATCH] Fix proxy server: remove URL param so it doesn't interfere with Next.js (#753) Fixes #748 --- src/ipc/utils/start_proxy_server.ts | 5 --- worker/proxy_server.js | 52 ++++------------------------- 2 files changed, 7 insertions(+), 50 deletions(-) diff --git a/src/ipc/utils/start_proxy_server.ts b/src/ipc/utils/start_proxy_server.ts index 3842ab4..c49bd30 100644 --- a/src/ipc/utils/start_proxy_server.ts +++ b/src/ipc/utils/start_proxy_server.ts @@ -29,11 +29,6 @@ export async function startProxy( const worker = new Worker( path.resolve(__dirname, "..", "..", "worker", "proxy_server.js"), { - env: { - ...process.env, // inherit parent env - - TARGET_URL: targetOrigin, - }, workerData: { targetOrigin, port, diff --git a/worker/proxy_server.js b/worker/proxy_server.js index ba3209b..733a317 100644 --- a/worker/proxy_server.js +++ b/worker/proxy_server.js @@ -2,12 +2,7 @@ * proxy.js – zero-dependency worker-based HTTP/WS forwarder */ -const { - Worker, - isMainThread, - parentPort, - workerData, -} = require("worker_threads"); +const { parentPort, workerData } = require("worker_threads"); const http = require("http"); const https = require("https"); @@ -16,33 +11,14 @@ const { URL } = require("url"); const fs = require("fs"); const path = require("path"); -/* ─────────────────── configuration (main thread only) ─────────────────── */ - -const LISTEN_HOST = "localhost"; - -if (isMainThread) { - // Stand-alone mode: fork the worker and pass through the env as-is - const w = new Worker(__filename, { - workerData: { - targetOrigin: process.env.TARGET_URL, // may be undefined - }, - }); - - w.on("message", (m) => console.log("[proxy-worker]", m)); - w.on("error", (e) => console.error("[proxy-worker] error:", e)); - w.on("exit", (c) => console.log("[proxy-worker] exited", c)); - console.log("proxy worker launching …"); - return; // do not execute the rest of the file in the main thread -} - /* ──────────────────────────── worker code ─────────────────────────────── */ - -const LISTEN_PORT = process.env.LISTEN_PORT || workerData.port; +const LISTEN_HOST = "localhost"; +const LISTEN_PORT = workerData.port; let rememberedOrigin = null; // e.g. "http://localhost:5173" -/* ---------- pre-configure rememberedOrigin from env or workerData ------- */ +/* ---------- pre-configure rememberedOrigin from workerData ------- */ { - const fixed = process.env.TARGET_URL || workerData?.targetOrigin; + const fixed = workerData?.targetOrigin; if (fixed) { try { rememberedOrigin = new URL(fixed).origin; @@ -51,7 +27,7 @@ let rememberedOrigin = null; // e.g. "http://localhost:5173" ); } catch { throw new Error( - `Invalid TARGET_URL "${fixed}". Must be absolute http/https URL.`, + `Invalid target origin "${fixed}". Must be absolute http/https URL.`, ); } } @@ -162,21 +138,7 @@ function injectHTML(buf) { /* ---------------- helper: build upstream URL from request -------------- */ function buildTargetURL(clientReq) { - // Support the old "?url=" mechanism - const parsedLocal = new URL(clientReq.url, `http://${LISTEN_HOST}`); - const urlParam = parsedLocal.searchParams.get("url"); - if (urlParam) { - const abs = new URL(urlParam); - if (!/^https?:$/.test(abs.protocol)) - throw new Error("only http/https targets allowed"); - rememberedOrigin = abs.origin; // remember for later - return abs; - } - - if (!rememberedOrigin) - throw new Error( - "No upstream configured. Use ?url=… once or set TARGET_URL env var.", - ); + if (!rememberedOrigin) throw new Error("No upstream configured."); // Forward to the remembered origin keeping path & query return new URL(clientReq.url, rememberedOrigin);