MCPcopy
hub / github.com/vercel/next.js / waitForServer

Function waitForServer

test/e2e/next-analyze/next-analyze.test.ts:92–138  ·  view source on GitHub ↗
(process: ChildProcess, timeoutMs: number = 30000)

Source from the content-addressed store, hash-verified

90})
91
92function waitForServer(process: ChildProcess, timeoutMs: number = 30000) {
93 const serverUrlPromise = new Promise<string>((resolve, reject) => {
94 const timeout = setTimeout(() => {
95 process.stdout.off('data', onStdout)
96 process.off('error', onError)
97 process.off('exit', onExit)
98 reject(new Error('Server did not start within timeout'))
99 }, timeoutMs)
100
101 function onStdout(data: Buffer) {
102 const urlMatch = data.toString().match(/http:\/\/[^\s]+/)
103 if (urlMatch) {
104 clearTimeout(timeout)
105 process.stdout.off('data', onStdout)
106 process.off('error', onError)
107 process.off('exit', onExit)
108 resolve(urlMatch[0])
109 }
110 }
111
112 function onError(error: Error) {
113 clearTimeout(timeout)
114 process.stdout.off('data', onStdout)
115 process.off('error', onError)
116 process.off('exit', onExit)
117 reject(error)
118 }
119
120 function onExit(code: number) {
121 clearTimeout(timeout)
122 process.stdout.off('data', onStdout)
123 process.off('error', onError)
124 process.off('exit', onExit)
125 reject(
126 new Error(
127 `Server process exited with code ${code} before URL was emitted`
128 )
129 )
130 }
131
132 process.stdout.on('data', onStdout)
133 process.on('error', onError)
134 process.on('exit', onExit)
135 })
136
137 return serverUrlPromise
138}

Callers 1

Calls 4

setTimeoutFunction · 0.50
rejectFunction · 0.50
offMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected