2021-09-30 05:14:56 +02:00
|
|
|
import * as express from "express"
|
|
|
|
import { Server } from "http"
|
|
|
|
import path from "path"
|
|
|
|
import { AuthType, DefaultedArgs } from "../cli"
|
|
|
|
import { version as codeServerVersion, vsRootPath } from "../constants"
|
|
|
|
import { ensureAuthenticated } from "../http"
|
|
|
|
import { loadAMDModule } from "../util"
|
|
|
|
import { Router as WsRouter, WebsocketRouter } from "../wsRouter"
|
|
|
|
import { errorHandler } from "./errors"
|
|
|
|
|
|
|
|
export interface VSServerResult {
|
|
|
|
router: express.Router
|
|
|
|
wsRouter: WebsocketRouter
|
|
|
|
vscodeServer: Server
|
2020-12-10 22:59:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
export const createVSServerRouter = async (args: DefaultedArgs): Promise<VSServerResult> => {
|
|
|
|
// Delete `VSCODE_CWD` very early even before
|
|
|
|
// importing bootstrap files. We have seen
|
|
|
|
// reports where `code .` would use the wrong
|
|
|
|
// current working directory due to our variable
|
|
|
|
// somehow escaping to the parent shell
|
|
|
|
// (https://github.com/microsoft/vscode/issues/126399)
|
|
|
|
delete process.env["VSCODE_CWD"]
|
2020-12-10 22:59:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
const bootstrap = require(path.join(vsRootPath, "out", "bootstrap"))
|
|
|
|
const bootstrapNode = require(path.join(vsRootPath, "out", "bootstrap-node"))
|
|
|
|
const product = require(path.join(vsRootPath, "product.json"))
|
2020-12-10 22:59:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
// Avoid Monkey Patches from Application Insights
|
|
|
|
bootstrap.avoidMonkeyPatchFromAppInsights()
|
2020-12-10 22:59:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
// Enable portable support
|
|
|
|
bootstrapNode.configurePortable(product)
|
2020-12-10 22:59:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
// Enable ASAR support
|
|
|
|
bootstrap.enableASARSupport()
|
2020-12-10 22:59:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
// Signal processes that we got launched as CLI
|
|
|
|
process.env["VSCODE_CLI"] = "1"
|
2020-12-10 22:59:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
const vscodeServerMain = await loadAMDModule<CodeServerLib.CreateVSServer>("vs/server/entry", "createVSServer")
|
2020-12-10 22:59:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
const serverUrl = new URL(`${args.cert ? "https" : "http"}://${args.host}:${args.port}`)
|
|
|
|
const vscodeServer = await vscodeServerMain({
|
|
|
|
codeServerVersion,
|
|
|
|
serverUrl,
|
|
|
|
args,
|
|
|
|
authed: args.auth !== AuthType.None,
|
|
|
|
disableUpdateCheck: !!args["disable-update-check"],
|
2020-12-10 22:59:24 +01:00
|
|
|
})
|
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
const router = express.Router()
|
|
|
|
const wsRouter = WsRouter()
|
2020-12-10 22:59:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
router.all("*", ensureAuthenticated, (req, res, next) => {
|
|
|
|
req.on("error", (error) => errorHandler(error, req, res, next))
|
2020-11-05 19:58:37 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
vscodeServer.emit("request", req, res)
|
|
|
|
})
|
2021-03-10 20:14:24 +01:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
wsRouter.ws("/", ensureAuthenticated, (req) => {
|
|
|
|
vscodeServer.emit("upgrade", req, req.socket, req.head)
|
2021-05-04 23:46:08 +02:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
req.socket.resume()
|
|
|
|
})
|
2021-05-04 23:46:08 +02:00
|
|
|
|
2021-09-30 05:14:56 +02:00
|
|
|
return {
|
|
|
|
router,
|
|
|
|
wsRouter,
|
|
|
|
vscodeServer,
|
2021-03-29 19:59:36 +02:00
|
|
|
}
|
2021-09-30 05:14:56 +02:00
|
|
|
}
|