UNPKG

@theia/core

Version:

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

66 lines • 3.2 kB
// ***************************************************************************** // Copyright (C) 2022 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 // ***************************************************************************** /* eslint-disable @typescript-eslint/no-explicit-any */ import { Socket } from 'net'; import { AbstractChannel, Disposable } from '../../common/index.js'; import { Uint8ArrayReadBuffer, Uint8ArrayWriteBuffer } from '../../common/message-rpc/uint8-array-message-buffer.js'; import { BinaryMessagePipe } from './binary-message-pipe.js'; /** * A {@link Channel} to send messages between two processes using a dedicated pipe/fd for binary messages. * This fd is opened as 5th channel in addition to the default stdios (stdin, stdout, stderr, ipc). This means the default channels * are not blocked and can be used by the respective process for additional custom message handling. */ export class IPCChannel extends AbstractChannel { constructor(childProcess) { super(); this.ipcErrorListener = error => this.onErrorEmitter.fire(error); if (childProcess) { this.setupChildProcess(childProcess); } else { this.setupProcess(); } this.messagePipe.onMessage(message => { this.onMessageEmitter.fire(() => new Uint8ArrayReadBuffer(message)); }); } setupChildProcess(childProcess) { childProcess.once('exit', code => this.onCloseEmitter.fire({ reason: 'Child process has been terminated', code: code ?? undefined })); this.messagePipe = new BinaryMessagePipe(childProcess.stdio[4]); childProcess.on('error', this.ipcErrorListener); this.toDispose.push(Disposable.create(() => { childProcess.removeListener('error', this.ipcErrorListener); this.messagePipe.dispose(); })); } setupProcess() { process.once('beforeExit', code => this.onCloseEmitter.fire({ reason: 'Process is about to be terminated', code })); this.messagePipe = new BinaryMessagePipe(new Socket({ fd: 4 })); process.on('uncaughtException', this.ipcErrorListener); this.toDispose.push(Disposable.create(() => { process.removeListener('uncaughtException', this.ipcErrorListener); this.messagePipe.dispose(); })); } getWriteBuffer() { const result = new Uint8ArrayWriteBuffer(); result.onCommit(buffer => { this.messagePipe.send(buffer); }); return result; } } //# sourceMappingURL=ipc-channel.js.map