| 1 |
#!/usr/bin/env node |
| 2 |
|
| 3 |
//===-------------------------------------------------------------------------------- |
| 4 |
// Copyright: Eran Ifrah |
| 5 |
// This scripts attempts to launch LSP server(s) |
| 6 |
// and pass the communication with them over socket |
| 7 |
// to CodeLite |
| 8 |
//===-------------------------------------------------------------------------------- |
| 9 |
let net = require('net'); |
| 10 |
const { spawn } = require('child_process'); |
| 11 |
|
| 12 |
///===------------------------------------------------------------------------------- |
| 13 |
/// Message |
| 14 |
///===------------------------------------------------------------------------------- |
| 15 |
class Message |
| 16 |
{ |
| 17 |
constructor() |
| 18 |
{ |
| 19 |
this.content = ""; |
| 20 |
this.headersMap = new Map() |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* read header from the buffer |
| 25 |
*/ |
| 26 |
|
| 27 |
getContentLength() |
| 28 |
{ |
| 29 |
if(this.headersMap.has("content-length")) { |
| 30 |
return parseInt(this.headersMap.get("content-length")); |
| 31 |
} |
| 32 |
return -1; |
| 33 |
} |
| 34 |
|
| 35 |
parse(buffer) |
| 36 |
{ |
| 37 |
let str = Buffer.isBuffer(buffer) ? buffer.toString() : buffer; |
| 38 |
let where = str.indexOf("\r\n\r\n"); |
| 39 |
if(where != -1) { |
| 40 |
this.content = str.substr(where + 4); |
| 41 |
let headers = str.substr(0, where).split("\r\n"); |
| 42 |
this.headersMap.clear(); |
| 43 |
headers.forEach(function(v) { |
| 44 |
let pair = v.split(":"); |
| 45 |
if(pair.length == 2) { |
| 46 |
let key = pair[0].trim().toLowerCase(); |
| 47 |
let val = pair[1].trim().toLowerCase(); |
| 48 |
this.headersMap.set(key, val); |
| 49 |
} |
| 50 |
}.bind(this)); |
| 51 |
} |
| 52 |
let contentLen = this.getContentLength(); |
| 53 |
if((contentLen == -1) || (contentLen > this.content.length)) { return undefined; } |
| 54 |
let messageBuffer = this.content.substr(0, contentLen); |
| 55 |
let remainder = this.content.substr(contentLen); |
| 56 |
|
| 57 |
try { |
| 58 |
let requestObject = JSON.parse(messageBuffer); |
| 59 |
return { request: requestObject, remainder: remainder }; |
| 60 |
} catch (e) { |
| 61 |
return undefined; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
///===--------------------------------------------------------------------------------------------- |
| 67 |
///===--------------------------------------------------------------------------------------------- |
| 68 |
///===--------------------------------------------------------------------------------------------- |
| 69 |
|
| 70 |
/** |
| 71 |
* attempt to consume a single message from the incoming buffer |
| 72 |
* @param conn socket on which the data received |
| 73 |
*/ |
| 74 |
function tryProcessMessage(conn) |
| 75 |
{ |
| 76 |
if(conn.incoming_buffer.length > 0) { |
| 77 |
let message = new Message(); |
| 78 |
let command = message.parse(conn.incoming_buffer); |
| 79 |
if(command == undefined) { |
| 80 |
console.log("<<< Message is incomplete. incoming_buffer: " + conn.incoming_buffer.length); |
| 81 |
return false; |
| 82 |
} |
| 83 |
conn.incoming_buffer = command.remainder; |
| 84 |
// console.log(`<<< Message is complete. incoming_buffer: ${conn.incoming_buffer.length}`); |
| 85 |
processCommand(command.request, conn); |
| 86 |
return true; |
| 87 |
} |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* start LSP server and associate it with the TCP connection |
| 93 |
*/ |
| 94 |
function processCommand(command, conn) |
| 95 |
{ |
| 96 |
switch(command.method) { |
| 97 |
case 'execute': |
| 98 |
// The 'execute' is a special command that we handle it ourself |
| 99 |
let lsp = {} |
| 100 |
try { |
| 101 |
console.log(JSON.stringify(command.command)); |
| 102 |
lsp = spawn(command.command, command.args, {cwd : command.wd}); |
| 103 |
console.log("LSP: process started"); |
| 104 |
|
| 105 |
} catch(e) { |
| 106 |
console.log(e.message); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
if(lsp != undefined) { |
| 111 |
conn.lsp_process = lsp; |
| 112 |
lsp.stdout.on('data', (data) => { |
| 113 |
console.log("LSP\n" + data.toString()); |
| 114 |
conn.write(data.toString()); // pass the data as-is back to CodeLite |
| 115 |
// Now that we got a reply from the LSP, try to process the next message |
| 116 |
tryProcessMessage(conn); |
| 117 |
}); |
| 118 |
lsp.stderr.on('data', (data) => { |
| 119 |
console.error("LSP\n" + data.toString()); |
| 120 |
// Now that we got a reply from the LSP, try to process the next message |
| 121 |
tryProcessMessage(conn); |
| 122 |
}); |
| 123 |
lsp.on('close', (code) => { |
| 124 |
// Close the tcp connection |
| 125 |
process.exit(0); |
| 126 |
}); |
| 127 |
} |
| 128 |
break; |
| 129 |
default: |
| 130 |
if(conn.lsp_process != undefined) { |
| 131 |
let asString = JSON.stringify(command); |
| 132 |
let jsonMessage = "Content-Length: " + asString.length + "\r\n\r\n" + asString; |
| 133 |
conn.lsp_process.stdin.write(jsonMessage); |
| 134 |
} |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* this callback will get called when data arrives on the socket |
| 141 |
* @param data string|buffer |
| 142 |
* @this the connection (socket) |
| 143 |
*/ |
| 144 |
function onDataRead(data) |
| 145 |
{ |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
//===-------------------------------------------------------------------------------- |
| 150 |
// Main |
| 151 |
//===-------------------------------------------------------------------------------- |
| 152 |
|
| 153 |
|
| 154 |
function connectToCodeLite() |
| 155 |
{ |
| 156 |
let socket = net.Socket(); |
| 157 |
|
| 158 |
// Make the server TCP/IP server on port 12898 |
| 159 |
let port = (process.argv.length > 2) ? process.argv[2] : 12898; |
| 160 |
socket.on('error', (code) => { |
| 161 |
console.log(`Unable to connect to CodeLite. ${code}`); |
| 162 |
process.exit(1); |
| 163 |
}); |
| 164 |
socket.on('data', (data) => { |
| 165 |
if(!socket.hasOwnProperty('incoming_buffer')) { socket['incoming_buffer'] = ""; } |
| 166 |
// Add the buffer to the connections' buffer storage |
| 167 |
let bufferRead = typeof data == "string" ? data : data.toString(); |
| 168 |
socket.incoming_buffer += bufferRead; |
| 169 |
// Try processing the buffer |
| 170 |
tryProcessMessage(socket); |
| 171 |
}); |
| 172 |
socket.on('close', (code) => { |
| 173 |
// if CodeLite termianted, exit |
| 174 |
console.log(`close event ${code}`); |
| 175 |
process.exit(0); |
| 176 |
}); |
| 177 |
socket.on('ready', () => { |
| 178 |
console.log("Connection established"); |
| 179 |
}); |
| 180 |
|
| 181 |
socket.connect(port, "127.0.0.1", function() { |
| 182 |
console.log(`>>> Successfully connected to tcp://127.0.0.1:${port}`); |
| 183 |
}); |
| 184 |
} |
| 185 |
connectToCodeLite(); |
| 186 |
|
| 187 |
//process.on('uncaughtException', err => { |
| 188 |
// process.stdin.read(1); |
| 189 |
//}); |