@@ -56,7 +56,9 @@ export abstract class Server {
5656 // The underlying web server.
5757 protected readonly server : http . Server ;
5858
59- public constructor ( ) {
59+ private listenPromise : Promise < string > | undefined ;
60+
61+ public constructor ( private readonly port : number ) {
6062 this . server = http . createServer ( async ( request , response ) : Promise < void > => {
6163 try {
6264 if ( request . method !== "GET" ) {
@@ -89,14 +91,19 @@ export abstract class Server {
8991 requestPath : string ,
9092 ) : Promise < [ string | Buffer , http . OutgoingHttpHeaders ] > ;
9193
92- public listen ( port : number ) : Promise < void > {
93- return new Promise ( ( resolve , reject ) => {
94- this . server . on ( "error" , reject ) ;
95- this . server . listen ( port , resolve ) ;
96- } ) ;
94+ public listen ( ) : Promise < string > {
95+ if ( ! this . listenPromise ) {
96+ this . listenPromise = new Promise ( ( resolve , reject ) => {
97+ this . server . on ( "error" , reject ) ;
98+ this . server . listen ( this . port , ( ) => {
99+ resolve ( this . address ( ) ) ;
100+ } ) ;
101+ } ) ;
102+ }
103+ return this . listenPromise ;
97104 }
98105
99- public get address ( ) : string {
106+ public address ( ) : string {
100107 const address = this . server . address ( ) ;
101108 const endpoint = typeof address !== "string"
102109 ? ( ( address . address === "::" ? "localhost" : address . address ) + ":" + address . port )
@@ -121,8 +128,8 @@ export class MainServer extends Server {
121128
122129 private readonly services = new ServiceCollection ( ) ;
123130
124- public constructor ( private readonly webviewServer : WebviewServer , args : ParsedArgs ) {
125- super ( ) ;
131+ public constructor ( port : number , private readonly webviewServer : WebviewServer , args : ParsedArgs ) {
132+ super ( port ) ;
126133
127134 this . server . on ( "upgrade" , async ( request , socket ) => {
128135 const protocol = this . createProtocol ( request , socket ) ;
@@ -175,7 +182,7 @@ export class MainServer extends Server {
175182 const remoteAuthority = request . headers . host as string ;
176183 const transformer = getUriTransformer ( remoteAuthority ) ;
177184
178- const webviewEndpoint = this . webviewServer . address ;
185+ const webviewEndpoint = await this . webviewServer . listen ( ) ;
179186
180187 const cwd = process . env . VSCODE_CWD || process . cwd ( ) ;
181188 const workspacePath = parsedUrl . query . workspace as string | undefined ;
0 commit comments