* Check if a port is available by attempting to bind to it. * * @param {number} port - The port to check * @param {string} host - The host to bind to * @returns {Promise } True if the port is available
(port, host = "127.0.0.1")
| 233 | * @returns {Promise<boolean>} True if the port is available |
| 234 | */ |
| 235 | function tryPort(port, host = "127.0.0.1") { |
| 236 | return new Promise((resolve) => { |
| 237 | const server = net.createServer(); |
| 238 | server.once("error", () => resolve(false)); |
| 239 | server.listen(port, host, () => { |
| 240 | server.close(() => resolve(true)); |
| 241 | }); |
| 242 | }); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Assert that all listed ports are available, throwing a descriptive error if |
no test coverage detected