UNPKG

@theia/core

Version:

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

130 lines • 5.59 kB
// ***************************************************************************** // Copyright (C) 2018 TypeFox 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 { injectable, inject, named } from 'inversify'; import { ContributionProvider, ConnectionHandler, bindContributionProvider, servicesPath } from '../../common/index.js'; import { MessagingService } from './messaging-service.js'; import { ConnectionContainerModule } from './connection-container-module.js'; import Route from 'route-parser'; import { ChannelMultiplexer } from '../../common/message-rpc/channel.js'; import { FrontendConnectionService } from './frontend-connection-service.js'; import { ILogger } from '../../common/logger.js'; export const MessagingContainer = Symbol('MessagingContainer'); export const MainChannel = Symbol('MainChannel'); let DefaultMessagingService = class DefaultMessagingService { constructor() { this.channelHandlers = new ConnectionHandlers(); } initialize() { this.registerConnectionHandler(servicesPath, (_, socket) => this.handleConnection(socket)); for (const contribution of this.contributions.getContributions()) { contribution.configure(this); } } registerConnectionHandler(path, callback) { this.frontendConnectionService.registerConnectionHandler(path, callback); } registerChannelHandler(spec, callback) { this.channelHandlers.push(spec, (params, channel) => callback(params, channel)); } handleConnection(channel) { const multiplexer = new ChannelMultiplexer(channel); const channelHandlers = this.getConnectionChannelHandlers(channel); multiplexer.onDidOpenChannel(event => { if (channelHandlers.route(event.id, event.channel)) { this.logger.debug(`Opening channel for service path '${event.id}'.`); event.channel.onClose(() => this.logger.info(`Closing channel on service path '${event.id}'.`)); } }); } createMainChannelContainer(socket) { const connectionContainer = this.container.createChild(); connectionContainer.bind(MainChannel).toConstantValue(socket); socket.onClose(() => connectionContainer.unbindAllAsync().catch(e => this.logger.error(e))); return connectionContainer; } getConnectionChannelHandlers(socket) { const connectionContainer = this.createMainChannelContainer(socket); bindContributionProvider(connectionContainer, ConnectionHandler); connectionContainer.load(...this.connectionModules.getContributions()); const connectionChannelHandlers = new ConnectionHandlers(this.channelHandlers); const connectionHandlers = connectionContainer.getNamed(ContributionProvider, ConnectionHandler); for (const connectionHandler of connectionHandlers.getContributions(true)) { connectionChannelHandlers.push(connectionHandler.path, (_, channel) => { connectionHandler.onConnection(channel); }); } return connectionChannelHandlers; } }; __decorate([ inject(MessagingContainer) ], DefaultMessagingService.prototype, "container", void 0); __decorate([ inject(FrontendConnectionService) ], DefaultMessagingService.prototype, "frontendConnectionService", void 0); __decorate([ inject(ContributionProvider), named(ConnectionContainerModule) ], DefaultMessagingService.prototype, "connectionModules", void 0); __decorate([ inject(ILogger), named('core:DefaultMessagingService') ], DefaultMessagingService.prototype, "logger", void 0); __decorate([ inject(ContributionProvider), named(MessagingService.Contribution) ], DefaultMessagingService.prototype, "contributions", void 0); DefaultMessagingService = __decorate([ injectable() ], DefaultMessagingService); export { DefaultMessagingService }; export class ConnectionHandlers { constructor(parent) { this.parent = parent; this.handlers = []; } push(spec, callback) { const route = new Route(spec); const handler = (path, channel) => { const params = route.match(path); if (!params) { return false; } callback(params, channel); return route.reverse(params); }; this.handlers.push(handler); } route(path, connection) { for (const handler of this.handlers) { try { const result = handler(path, connection); if (result) { return result; } } catch (e) { console.error(e); } } if (this.parent) { return this.parent.route(path, connection); } return false; } } //# sourceMappingURL=default-messaging-service.js.map