|
1 | | -import { createConnection, createServer } from 'net'; |
| 1 | +import { createServer } from 'net'; |
| 2 | +import { request as httpRequest } from 'node:http'; |
2 | 3 |
|
3 | 4 | /** Check if a port is available on a specific host */ |
4 | 5 | function checkPort(port: number, host: string): Promise<boolean> { |
@@ -39,24 +40,22 @@ export async function waitForPort(port: number, timeoutMs = 3000): Promise<boole |
39 | 40 | return false; |
40 | 41 | } |
41 | 42 |
|
42 | | -/** Wait until a server is accepting connections on the given port, with timeout. */ |
43 | | -export async function waitForServerReady(port: number, timeoutMs = 60000): Promise<boolean> { |
| 43 | +/** Wait until a server is responding to HTTP requests on the given port, with timeout. */ |
| 44 | +export function waitForServerReady(port: number, timeoutMs = 60000): Promise<boolean> { |
44 | 45 | const start = Date.now(); |
45 | | - while (Date.now() - start < timeoutMs) { |
46 | | - const listening = await new Promise<boolean>(resolve => { |
47 | | - const socket = createConnection({ port, host: '127.0.0.1' }, () => { |
48 | | - socket.destroy(); |
| 46 | + return new Promise(resolve => { |
| 47 | + const attempt = () => { |
| 48 | + if (Date.now() - start > timeoutMs) return resolve(false); |
| 49 | + const req = httpRequest({ hostname: '127.0.0.1', port, path: '/', method: 'GET', timeout: 2000 }, res => { |
| 50 | + res.resume(); |
49 | 51 | resolve(true); |
50 | 52 | }); |
51 | | - socket.on('error', () => { |
52 | | - socket.destroy(); |
53 | | - resolve(false); |
54 | | - }); |
55 | | - }); |
56 | | - if (listening) return true; |
57 | | - await new Promise(resolve => setTimeout(resolve, 500)); |
58 | | - } |
59 | | - return false; |
| 53 | + req.on('error', () => setTimeout(attempt, 500)); |
| 54 | + req.on('timeout', () => req.destroy()); |
| 55 | + req.end(); |
| 56 | + }; |
| 57 | + attempt(); |
| 58 | + }); |
60 | 59 | } |
61 | 60 |
|
62 | 61 | /** Sleep helper for retry delays. */ |
|
0 commit comments