| 16 | } |
| 17 | |
| 18 | function decodeMessages(buffer) { |
| 19 | const messages = []; |
| 20 | let remaining = buffer; |
| 21 | |
| 22 | while (remaining.length > 0) { |
| 23 | const headerEnd = remaining.indexOf('\r\n\r\n'); |
| 24 | if (headerEnd === -1) break; |
| 25 | |
| 26 | const header = remaining.slice(0, headerEnd); |
| 27 | const lengthMatch = header.match(/Content-Length: (\d+)/); |
| 28 | if (!lengthMatch) break; |
| 29 | |
| 30 | const contentLength = parseInt(lengthMatch[1]); |
| 31 | const contentStart = headerEnd + 4; |
| 32 | |
| 33 | if (remaining.length < contentStart + contentLength) break; |
| 34 | |
| 35 | const content = remaining.slice(contentStart, contentStart + contentLength); |
| 36 | try { |
| 37 | messages.push(JSON.parse(content)); |
| 38 | } catch {} |
| 39 | |
| 40 | remaining = remaining.slice(contentStart + contentLength); |
| 41 | } |
| 42 | |
| 43 | return { messages, remaining }; |
| 44 | } |
| 45 | |
| 46 | // Detect which language server to use based on project files |
| 47 | function detectServer(cwd) { |