UNPKG

@eclipse-glsp/vscode-integration

Version:

Glue code to integrate GLSP diagrams in VSCode extensions (extension part)

75 lines 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidWebSocketAddress = exports.WebSocketGlspVscodeServer = void 0; /******************************************************************************** * Copyright (c) 2021-2023 EclipseSource and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ const protocol_1 = require("@eclipse-glsp/protocol"); const common_1 = require("../../common"); /** * This component can be used to bootstrap your extension when using an external * GLSP server implementation that communicates via WebSocket connection * * It sets up a JSON-RPC connection to a server running on a specified port and * provides an interface, ready to be used by the `GlspVscodeConnector` for the * GLSP-VSCode integration. */ class WebSocketGlspVscodeServer extends common_1.BaseGlspVscodeServer { constructor(options) { super(options); this.options = options; } getWebSocketAddress() { var _a, _b; const opts = this.options.connectionOptions; if ('webSocketAddress' in opts) { return opts.webSocketAddress; } const protocol = (_a = opts.protocol) !== null && _a !== void 0 ? _a : 'ws'; const host = (_b = opts.host) !== null && _b !== void 0 ? _b : 'localhost'; return `${protocol}://${host}:${opts.port}/${opts.path}`; } async createGLSPClient() { const connection = await this.createConnection(); this.toDispose.push(connection); return new protocol_1.BaseJsonrpcGLSPClient({ id: this.options.clientId, connectionProvider: connection }); } async createConnection() { const webSocketAddress = this.getWebSocketAddress(); if (!webSocketAddress || !isValidWebSocketAddress(webSocketAddress)) { throw new Error(`Could not connect to to GLSP Server. The WebSocket address is invalid: '${webSocketAddress}'`); } return this.createWebSocketConnection(webSocketAddress); } createWebSocketConnection(address) { const webSocket = new WebSocket(address); return (0, protocol_1.listen)(webSocket); } } exports.WebSocketGlspVscodeServer = WebSocketGlspVscodeServer; function isValidWebSocketAddress(address) { try { const { protocol } = new URL(address); return protocol === 'ws:' || protocol === 'wss:'; } catch (error) { return false; } } exports.isValidWebSocketAddress = isValidWebSocketAddress; //# sourceMappingURL=websocket-glsp-vscode-server.js.map