forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.ts
More file actions
177 lines (149 loc) · 4.87 KB
/
Copy pathconnection.ts
File metadata and controls
177 lines (149 loc) · 4.87 KB
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
import * as cp from "child_process";
import { getPathFromAmdModule } from "vs/base/common/amd";
import { VSBuffer } from "vs/base/common/buffer";
import { Emitter } from "vs/base/common/event";
import { ISocket } from "vs/base/parts/ipc/common/ipc.net";
import { NodeSocket, WebSocketNodeSocket } from "vs/base/parts/ipc/node/ipc.net";
import { ILogService } from "vs/platform/log/common/log";
import { uriTransformerPath } from "vs/server/channel";
import { IExtHostReadyMessage, IExtHostSocketMessage } from "vs/workbench/services/extensions/common/extensionHostProtocol";
import { Protocol } from "vs/server/protocol";
export abstract class Connection {
private readonly _onClose = new Emitter<void>();
public readonly onClose = this._onClose.event;
private timeout: NodeJS.Timeout | undefined;
private readonly wait = 1000 * 60;
private closed: boolean = false;
public constructor(protected protocol: Protocol) {
// onClose seems to mean we want to disconnect, so close immediately.
protocol.onClose(() => this.close());
// If the socket closes, we want to wait before closing so we can
// reconnect in the meantime.
protocol.onSocketClose(() => {
this.timeout = setTimeout(() => {
this.close();
}, this.wait);
});
}
/**
* Set up the connection on a new socket.
*/
public reconnect(protocol: Protocol, buffer: VSBuffer): void {
if (this.closed) {
throw new Error("Cannot reconnect to closed connection");
}
clearTimeout(this.timeout as any); // Not sure why the type doesn't work.
this.protocol = protocol;
this.connect(protocol.getSocket(), buffer);
}
/**
* Close and clean up connection. This will also kill the socket the
* connection is on. Probably not safe to reconnect once this has happened.
*/
protected close(): void {
if (!this.closed) {
this.closed = true;
this.protocol.sendDisconnect();
this.dispose();
this.protocol.dispose();
this._onClose.fire();
}
}
/**
* Clean up the connection.
*/
protected abstract dispose(): void;
/**
* Connect to a new socket.
*/
protected abstract connect(socket: ISocket, buffer: VSBuffer): void;
}
/**
* Used for all the IPC channels.
*/
export class ManagementConnection extends Connection {
protected dispose(): void {
// Nothing extra to do here.
}
protected connect(socket: ISocket, buffer: VSBuffer): void {
this.protocol.beginAcceptReconnection(socket, buffer);
this.protocol.endAcceptReconnection();
}
}
/**
* Manage the extension host process.
*/
export class ExtensionHostConnection extends Connection {
private process: cp.ChildProcess;
public constructor(protocol: Protocol, private readonly log: ILogService) {
super(protocol);
const socket = this.protocol.getSocket();
const buffer = this.protocol.readEntireBuffer();
this.process = this.spawn(socket, buffer);
}
protected dispose(): void {
this.process.kill();
}
protected connect(socket: ISocket, buffer: VSBuffer): void {
this.sendInitMessage(socket, buffer);
}
private sendInitMessage(nodeSocket: ISocket, buffer: VSBuffer): void {
const socket = nodeSocket instanceof NodeSocket
? nodeSocket.socket
: (nodeSocket as WebSocketNodeSocket).socket.socket;
socket.pause();
const initMessage: IExtHostSocketMessage = {
type: "VSCODE_EXTHOST_IPC_SOCKET",
initialDataChunk: (buffer.buffer as Buffer).toString("base64"),
skipWebSocketFrames: nodeSocket instanceof NodeSocket,
};
this.process.send(initMessage, socket);
}
private spawn(socket: ISocket, buffer: VSBuffer): cp.ChildProcess {
const proc = cp.fork(
getPathFromAmdModule(require, "bootstrap-fork"),
[
"--type=extensionHost",
`--uriTransformerPath=${uriTransformerPath}`
],
{
env: {
...process.env,
AMD_ENTRYPOINT: "vs/workbench/services/extensions/node/extensionHostProcess",
PIPE_LOGGING: "true",
VERBOSE_LOGGING: "true",
VSCODE_EXTHOST_WILL_SEND_SOCKET: "true",
VSCODE_HANDLES_UNCAUGHT_ERRORS: "true",
VSCODE_LOG_STACK: "false",
},
silent: true,
},
);
proc.on("error", (error) => {
console.error(error);
this.close();
});
proc.on("exit", (code, signal) => {
console.error("Extension host exited", { code, signal });
this.close();
});
proc.stdout.setEncoding("utf8");
proc.stderr.setEncoding("utf8");
proc.stdout.on("data", (data) => this.log.info("Extension host stdout", data));
proc.stderr.on("data", (data) => this.log.error("Extension host stderr", data));
proc.on("message", (event) => {
if (event && event.type === "__$console") {
const severity = this.log[event.severity] ? event.severity : "info";
this.log[severity]("Extension host", event.arguments);
}
});
const listen = (message: IExtHostReadyMessage) => {
if (message.type === "VSCODE_EXTHOST_IPC_READY") {
proc.removeListener("message", listen);
this.sendInitMessage(socket, buffer);
}
};
proc.on("message", listen);
return proc;
}
}