@eclipse-glsp/theia-integration
Version:
Glue code to integrate GLSP clients into Eclipse Theia
127 lines • 6.3 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GLSPNodeServerContribution = exports.ServerContainerFactory = void 0;
exports.configureServerContainer = configureServerContainer;
/********************************************************************************
* Copyright (c) 2023-2024 STMicroelectronics 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 core_1 = require("@theia/core");
const inversify_1 = require("@theia/core/shared/inversify");
const common_1 = require("../common");
const glsp_server_contribution_1 = require("./glsp-server-contribution");
exports.ServerContainerFactory = Symbol('ServerContainerFactory');
/**
* A reusable base implementation for {@link GLSPServerContribution}s that are running in a node backed and directly
* communicate with a Node GLSP Server
**/
let GLSPNodeServerContribution = class GLSPNodeServerContribution extends glsp_server_contribution_1.BaseGLSPServerContribution {
createContributionOptions() {
return {
launchedExternally: false
};
}
doConnect(clientChannel) {
try {
const clientConnection = this.createMessageConnection(clientChannel);
const connectionModule = this.createConnectionModule(clientConnection);
const container = this.createContainer(connectionModule);
const server = container.get(protocol_1.GLSPServer);
(0, protocol_1.configureClientConnection)(clientConnection, server);
return core_1.Disposable.create(() => {
server.shutdown();
container.unbindAll();
clientConnection.dispose();
clientChannel.close();
});
}
catch (error) {
console.error(error);
return core_1.Disposable.NULL;
}
}
createContainer(...additionalConfiguration) {
const container = this.serverContainerFactory();
return configureServerContainer(container, ...this.createServerModules(), ...additionalConfiguration);
}
createConnectionModule(clientConnection) {
return new inversify_1.ContainerModule(bind => {
bind(protocol_1.GLSPClientProxy).toDynamicValue(ctx => {
const proxy = ctx.container.resolve(protocol_1.JsonrpcClientProxy);
proxy.initialize(clientConnection);
return proxy;
});
});
}
configureClientConnection(clientConnection, server) {
(0, protocol_1.configureClientConnection)(clientConnection, server);
}
createMessageConnection(clientChannel) {
return (0, common_1.createChannelConnection)(clientChannel);
}
};
exports.GLSPNodeServerContribution = GLSPNodeServerContribution;
__decorate([
(0, inversify_1.inject)(exports.ServerContainerFactory),
__metadata("design:type", Function)
], GLSPNodeServerContribution.prototype, "serverContainerFactory", void 0);
exports.GLSPNodeServerContribution = GLSPNodeServerContribution = __decorate([
(0, inversify_1.injectable)()
], GLSPNodeServerContribution);
/**
* Initializes a container with the given {@link ContainerConfiguration}. The container configuration
* consists of the set of {@link ContainerModule}s that should be loaded in the container and/or
* In addition, for more fine-grained control {@link ModuleConfiguration}s can be passed as part fo the container configuration
* Module loading is distinct,this means each module will only get loaded once even if it is configured multiple times.
@param containerConfiguration
* Custom modules to be loaded in addition to the default modules and/or default modules that should be excluded.
@returns The initialized container.
*/
function configureServerContainer(container, ...containerConfiguration) {
const modules = [];
containerConfiguration.forEach(config => {
if (isContainerModule(config)) {
(0, protocol_1.distinctAdd)(modules, config);
}
else {
if (config.remove) {
(0, protocol_1.remove)(modules, ...(0, protocol_1.asArray)(config.remove));
}
if (config.add) {
(0, protocol_1.distinctAdd)(modules, ...(0, protocol_1.asArray)(config.add));
}
}
});
container.load(...modules);
return container;
}
/**
* The container modules might originate form different inversify contexts (e.g. `inversify` vs. `@theia/core/shared/inversify`).
* If this is the case an instanceof check can return false negative.
* => use a simple typeguard instead.
*/
function isContainerModule(config) {
return (0, protocol_1.hasNumberProp)(config, 'id') && (0, protocol_1.hasFunctionProp)(config, 'registry');
}
//# sourceMappingURL=glsp-node-server-contribution.js.map