Fix proxy server: remove URL param so it doesn't interfere with Next.js (#753)
Fixes #748
This commit is contained in:
@@ -29,11 +29,6 @@ export async function startProxy(
|
|||||||
const worker = new Worker(
|
const worker = new Worker(
|
||||||
path.resolve(__dirname, "..", "..", "worker", "proxy_server.js"),
|
path.resolve(__dirname, "..", "..", "worker", "proxy_server.js"),
|
||||||
{
|
{
|
||||||
env: {
|
|
||||||
...process.env, // inherit parent env
|
|
||||||
|
|
||||||
TARGET_URL: targetOrigin,
|
|
||||||
},
|
|
||||||
workerData: {
|
workerData: {
|
||||||
targetOrigin,
|
targetOrigin,
|
||||||
port,
|
port,
|
||||||
|
|||||||
@@ -2,12 +2,7 @@
|
|||||||
* proxy.js – zero-dependency worker-based HTTP/WS forwarder
|
* proxy.js – zero-dependency worker-based HTTP/WS forwarder
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const {
|
const { parentPort, workerData } = require("worker_threads");
|
||||||
Worker,
|
|
||||||
isMainThread,
|
|
||||||
parentPort,
|
|
||||||
workerData,
|
|
||||||
} = require("worker_threads");
|
|
||||||
|
|
||||||
const http = require("http");
|
const http = require("http");
|
||||||
const https = require("https");
|
const https = require("https");
|
||||||
@@ -16,33 +11,14 @@ const { URL } = require("url");
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
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 ─────────────────────────────── */
|
/* ──────────────────────────── worker code ─────────────────────────────── */
|
||||||
|
const LISTEN_HOST = "localhost";
|
||||||
const LISTEN_PORT = process.env.LISTEN_PORT || workerData.port;
|
const LISTEN_PORT = workerData.port;
|
||||||
let rememberedOrigin = null; // e.g. "http://localhost:5173"
|
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) {
|
if (fixed) {
|
||||||
try {
|
try {
|
||||||
rememberedOrigin = new URL(fixed).origin;
|
rememberedOrigin = new URL(fixed).origin;
|
||||||
@@ -51,7 +27,7 @@ let rememberedOrigin = null; // e.g. "http://localhost:5173"
|
|||||||
);
|
);
|
||||||
} catch {
|
} catch {
|
||||||
throw new Error(
|
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 -------------- */
|
/* ---------------- helper: build upstream URL from request -------------- */
|
||||||
function buildTargetURL(clientReq) {
|
function buildTargetURL(clientReq) {
|
||||||
// Support the old "?url=" mechanism
|
if (!rememberedOrigin) throw new Error("No upstream configured.");
|
||||||
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.",
|
|
||||||
);
|
|
||||||
|
|
||||||
// Forward to the remembered origin keeping path & query
|
// Forward to the remembered origin keeping path & query
|
||||||
return new URL(clientReq.url, rememberedOrigin);
|
return new URL(clientReq.url, rememberedOrigin);
|
||||||
|
|||||||
Reference in New Issue
Block a user