-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (110 loc) · 3.72 KB
/
index.js
File metadata and controls
116 lines (110 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const cp = require("child_process")
const express = require("express")
const glob = require("glob")
const WebSocket = require("ws").WebSocket
const url = require("url")
const CONFIG_DIR = process.platform === 'darwin' ? 'config_mac' : process.platform === 'linux' ? 'config_linux' : 'config_win'
const BASE_URI = '/opt/jdt-language-server'
const PORT = 5036
const launchersFound = glob.sync('**/plugins/org.eclipse.equinox.launcher_*.jar', {cwd: `${BASE_URI}`})
if (launchersFound.length === 0 || !launchersFound) {
throw new Error('**/plugins/org.eclipse.equinox.launcher_*.jar Not Found!')
}
const params =
[
'-Xmx1G',
'-Xms1G',
//'-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=,quiet=y',
'-Declipse.application=org.eclipse.jdt.ls.core.id1',
'-Dosgi.bundles.defaultStartLevel=4',
'-Dlog.level=ALL',
//'-noverify',
'-Declipse.product=org.eclipse.jdt.ls.core.product',
'-jar',
`${BASE_URI}/${launchersFound[0]}`,
'-configuration',
`${BASE_URI}/${CONFIG_DIR}`
]
let app = express()
let server = app.listen(PORT)
let ws = new WebSocket.Server({
noServer: true,
perMessageDeflate: false
})
server.on('upgrade', function (request, socket, head) {
let pathname = request.url ? url.parse(request.url).pathname : undefined
console.log(pathname)
if (pathname === '/java-lsp') {
ws.handleUpgrade(request, socket, head, function (webSocket) {
let lspSocket = {
send: function (content) {
return webSocket.send(content, function (error) {
if (error) {
throw error
}
})
},
onMessage: function (cb) {
return webSocket.on('message', cb)
},
onError: function (cb) {
return webSocket.on('error', cb)
},
onClose: function (cb) {
return webSocket.on('close', cb)
},
dispose: function () {
return webSocket.close()
}
}
if (webSocket.readyState === webSocket.OPEN) {
launch(lspSocket)
} else {
webSocket.on('open', function () {
return launch(lspSocket)
})
}
})
}
})
function launch(socket) {
let process = cp.spawn('java', params)
let data = ''
let left = 0, start = 0, last = 0
process.stdin.setEncoding('utf-8')
socket.onMessage(function (data) {
console.log(`Receive:${data.toString()}`)
process.stdin.write('Content-Length: ' + data.length + '\n\n')
process.stdin.write(data.toString())
})
socket.onClose(function () {
console.log('Socket Closed')
process.kill()
})
process.stdout.on('data', function (respose) {
data += respose.toString()
let end = 0
for(let i = last; i < data.length; i++) {
if(data.charAt(i) == '{') {
if(left == 0) {
start = i
}
left++
} else if(data.charAt(i) == '}') {
left--
if(left == 0) {
let json = data.substring(start, i + 1)
end = i + 1
console.log(`Send: ${json}`)
socket.send(json)
}
}
}
data = data.substring(end)
last = data.length - end
start -= end
})
process.stderr.on('data', function (respose) {
console.error(`Error: ${respose.toString()}`)
})
}