2020-10-21 06:05:58 +07: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-04 03:40:06 +07:00
|
|
|
import { isFile } from "./util"
|
2020-11-04 06:14:04 +07:00
|
|
|
import { ipcMain } from "./wrapper"
|
2020-10-21 06:05:58 +07:00
|
|
|
|
|
|
|
export class VscodeProvider {
|
|
|
|
public readonly serverRootPath: string
|
|
|
|
public readonly vsRootPath: string
|
|
|
|
private _vscode?: Promise<cp.ChildProcess>
|
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
this.vsRootPath = path.resolve(rootPath, "lib/vscode")
|
|
|
|
this.serverRootPath = path.join(this.vsRootPath, "out/vs/server")
|
2020-11-04 06:14:04 +07:00
|
|
|
ipcMain.onDispose(() => this.dispose())
|
2020-10-21 06:05:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
public async dispose(): Promise<void> {
|
|
|
|
if (this._vscode) {
|
|
|
|
const vscode = await this._vscode
|
|
|
|
vscode.removeAllListeners()
|
|
|
|
vscode.kill()
|
2020-11-04 03:36:27 +07:00
|
|
|
this._vscode = undefined
|
2020-10-21 06:05:58 +07: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...")
|
|
|
|
return new Promise<ipc.WorkbenchOptions>((resolve, reject) => {
|
2020-11-04 03:54:27 +07:00
|
|
|
const onExit = (code: number | null) => reject(new Error(`VS Code exited unexpectedly with code ${code}`))
|
|
|
|
|
2020-10-21 06:05:58 +07:00
|
|
|
vscode.once("message", (message: ipc.VscodeMessage) => {
|
|
|
|
logger.debug("got message from vs code", field("message", message))
|
2020-11-04 03:54:27 +07:00
|
|
|
vscode.off("error", reject)
|
|
|
|
vscode.off("exit", onExit)
|
2020-10-21 06:05:58 +07:00
|
|
|
return message.type === "options" && message.id === id
|
|
|
|
? resolve(message.options)
|
|
|
|
: reject(new Error("Unexpected response during initialization"))
|
|
|
|
})
|
2020-11-04 03:54:27 +07:00
|
|
|
|
2020-10-21 06:05:58 +07:00
|
|
|
vscode.once("error", reject)
|
2020-11-04 03:54:27 +07:00
|
|
|
vscode.once("exit", onExit)
|
|
|
|
|
2020-10-21 06:05:58 +07:00
|
|
|
this.send(
|
|
|
|
{
|
|
|
|
type: "init",
|
|
|
|
id,
|
|
|
|
options: {
|
|
|
|
...options,
|
|
|
|
startPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
vscode,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private fork(): Promise<cp.ChildProcess> {
|
2020-11-04 03:42:37 +07:00
|
|
|
if (this._vscode) {
|
|
|
|
return this._vscode
|
|
|
|
}
|
2020-10-21 06:05:58 +07:00
|
|
|
|
2020-11-04 03:42:37 +07:00
|
|
|
logger.debug("forking vs code...")
|
|
|
|
const vscode = cp.fork(path.join(this.serverRootPath, "fork"))
|
|
|
|
vscode.on("error", (error) => {
|
|
|
|
logger.error(error.message)
|
|
|
|
this._vscode = undefined
|
|
|
|
})
|
|
|
|
vscode.on("exit", (code) => {
|
|
|
|
logger.error(`VS Code exited unexpectedly with code ${code}`)
|
|
|
|
this._vscode = undefined
|
|
|
|
})
|
|
|
|
|
|
|
|
this._vscode = new Promise((resolve, reject) => {
|
2020-11-04 03:54:27 +07:00
|
|
|
const onExit = (code: number | null) => reject(new Error(`VS Code exited unexpectedly with code ${code}`))
|
|
|
|
|
2020-11-04 03:42:37 +07:00
|
|
|
vscode.once("message", (message: ipc.VscodeMessage) => {
|
|
|
|
logger.debug("got message from vs code", field("message", message))
|
2020-11-04 03:54:27 +07:00
|
|
|
vscode.off("error", reject)
|
|
|
|
vscode.off("exit", onExit)
|
2020-11-04 03:42:37 +07:00
|
|
|
return message.type === "ready"
|
|
|
|
? resolve(vscode)
|
|
|
|
: reject(new Error("Unexpected response waiting for ready response"))
|
2020-10-21 06:05:58 +07:00
|
|
|
})
|
2020-11-04 03:54:27 +07:00
|
|
|
|
2020-11-04 03:42:37 +07:00
|
|
|
vscode.once("error", reject)
|
2020-11-04 03:54:27 +07:00
|
|
|
vscode.once("exit", onExit)
|
2020-11-04 03:42:37 +07:00
|
|
|
})
|
2020-10-21 06:05:58 +07:00
|
|
|
|
|
|
|
return this._vscode
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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> {
|
|
|
|
// TODO: TLS socket proxy.
|
|
|
|
const vscode = await this._vscode
|
|
|
|
this.send({ type: "socket", query }, vscode, socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Choose the first non-empty path.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|