Don't wait to start main server

This commit is contained in:
Asher 2019-07-05 10:54:44 -05:00
parent f8f4bfd76f
commit 6156eb9ff4
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 25 additions and 18 deletions

16
cli.ts
View File

@ -57,14 +57,14 @@ const main = async (): Promise<void> => {
return process.exit(0); return process.exit(0);
} }
const webviewServer = new WebviewServer(); const webviewServer = new WebviewServer(typeof args["webview-port"] !== "undefined" && parseInt(args["webview-port"], 10) || 8444);
const server = new MainServer(webviewServer, args); const server = new MainServer(typeof args.port !== "undefined" && parseInt(args.port, 10) || 8443, webviewServer, args);
// The main server inserts webview server address to the root HTML, so we'll const [webviewAddress, serverAddress] = await Promise.all([
// need to wait for it to listen otherwise the address will be null. webviewServer.listen(),
await webviewServer.listen(typeof args["webview-port"] !== "undefined" && parseInt(args["webview-port"], 10) || 8444); server.listen()
await server.listen(typeof args.port !== "undefined" && parseInt(args.port, 10) || 8443); ]);
console.log(`Main server serving ${server.address}`); console.log(`Main server serving ${serverAddress}`);
console.log(`Webview server serving ${webviewServer.address}`); console.log(`Webview server serving ${webviewAddress}`);
}; };
main().catch((error) => { main().catch((error) => {

View File

@ -56,7 +56,9 @@ export abstract class Server {
// The underlying web server. // The underlying web server.
protected readonly server: http.Server; protected readonly server: http.Server;
public constructor() { private listenPromise: Promise<string> | undefined;
public constructor(private readonly port: number) {
this.server = http.createServer(async (request, response): Promise<void> => { this.server = http.createServer(async (request, response): Promise<void> => {
try { try {
if (request.method !== "GET") { if (request.method !== "GET") {
@ -89,14 +91,19 @@ export abstract class Server {
requestPath: string, requestPath: string,
): Promise<[string | Buffer, http.OutgoingHttpHeaders]>; ): Promise<[string | Buffer, http.OutgoingHttpHeaders]>;
public listen(port: number): Promise<void> { public listen(): Promise<string> {
return new Promise((resolve, reject) => { if (!this.listenPromise) {
this.listenPromise = new Promise((resolve, reject) => {
this.server.on("error", reject); this.server.on("error", reject);
this.server.listen(port, resolve); this.server.listen(this.port, () => {
resolve(this.address());
});
}); });
} }
return this.listenPromise;
}
public get address(): string { public address(): string {
const address = this.server.address(); const address = this.server.address();
const endpoint = typeof address !== "string" const endpoint = typeof address !== "string"
? ((address.address === "::" ? "localhost" : address.address) + ":" + address.port) ? ((address.address === "::" ? "localhost" : address.address) + ":" + address.port)
@ -121,8 +128,8 @@ export class MainServer extends Server {
private readonly services = new ServiceCollection(); private readonly services = new ServiceCollection();
public constructor(private readonly webviewServer: WebviewServer, args: ParsedArgs) { public constructor(port: number, private readonly webviewServer: WebviewServer, args: ParsedArgs) {
super(); super(port);
this.server.on("upgrade", async (request, socket) => { this.server.on("upgrade", async (request, socket) => {
const protocol = this.createProtocol(request, socket); const protocol = this.createProtocol(request, socket);
@ -175,7 +182,7 @@ export class MainServer extends Server {
const remoteAuthority = request.headers.host as string; const remoteAuthority = request.headers.host as string;
const transformer = getUriTransformer(remoteAuthority); const transformer = getUriTransformer(remoteAuthority);
const webviewEndpoint = this.webviewServer.address; const webviewEndpoint = await this.webviewServer.listen();
const cwd = process.env.VSCODE_CWD || process.cwd(); const cwd = process.env.VSCODE_CWD || process.cwd();
const workspacePath = parsedUrl.query.workspace as string | undefined; const workspacePath = parsedUrl.query.workspace as string | undefined;