Skip to content
Open
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
18 changes: 12 additions & 6 deletions skills/dev-browser/scripts/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,31 @@ try {
console.log("You may need to run: npx playwright install chromium");
}

// Parse port configuration from environment
const port = parseInt(process.env.PORT || "9222", 10);
const cdpPort = process.env.CDP_PORT ? parseInt(process.env.CDP_PORT, 10) : undefined;
const effectiveCdpPort = cdpPort || port + 1;

// Check if server is already running
console.log("Checking for existing servers...");
try {
const res = await fetch("http://localhost:9222", {
const res = await fetch(`http://localhost:${port}`, {
signal: AbortSignal.timeout(1000),
});
if (res.ok) {
console.log("Server already running on port 9222");
console.log(`Server already running on port ${port}`);
process.exit(0);
}
} catch {
// Server not running, continue to start
}

// Clean up stale CDP port if HTTP server isn't running (crash recovery)
// This handles the case where Node crashed but Chrome is still running on 9223
// This handles the case where Node crashed but Chrome is still running
try {
const pid = execSync("lsof -ti:9223", { encoding: "utf-8" }).trim();
const pid = execSync(`lsof -ti:${effectiveCdpPort}`, { encoding: "utf-8" }).trim();
if (pid) {
console.log(`Cleaning up stale Chrome process on CDP port 9223 (PID: ${pid})`);
console.log(`Cleaning up stale Chrome process on CDP port ${effectiveCdpPort} (PID: ${pid})`);
execSync(`kill -9 ${pid}`);
}
} catch {
Expand All @@ -101,7 +106,8 @@ try {
console.log("Starting dev browser server...");
const headless = process.env.HEADLESS === "true";
const server = await serve({
port: 9222,
port,
cdpPort,
headless,
profileDir,
});
Expand Down
9 changes: 8 additions & 1 deletion skills/dev-browser/server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ cd "$SCRIPT_DIR"

# Parse command line arguments
HEADLESS=false
PORT=9222
CDP_PORT=""

while [[ "$#" -gt 0 ]]; do
case $1 in
--headless) HEADLESS=true ;;
--port) PORT="$2"; shift ;;
--cdp-port) CDP_PORT="$2"; shift ;;
*) echo "Unknown parameter: $1"; exit 1 ;;
esac
shift
Expand All @@ -19,6 +24,8 @@ done
echo "Installing dependencies..."
npm install

echo "Starting dev-browser server..."
echo "Starting dev-browser server on port $PORT..."
export HEADLESS=$HEADLESS
export PORT=$PORT
export CDP_PORT=$CDP_PORT
npx tsx scripts/start-server.ts