MCPcopy
hub / github.com/garrytan/gstack / findPort

Function findPort

browse/src/server.ts:879–900  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

877
878// Find port: explicit BROWSE_PORT, or random in 10000-60000
879async function findPort(): Promise<number> {
880 // Explicit port override (for debugging)
881 if (BROWSE_PORT) {
882 const result = await checkPortAvailable(BROWSE_PORT);
883 if (result.available) {
884 return BROWSE_PORT;
885 }
886 throw formatExplicitPortUnavailableError(BROWSE_PORT, result);
887 }
888
889 // Random port with retry
890 const attempts: FailedPortAttempt[] = [];
891 for (let attempt = 0; attempt < RANDOM_PORT_RETRIES; attempt++) {
892 const port = RANDOM_PORT_MIN + Math.floor(Math.random() * (RANDOM_PORT_MAX - RANDOM_PORT_MIN));
893 const result = await checkPortAvailable(port);
894 if (result.available) {
895 return port;
896 }
897 attempts.push({ port, result });
898 }
899 throw formatRandomPortUnavailableError(attempts);
900}
901
902/**
903 * Translate Playwright errors into actionable messages for AI agents.

Callers 1

startFunction · 0.85

Calls 4

checkPortAvailableFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected