@eclipse-glsp/server 1.1.0-next.44aae0c.44 → 1.1.0-next.5c40594.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/lib/browser/launch/worker-server-launcher.d.ts +8 -5
  2. package/lib/browser/launch/worker-server-launcher.d.ts.map +1 -1
  3. package/lib/browser/launch/worker-server-launcher.js +9 -11
  4. package/lib/browser/launch/worker-server-launcher.js.map +1 -1
  5. package/lib/common/command/recording-command.d.ts +7 -7
  6. package/lib/common/command/recording-command.d.ts.map +1 -1
  7. package/lib/common/command/recording-command.js +6 -6
  8. package/lib/common/command/recording-command.js.map +1 -1
  9. package/lib/common/features/layout/computed-bounds-action-handler.js +1 -1
  10. package/lib/common/features/layout/computed-bounds-action-handler.js.map +1 -1
  11. package/lib/common/index.d.ts +3 -1
  12. package/lib/common/index.d.ts.map +1 -1
  13. package/lib/common/index.js +3 -1
  14. package/lib/common/index.js.map +1 -1
  15. package/lib/common/launch/glsp-server-launcher.d.ts +7 -6
  16. package/lib/common/launch/glsp-server-launcher.d.ts.map +1 -1
  17. package/lib/common/launch/glsp-server-launcher.js +25 -2
  18. package/lib/common/launch/glsp-server-launcher.js.map +1 -1
  19. package/lib/common/utils/layout-util.d.ts +1 -1
  20. package/lib/common/utils/layout-util.d.ts.map +1 -1
  21. package/lib/common/utils/layout-util.js +3 -3
  22. package/lib/common/utils/layout-util.js.map +1 -1
  23. package/lib/node/index.d.ts +1 -0
  24. package/lib/node/index.d.ts.map +1 -1
  25. package/lib/node/index.js +1 -0
  26. package/lib/node/index.js.map +1 -1
  27. package/lib/node/launch/socket-server-launcher.d.ts +2 -1
  28. package/lib/node/launch/socket-server-launcher.d.ts.map +1 -1
  29. package/lib/node/launch/socket-server-launcher.js +11 -10
  30. package/lib/node/launch/socket-server-launcher.js.map +1 -1
  31. package/lib/node/launch/websocket-server-launcher.d.ts +59 -0
  32. package/lib/node/launch/websocket-server-launcher.d.ts.map +1 -0
  33. package/lib/node/launch/websocket-server-launcher.js +145 -0
  34. package/lib/node/launch/websocket-server-launcher.js.map +1 -0
  35. package/package.json +7 -5
  36. package/src/browser/launch/worker-server-launcher.ts +16 -12
  37. package/src/common/command/recording-command.ts +11 -11
  38. package/src/common/features/layout/computed-bounds-action-handler.ts +2 -2
  39. package/src/common/index.ts +3 -1
  40. package/src/common/launch/glsp-server-launcher.ts +13 -7
  41. package/src/common/utils/layout-util.ts +1 -1
  42. package/src/node/index.ts +1 -0
  43. package/src/node/launch/socket-server-launcher.ts +13 -7
  44. package/src/node/launch/websocket-server-launcher.ts +164 -0
@@ -0,0 +1,164 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2023 EclipseSource and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+
17
+ import { createWebSocketConnection, Disposable, MaybePromise, WebSocketWrapper } from '@eclipse-glsp/protocol';
18
+ import * as http from 'http';
19
+ import { Container, inject, injectable } from 'inversify';
20
+ import * as net from 'net';
21
+ import * as jsonrpc from 'vscode-jsonrpc';
22
+ import { Server, WebSocket } from 'ws';
23
+ import { GLSPServerLauncher } from '../../common/launch/glsp-server-launcher';
24
+ import { GLSPServer, JsonRpcGLSPServer } from '../../common/protocol/glsp-server';
25
+ import { Logger } from '../../common/utils/logger';
26
+ import { START_UP_COMPLETE_MSG } from './socket-server-launcher';
27
+
28
+ export interface WebSocketServerOptions {
29
+ host?: string;
30
+ port?: number;
31
+ path: string;
32
+ }
33
+
34
+ export interface ResolvedWebSocketServerOptions {
35
+ host?: string;
36
+ port: number;
37
+ path: string;
38
+ server: http.Server;
39
+ }
40
+
41
+ export interface WebsocketConnectionData {
42
+ socket: WebSocket;
43
+ connection: jsonrpc.MessageConnection;
44
+ }
45
+
46
+ const STATUS_UPGRADE_REQUIRED = 426;
47
+
48
+ @injectable()
49
+ export class WebSocketServerLauncher extends GLSPServerLauncher<WebSocketServerOptions> {
50
+ @inject(Logger)
51
+ protected override logger: Logger;
52
+
53
+ protected server: Server;
54
+ protected startupCompleteMessage = START_UP_COMPLETE_MSG;
55
+ protected currentConnections: jsonrpc.MessageConnection[] = [];
56
+
57
+ constructor() {
58
+ super();
59
+ this.toDispose.push(
60
+ Disposable.create(() => {
61
+ this.currentConnections.forEach(connection => connection.dispose());
62
+ this.server.close();
63
+ })
64
+ );
65
+ }
66
+
67
+ protected async run(options: WebSocketServerOptions): Promise<void> {
68
+ const resolvedOptions = await this.resolveOptions(options);
69
+ this.server = new Server({ server: resolvedOptions.server, path: resolvedOptions.path });
70
+ const endpoint = `ws://${resolvedOptions.host}:${resolvedOptions.port}:${resolvedOptions.path}`;
71
+ this.logger.info(`The GLSP Websocket launcher is ready to accept new client requests on endpoint '${endpoint}'`);
72
+ console.log(this.startupCompleteMessage.concat(resolvedOptions.port.toString()));
73
+
74
+ this.server.on('connection', (ws, req) => {
75
+ this.createClientConnection(ws);
76
+ });
77
+
78
+ return new Promise((resolve, reject) => {
79
+ this.server.on('close', () => resolve(undefined));
80
+ this.server.on('error', error => reject(error));
81
+ });
82
+ }
83
+
84
+ protected async createClientConnection(socket: WebSocket): Promise<void> {
85
+ const container = this.createContainer();
86
+ const connection = this.createConnection(socket);
87
+ this.currentConnections.push(connection);
88
+ const glspServer = container.get<JsonRpcGLSPServer>(JsonRpcGLSPServer);
89
+ glspServer.connect(connection);
90
+ this.logger.info('Starting GLSP server connection');
91
+ connection.listen();
92
+ connection.onDispose(() => this.disposeClientConnection(container, glspServer));
93
+ socket.on('close', () => this.disposeClientConnection(container, glspServer));
94
+ connection.onClose(() => console.log('GOD dam'));
95
+ return new Promise((resolve, rejects) => {
96
+ connection.onClose(() => resolve(undefined));
97
+ connection.onError(error => rejects(error));
98
+ });
99
+ }
100
+
101
+ protected disposeClientConnection(container: Container, glspServer: GLSPServer): void {
102
+ glspServer.shutdown();
103
+ container.unbindAll();
104
+ }
105
+
106
+ protected createConnection(socket: WebSocket): jsonrpc.MessageConnection {
107
+ return createWebSocketConnection(wrapWebSocket(socket));
108
+ }
109
+
110
+ protected async resolveOptions(options: WebSocketServerOptions): Promise<ResolvedWebSocketServerOptions> {
111
+ const port = !options.port || options.port === 0 ? await getFreePort() : options.port;
112
+ const path = options.path.startsWith('/') ? options.path : `/${options.path}`;
113
+ const server = this.createHttpServer(port, options.host);
114
+
115
+ return { ...options, port, path, server };
116
+ }
117
+
118
+ protected createHttpServer(port: number, host?: string): http.Server {
119
+ const server = http.createServer((req, res) => {
120
+ const body = http.STATUS_CODES[STATUS_UPGRADE_REQUIRED];
121
+
122
+ res.writeHead(426, {
123
+ 'Content-Length': body?.length,
124
+ 'Content-Type': 'text/plain'
125
+ });
126
+ res.end(body);
127
+ });
128
+ server.listen(port, host);
129
+ // Set server timeout to infinite
130
+ server.setTimeout(0);
131
+ return server;
132
+ }
133
+
134
+ override start(options: WebSocketServerOptions): MaybePromise<void> {
135
+ super.start(options);
136
+ }
137
+ }
138
+
139
+ async function getFreePort(): Promise<number> {
140
+ return new Promise((resolve, reject) => {
141
+ const srv = net.createServer();
142
+ srv.listen(0, () => {
143
+ const address = srv.address();
144
+ if (!address || typeof address === 'string') {
145
+ reject(new Error('Could not address to compute a free port'));
146
+ return;
147
+ }
148
+ srv.close(() => resolve(address.port));
149
+ });
150
+ });
151
+ }
152
+ /**
153
+ * Creates a {@link WebSocketWrapper} for the given plain WebSocket
154
+ * @param socket The socket to wrap
155
+ */
156
+ export function wrapWebSocket(socket: WebSocket): WebSocketWrapper {
157
+ return {
158
+ send: content => socket.send(content),
159
+ onMessage: cb => socket.on('message', cb),
160
+ onClose: cb => socket.on('close', cb),
161
+ onError: cb => socket.on('error', cb),
162
+ dispose: () => socket.close()
163
+ };
164
+ }