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

Function findFreePort

scripts/dev-safe.mjs:208–226  ·  view source on GitHub ↗
(preferredPort, host = "127.0.0.1")

Source from the content-addressed store, hash-verified

206 * @returns {Promise<number>} The actual port that was acquired
207 */
208export async function findFreePort(preferredPort, host = "127.0.0.1") {
209 // If preferredPort is 0, skip the check and go straight to OS assignment
210 if (preferredPort > 0) {
211 const preferredAvailable = await tryPort(preferredPort, host);
212 if (preferredAvailable) {
213 return preferredPort;
214 }
215 }
216
217 // Fall back to OS-assigned port
218 return new Promise((resolve, reject) => {
219 const server = net.createServer();
220 server.once("error", reject);
221 server.listen(0, host, () => {
222 const { port } = server.address();
223 server.close(() => resolve(port));
224 });
225 });
226}
227
228/**
229 * Check if a port is available by attempting to bind to it.

Callers 2

dev-safe.test.tsFile · 0.90
findFreePortsFunction · 0.85

Calls 2

tryPortFunction · 0.85
closeMethod · 0.80

Tested by

no test coverage detected