Skip to content

Commit 0e0cfd1

Browse files
committed
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 004004c commit 0e0cfd1

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

src/node/entry.ts

+30-32
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"
@@ -26,38 +26,25 @@ const main = async (args: Args): Promise<void> => {
2626
}
2727

2828
// Spawn the main HTTP server.
29-
const options = {
29+
const options: HttpServerOptions = {
3030
auth,
31-
cert: args.cert ? args.cert.value : undefined,
32-
certKey: args["cert-key"],
33-
sshHostKey: args["ssh-host-key"],
3431
commit: commit || "development",
3532
host: args.host || (args.auth === AuthType.Password && typeof args.cert !== "undefined" ? "0.0.0.0" : "localhost"),
3633
password: originalPassword ? hash(originalPassword) : undefined,
3734
port: typeof args.port !== "undefined" ? args.port : process.env.PORT ? parseInt(process.env.PORT, 10) : 8080,
3835
socket: args.socket,
36+
...(args.cert && !args.cert.value
37+
? await generateCertificate()
38+
: {
39+
cert: args.cert && args.cert.value,
40+
certKey: args["cert-key"],
41+
}),
3942
}
4043

41-
if (!options.cert && args.cert) {
42-
const { cert, certKey } = await generateCertificate()
43-
options.cert = cert
44-
options.certKey = certKey
45-
} else if (args.cert && !args["cert-key"]) {
44+
if (options.cert && !options.certKey) {
4645
throw new Error("--cert-key is missing")
4746
}
4847

49-
if (!args["disable-ssh"]) {
50-
if (!options.sshHostKey && typeof options.sshHostKey !== "undefined") {
51-
throw new Error("--ssh-host-key cannot be blank")
52-
} else if (!options.sshHostKey) {
53-
try {
54-
options.sshHostKey = await generateSshHostKey()
55-
} catch (error) {
56-
logger.error("Unable to start SSH server", field("error", error.message))
57-
}
58-
}
59-
}
60-
6148
const httpServer = new HttpServer(options)
6249
const vscode = httpServer.registerHttpProvider("/", VscodeHttpProvider, args)
6350
const api = httpServer.registerHttpProvider("/api", ApiHttpProvider, httpServer, vscode, args["user-data-dir"])
@@ -70,18 +57,28 @@ const main = async (args: Args): Promise<void> => {
7057

7158
logger.info(`code-server ${require("../../package.json").version}`)
7259

73-
let sshPort = ""
74-
if (!args["disable-ssh"] && options.sshHostKey) {
75-
const sshProvider = httpServer.registerHttpProvider("/ssh", SshProvider, options.sshHostKey as string)
60+
// Spawn the SSH server.
61+
let sshHostKey = args["ssh-host-key"]
62+
if (!args["disable-ssh"] && !sshHostKey) {
63+
try {
64+
sshHostKey = await generateSshHostKey()
65+
} catch (error) {
66+
logger.error("Unable to start SSH server", field("error", error.message))
67+
}
68+
}
69+
70+
let sshPort: string | undefined
71+
if (!args["disable-ssh"] && sshHostKey) {
72+
const sshProvider = httpServer.registerHttpProvider("/ssh", SshProvider, sshHostKey)
7673
sshPort = await sshProvider.listen()
7774
}
7875

7976
const serverAddress = await httpServer.listen()
80-
logger.info(`Server listening on ${serverAddress}`)
77+
logger.info(`HTTP server listening on ${serverAddress}`)
8178

8279
if (auth === AuthType.Password && !process.env.PASSWORD) {
8380
logger.info(` - Password is ${originalPassword}`)
84-
logger.info(" - To use your own password, set the PASSWORD environment variable")
81+
logger.info(" - To use your own password set the PASSWORD environment variable")
8582
if (!args.auth) {
8683
logger.info(" - To disable use `--auth none`")
8784
}
@@ -93,27 +90,28 @@ const main = async (args: Args): Promise<void> => {
9390

9491
if (httpServer.protocol === "https") {
9592
logger.info(
96-
typeof args.cert === "string"
93+
args.cert && args.cert.value
9794
? ` - Using provided certificate and key for HTTPS`
9895
: ` - Using generated certificate and key for HTTPS`,
9996
)
10097
} else {
10198
logger.info(" - Not serving HTTPS")
10299
}
103100

104-
logger.info(` - Automatic updates are ${update.enabled ? "enabled" : "disabled"}`)
101+
logger.info(`Automatic updates are ${update.enabled ? "enabled" : "disabled"}`)
105102

106103
if (sshPort) {
107-
logger.info(` - SSH Server - Listening :${sshPort}`)
104+
logger.info(`SSH server listening on :${sshPort}`)
105+
logger.info(" - To disable use `--disable-ssh`")
108106
} else {
109-
logger.info(" - SSH Server - Disabled")
107+
logger.info("SSH server disabled")
110108
}
111109

112110
if (serverAddress && !options.socket && args.open) {
113111
// The web socket doesn't seem to work if browsing with 0.0.0.0.
114112
const openAddress = serverAddress.replace(/:\/\/0.0.0.0/, "://localhost")
115113
await open(openAddress).catch(console.error)
116-
logger.info(` - Opened ${openAddress}`)
114+
logger.info(`Opened ${openAddress}`)
117115
}
118116
}
119117

0 commit comments

Comments
 (0)