@@ -33,21 +33,21 @@ class Server extends EventEmitter<Server.Events> {
3333 * Create a new HTTP server.
3434 * @param options Server options.
3535 */
36- public constructor ( options : Server . Options ) {
36+ public constructor ( options ? : Server . Options ) {
3737 super ( ) ;
3838 this . server = http . createServer ( {
3939 joinDuplicateHeaders : true ,
4040 } , this . listener . bind ( this ) ) ;
4141
42- this . globalHeaders = new Headers ( options . globalHeaders ) ;
42+ this . globalHeaders = new Headers ( options ? .globalHeaders ) ;
4343 if ( ! this . globalHeaders . has ( "server" ) )
4444 this . globalHeaders . set ( "Server" , `cldn/${ packageJson . version } ` ) ;
4545
46- this . port = options . port ;
47- this . copyOrigin = options . copyOrigin ?? false ;
48- this . handleConditionalRequests = options . handleConditionalRequests ?? true ;
46+ this . port = options ? .port ;
47+ this . copyOrigin = options ? .copyOrigin ?? false ;
48+ this . handleConditionalRequests = options ? .handleConditionalRequests ?? true ;
4949
50- if ( this . port !== undefined ) this . listen ( this . port ) ;
50+ if ( this . port !== undefined ) this . listen ( this . port ) . then ( ) ;
5151
5252 this . once ( "listening" , ( ) => {
5353 if ( this . listenerCount ( "error" ) === 0 )
@@ -87,10 +87,15 @@ class Server extends EventEmitter<Server.Events> {
8787 /**
8888 * Start listening for connections.
8989 */
90- public listen ( port : number ) {
90+ public listen ( port : number ) : Promise < void > {
9191 if ( this . server . listening )
9292 throw new Error ( "Server is already listening." ) ;
93- this . server . listen ( port , process . env . HOST , ( ) => this . emit ( "listening" ) ) ;
93+ return new Promise ( resolve => {
94+ this . server . listen ( port , process . env . HOST , ( ) => {
95+ this . emit ( "listening" , port , process . env . HOST ) ;
96+ resolve ( ) ;
97+ } ) ;
98+ } ) ;
9499 }
95100
96101 private async listener ( req : http . IncomingMessage , res : http . ServerResponse ) {
@@ -185,9 +190,9 @@ namespace Server {
185190 export interface Options {
186191 /**
187192 * The HTTP listener port. From 1 to 65535. Ports 1–1023 require
188- * privileges.
193+ * privileges. If not set, { @link Server#listen|Server.listen()} must be called manually.
189194 */
190- readonly port : number ;
195+ readonly port ? : number ;
191196
192197 /**
193198 * Headers to send with every response.
@@ -217,7 +222,7 @@ namespace Server {
217222 /**
218223 * Server is listening and ready to accept connections.
219224 */
220- listening : [ void ] ;
225+ listening : [ port : number , host ?: string ] ;
221226
222227 /**
223228 * The server is closing and not accepting new connections.
0 commit comments