@@ -26,6 +26,13 @@ const ERR_INIT = 'Can not initialize an Application';
2626const ERR_TEST = 'Application tests failed' ;
2727const TEST_DELAY = 100 ;
2828
29+ const placeFromPath = ( filePath , appPath ) => {
30+ const relPath = filePath . substring ( appPath . length + 1 ) ;
31+ const sepIndex = relPath . indexOf ( node . path . sep ) ;
32+ const place = relPath . substring ( 0 , sepIndex ) ;
33+ return { relPath, place } ;
34+ } ;
35+
2936class Application extends EventEmitter {
3037 constructor ( ) {
3138 super ( ) ;
@@ -98,35 +105,42 @@ class Application extends EventEmitter {
98105 async start ( ) {
99106 const { sandbox, config, cert, mode } = this ;
100107 const { kind, port } = workerData ;
108+ if ( sandbox . api . auth ) {
109+ const provider = sandbox . api . auth . provider || auth ( config . sessions ) ;
110+ this . auth = provider ;
111+ sandbox . api . auth . provider = provider ;
112+ }
113+ const { concurrency, size, timeout } = config . server . queue ;
114+ this . semaphore = new Semaphore ( { concurrency, size, timeout } ) ;
101115 if ( kind === 'balancer' ) {
102116 const options = { ...config . server , port } ;
103117 this . server = new Balancer ( this , options ) ;
104118 await this . server . listen ( ) ;
105119 } else if ( kind === 'server' ) {
106120 const options = { ...config . server , port } ;
107- if ( config . server . protocol === 'https' ) {
121+ const isHttps = config . server . protocol === 'https' ;
122+ if ( isHttps ) {
108123 options . SNICallback = ( servername , callback ) => {
109124 const domain = cert . get ( servername ) ;
110- if ( ! domain ) callback ( new Error ( `No certificate for ${ servername } ` ) ) ;
111- else callback ( null , domain . creds ) ;
125+ if ( domain === undefined ) {
126+ const err = new Error ( `No certificate for ${ servername } ` ) ;
127+ callback ( err ) ;
128+ } else {
129+ callback ( null , domain . creds ) ;
130+ }
112131 } ;
113132 }
114133 this . server = new Server ( this , options ) ;
115134 this . server . httpServer . on ( 'request' , ( req , res ) => {
116- if ( ! req . url . startsWith ( '/api' ) ) this . static . serve ( req . url , req , res ) ;
135+ const isApi = req . url . startsWith ( '/api' ) ;
136+ if ( ! isApi ) this . static . serve ( req . url , req , res ) ;
117137 } ) ;
118138 await this . server . listen ( ) ;
119139 }
120- if ( sandbox . api . auth ) {
121- const provider = sandbox . api . auth . provider || auth ( config . sessions ) ;
122- this . auth = provider ;
123- sandbox . api . auth . provider = provider ;
124- }
125- const { concurrency, size, timeout } = config . server . queue ;
126- this . semaphore = new Semaphore ( { concurrency, size, timeout } ) ;
127140 this . initialization = false ;
128141 sandbox . application . emit ( 'started' ) ;
129- if ( mode === 'test' && threadId === 1 ) this . runTests ( ) ;
142+ const shouldRunTests = mode === 'test' && threadId === 1 ;
143+ if ( shouldRunTests ) this . runTests ( ) ;
130144 }
131145
132146 async runTests ( ) {
@@ -201,9 +215,7 @@ class Application extends EventEmitter {
201215 this . watcher = new DirectoryWatcher ( { timeout } ) ;
202216
203217 this . watcher . on ( 'change' , ( filePath ) => {
204- const relPath = filePath . substring ( this . path . length + 1 ) ;
205- const sepIndex = relPath . indexOf ( node . path . sep ) ;
206- const place = relPath . substring ( 0 , sepIndex ) ;
218+ const { relPath, place } = placeFromPath ( filePath , this . path ) ;
207219 const target = this [ place ] ;
208220 if ( ! target ) return ;
209221 node . fs . stat ( filePath , ( error , stat ) => {
@@ -221,9 +233,7 @@ class Application extends EventEmitter {
221233 } ) ;
222234
223235 this . watcher . on ( 'delete' , async ( filePath ) => {
224- const relPath = filePath . substring ( this . path . length + 1 ) ;
225- const sepIndex = relPath . indexOf ( node . path . sep ) ;
226- const place = relPath . substring ( 0 , sepIndex ) ;
236+ const { relPath, place } = placeFromPath ( filePath , this . path ) ;
227237 const target = this [ place ] ;
228238 if ( ! target ) return ;
229239 target . delete ( filePath ) ;
0 commit comments