UNPKG

@theia/core

Version:

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

90 lines • 3.76 kB
// ***************************************************************************** // 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 import { __decorate } from "tslib"; import { inject, injectable, named } from 'inversify'; import { Server } from 'socket.io'; import { WsRequestValidator } from '../ws-request-validators.js'; import { MessagingListener } from './messaging-listeners.js'; import { ConnectionHandlers } from './default-messaging-service.js'; import { ILogger } from '../../common/logger.js'; let WebsocketEndpoint = class WebsocketEndpoint { constructor() { this.checkAliveTimeout = 30000; // 30 seconds this.maxHttpBufferSize = 1e8; // 100 MB this.wsHandlers = new ConnectionHandlers(); } registerConnectionHandler(spec, callback) { this.wsHandlers.push(spec, callback); } onStart(server) { const socketServer = new 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 => { this.logger.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); } } }; __decorate([ inject(WsRequestValidator) ], WebsocketEndpoint.prototype, "wsRequestValidator", void 0); __decorate([ inject(MessagingListener) ], WebsocketEndpoint.prototype, "messagingListener", void 0); __decorate([ inject(ILogger), named('core:WebsocketEndpoint') ], WebsocketEndpoint.prototype, "logger", void 0); WebsocketEndpoint = __decorate([ injectable() ], WebsocketEndpoint); export { WebsocketEndpoint }; //# sourceMappingURL=websocket-endpoint.js.map