@@ -20,115 +20,116 @@ var yaml = require('js-yaml');
2020 */ 
2121function  initApp ( options )  { 
2222
23-     // the main application object 
24-     var  app  =  express ( ) ; 
25- 
26-     // get the options and make them available in the app 
27-     app . logger  =  options . logger ;     // the logging device 
28-     app . metrics  =  options . metrics ;   // the metrics 
29-     app . conf  =  options . config ;       // this app's config options 
30-     app . info  =  packageInfo ;          // this app's package info 
31- 
32-     // ensure some sane defaults 
33-     if ( ! app . conf . port )  {  app . conf . port  =  8888 ;  } 
34-     if ( ! app . conf . interface )  {  app . conf . interface  =  '0.0.0.0' ;  } 
35-     if ( app . conf . compression_level  ===  undefined )  {  app . conf . compression_level  =  3 ;  } 
36-     if ( app . conf . cors  ===  undefined )  {  app . conf . cors  =  '*' ;  } 
37-     if ( app . conf . csp  ===  undefined )  { 
38-         app . conf . csp  = 
39-             "default-src 'self'; object-src 'none'; media-src *; img-src *; style-src *; frame-ancestors 'self'" ; 
40-     } 
41- 
42-     // set outgoing proxy 
43-     if ( app . conf . proxy )  { 
44-         process . env . HTTP_PROXY  =  app . conf . proxy ; 
45-         // if there is a list of domains which should 
46-         // not be proxied, set it 
47-         if ( app . conf . no_proxy_list )  { 
48-             if ( Array . isArray ( app . conf . no_proxy_list ) )  { 
49-                 process . env . NO_PROXY  =  app . conf . no_proxy_list . join ( ',' ) ; 
50-             }  else  { 
51-                 process . env . NO_PROXY  =  app . conf . no_proxy_list ; 
23+     return  BBPromise . try ( ( )  =>  { 
24+         // the main application object 
25+         var  app  =  express ( ) ; 
26+ 
27+         // get the options and make them available in the app 
28+         app . logger  =  options . logger ;     // the logging device 
29+         app . metrics  =  options . metrics ;   // the metrics 
30+         app . conf  =  options . config ;       // this app's config options 
31+         app . info  =  packageInfo ;          // this app's package info 
32+ 
33+         // ensure some sane defaults 
34+         if ( ! app . conf . port )  {  app . conf . port  =  8888 ;  } 
35+         if ( ! app . conf . interface )  {  app . conf . interface  =  '0.0.0.0' ;  } 
36+         if ( app . conf . compression_level  ===  undefined )  {  app . conf . compression_level  =  3 ;  } 
37+         if ( app . conf . cors  ===  undefined )  {  app . conf . cors  =  '*' ;  } 
38+         if ( app . conf . csp  ===  undefined )  { 
39+             app . conf . csp  = 
40+                 "default-src 'self'; object-src 'none'; media-src *; img-src *; style-src *; frame-ancestors 'self'" ; 
41+         } 
42+ 
43+         // set outgoing proxy 
44+         if ( app . conf . proxy )  { 
45+             process . env . HTTP_PROXY  =  app . conf . proxy ; 
46+             // if there is a list of domains which should 
47+             // not be proxied, set it 
48+             if ( app . conf . no_proxy_list )  { 
49+                 if ( Array . isArray ( app . conf . no_proxy_list ) )  { 
50+                     process . env . NO_PROXY  =  app . conf . no_proxy_list . join ( ',' ) ; 
51+                 }  else  { 
52+                     process . env . NO_PROXY  =  app . conf . no_proxy_list ; 
53+                 } 
5254            } 
5355        } 
54-     } 
55- 
56-     // set up header whitelisting for logging 
57-     if ( ! app . conf . log_header_whitelist )  { 
58-         app . conf . log_header_whitelist  =  [ 
59-                 'cache-control' ,  'content-type' ,  'content-length' ,  'if-match' , 
60-                 'user-agent' ,  'x-request-id' 
61-         ] ; 
62-     } 
63-     app . conf . log_header_whitelist  =  new  RegExp ( '^(?:'  +  app . conf . log_header_whitelist . map ( function ( item )  { 
64-         return  item . trim ( ) ; 
65-     } ) . join ( '|' )  +  ')$' ,  'i' ) ; 
66- 
67-     // set up the request templates for the APIs 
68-     apiUtil . setupApiTemplates ( app ) ; 
69- 
70-     // set up the spec 
71-     if ( ! app . conf . spec )  { 
72-         app . conf . spec  =  __dirname  +  '/spec.yaml' ; 
73-     } 
74-     if ( app . conf . spec . constructor  !==  Object )  { 
75-         try  { 
76-             app . conf . spec  =  yaml . safeLoad ( fs . readFileSync ( app . conf . spec ) ) ; 
77-         }  catch ( e )  { 
78-             app . logger . log ( 'warn/spec' ,  'Could not load the spec: '  +  e ) ; 
79-             app . conf . spec  =  { } ; 
56+ 
57+         // set up header whitelisting for logging 
58+         if ( ! app . conf . log_header_whitelist )  { 
59+             app . conf . log_header_whitelist  =  [ 
60+                     'cache-control' ,  'content-type' ,  'content-length' ,  'if-match' , 
61+                     'user-agent' ,  'x-request-id' 
62+             ] ; 
63+         } 
64+         app . conf . log_header_whitelist  =  new  RegExp ( '^(?:'  +  app . conf . log_header_whitelist . map ( function ( item )  { 
65+             return  item . trim ( ) ; 
66+         } ) . join ( '|' )  +  ')$' ,  'i' ) ; 
67+ 
68+         // set up the request templates for the APIs 
69+         apiUtil . setupApiTemplates ( app ) ; 
70+ 
71+         // set up the spec 
72+         if ( ! app . conf . spec )  { 
73+             app . conf . spec  =  __dirname  +  '/spec.yaml' ; 
8074        } 
81-     } 
82-     if ( ! app . conf . spec . swagger )  { 
83-         app . conf . spec . swagger  =  '2.0' ; 
84-     } 
85-     if ( ! app . conf . spec . info )  { 
86-         app . conf . spec . info  =  { 
87-             version : app . info . version , 
88-             title : app . info . name , 
89-             description : app . info . description 
90-         } ; 
91-     } 
92-     app . conf . spec . info . version  =  app . info . version ; 
93-     if ( ! app . conf . spec . paths )  { 
94-         app . conf . spec . paths  =  { } ; 
95-     } 
96- 
97-     // set the CORS and CSP headers 
98-     app . all ( '*' ,  function ( req ,  res ,  next )  { 
99-         if ( app . conf . cors  !==  false )  { 
100-             res . header ( 'access-control-allow-origin' ,  app . conf . cors ) ; 
101-             res . header ( 'access-control-allow-headers' ,  'accept, x-requested-with, content-type' ) ; 
102-             res . header ( 'access-control-expose-headers' ,  'etag' ) ; 
75+         if ( app . conf . spec . constructor  !==  Object )  { 
76+             try  { 
77+                 app . conf . spec  =  yaml . safeLoad ( fs . readFileSync ( app . conf . spec ) ) ; 
78+             }  catch ( e )  { 
79+                 app . logger . log ( 'warn/spec' ,  'Could not load the spec: '  +  e ) ; 
80+                 app . conf . spec  =  { } ; 
81+             } 
10382        } 
104-         if ( app . conf . csp  !==  false )  { 
105-             res . header ( 'x-xss-protection' ,  '1; mode=block' ) ; 
106-             res . header ( 'x-content-type-options' ,  'nosniff' ) ; 
107-             res . header ( 'x-frame-options' ,  'SAMEORIGIN' ) ; 
108-             res . header ( 'content-security-policy' ,  app . conf . csp ) ; 
109-             res . header ( 'x-content-security-policy' ,  app . conf . csp ) ; 
110-             res . header ( 'x-webkit-csp' ,  app . conf . csp ) ; 
83+         if ( ! app . conf . spec . swagger )  { 
84+             app . conf . spec . swagger  =  '2.0' ; 
85+         } 
86+         if ( ! app . conf . spec . info )  { 
87+             app . conf . spec . info  =  { 
88+                 version : app . info . version , 
89+                 title : app . info . name , 
90+                 description : app . info . description 
91+             } ; 
92+         } 
93+         app . conf . spec . info . version  =  app . info . version ; 
94+         if ( ! app . conf . spec . paths )  { 
95+             app . conf . spec . paths  =  { } ; 
11196        } 
112-         sUtil . initAndLogRequest ( req ,  app ) ; 
113-         next ( ) ; 
114-     } ) ; 
11597
116-     // set up the user agent header string to use for requests 
117-     app . conf . user_agent  =  app . conf . user_agent  ||  app . info . name ; 
98+         // set the CORS and CSP headers 
99+         app . all ( '*' ,  function ( req ,  res ,  next )  { 
100+             if ( app . conf . cors  !==  false )  { 
101+                 res . header ( 'access-control-allow-origin' ,  app . conf . cors ) ; 
102+                 res . header ( 'access-control-allow-headers' ,  'accept, x-requested-with, content-type' ) ; 
103+                 res . header ( 'access-control-expose-headers' ,  'etag' ) ; 
104+             } 
105+             if ( app . conf . csp  !==  false )  { 
106+                 res . header ( 'x-xss-protection' ,  '1; mode=block' ) ; 
107+                 res . header ( 'x-content-type-options' ,  'nosniff' ) ; 
108+                 res . header ( 'x-frame-options' ,  'SAMEORIGIN' ) ; 
109+                 res . header ( 'content-security-policy' ,  app . conf . csp ) ; 
110+                 res . header ( 'x-content-security-policy' ,  app . conf . csp ) ; 
111+                 res . header ( 'x-webkit-csp' ,  app . conf . csp ) ; 
112+             } 
113+             sUtil . initAndLogRequest ( req ,  app ) ; 
114+             next ( ) ; 
115+         } ) ; 
118116
119-     // disable the X-Powered-By header 
120-     app . set ( 'x-powered-by' ,  false ) ; 
121-     // disable the ETag header - users should provide them! 
122-     app . set ( 'etag' ,  false ) ; 
123-     // enable compression 
124-     app . use ( compression ( { level : app . conf . compression_level } ) ) ; 
125-     // use the JSON body parser 
126-     app . use ( bodyParser . json ( ) ) ; 
127-     // use the application/x-www-form-urlencoded parser 
128-     app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; 
117+         // set up the user agent header string to use for requests 
118+         app . conf . user_agent  =  app . conf . user_agent  ||  app . info . name ; 
129119
130-     return  BBPromise . resolve ( app ) ; 
120+         // disable the X-Powered-By header 
121+         app . set ( 'x-powered-by' ,  false ) ; 
122+         // disable the ETag header - users should provide them! 
123+         app . set ( 'etag' ,  false ) ; 
124+         // enable compression 
125+         app . use ( compression ( { level : app . conf . compression_level } ) ) ; 
126+         // use the JSON body parser 
127+         app . use ( bodyParser . json ( ) ) ; 
128+         // use the application/x-www-form-urlencoded parser 
129+         app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; 
131130
131+         return  app ; 
132+     } ) ; 
132133} 
133134
134135
@@ -141,7 +142,7 @@ function loadRoutes (app) {
141142
142143    // get the list of files in routes/ 
143144    return  fs . readdirAsync ( __dirname  +  '/routes' ) . map ( function ( fname )  { 
144-         return  BBPromise . try ( function ( )  { 
145+         return  BBPromise . try ( ( )   =>  { 
145146            // ... and then load each route 
146147            // but only if it's a js file 
147148            if ( ! / \. j s $ / . test ( fname ) )  { 
@@ -150,7 +151,8 @@ function loadRoutes (app) {
150151            // import the route file 
151152            var  route  =  require ( __dirname  +  '/routes/'  +  fname ) ; 
152153            return  route ( app ) ; 
153-         } ) . then ( function ( route )  { 
154+         } ) 
155+         . then ( route  =>  { 
154156            if ( route  ===  undefined )  { 
155157                return  undefined ; 
156158            } 
@@ -173,11 +175,12 @@ function loadRoutes (app) {
173175            // all good, use that route 
174176            app . use ( route . path ,  route . router ) ; 
175177        } ) ; 
176-     } ) . then ( function  ( )  { 
178+     } ) 
179+     . then ( ( )  =>  { 
177180        // catch errors 
178181        sUtil . setErrorHandler ( app ) ; 
179182        // route loading is now complete, return the app object 
180-         return  BBPromise . resolve ( app ) ; 
183+         return  app ; 
181184    } ) ; 
182185
183186} 
@@ -194,20 +197,21 @@ function createServer(app) {
194197    // attaches the app to it, and starts accepting 
195198    // incoming client requests 
196199    var  server ; 
197-     return  new  BBPromise ( function   ( resolve )  { 
200+     return  new  BBPromise ( resolve   =>  { 
198201        server  =  http . createServer ( app ) . listen ( 
199202            app . conf . port , 
200203            app . conf . interface , 
201204            resolve 
202205        ) ; 
203-     } ) . then ( function  ( )  { 
206+     } ) 
207+     . then ( ( )  =>  { 
204208        app . logger . log ( 'info' , 
205209            'Worker '  +  process . pid  +  ' listening on '  +  ( app . conf . interface  ||  '*' )  +  ':'  +  app . conf . port ) ; 
206210
207211        // Don't delay incomplete packets for 40ms (Linux default) on 
208212        // pipelined HTTP sockets. We write in large chunks or buffers, so 
209213        // lack of coalescing should not be an issue here. 
210-         server . on ( "connection" ,  function ( socket )  { 
214+         server . on ( "connection" ,  function   ( socket )  { 
211215            socket . setNoDelay ( true ) ; 
212216        } ) ; 
213217
@@ -227,7 +231,7 @@ module.exports = function(options) {
227231
228232    return  initApp ( options ) 
229233    . then ( loadRoutes ) 
230-     . then ( function ( app )  { 
234+     . then ( app   =>  { 
231235        // serve static files from static/ 
232236        app . use ( '/static' ,  express . static ( __dirname  +  '/static' ) ) ; 
233237        return  app ; 
0 commit comments