forked from coder/code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharedProcess.ts
More file actions
133 lines (121 loc) · 3.55 KB
/
Copy pathsharedProcess.ts
File metadata and controls
133 lines (121 loc) · 3.55 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
import { ChildProcess } from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { forkModule } from "./bootstrapFork";
import { StdioIpcHandler } from "../ipc";
import { logger, field } from "@coder/logger/src";
import { ParsedArgs } from "vs/platform/environment/common/environment";
import { LogLevel } from "vs/platform/log/common/log";
import { Emitter, Event } from '@coder/events/src';
export enum SharedProcessState {
Stopped,
Starting,
Ready,
}
export type SharedProcessEvent = {
readonly state: SharedProcessState.Ready | SharedProcessState.Starting;
} | {
readonly state: SharedProcessState.Stopped;
readonly error: string;
};
export class SharedProcess {
public readonly socketPath: string = path.join(os.tmpdir(), `.vscode-online${Math.random().toString()}`);
private _state: SharedProcessState = SharedProcessState.Stopped;
private activeProcess: ChildProcess | undefined;
private ipcHandler: StdioIpcHandler | undefined;
private readonly onStateEmitter: Emitter<SharedProcessEvent>;
private readonly logger = logger.named("SHDR PROC");
public constructor(
private readonly userDataDir: string,
) {
this.onStateEmitter = new Emitter();
this.restart();
}
public get onState(): Event<SharedProcessEvent> {
return this.onStateEmitter.event;
}
public get state(): SharedProcessState {
return this._state;
}
public restart(): void {
if (this.activeProcess && !this.activeProcess.killed) {
this.activeProcess.kill();
}
const extensionsDir = path.join(this.userDataDir, "extensions");
const mkdir = (dir: string): void => {
try {
fs.mkdirSync(dir);
} catch (ex) {
if (ex.code !== "EEXIST") {
throw ex;
}
}
};
mkdir(this.userDataDir);
mkdir(extensionsDir);
this.setState({
state: SharedProcessState.Starting,
});
let resolved: boolean = false;
this.activeProcess = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain");
this.activeProcess.on("exit", (err) => {
if (this._state !== SharedProcessState.Stopped) {
this.setState({
error: `Exited with ${err}`,
state: SharedProcessState.Stopped,
});
}
this.restart();
});
this.ipcHandler = new StdioIpcHandler(this.activeProcess);
this.ipcHandler.once("handshake:hello", () => {
const data: {
sharedIPCHandle: string;
args: ParsedArgs;
logLevel: LogLevel;
} = {
args: {
"builtin-extensions-dir": path.join(process.env.BUILD_DIR || path.join(__dirname, "../.."), "build/extensions"),
"user-data-dir": this.userDataDir,
"extensions-dir": extensionsDir,
} as any,
logLevel: 0,
sharedIPCHandle: this.socketPath,
};
this.ipcHandler!.send("handshake:hey there", "", data);
});
this.ipcHandler.once("handshake:im ready", () => {
resolved = true;
this.setState({
state: SharedProcessState.Ready,
});
});
this.activeProcess.stdout.on("data", (data) => {
this.logger.debug("stdout", field("message", data.toString()));
});
this.activeProcess.stderr.on("data", (data) => {
this.logger.debug("stderr", field("message", data.toString()));
if (!resolved) {
this.setState({
error: data.toString(),
state: SharedProcessState.Stopped,
});
if (!this.activeProcess) {
return;
}
this.activeProcess.kill();
}
});
}
public dispose(): void {
if (this.ipcHandler) {
this.ipcHandler.send("handshake:goodbye");
}
this.ipcHandler = undefined;
}
private setState(event: SharedProcessEvent): void {
this._state = event.state;
this.onStateEmitter.emit(event);
}
}