MCPcopy Index your code
hub / github.com/webpack/webpack-dev-server / getPorts

Function getPorts

lib/getPort.js:92–128  ·  view source on GitHub ↗

* @param {number} basePort base port * @param {string=} host host * @returns {Promise } resolved port

(basePort, host)

Source from the content-addressed store, hash-verified

90 * @returns {Promise<number>} resolved port
91 */
92async 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
130export default getPorts;

Callers

nothing calls this directly

Calls 2

getLocalHostsFunction · 0.85
getAvailablePortFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…