Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/extensions/common/extensionHostEnv.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IProcessEnvironment } from '../../../../base/common/platform.js';
7
8
export const enum ExtHostConnectionType {
9
IPC = 1,
10
Socket = 2,
11
MessagePort = 3
12
}
13
14
/**
15
* The extension host will connect via named pipe / domain socket to its renderer.
16
*/
17
export class IPCExtHostConnection {
18
public static ENV_KEY = 'VSCODE_EXTHOST_IPC_HOOK';
19
20
public readonly type = ExtHostConnectionType.IPC;
21
22
constructor(
23
public readonly pipeName: string
24
) { }
25
26
public serialize(env: IProcessEnvironment): void {
27
env[IPCExtHostConnection.ENV_KEY] = this.pipeName;
28
}
29
}
30
31
/**
32
* The extension host will receive via nodejs IPC the socket to its renderer.
33
*/
34
export class SocketExtHostConnection {
35
public static ENV_KEY = 'VSCODE_EXTHOST_WILL_SEND_SOCKET';
36
37
public readonly type = ExtHostConnectionType.Socket;
38
39
public serialize(env: IProcessEnvironment): void {
40
env[SocketExtHostConnection.ENV_KEY] = '1';
41
}
42
}
43
44
/**
45
* The extension host will receive via nodejs IPC the MessagePort to its renderer.
46
*/
47
export class MessagePortExtHostConnection {
48
public static ENV_KEY = 'VSCODE_WILL_SEND_MESSAGE_PORT';
49
50
public readonly type = ExtHostConnectionType.MessagePort;
51
52
public serialize(env: IProcessEnvironment): void {
53
env[MessagePortExtHostConnection.ENV_KEY] = '1';
54
}
55
}
56
57
export type ExtHostConnection = IPCExtHostConnection | SocketExtHostConnection | MessagePortExtHostConnection;
58
59
function clean(env: IProcessEnvironment): void {
60
delete env[IPCExtHostConnection.ENV_KEY];
61
delete env[SocketExtHostConnection.ENV_KEY];
62
delete env[MessagePortExtHostConnection.ENV_KEY];
63
}
64
65
/**
66
* Write `connection` into `env` and clean up `env`.
67
*/
68
export function writeExtHostConnection(connection: ExtHostConnection, env: IProcessEnvironment): void {
69
// Avoid having two different keys that might introduce amiguity or problems.
70
clean(env);
71
connection.serialize(env);
72
}
73
74
/**
75
* Read `connection` from `env` and clean up `env`.
76
*/
77
export function readExtHostConnection(env: IProcessEnvironment): ExtHostConnection {
78
if (env[IPCExtHostConnection.ENV_KEY]) {
79
return cleanAndReturn(env, new IPCExtHostConnection(env[IPCExtHostConnection.ENV_KEY]!));
80
}
81
if (env[SocketExtHostConnection.ENV_KEY]) {
82
return cleanAndReturn(env, new SocketExtHostConnection());
83
}
84
if (env[MessagePortExtHostConnection.ENV_KEY]) {
85
return cleanAndReturn(env, new MessagePortExtHostConnection());
86
}
87
throw new Error(`No connection information defined in environment!`);
88
}
89
90
function cleanAndReturn(env: IProcessEnvironment, result: ExtHostConnection): ExtHostConnection {
91
clean(env);
92
return result;
93
}
94
95