* @param {number} basePort base port * @param {string=} host host * @returns {Promise } resolved port
(basePort, host)
| 90 | * @returns {Promise<number>} resolved port |
| 91 | */ |
| 92 | async function getPorts(basePort, host) { |
| 93 | if (basePort < minPort || basePort > maxPort) { |
| 94 | throw new Error(`Port number must lie between ${minPort} and ${maxPort}`); |
| 95 | } |
| 96 | |
| 97 | let port = basePort; |
| 98 | |
| 99 | const localhosts = getLocalHosts(); |
| 100 | const hosts = |
| 101 | host && !localhosts.has(host) |
| 102 | ? new Set([host]) |
| 103 | : /* If the host is equivalent to localhost |
| 104 | we need to check every equivalent host |
| 105 | else the port might falsely appear as available |
| 106 | on some operating systems */ |
| 107 | localhosts; |
| 108 | /** @type {Set<string | undefined>} */ |
| 109 | const portUnavailableErrors = new Set(["EADDRINUSE", "EACCES"]); |
| 110 | while (port <= maxPort) { |
| 111 | try { |
| 112 | const availablePort = await getAvailablePort(port, hosts); |
| 113 | return availablePort; |
| 114 | } catch (error) { |
| 115 | /* Try next port if port is busy; throw for any other error */ |
| 116 | if ( |
| 117 | !portUnavailableErrors.has( |
| 118 | /** @type {NodeJS.ErrnoException} */ (error).code, |
| 119 | ) |
| 120 | ) { |
| 121 | throw error; |
| 122 | } |
| 123 | port += 1; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | throw new Error("No available ports found"); |
| 128 | } |
| 129 | |
| 130 | export default getPorts; |
nothing calls this directly
no test coverage detected
searching dependent graphs…