MCPcopy
hub / github.com/vitejs/vite / httpServerStart

Function httpServerStart

packages/vite/src/node/http.ts:218–269  ·  view source on GitHub ↗
(
  httpServer: HttpServer,
  serverOptions: {
    port: number
    strictPort: boolean | undefined
    host: string | undefined
    logger: Logger
  },
)

Source from the content-addressed store, hash-verified

216const MAX_PORT = 65535
217
218export async function httpServerStart(
219 httpServer: HttpServer,
220 serverOptions: {
221 port: number
222 strictPort: boolean | undefined
223 host: string | undefined
224 logger: Logger
225 },
226): Promise<number> {
227 const { port: startPort, strictPort, host, logger } = serverOptions
228
229 for (let port = startPort; port <= MAX_PORT; port++) {
230 // Pre-check port availability on wildcard addresses (0.0.0.0, ::)
231 // so that we avoid conflicts with other servers listening on all interfaces
232 const portAvailableOnWildcard = await isPortAvailable(port)
233
234 // If port is not available on a wildcard address but strictPort is set,
235 // we still try binding directly before giving up.
236 if (strictPort) {
237 const result = await tryBindServer(httpServer, port, host)
238 if (result.success) {
239 if (!portAvailableOnWildcard) {
240 logger.warn(
241 colors.yellow(
242 `Port ${port} is in use on a wildcard address, but ${host ?? 'localhost'}:${port} is available. ` +
243 `There may be another server running on a wildcard IP on port ${port}.`,
244 ),
245 )
246 }
247 return port
248 }
249 if (result.error.code !== 'EADDRINUSE') {
250 throw result.error
251 }
252 throw new Error(`Port ${port} is already in use`)
253 }
254
255 if (portAvailableOnWildcard) {
256 const result = await tryBindServer(httpServer, port, host)
257 if (result.success) {
258 return port
259 }
260 if (result.error.code !== 'EADDRINUSE') {
261 throw result.error
262 }
263 }
264 logger.info(`Port ${port} is in use, trying another one...`)
265 }
266 throw new Error(
267 `No available ports found between ${startPort} and ${MAX_PORT}`,
268 )
269}
270
271export function setClientErrorHandler(
272 server: HttpServer,

Callers 2

previewFunction · 0.90
startServerFunction · 0.90

Calls 4

isPortAvailableFunction · 0.85
tryBindServerFunction · 0.85
warnMethod · 0.65
infoMethod · 0.65

Tested by

no test coverage detected