|
| 1 | +import { isSafePort } from "./unsafe-ports"; |
| 2 | + |
| 3 | +import type { |
| 4 | + GetPortInput, |
| 5 | + PortNumber, |
| 6 | + GetPortOptions, |
| 7 | + HostAddress, |
| 8 | + WaitForPortOptions, |
| 9 | +} from "./types"; |
| 10 | + |
| 11 | +import { |
| 12 | + GetPortError, |
| 13 | + _findPort, |
| 14 | + _fmtOnHost, |
| 15 | + _generateRange, |
| 16 | + _getLocalHosts, |
| 17 | + _tryPort, |
| 18 | + _log, |
| 19 | + _validateHostname, |
| 20 | +} from "./_internal"; |
| 21 | + |
| 22 | +export async function getPort( |
| 23 | + _userOptions: GetPortInput = {}, |
| 24 | +): Promise<PortNumber> { |
| 25 | + if (typeof _userOptions === "number" || typeof _userOptions === "string") { |
| 26 | + _userOptions = { port: Number.parseInt(_userOptions + "") || 0 }; |
| 27 | + } |
| 28 | + |
| 29 | + const _port = Number(_userOptions.port ?? process.env.PORT ?? 3000); |
| 30 | + |
| 31 | + const options = { |
| 32 | + name: "default", |
| 33 | + random: _port === 0, |
| 34 | + ports: [], |
| 35 | + portRange: [], |
| 36 | + alternativePortRange: _userOptions.port ? [] : [3000, 3100], |
| 37 | + verbose: false, |
| 38 | + ..._userOptions, |
| 39 | + port: _port, |
| 40 | + host: _validateHostname(_userOptions.host ?? process.env.HOST), |
| 41 | + } as GetPortOptions; |
| 42 | + |
| 43 | + if (options.random) { |
| 44 | + return getRandomPort(options.host); |
| 45 | + } |
| 46 | + |
| 47 | + // Generate list of ports to check |
| 48 | + const portsToCheck: PortNumber[] = [ |
| 49 | + options.port, |
| 50 | + ...options.ports, |
| 51 | + ..._generateRange(...options.portRange), |
| 52 | + ].filter((port) => { |
| 53 | + if (!port) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + if (!isSafePort(port)) { |
| 57 | + _log(options.verbose, `Ignoring unsafe port: ${port}`); |
| 58 | + return false; |
| 59 | + } |
| 60 | + return true; |
| 61 | + }); |
| 62 | + |
| 63 | + // Try to find a port |
| 64 | + let availablePort = await _findPort(portsToCheck, options.host); |
| 65 | + |
| 66 | + // Try fallback port range |
| 67 | + if (!availablePort && options.alternativePortRange.length > 0) { |
| 68 | + availablePort = await _findPort( |
| 69 | + _generateRange(...options.alternativePortRange), |
| 70 | + options.host, |
| 71 | + ); |
| 72 | + _log( |
| 73 | + options.verbose, |
| 74 | + `Unable to find an available port (tried ${options.alternativePortRange.join( |
| 75 | + "-", |
| 76 | + )} ${_fmtOnHost(options.host)}). Using alternative port ${availablePort}`, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + // Try random port |
| 81 | + if (!availablePort && _userOptions.random !== false) { |
| 82 | + availablePort = await getRandomPort(options.host); |
| 83 | + if (availablePort) { |
| 84 | + _log(options.verbose, `Using random port ${availablePort}`); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Throw error if no port is available |
| 89 | + if (!availablePort) { |
| 90 | + const triedRanges = [ |
| 91 | + options.port, |
| 92 | + options.portRange.join("-"), |
| 93 | + options.alternativePortRange.join("-"), |
| 94 | + ] |
| 95 | + .filter(Boolean) |
| 96 | + .join(", "); |
| 97 | + throw new GetPortError( |
| 98 | + `Unable to find find available port ${_fmtOnHost( |
| 99 | + options.host, |
| 100 | + )} (tried ${triedRanges})`, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return availablePort; |
| 105 | +} |
| 106 | + |
| 107 | +export async function getRandomPort(host?: HostAddress) { |
| 108 | + const port = await checkPort(0, host); |
| 109 | + if (port === false) { |
| 110 | + throw new GetPortError( |
| 111 | + `Unable to find any random port ${_fmtOnHost(host)}`, |
| 112 | + ); |
| 113 | + } |
| 114 | + return port; |
| 115 | +} |
| 116 | + |
| 117 | +export async function waitForPort( |
| 118 | + port: PortNumber, |
| 119 | + options: WaitForPortOptions = {}, |
| 120 | +) { |
| 121 | + const delay = options.delay || 500; |
| 122 | + const retries = options.retries || 4; |
| 123 | + for (let index = retries; index > 0; index--) { |
| 124 | + if ((await _tryPort(port, options.host)) === false) { |
| 125 | + return; |
| 126 | + } |
| 127 | + await new Promise((resolve) => setTimeout(resolve, delay)); |
| 128 | + } |
| 129 | + throw new GetPortError( |
| 130 | + `Timeout waiting for port ${port} after ${retries} retries with ${delay}ms interval.`, |
| 131 | + ); |
| 132 | +} |
| 133 | + |
| 134 | +export async function checkPort( |
| 135 | + port: PortNumber, |
| 136 | + host: HostAddress | HostAddress[] = process.env.HOST, |
| 137 | + verbose?: boolean, |
| 138 | +): Promise<PortNumber | false> { |
| 139 | + if (!host) { |
| 140 | + host = _getLocalHosts([undefined /* default */, "0.0.0.0"]); |
| 141 | + } |
| 142 | + if (!Array.isArray(host)) { |
| 143 | + return _tryPort(port, host); |
| 144 | + } |
| 145 | + for (const _host of host) { |
| 146 | + const _port = await _tryPort(port, _host); |
| 147 | + if (_port === false) { |
| 148 | + if (port < 1024 && verbose) { |
| 149 | + _log( |
| 150 | + verbose, |
| 151 | + `Unable to listen to the priviliged port ${port} ${_fmtOnHost( |
| 152 | + _host, |
| 153 | + )}`, |
| 154 | + ); |
| 155 | + } |
| 156 | + return false; |
| 157 | + } |
| 158 | + if (port === 0 && _port !== 0) { |
| 159 | + port = _port; |
| 160 | + } |
| 161 | + } |
| 162 | + return port; |
| 163 | +} |
0 commit comments