forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.ts
231 lines (201 loc) · 6.99 KB
/
connection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { field, Logger, logger } from '@coder/logger';
import * as cp from 'child_process';
import { VSBuffer } from 'vs/base/common/buffer';
import { Emitter } from 'vs/base/common/event';
import { FileAccess } from 'vs/base/common/network';
import { INativeEnvironmentService } from 'vs/platform/environment/common/environment';
import { IRemoteExtensionHostStartParams } from 'vs/platform/remote/common/remoteAgentConnection';
import { getNlsConfiguration } from 'vs/server/node/nls';
import { Protocol } from 'vs/server/node/protocol';
import { IExtHostReadyMessage } from 'vs/workbench/services/extensions/common/extensionHostProtocol';
export abstract class Connection {
private readonly _onClose = new Emitter<void>();
/**
* Fire when the connection is closed (not just disconnected). This should
* only happen when the connection is offline and old or has an error.
*/
public readonly onClose = this._onClose.event;
private disposed = false;
private _offline: number | undefined;
protected readonly logger: Logger;
public constructor(
protected readonly protocol: Protocol,
public readonly name: string,
) {
this.logger = logger.named(
this.name,
field('token', this.protocol.options.reconnectionToken),
);
this.logger.debug('Connecting...');
this.onClose(() => this.logger.debug('Closed'));
}
public get offline(): number | undefined {
return this._offline;
}
public reconnect(protocol: Protocol): void {
this.logger.debug('Reconnecting...');
this._offline = undefined;
this.doReconnect(protocol);
}
public dispose(reason?: string): void {
this.logger.debug('Disposing...', field('reason', reason));
if (!this.disposed) {
this.disposed = true;
this.doDispose();
this._onClose.fire();
}
}
protected setOffline(): void {
this.logger.debug('Disconnected');
if (!this._offline) {
this._offline = Date.now();
}
}
/**
* Set up the connection on a new socket.
*/
protected abstract doReconnect(protcol: Protocol): void;
/**
* Dispose/destroy everything permanently.
*/
protected abstract doDispose(): void;
}
/**
* Used for all the IPC channels.
*/
export class ManagementConnection extends Connection {
public constructor(protocol: Protocol) {
super(protocol, 'management');
protocol.onDidDispose(() => this.dispose()); // Explicit close.
protocol.onSocketClose(() => this.setOffline()); // Might reconnect.
protocol.sendMessage({ type: 'ok' });
}
protected doDispose(): void {
this.protocol.destroy();
}
protected doReconnect(protocol: Protocol): void {
protocol.sendMessage({ type: 'ok' });
this.protocol.beginAcceptReconnection(protocol.getSocket(), protocol.readEntireBuffer());
this.protocol.endAcceptReconnection();
protocol.dispose();
}
}
interface DisconnectedMessage {
type: 'VSCODE_EXTHOST_DISCONNECTED';
}
interface ConsoleMessage {
type: '__$console';
// See bootstrap-fork.js#L135.
severity: 'log' | 'warn' | 'error';
arguments: any[];
}
type ExtHostMessage = DisconnectedMessage | ConsoleMessage | IExtHostReadyMessage;
export class ExtensionHostConnection extends Connection {
private process?: cp.ChildProcess;
public constructor(
protocol: Protocol,
private readonly params: IRemoteExtensionHostStartParams,
private readonly environment: INativeEnvironmentService,
) {
super(protocol, 'exthost');
protocol.sendMessage({ debugPort: this.params.port });
const buffer = protocol.readEntireBuffer();
const inflateBytes = protocol.inflateBytes;
protocol.dispose();
protocol.getUnderlyingSocket().pause();
this.spawn(buffer, inflateBytes).then((p) => this.process = p);
}
protected doDispose(): void {
this.protocol.destroy();
if (this.process) {
this.process.kill();
}
}
protected doReconnect(protocol: Protocol): void {
protocol.sendMessage({ debugPort: this.params.port });
const buffer = protocol.readEntireBuffer();
const inflateBytes = protocol.inflateBytes;
protocol.dispose();
protocol.getUnderlyingSocket().pause();
this.protocol.setSocket(protocol.getSocket());
this.sendInitMessage(buffer, inflateBytes);
}
private sendInitMessage(buffer: VSBuffer, inflateBytes: Uint8Array | undefined): void {
if (!this.process) {
throw new Error('Tried to initialize VS Code before spawning');
}
this.logger.debug('Sending socket');
// TODO: Do something with the debug port.
this.process.send({
type: 'VSCODE_EXTHOST_IPC_SOCKET',
initialDataChunk: Buffer.from(buffer.buffer).toString('base64'),
skipWebSocketFrames: this.protocol.options.skipWebSocketFrames,
permessageDeflate: this.protocol.options.permessageDeflate,
inflateBytes: inflateBytes ? Buffer.from(inflateBytes).toString('base64') : undefined,
}, this.protocol.getUnderlyingSocket());
}
private async spawn(buffer: VSBuffer, inflateBytes: Uint8Array | undefined): Promise<cp.ChildProcess> {
this.logger.debug('Getting NLS configuration...');
const config = await getNlsConfiguration(this.params.language, this.environment.userDataPath);
this.logger.debug('Spawning extension host...');
const proc = cp.fork(
FileAccess.asFileUri('bootstrap-fork', require).fsPath,
// While not technically necessary, makes it easier to tell which process
// bootstrap-fork is executing. Can also do pkill -f extensionHost
// Other spawns in the VS Code codebase behave similarly.
[ '--type=extensionHost' ],
{
env: {
...process.env,
VSCODE_AMD_ENTRYPOINT: 'vs/workbench/services/extensions/node/extensionHostProcess',
VSCODE_PIPE_LOGGING: 'true',
VSCODE_VERBOSE_LOGGING: 'true',
VSCODE_EXTHOST_WILL_SEND_SOCKET: 'true',
VSCODE_HANDLES_UNCAUGHT_ERRORS: 'true',
VSCODE_LOG_STACK: 'false',
VSCODE_LOG_LEVEL: process.env.LOG_LEVEL,
VSCODE_NLS_CONFIG: JSON.stringify(config),
VSCODE_PARENT_PID: String(process.pid),
},
silent: true,
},
);
proc.on('error', (error) => {
this.logger.error('Exited unexpectedly', field('error', error));
this.dispose();
});
proc.on('exit', (code) => {
this.logger.debug('Exited', field('code', code));
this.dispose();
});
if (proc.stdout && proc.stderr) {
proc.stdout.setEncoding('utf8').on('data', (d) => this.logger.info(d));
proc.stderr.setEncoding('utf8').on('data', (d) => this.logger.error(d));
}
proc.on('message', (event: ExtHostMessage) => {
switch (event.type) {
case '__$console':
const fn = this.logger[event.severity === 'log' ? 'info' : event.severity];
if (fn) {
fn.bind(this.logger)('console', field('arguments', event.arguments));
} else {
this.logger.error('Unexpected severity', field('event', event));
}
break;
case 'VSCODE_EXTHOST_DISCONNECTED':
this.logger.debug('Got disconnected message');
this.setOffline();
break;
case 'VSCODE_EXTHOST_IPC_READY':
this.logger.debug('Handshake completed');
this.sendInitMessage(buffer, inflateBytes);
break;
default:
this.logger.error('Unexpected message', field('event', event));
break;
}
});
this.logger.debug('Waiting for handshake...');
return proc;
}
}