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

Function startedOnPort

playground/cli/__tests__/serve.ts:120–145  ·  view source on GitHub ↗
(serverProcess, port, timeout)

Source from the content-addressed store, hash-verified

118
119// helper to validate that server was started on the correct port
120async function startedOnPort(serverProcess, port, timeout) {
121 let checkPort
122 const startedPromise = new Promise<void>((resolve, reject) => {
123 checkPort = (data) => {
124 const str = stripVTControlCharacters(data.toString())
125 const match = str.match(
126 /http:\/\/(?:localhost|127\.0\.0\.1|\[::1\]):(\d{4})/,
127 )
128 if (match) {
129 const startedPort = parseInt(match[1], 10)
130 if (startedPort === port) {
131 resolve()
132 } else {
133 const msg = `server listens on port ${startedPort} instead of ${port}`
134 reject(msg)
135 }
136 }
137 }
138 serverProcess.stdout.on('data', checkPort)
139 })
140 return resolvedOrTimeout(
141 startedPromise,
142 timeout,
143 `failed to start within ${timeout}ms`,
144 ).finally(() => serverProcess.stdout.off('data', checkPort))
145}
146
147// helper function that rejects with errorMessage if promise isn't settled within ms
148async function resolvedOrTimeout(promise, ms, errorMessage) {

Callers 1

serveFunction · 0.70

Calls 4

resolvedOrTimeoutFunction · 0.70
onMethod · 0.65
offMethod · 0.65
resolveFunction · 0.50

Tested by

no test coverage detected