(port: number, hostname: string = '127.0.0.1')
| 851 | // in the Node.js polyfill where listen/close are async but the caller |
| 852 | // expects synchronous bind semantics. See: #486 |
| 853 | function checkPortAvailable(port: number, hostname: string = '127.0.0.1'): Promise<PortCheckResult> { |
| 854 | return new Promise((resolve) => { |
| 855 | const srv = net.createServer(); |
| 856 | let settled = false; |
| 857 | const finish = (result: PortCheckResult) => { |
| 858 | if (settled) return; |
| 859 | settled = true; |
| 860 | resolve(result); |
| 861 | }; |
| 862 | |
| 863 | srv.once('error', (err) => finish(normalizePortError(err))); |
| 864 | try { |
| 865 | srv.listen(port, hostname, () => { |
| 866 | srv.close(() => finish({ available: true })); |
| 867 | }); |
| 868 | } catch (err) { |
| 869 | finish(normalizePortError(err)); |
| 870 | } |
| 871 | }); |
| 872 | } |
| 873 | |
| 874 | function isPortAvailable(port: number, hostname: string = '127.0.0.1'): Promise<boolean> { |
| 875 | return checkPortAvailable(port, hostname).then((result) => result.available); |
no test coverage detected