* Wait for the LSP server to be ready. * * This function polls for a ready signal (set when stdout contains "listening")
(
url: string,
options: WaitOptions = {},
)
| 957 | * This function polls for a ready signal (set when stdout contains "listening") |
| 958 | */ |
| 959 | async function waitForWebSocket( |
| 960 | url: string, |
| 961 | options: WaitOptions = {}, |
| 962 | ): Promise<void> { |
| 963 | const { |
| 964 | delay = 100, // Poll interval |
| 965 | probeTimeout = 5000, // Max wait time |
| 966 | } = options; |
| 967 | |
| 968 | // Extract server ID from URL (e.g., "ws://127.0.0.1:2090" -> check by port) |
| 969 | const portMatch = url.match(/:(\d+)/); |
| 970 | const port = portMatch ? portMatch[1] : null; |
| 971 | |
| 972 | // Find the server ID that's starting on this port |
| 973 | let targetServerId: string | null = null; |
| 974 | const entries = Array.from(managedServers.entries()); |
| 975 | for (const [serverId, entry] of entries) { |
| 976 | if ( |
| 977 | entry.command.includes(`--port ${port}`) || |
| 978 | entry.command.includes(`:${port}`) |
| 979 | ) { |
| 980 | targetServerId = serverId; |
| 981 | break; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | const deadline = Date.now() + probeTimeout; |
| 986 | |
| 987 | while (Date.now() < deadline) { |
| 988 | // Check if we got a ready signal |
| 989 | if (targetServerId && serverReadySignals.has(targetServerId)) { |
| 990 | // Server is ready, clear the signal and return |
| 991 | serverReadySignals.delete(targetServerId); |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | await sleep(delay); |
| 996 | } |
| 997 | |
| 998 | // Timeout reached, proceed anyway (transport will retry if needed) |
| 999 | console.debug( |
| 1000 | `[LSP] waitForWebSocket timed out for ${url}, proceeding anyway`, |
| 1001 | ); |
| 1002 | } |
| 1003 | |
| 1004 | export interface EnsureServerResult { |
| 1005 | uuid: string | null; |
no test coverage detected