@@ -143,6 +143,11 @@ export async function sendCommand(
|
||||
sessionId: string,
|
||||
timeout = SEND_COMMAND_TIMEOUT,
|
||||
): Promise<DaemonResponse> {
|
||||
// Before connecting and sending, verify the daemon is still alive.
|
||||
if (!isDaemonRunning(sessionId)) {
|
||||
throw new Error('Daemon is not running.');
|
||||
}
|
||||
|
||||
const socketPath = getSocketPath(sessionId);
|
||||
|
||||
const socket = net.createConnection({
|
||||
|
||||
@@ -262,7 +262,7 @@ async function startSocketServer() {
|
||||
});
|
||||
}
|
||||
|
||||
async function cleanup() {
|
||||
async function cleanup(exitCode = 0) {
|
||||
console.log('Cleaning up daemon...');
|
||||
|
||||
try {
|
||||
@@ -291,7 +291,7 @@ async function cleanup() {
|
||||
if (fs.existsSync(pidFilePath)) {
|
||||
fs.unlinkSync(pidFilePath);
|
||||
}
|
||||
process.exit(0);
|
||||
process.exit(exitCode);
|
||||
}
|
||||
|
||||
// Handle shutdown signals
|
||||
@@ -308,13 +308,15 @@ process.on('SIGHUP', () => {
|
||||
// Handle uncaught errors
|
||||
process.on('uncaughtException', error => {
|
||||
logger?.('Uncaught exception:', error);
|
||||
void cleanup(1);
|
||||
});
|
||||
process.on('unhandledRejection', error => {
|
||||
logger?.('Unhandled rejection:', error);
|
||||
void cleanup(1);
|
||||
});
|
||||
|
||||
// Start the server
|
||||
const started = startSocketServer().catch(error => {
|
||||
logger?.('Failed to start daemon server:', error);
|
||||
process.exit(1);
|
||||
void cleanup(1);
|
||||
});
|
||||
|
||||
+3
-1
@@ -26,13 +26,15 @@ export const DAEMON_CLIENT_NAME = 'chrome-devtools-cli-daemon';
|
||||
// Using these paths due to strict limits on the POSIX socket path length.
|
||||
export function getSocketPath(sessionId: string): string {
|
||||
const uid = os.userInfo().uid;
|
||||
const username = os.userInfo().username;
|
||||
const suffix = sessionId ? `-${sessionId}` : '';
|
||||
const appName = APP_NAME + suffix;
|
||||
|
||||
if (IS_WINDOWS) {
|
||||
// Windows uses Named Pipes, not file paths.
|
||||
// This format is required for server.listen()
|
||||
return path.join('\\\\.\\pipe', appName, 'server.sock');
|
||||
// Append username to prevent cross-user named pipe squatting
|
||||
return path.join('\\\\.\\pipe', `${appName}-${username}`, 'server.sock');
|
||||
}
|
||||
|
||||
// 1. Try XDG_RUNTIME_DIR (Linux standard, sometimes macOS)
|
||||
|
||||
Reference in New Issue
Block a user