Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a68b5b7

Browse files
committedMar 30, 2020
Minor startup code improvements
- Add type to HTTP options. - Fix certificate message always saying it was generated. - Dedent output not directly related to the HTTP server. - Remove unnecessary comma.
1 parent a4c0fd1 commit a68b5b7

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed
 

‎src/node/entry.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { StaticHttpProvider } from "./app/static"
99
import { UpdateHttpProvider } from "./app/update"
1010
import { VscodeHttpProvider } from "./app/vscode"
1111
import { Args, optionDescriptions, parse } from "./cli"
12-
import { AuthType, HttpServer } from "./http"
12+
import { AuthType, HttpServer, HttpServerOptions } from "./http"
1313
import { SshProvider } from "./ssh/server"
1414
import { generateCertificate, generatePassword, generateSshHostKey, hash, open } from "./util"
1515
import { ipcMain, wrap } from "./wrapper"
@@ -36,38 +36,25 @@ const main = async (args: Args): Promise<void> => {
3636
const originalPassword = auth === AuthType.Password && (process.env.PASSWORD || (await generatePassword()))
3737

3838
// Spawn the main HTTP server.
39-
const options = {
39+
const options: HttpServerOptions = {
4040
auth,
41-
cert: args.cert ? args.cert.value : undefined,
42-
certKey: args["cert-key"],
43-
sshHostKey: args["ssh-host-key"],
4441
commit,
4542
host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
4643
password: originalPassword ? hash(originalPassword) : undefined,
4744
port: typeof args.port !== "undefined" ? args.port : process.env.PORT ? parseInt(process.env.PORT, 10) : 8080,
4845
socket: args.socket,
46+
...(args.cert && !args.cert.value
47+
? await generateCertificate()
48+
: {
49+
cert: args.cert && args.cert.value,
50+
certKey: args["cert-key"],
51+
}),
4952
}
5053

51-
if (!options.cert && args.cert) {
52-
const { cert, certKey } = await generateCertificate()
53-
options.cert = cert
54-
options.certKey = certKey
55-
} else if (args.cert && !args["cert-key"]) {
54+
if (options.cert && !options.certKey) {
5655
throw new Error("--cert-key is missing")
5756
}
5857

59-
if (!args["disable-ssh"]) {
60-
if (!options.sshHostKey && typeof options.sshHostKey !== "undefined") {
61-
throw new Error("--ssh-host-key cannot be blank")
62-
} else if (!options.sshHostKey) {
63-
try {
64-
options.sshHostKey = await generateSshHostKey()
65-
} catch (error) {
66-
logger.error("Unable to start SSH server", field("error", error.message))
67-
}
68-
}
69-
}
70-
7158
const httpServer = new HttpServer(options)
7259
const vscode = httpServer.registerHttpProvider("/", VscodeHttpProvider, args)
7360
const api = httpServer.registerHttpProvider("/api", ApiHttpProvider, httpServer, vscode, args["user-data-dir"])
@@ -84,7 +71,7 @@ const main = async (args: Args): Promise<void> => {
8471

8572
if (auth === AuthType.Password && !process.env.PASSWORD) {
8673
logger.info(` - Password is ${originalPassword}`)
87-
logger.info(" - To use your own password, set the PASSWORD environment variable")
74+
logger.info(" - To use your own password set the PASSWORD environment variable")
8875
if (!args.auth) {
8976
logger.info(" - To disable use `--auth none`")
9077
}
@@ -96,7 +83,7 @@ const main = async (args: Args): Promise<void> => {
9683

9784
if (httpServer.protocol === "https") {
9885
logger.info(
99-
typeof args.cert === "string"
86+
args.cert && args.cert.value
10087
? ` - Using provided certificate and key for HTTPS`
10188
: ` - Using generated certificate and key for HTTPS`,
10289
)
@@ -106,18 +93,24 @@ const main = async (args: Args): Promise<void> => {
10693

10794
logger.info(`Automatic updates are ${update.enabled ? "enabled" : "disabled"}`)
10895

109-
let sshPort: number | undefined
110-
if (!args["disable-ssh"] && options.sshHostKey) {
111-
const sshProvider = httpServer.registerHttpProvider("/ssh", SshProvider, options.sshHostKey as string)
96+
let sshHostKey = args["ssh-host-key"]
97+
if (!args["disable-ssh"] && !sshHostKey) {
11298
try {
113-
sshPort = await sshProvider.listen()
99+
sshHostKey = await generateSshHostKey()
114100
} catch (error) {
115-
logger.warn(`SSH server: ${error.message}`)
101+
logger.error("Unable to start SSH server", field("error", error.message))
116102
}
117103
}
118104

105+
let sshPort: number | undefined
106+
if (!args["disable-ssh"] && sshHostKey) {
107+
const sshProvider = httpServer.registerHttpProvider("/ssh", SshProvider, sshHostKey)
108+
sshPort = await sshProvider.listen()
109+
}
110+
119111
if (typeof sshPort !== "undefined") {
120112
logger.info(`SSH server listening on localhost:${sshPort}`)
113+
logger.info(" - To disable use `--disable-ssh`")
121114
} else {
122115
logger.info("SSH server disabled")
123116
}

0 commit comments

Comments
 (0)
Please sign in to comment.