MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / resolveFrontendPort

Function resolveFrontendPort

scripts/dev-safe.mjs:351–377  ·  view source on GitHub ↗
(
  preferredPort,
  { bindHost = "127.0.0.1", reservedPorts = [] } = {},
)

Source from the content-addressed store, hash-verified

349 * @returns {Promise<number>} The port the frontend should bind
350 */
351export async function resolveFrontendPort(
352 preferredPort,
353 { bindHost = "127.0.0.1", reservedPorts = [] } = {},
354) {
355 const answersOnLoopback = async (port) =>
356 (
357 await Promise.all([
358 isPortBusy(port, "127.0.0.1"),
359 isPortBusy(port, "::1"),
360 ])
361 ).includes(true);
362 const isUsable = async (port) =>
363 !reservedPorts.includes(port) &&
364 (await tryPort(port, bindHost)) &&
365 !(await answersOnLoopback(port));
366
367 if (await isUsable(preferredPort)) return preferredPort;
368
369 const maxAttempts = 100;
370 for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
371 const port = await findFreePort(0, bindHost);
372 if (await isUsable(port)) return port;
373 }
374 throw new Error(
375 `Could not allocate a frontend port after ${maxAttempts} attempts`,
376 );
377}
378
379export function formatMissingUvxGuidance(cwd = process.cwd()) {
380 const readmePath = path.join(cwd, "README.md");

Callers 2

buildConfigFunction · 0.90
dev-safe.test.tsFile · 0.90

Calls 2

isUsableFunction · 0.85
findFreePortFunction · 0.85

Tested by

no test coverage detected