Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions node/.changeset/fix-windows-debug-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cluic/codex-remote-proxy": patch
---

Fix Windows startup detection so `crp start --debug` keeps the proxy process running.
41 changes: 40 additions & 1 deletion node/src/server.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import http from "node:http";
import https from "node:https";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { readFileSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { URL } from "node:url";
Expand Down Expand Up @@ -560,6 +562,43 @@ export function startServer(settings = loadConfig()) {
return app;
}

if (import.meta.url === `file://${process.argv[1]}`) {
function isWindowsStylePath(filePath) {
return /^[A-Za-z]:[\\/]/.test(filePath);
}

function modulePathFromMetaUrl(metaUrl) {
const url = new URL(metaUrl);
if (url.protocol !== "file:") {
return null;
}
const pathname = decodeURIComponent(url.pathname);
if (/^\/[A-Za-z]:\//.test(pathname)) {
return path.win32.normalize(pathname.slice(1));
}
return fileURLToPath(metaUrl);
}

function normalizeExecutionPath(filePath) {
if (!filePath) {
return "";
}
if (isWindowsStylePath(filePath)) {
return path.win32.normalize(filePath).toLowerCase();
}
return resolve(filePath);
}

export function isDirectExecution(metaUrl = import.meta.url, argv1 = process.argv[1]) {
if (!argv1) {
return false;
}
const modulePath = modulePathFromMetaUrl(metaUrl);
if (!modulePath) {
return false;
}
return normalizeExecutionPath(modulePath) === normalizeExecutionPath(argv1);
}

if (isDirectExecution()) {
startServer();
}
21 changes: 20 additions & 1 deletion node/test/server.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { join } from "node:path";
import { once } from "node:events";
import { DatabaseSync } from "node:sqlite";

import { createApp } from "../src/server.mjs";
import { createApp, isDirectExecution } from "../src/server.mjs";

function makeTempDir(prefix) {
return join(os.tmpdir(), `${prefix}-${Date.now()}-${Math.random().toString(16).slice(2)}`);
Expand Down Expand Up @@ -133,3 +133,22 @@ test("server writes proxied request and response to sqlite", async () => {

rmSync(dir, { recursive: true, force: true });
});

test("isDirectExecution handles both POSIX and Windows paths", () => {
assert.equal(
isDirectExecution("file:///Users/example/project/node/src/server.mjs", "/Users/example/project/node/src/server.mjs"),
true
);
assert.equal(
isDirectExecution("file:///C:/Users/Xingh/project/node/src/server.mjs", "C:\\Users\\Xingh\\project\\node\\src\\server.mjs"),
true
);
assert.equal(
isDirectExecution("file:///c:/Users/Xingh/project/node/src/server.mjs", "C:/Users/Xingh/project/node/src/server.mjs"),
true
);
assert.equal(
isDirectExecution("file:///C:/Users/Xingh/project/node/src/server.mjs", "C:\\Users\\Xingh\\project\\node\\src\\other.mjs"),
false
);
});
Loading