@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
96 lines • 4.34 kB
JavaScript
;
// *****************************************************************************
// Copyright (C) 2023 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-only WITH Classpath-exception-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebsocketEndpoint = void 0;
const tslib_1 = require("tslib");
const inversify_1 = require("inversify");
const socket_io_1 = require("socket.io");
const ws_request_validators_1 = require("../ws-request-validators");
const messaging_listeners_1 = require("./messaging-listeners");
const default_messaging_service_1 = require("./default-messaging-service");
const logger_1 = require("../../common/logger");
let WebsocketEndpoint = class WebsocketEndpoint {
constructor() {
this.checkAliveTimeout = 30000; // 30 seconds
this.maxHttpBufferSize = 1e8; // 100 MB
this.wsHandlers = new default_messaging_service_1.ConnectionHandlers();
}
registerConnectionHandler(spec, callback) {
this.wsHandlers.push(spec, callback);
}
onStart(server) {
const socketServer = new socket_io_1.Server(server, {
pingInterval: this.checkAliveTimeout,
pingTimeout: this.checkAliveTimeout * 2,
maxHttpBufferSize: this.maxHttpBufferSize,
allowRequest: (req, callback) => {
// eslint-disable-next-line no-null/no-null
const noError = null;
this.wsRequestValidator.allowWsUpgrade(req).then(allowed => callback(noError, allowed), error => {
console.error('Error during WebSocket allowRequest validation:', error);
callback(error?.message ?? 'Validation error', false);
});
}
});
// Accept every namespace by using /.*/
socketServer.of(/.*/).on('connection', async (socket) => {
if (await this.allowConnect(socket.request)) {
await this.handleConnection(socket);
this.messagingListener.onDidWebSocketUpgrade(socket.request, socket);
}
else {
socket.disconnect(true);
}
});
}
/**
* Secondary validation after connection. The primary check happens in the
* Socket.IO `allowRequest` callback at handshake time; this is kept as
* defense-in-depth in case a Socket.IO upgrade path bypasses `allowRequest`.
*/
async allowConnect(request) {
try {
return this.wsRequestValidator.allowWsUpgrade(request);
}
catch (e) {
return false;
}
}
async handleConnection(socket) {
const pathname = socket.nsp.name;
if (pathname && !this.wsHandlers.route(pathname, socket)) {
this.logger.error('Cannot find a ws handler for the path: ' + pathname);
}
}
};
exports.WebsocketEndpoint = WebsocketEndpoint;
tslib_1.__decorate([
(0, inversify_1.inject)(ws_request_validators_1.WsRequestValidator),
tslib_1.__metadata("design:type", ws_request_validators_1.WsRequestValidator)
], WebsocketEndpoint.prototype, "wsRequestValidator", void 0);
tslib_1.__decorate([
(0, inversify_1.inject)(messaging_listeners_1.MessagingListener),
tslib_1.__metadata("design:type", messaging_listeners_1.MessagingListener)
], WebsocketEndpoint.prototype, "messagingListener", void 0);
tslib_1.__decorate([
(0, inversify_1.inject)(logger_1.ILogger),
(0, inversify_1.named)('core:WebsocketEndpoint'),
tslib_1.__metadata("design:type", Object)
], WebsocketEndpoint.prototype, "logger", void 0);
exports.WebsocketEndpoint = WebsocketEndpoint = tslib_1.__decorate([
(0, inversify_1.injectable)()
], WebsocketEndpoint);
//# sourceMappingURL=websocket-endpoint.js.map