| 66 | |
| 67 | /** Ask the OS for a free localhost port (for SSH tunnels). */ |
| 68 | const freePort = (): Promise<number> => |
| 69 | new Promise((resolve, reject) => { |
| 70 | const srv = net.createServer(); |
| 71 | srv.on("error", reject); |
| 72 | srv.listen(0, "127.0.0.1", () => { |
| 73 | const port = (srv.address() as net.AddressInfo).port; |
| 74 | srv.close(() => resolve(port)); |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | /** Resolve once a TCP connect to localhost:port succeeds (SSH bound the forward). */ |
| 79 | const waitLocalPort = async (port: number, attempts = 40): Promise<void> => { |