Path: blob/main/src/vs/workbench/services/extensions/common/extensionHostEnv.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { IProcessEnvironment } from '../../../../base/common/platform.js';67export const enum ExtHostConnectionType {8IPC = 1,9Socket = 2,10MessagePort = 311}1213/**14* The extension host will connect via named pipe / domain socket to its renderer.15*/16export class IPCExtHostConnection {17public static ENV_KEY = 'VSCODE_EXTHOST_IPC_HOOK';1819public readonly type = ExtHostConnectionType.IPC;2021constructor(22public readonly pipeName: string23) { }2425public serialize(env: IProcessEnvironment): void {26env[IPCExtHostConnection.ENV_KEY] = this.pipeName;27}28}2930/**31* The extension host will receive via nodejs IPC the socket to its renderer.32*/33export class SocketExtHostConnection {34public static ENV_KEY = 'VSCODE_EXTHOST_WILL_SEND_SOCKET';3536public readonly type = ExtHostConnectionType.Socket;3738public serialize(env: IProcessEnvironment): void {39env[SocketExtHostConnection.ENV_KEY] = '1';40}41}4243/**44* The extension host will receive via nodejs IPC the MessagePort to its renderer.45*/46export class MessagePortExtHostConnection {47public static ENV_KEY = 'VSCODE_WILL_SEND_MESSAGE_PORT';4849public readonly type = ExtHostConnectionType.MessagePort;5051public serialize(env: IProcessEnvironment): void {52env[MessagePortExtHostConnection.ENV_KEY] = '1';53}54}5556export type ExtHostConnection = IPCExtHostConnection | SocketExtHostConnection | MessagePortExtHostConnection;5758function clean(env: IProcessEnvironment): void {59delete env[IPCExtHostConnection.ENV_KEY];60delete env[SocketExtHostConnection.ENV_KEY];61delete env[MessagePortExtHostConnection.ENV_KEY];62}6364/**65* Write `connection` into `env` and clean up `env`.66*/67export function writeExtHostConnection(connection: ExtHostConnection, env: IProcessEnvironment): void {68// Avoid having two different keys that might introduce amiguity or problems.69clean(env);70connection.serialize(env);71}7273/**74* Read `connection` from `env` and clean up `env`.75*/76export function readExtHostConnection(env: IProcessEnvironment): ExtHostConnection {77if (env[IPCExtHostConnection.ENV_KEY]) {78return cleanAndReturn(env, new IPCExtHostConnection(env[IPCExtHostConnection.ENV_KEY]!));79}80if (env[SocketExtHostConnection.ENV_KEY]) {81return cleanAndReturn(env, new SocketExtHostConnection());82}83if (env[MessagePortExtHostConnection.ENV_KEY]) {84return cleanAndReturn(env, new MessagePortExtHostConnection());85}86throw new Error(`No connection information defined in environment!`);87}8889function cleanAndReturn(env: IProcessEnvironment, result: ExtHostConnection): ExtHostConnection {90clean(env);91return result;92}939495