Skip to content

Commit 809e4ad

Browse files
committed
Improve proxy server worker path resolution and error handling
- Add proper path resolution for development vs production environments - Use getElectron() to determine correct worker file location - Enhance error handling with better logging and error messages - Add graceful exit handling for proxy worker
1 parent f7aba06 commit 809e4ad

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/ipc/utils/start_proxy_server.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Worker } from "worker_threads";
44
import path from "path";
55
import { findAvailablePort } from "./port_utils";
66
import log from "electron-log";
7+
import { getElectron } from "../../paths/paths";
78

89
const logger = log.scope("start_proxy_server");
910

@@ -26,15 +27,26 @@ export async function startProxy(
2627
onStarted,
2728
} = opts;
2829

29-
const worker = new Worker(
30-
path.resolve(__dirname, "..", "..", "worker", "proxy_server.js"),
31-
{
32-
workerData: {
33-
targetOrigin,
34-
port,
35-
},
30+
// Get the correct path to the worker file in both development and production
31+
let workerPath: string;
32+
const electron = getElectron();
33+
34+
if (electron && !process.env.NODE_ENV?.includes("development")) {
35+
// In production/built app, use the app's resource path
36+
workerPath = path.resolve(__dirname, "..", "..", "..", "worker", "proxy_server.js");
37+
} else {
38+
// In development, use the project root
39+
workerPath = path.resolve(process.cwd(), "worker", "proxy_server.js");
40+
}
41+
42+
logger.info(`Starting proxy worker from path: ${workerPath}`);
43+
44+
const worker = new Worker(workerPath, {
45+
workerData: {
46+
targetOrigin,
47+
port,
3648
},
37-
);
49+
});
3850

3951
worker.on("message", (m) => {
4052
logger.info("[proxy]", m);
@@ -43,8 +55,18 @@ export async function startProxy(
4355
onStarted?.(url);
4456
}
4557
});
46-
worker.on("error", (e) => logger.error("[proxy] error:", e));
47-
worker.on("exit", (c) => logger.info("[proxy] exit", c));
58+
worker.on("error", (e) => {
59+
logger.error("[proxy] error:", e);
60+
// Optionally, you can re-throw or handle the error more gracefully
61+
throw new Error(`Proxy worker failed: ${e.message}`);
62+
});
63+
worker.on("exit", (c) => {
64+
if (c !== 0) {
65+
logger.error(`[proxy] worker stopped with exit code ${c}`);
66+
} else {
67+
logger.info("[proxy] worker exited gracefully");
68+
}
69+
});
4870

4971
return worker; // let the caller keep a handle if desired
5072
}

0 commit comments

Comments
 (0)