Skip to content

Commit 03c200b

Browse files
authored
Fix proxy server: remove URL param so it doesn't interfere with Next.js (#753)
Fixes #748
1 parent e5e1495 commit 03c200b

File tree

2 files changed

+7
-50
lines changed

2 files changed

+7
-50
lines changed

src/ipc/utils/start_proxy_server.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ export async function startProxy(
2929
const worker = new Worker(
3030
path.resolve(__dirname, "..", "..", "worker", "proxy_server.js"),
3131
{
32-
env: {
33-
...process.env, // inherit parent env
34-
35-
TARGET_URL: targetOrigin,
36-
},
3732
workerData: {
3833
targetOrigin,
3934
port,

worker/proxy_server.js

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
* proxy.js – zero-dependency worker-based HTTP/WS forwarder
33
*/
44

5-
const {
6-
Worker,
7-
isMainThread,
8-
parentPort,
9-
workerData,
10-
} = require("worker_threads");
5+
const { parentPort, workerData } = require("worker_threads");
116

127
const http = require("http");
138
const https = require("https");
@@ -16,33 +11,14 @@ const { URL } = require("url");
1611
const fs = require("fs");
1712
const path = require("path");
1813

19-
/* ─────────────────── configuration (main thread only) ─────────────────── */
20-
21-
const LISTEN_HOST = "localhost";
22-
23-
if (isMainThread) {
24-
// Stand-alone mode: fork the worker and pass through the env as-is
25-
const w = new Worker(__filename, {
26-
workerData: {
27-
targetOrigin: process.env.TARGET_URL, // may be undefined
28-
},
29-
});
30-
31-
w.on("message", (m) => console.log("[proxy-worker]", m));
32-
w.on("error", (e) => console.error("[proxy-worker] error:", e));
33-
w.on("exit", (c) => console.log("[proxy-worker] exited", c));
34-
console.log("proxy worker launching …");
35-
return; // do not execute the rest of the file in the main thread
36-
}
37-
3814
/* ──────────────────────────── worker code ─────────────────────────────── */
39-
40-
const LISTEN_PORT = process.env.LISTEN_PORT || workerData.port;
15+
const LISTEN_HOST = "localhost";
16+
const LISTEN_PORT = workerData.port;
4117
let rememberedOrigin = null; // e.g. "http://localhost:5173"
4218

43-
/* ---------- pre-configure rememberedOrigin from env or workerData ------- */
19+
/* ---------- pre-configure rememberedOrigin from workerData ------- */
4420
{
45-
const fixed = process.env.TARGET_URL || workerData?.targetOrigin;
21+
const fixed = workerData?.targetOrigin;
4622
if (fixed) {
4723
try {
4824
rememberedOrigin = new URL(fixed).origin;
@@ -51,7 +27,7 @@ let rememberedOrigin = null; // e.g. "http://localhost:5173"
5127
);
5228
} catch {
5329
throw new Error(
54-
`Invalid TARGET_URL "${fixed}". Must be absolute http/https URL.`,
30+
`Invalid target origin "${fixed}". Must be absolute http/https URL.`,
5531
);
5632
}
5733
}
@@ -162,21 +138,7 @@ function injectHTML(buf) {
162138

163139
/* ---------------- helper: build upstream URL from request -------------- */
164140
function buildTargetURL(clientReq) {
165-
// Support the old "?url=" mechanism
166-
const parsedLocal = new URL(clientReq.url, `http://${LISTEN_HOST}`);
167-
const urlParam = parsedLocal.searchParams.get("url");
168-
if (urlParam) {
169-
const abs = new URL(urlParam);
170-
if (!/^https?:$/.test(abs.protocol))
171-
throw new Error("only http/https targets allowed");
172-
rememberedOrigin = abs.origin; // remember for later
173-
return abs;
174-
}
175-
176-
if (!rememberedOrigin)
177-
throw new Error(
178-
"No upstream configured. Use ?url=… once or set TARGET_URL env var.",
179-
);
141+
if (!rememberedOrigin) throw new Error("No upstream configured.");
180142

181143
// Forward to the remembered origin keeping path & query
182144
return new URL(clientReq.url, rememberedOrigin);

0 commit comments

Comments
 (0)