refactor: nodecar cleanup

This commit is contained in:
zhom
2025-08-01 00:58:15 +04:00
parent f4c33ad96e
commit da7f791274
10 changed files with 52 additions and 85 deletions
+7 -12
View File
@@ -87,14 +87,13 @@ export async function startCamoufoxProcess(
if (line.trim()) {
try {
const parsed = JSON.parse(line.trim());
if (parsed.success && parsed.id === id && parsed.port) {
if (parsed.success && parsed.id === id && parsed.processId) {
if (!resolved) {
resolved = true;
clearTimeout(timeout);
// Update config with server details
config.port = parsed.port;
config.wsEndpoint = parsed.wsEndpoint;
config.processId = parsed.processId;
saveCamoufoxConfig(config);
// Unref immediately after success to detach properly
child.unref();
resolve(config);
@@ -177,25 +176,21 @@ export async function stopCamoufoxProcess(id: string): Promise<boolean> {
}
try {
// Try to find and kill the worker process using multiple methods
const { spawn } = await import("node:child_process");
// Method 1: Kill by process pattern
const killByPattern = spawn("pkill", ["-f", `camoufox-worker.*${id}`], {
stdio: "ignore",
});
// Method 2: If we have a port (which is actually the process PID), kill by PID
if (config.port) {
// Method 2: If we have a process ID, kill by PID
if (config.processId) {
try {
process.kill(config.port, "SIGTERM");
process.kill(config.processId, "SIGTERM");
// Give it a moment to terminate gracefully
await new Promise((resolve) => setTimeout(resolve, 2000));
// Force kill if still running
try {
process.kill(config.port, "SIGKILL");
process.kill(config.processId, "SIGKILL");
} catch {
// Process already terminated
}
+1 -19
View File
@@ -8,8 +8,7 @@ export interface CamoufoxConfig {
options: LaunchOptions;
profilePath?: string;
url?: string;
port?: number;
wsEndpoint?: string;
processId?: number;
}
const STORAGE_DIR = path.join(tmp.tmpdir, "donutbrowser", "camoufox");
@@ -126,23 +125,6 @@ export function updateCamoufoxConfig(config: CamoufoxConfig): boolean {
}
}
/**
* Check if a Camoufox server is running
* @param port The port to check
* @returns True if running, false otherwise
*/
export async function isServerRunning(port: number): Promise<boolean> {
try {
const response = await fetch(`http://localhost:${port}/json/version`, {
method: "GET",
signal: AbortSignal.timeout(1000),
});
return response.ok;
} catch {
return false;
}
}
/**
* Generate a unique ID for a Camoufox instance
* @returns A unique ID string
+9 -17
View File
@@ -27,16 +27,14 @@ export async function runCamoufoxWorker(id: string): Promise<void> {
JSON.stringify({
success: true,
id: id,
port: processId,
wsEndpoint: `ws://localhost:0/camoufox-${id}`,
processId,
profilePath: config.profilePath,
message: "Camoufox worker started successfully",
}),
);
// Update config with process details
config.port = processId;
config.wsEndpoint = `ws://localhost:0/camoufox-${id}`;
config.processId = processId;
saveCamoufoxConfig(config);
// Handle process termination gracefully
@@ -60,11 +58,16 @@ export async function runCamoufoxWorker(id: string): Promise<void> {
camoufoxOptions.user_data_dir = config.profilePath;
}
// Theming
// Remove custom properties before passing to Camoufox
camoufoxOptions.disableTheming = true;
camoufoxOptions.showcursor = false;
// Default to headless for tests
// Set Firefox preferences for theming
if (!camoufoxOptions.firefox_user_prefs) {
camoufoxOptions.firefox_user_prefs = {};
}
// Default to non-headless for visibility
if (camoufoxOptions.headless === undefined) {
camoufoxOptions.headless = false;
}
@@ -72,17 +75,6 @@ export async function runCamoufoxWorker(id: string): Promise<void> {
const browser = await Camoufox(camoufoxOptions);
const context = await browser.newContext();
// Update config with actual browser details
let wsEndpoint: string | undefined;
try {
const browserWithWs = browser as any;
wsEndpoint =
browserWithWs.wsEndpoint?.() || `ws://localhost:0/camoufox-${id}`;
} catch {
wsEndpoint = `ws://localhost:0/camoufox-${id}`;
}
config.wsEndpoint = wsEndpoint;
saveCamoufoxConfig(config);
// Handle URL opening if provided
+3 -4
View File
@@ -396,9 +396,9 @@ program
}
}
// Theming
// Theming and cursor - these are custom properties for camoufox-js
if (options.disableTheming) camoufoxOptions.disableTheming = true;
if (options.noShowcursor) camoufoxOptions.showcursor = false;
if (options.showcursor === false) camoufoxOptions.showcursor = false;
// Use the launcher to start Camoufox properly
const config = await startCamoufoxProcess(
@@ -413,8 +413,7 @@ program
console.log(
JSON.stringify({
id: config.id,
port: config.port,
wsEndpoint: config.wsEndpoint,
processId: config.processId,
profilePath: config.profilePath,
url: config.url,
}),