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

Function startedOnPort

playground/cli-module/__tests__/serve.ts:117–142  ·  view source on GitHub ↗
(serverProcess, port, timeout)

Source from the content-addressed store, hash-verified

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