2020-10-20 23:05:58 +00:00
|
|
|
import { field, logger } from "@coder/logger"
|
|
|
|
import * as cp from "child_process"
|
|
|
|
import * as net from "net"
|
|
|
|
import * as path from "path"
|
|
|
|
import * as ipc from "../../lib/vscode/src/vs/server/ipc"
|
|
|
|
import { arrayify, generateUuid } from "../common/util"
|
|
|
|
import { rootPath } from "./constants"
|
|
|
|
import { settings } from "./settings"
|
2020-11-10 23:24:07 +00:00
|
|
|
import { SocketProxyProvider } from "./socket"
|
2020-11-03 20:40:06 +00:00
|
|
|
import { isFile } from "./util"
|
2020-11-18 17:43:25 +00:00
|
|
|
import { wrapper } from "./wrapper"
|
2020-10-20 23:05:58 +00:00
|
|
|
|
|
|
|
export class VscodeProvider {
|
|
|
|
public readonly serverRootPath: string
|
|
|
|
public readonly vsRootPath: string
|
|
|
|
private _vscode?: Promise<cp.ChildProcess>
|
2020-11-10 21:46:53 +00:00
|
|
|
private timeoutInterval = 10000 // 10s, matches VS Code's timeouts.
|
2020-11-10 23:24:07 +00:00
|
|
|
private readonly socketProvider = new SocketProxyProvider()
|
2020-10-20 23:05:58 +00:00
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
this.vsRootPath = path.resolve(rootPath, "lib/vscode")
|
|
|
|
this.serverRootPath = path.join(this.vsRootPath, "out/vs/server")
|
2020-11-18 17:43:25 +00:00
|
|
|
wrapper.onDispose(() => this.dispose())
|
2020-10-20 23:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async dispose(): Promise<void> {
|
2020-11-10 23:24:07 +00:00
|
|
|
this.socketProvider.stop()
|
2020-10-20 23:05:58 +00:00
|
|
|
if (this._vscode) {
|
|
|
|
const vscode = await this._vscode
|
|
|
|
vscode.removeAllListeners()
|
|
|
|
vscode.kill()
|
2020-11-03 20:36:27 +00:00
|
|
|
this._vscode = undefined
|
2020-10-20 23:05:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async initialize(
|
|
|
|
options: Omit<ipc.VscodeOptions, "startPath">,
|
|
|
|
query: ipc.Query,
|
|
|
|
): Promise<ipc.WorkbenchOptions> {
|
|
|
|
const { lastVisited } = await settings.read()
|
|
|
|
const startPath = await this.getFirstPath([
|
|
|
|
{ url: query.workspace, workspace: true },
|
|
|
|
{ url: query.folder, workspace: false },
|
|
|
|
options.args._ && options.args._.length > 0
|
|
|
|
? { url: path.resolve(options.args._[options.args._.length - 1]) }
|
|
|
|
: undefined,
|
|
|
|
lastVisited,
|
|
|
|
])
|
|
|
|
|
|
|
|
settings.write({
|
|
|
|
lastVisited: startPath,
|
|
|
|
query,
|
|
|
|
})
|
|
|
|
|
|
|
|
const id = generateUuid()
|
|
|
|
const vscode = await this.fork()
|
|
|
|
|
|
|
|
logger.debug("setting up vs code...")
|
2020-11-03 20:54:27 +00:00
|
|
|
|
2020-11-10 21:46:53 +00:00
|
|
|
this.send(
|
|
|
|
{
|
|
|
|
type: "init",
|
|
|
|
id,
|
|
|
|
options: {
|
|
|
|
...options,
|
|
|
|
startPath,
|
2020-10-20 23:05:58 +00:00
|
|
|
},
|
2020-11-10 21:46:53 +00:00
|
|
|
},
|
|
|
|
vscode,
|
|
|
|
)
|
|
|
|
|
2020-11-12 17:17:45 +00:00
|
|
|
const message = await this.onMessage(vscode, (message): message is ipc.OptionsMessage => {
|
2020-11-10 21:46:53 +00:00
|
|
|
// There can be parallel initializations so wait for the right ID.
|
|
|
|
return message.type === "options" && message.id === id
|
2020-10-20 23:05:58 +00:00
|
|
|
})
|
2020-11-10 21:46:53 +00:00
|
|
|
|
|
|
|
return message.options
|
2020-10-20 23:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fork(): Promise<cp.ChildProcess> {
|
2020-11-03 20:42:37 +00:00
|
|
|
if (this._vscode) {
|
|
|
|
return this._vscode
|
|
|
|
}
|
2020-10-20 23:05:58 +00:00
|
|
|
|
2020-11-03 20:42:37 +00:00
|
|
|
logger.debug("forking vs code...")
|
|
|
|
const vscode = cp.fork(path.join(this.serverRootPath, "fork"))
|
2020-11-10 21:46:53 +00:00
|
|
|
|
|
|
|
const dispose = () => {
|
|
|
|
vscode.removeAllListeners()
|
|
|
|
vscode.kill()
|
2020-11-03 20:42:37 +00:00
|
|
|
this._vscode = undefined
|
2020-11-10 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vscode.on("error", (error: Error) => {
|
|
|
|
logger.error(error.message)
|
|
|
|
if (error.stack) {
|
|
|
|
logger.debug(error.stack)
|
|
|
|
}
|
|
|
|
dispose()
|
2020-11-03 20:42:37 +00:00
|
|
|
})
|
2020-11-10 21:46:53 +00:00
|
|
|
|
2020-11-03 20:42:37 +00:00
|
|
|
vscode.on("exit", (code) => {
|
|
|
|
logger.error(`VS Code exited unexpectedly with code ${code}`)
|
2020-11-10 21:46:53 +00:00
|
|
|
dispose()
|
2020-11-03 20:42:37 +00:00
|
|
|
})
|
|
|
|
|
2020-11-12 17:17:45 +00:00
|
|
|
this._vscode = this.onMessage(vscode, (message): message is ipc.ReadyMessage => {
|
2020-11-10 21:46:53 +00:00
|
|
|
return message.type === "ready"
|
|
|
|
}).then(() => vscode)
|
|
|
|
|
|
|
|
return this._vscode
|
|
|
|
}
|
2020-11-03 20:54:27 +00:00
|
|
|
|
2020-11-10 21:46:53 +00:00
|
|
|
/**
|
|
|
|
* Listen to a single message from a process. Reject if the process errors,
|
|
|
|
* exits, or times out.
|
|
|
|
*
|
|
|
|
* `fn` is a function that determines whether the message is the one we're
|
|
|
|
* waiting for.
|
|
|
|
*/
|
|
|
|
private onMessage<T extends ipc.VscodeMessage>(
|
|
|
|
proc: cp.ChildProcess,
|
|
|
|
fn: (message: ipc.VscodeMessage) => message is T,
|
|
|
|
): Promise<T> {
|
2020-11-12 18:07:45 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-11-12 17:16:21 +00:00
|
|
|
const cleanup = () => {
|
2020-11-12 18:07:45 +00:00
|
|
|
proc.off("error", onError)
|
2020-11-12 17:16:21 +00:00
|
|
|
proc.off("exit", onExit)
|
2020-11-12 18:03:28 +00:00
|
|
|
proc.off("message", onMessage)
|
2020-11-10 21:46:53 +00:00
|
|
|
clearTimeout(timeout)
|
2020-11-12 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
cleanup()
|
2020-11-12 18:07:45 +00:00
|
|
|
reject(new Error("timed out"))
|
2020-11-12 17:16:21 +00:00
|
|
|
}, this.timeoutInterval)
|
|
|
|
|
2020-11-12 18:07:45 +00:00
|
|
|
const onError = (error: Error) => {
|
2020-11-12 17:16:21 +00:00
|
|
|
cleanup()
|
2020-11-12 18:07:45 +00:00
|
|
|
reject(error)
|
2020-11-10 21:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const onExit = (code: number | null) => {
|
2020-11-12 18:07:45 +00:00
|
|
|
cleanup()
|
2020-11-10 21:46:53 +00:00
|
|
|
reject(new Error(`VS Code exited unexpectedly with code ${code}`))
|
|
|
|
}
|
|
|
|
|
2020-11-12 18:03:28 +00:00
|
|
|
const onMessage = (message: ipc.VscodeMessage) => {
|
2020-11-12 17:52:02 +00:00
|
|
|
logger.trace("got message from vscode", field("message", message))
|
2020-11-10 21:46:53 +00:00
|
|
|
if (fn(message)) {
|
2020-11-12 17:16:21 +00:00
|
|
|
cleanup()
|
2020-11-10 21:46:53 +00:00
|
|
|
resolve(message)
|
|
|
|
}
|
2020-11-12 18:03:28 +00:00
|
|
|
}
|
2020-11-03 20:54:27 +00:00
|
|
|
|
2020-11-12 18:03:28 +00:00
|
|
|
proc.on("message", onMessage)
|
2020-11-12 18:07:45 +00:00
|
|
|
proc.on("error", onError)
|
2020-11-12 18:03:28 +00:00
|
|
|
proc.on("exit", onExit)
|
2020-11-03 20:42:37 +00:00
|
|
|
})
|
2020-10-20 23:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VS Code expects a raw socket. It will handle all the web socket frames.
|
|
|
|
*/
|
|
|
|
public async sendWebsocket(socket: net.Socket, query: ipc.Query): Promise<void> {
|
|
|
|
const vscode = await this._vscode
|
2020-11-10 23:24:07 +00:00
|
|
|
// TLS sockets cannot be transferred to child processes so we need an
|
|
|
|
// in-between. Non-TLS sockets will be returned as-is.
|
|
|
|
const socketProxy = await this.socketProvider.createProxy(socket)
|
|
|
|
this.send({ type: "socket", query }, vscode, socketProxy)
|
2020-10-20 23:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private send(message: ipc.CodeServerMessage, vscode?: cp.ChildProcess, socket?: net.Socket): void {
|
|
|
|
if (!vscode || vscode.killed) {
|
|
|
|
throw new Error("vscode is not running")
|
|
|
|
}
|
|
|
|
vscode.send(message, socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-10 23:02:39 +00:00
|
|
|
* Choose the first non-empty path from the provided array.
|
|
|
|
*
|
|
|
|
* Each array item consists of `url` and an optional `workspace` boolean that
|
|
|
|
* indicates whether that url is for a workspace.
|
|
|
|
*
|
|
|
|
* `url` can be a fully qualified URL or just the path portion.
|
|
|
|
*
|
|
|
|
* `url` can also be a query object to make it easier to pass in query
|
|
|
|
* variables directly but anything that isn't a string or string array is not
|
|
|
|
* valid and will be ignored.
|
2020-10-20 23:05:58 +00:00
|
|
|
*/
|
|
|
|
private async getFirstPath(
|
|
|
|
startPaths: Array<{ url?: string | string[] | ipc.Query | ipc.Query[]; workspace?: boolean } | undefined>,
|
|
|
|
): Promise<ipc.StartPath | undefined> {
|
|
|
|
for (let i = 0; i < startPaths.length; ++i) {
|
|
|
|
const startPath = startPaths[i]
|
|
|
|
const url = arrayify(startPath && startPath.url).find((p) => !!p)
|
|
|
|
if (startPath && url && typeof url === "string") {
|
|
|
|
return {
|
|
|
|
url,
|
|
|
|
// The only time `workspace` is undefined is for the command-line
|
|
|
|
// argument, in which case it's a path (not a URL) so we can stat it
|
|
|
|
// without having to parse it.
|
|
|
|
workspace: typeof startPath.workspace !== "undefined" ? startPath.workspace : await isFile(url),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
}
|