1
- import Promise from 'bluebird'
1
+ import Bluebird from 'bluebird'
2
2
import express from 'express'
3
3
import http from 'http'
4
4
import https from 'https'
@@ -31,11 +31,12 @@ function createExpressApp (requestCallback: (req) => void) {
31
31
return app
32
32
}
33
33
34
- function getCAInformation ( ) {
35
- return CA . create ( )
36
- . then ( ( ca ) => {
37
- return Promise . all ( [ ca . generateServerCertificateKeys ( 'localhost' ) , ca . getCACertPath ( ) ] )
38
- } )
34
+ async function getCAInformation ( ) {
35
+ const ca = await CA . create ( )
36
+
37
+ const [ serverCertificateKeys , caCertificatePath ] = await Promise . all ( [ ca . generateServerCertificateKeys ( 'localhost' ) , ca . getCACertPath ( ) ] )
38
+
39
+ return { serverCertificateKeys, caCertificatePath }
39
40
}
40
41
41
42
function onWsConnection ( socket ) {
@@ -51,43 +52,42 @@ export class Servers {
51
52
caCertificatePath : string
52
53
lastRequestHeaders : any
53
54
54
- start ( httpPort : number , httpsPort : number ) {
55
- return Promise . join (
55
+ async start ( httpPort : number , httpsPort : number ) {
56
+ const [ app , { serverCertificateKeys , caCertificatePath } ] : [ Express . Application , { serverCertificateKeys : string [ ] , caCertificatePath : string } ] = await Promise . all ( [
56
57
createExpressApp ( ( req ) => this . lastRequestHeaders = req . headers ) ,
57
58
getCAInformation ( ) ,
58
- )
59
- . spread ( ( app : Express . Application , [ serverCertificateKeys , caCertificatePath ] : [ serverCertificateKeys : string [ ] , caCertificatePath : string ] ) => {
60
- this . httpServer = Promise . promisifyAll (
61
- allowDestroy ( http . createServer ( app ) ) ,
62
- ) as http . Server & AsyncServer
63
-
64
- this . wsServer = new SocketIOServer ( this . httpServer )
65
-
66
- this . caCertificatePath = caCertificatePath
67
- this . https = { cert : serverCertificateKeys [ 0 ] , key : serverCertificateKeys [ 1 ] }
68
- this . httpsServer = Promise . promisifyAll (
69
- allowDestroy ( https . createServer ( this . https , < http . RequestListener > app ) ) ,
70
- ) as https . Server & AsyncServer
71
-
72
- this . wssServer = new SocketIOServer ( this . httpsServer )
73
-
74
- ; [ this . wsServer , this . wssServer ] . map ( ( ws ) => {
75
- ws . on ( 'connection' , onWsConnection )
76
- } )
77
-
78
- // @ts -skip
79
- return Promise . join (
80
- this . httpServer . listenAsync ( httpPort ) ,
81
- this . httpsServer . listenAsync ( httpsPort ) ,
82
- )
83
- . return ( )
59
+ ] )
60
+
61
+ this . httpServer = Bluebird . promisifyAll (
62
+ allowDestroy ( http . createServer ( app ) ) ,
63
+ ) as http . Server & AsyncServer
64
+
65
+ this . wsServer = new SocketIOServer ( this . httpServer )
66
+
67
+ this . caCertificatePath = caCertificatePath
68
+ this . https = { cert : serverCertificateKeys [ 0 ] , key : serverCertificateKeys [ 1 ] }
69
+ this . httpsServer = Bluebird . promisifyAll (
70
+ allowDestroy ( https . createServer ( this . https , < http . RequestListener > app ) ) ,
71
+ ) as https . Server & AsyncServer
72
+
73
+ this . wssServer = new SocketIOServer ( this . httpsServer )
74
+
75
+ ; [ this . wsServer , this . wssServer ] . map ( ( ws ) => {
76
+ ws . on ( 'connection' , onWsConnection )
84
77
} )
78
+
79
+ await Promise . all ( [
80
+ this . httpServer . listenAsync ( httpPort ) ,
81
+ this . httpsServer . listenAsync ( httpsPort ) ,
82
+ ] )
83
+
84
+ return undefined
85
85
}
86
86
87
- stop ( ) {
88
- return Promise . join (
87
+ async stop ( ) {
88
+ await Promise . all ( [
89
89
this . httpServer . destroyAsync ( ) ,
90
90
this . httpsServer . destroyAsync ( ) ,
91
- )
91
+ ] )
92
92
}
93
93
}
0 commit comments