UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

114 lines • 4.75 kB
// ***************************************************************************** // Copyright (C) 2020 Ericsson 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-only WITH Classpath-exception-2.0 // ***************************************************************************** import { __decorate } from "tslib"; import { inject, injectable, postConstruct } from 'inversify'; import { ChannelMultiplexer } from '../../common/message-rpc/channel.js'; import { Deferred } from '../../common/promise-util.js'; import { ConnectionSource } from './connection-source.js'; import { RpcProxyFactory } from '../../common/messaging/proxy-factory.js'; /** * Service id for the local connection provider */ export const LocalConnectionProvider = Symbol('LocalConnectionProvider'); /** * Service id for the remote connection provider */ export const RemoteConnectionProvider = Symbol('RemoteConnectionProvider'); /** * This class manages the channels for remote services in the back end. * * Since we have the ability to use a remote back end via SSH, we need to distinguish * between two types of services: those that will be redirected to the remote back end * and those which must remain in the local back end. For example the service that manages * the remote ssh connections and port forwarding to the remote instance must remain local * while e.g. the file system service will run in the remote back end. For each set * of services, we will bind an instance of this class to {@linkcode LocalConnectionProvider} * and {@linkcode RemoteConnectionProvider} respectively. */ let ServiceConnectionProvider = class ServiceConnectionProvider { constructor() { this.channelHandlers = new Map(); this.channelReadyDeferred = new Deferred(); } static createProxy(container, path, arg) { return container.get(RemoteConnectionProvider).createProxy(path, arg); } static createLocalProxy(container, path, arg) { return container.get(LocalConnectionProvider).createProxy(path, arg); } static createHandler(container, path, arg) { const remote = container.get(RemoteConnectionProvider); const local = container.get(LocalConnectionProvider); remote.createProxy(path, arg); if (remote !== local) { local.createProxy(path, arg); } } createProxy(path, arg) { const factory = arg instanceof RpcProxyFactory ? arg : new RpcProxyFactory(arg); this.listen(path, (_, c) => factory.listen(c), true); return factory.createProxy(); } get channelReady() { return this.channelReadyDeferred.promise; } init() { this.connectionSource.onConnectionDidOpen(channel => this.handleChannelCreated(channel)); } /** * This method must be invoked by subclasses when they have created the main channel. * @param mainChannel */ handleChannelCreated(channel) { channel.onClose(() => { this.handleChannelClosed(channel); }); this.channelMultiplexer = new ChannelMultiplexer(channel); this.channelReadyDeferred.resolve(); for (const entry of this.channelHandlers.entries()) { this.openChannel(entry[0], entry[1]); } } handleChannelClosed(channel) { this.channelReadyDeferred = new Deferred(); } /** * Install a connection handler for the given path. */ listen(path, handler, reconnect) { this.openChannel(path, handler).then(() => { if (reconnect) { this.channelHandlers.set(path, handler); } }); } async openChannel(path, handler) { await this.channelReady; const newChannel = await this.channelMultiplexer.open(path); handler(path, newChannel); } }; __decorate([ postConstruct() ], ServiceConnectionProvider.prototype, "init", null); __decorate([ inject(ConnectionSource) ], ServiceConnectionProvider.prototype, "connectionSource", void 0); ServiceConnectionProvider = __decorate([ injectable() ], ServiceConnectionProvider); export { ServiceConnectionProvider }; //# sourceMappingURL=service-connection-provider.js.map