-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathroosevelt.js
executable file
·425 lines (357 loc) · 16.1 KB
/
roosevelt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
require('@colors/colors')
const express = require('express')
const path = require('path')
const fs = require('fs-extra')
const appModulePath = require('app-module-path')
const Logger = require('roosevelt-logger')
const certsGenerator = require('./lib/scripts/certsGenerator.js')
const sessionSecretGenerator = require('./lib/scripts/sessionSecretGenerator.js')
const csrfSecretGenerator = require('./lib/scripts/csrfSecretGenerator.js')
const roosevelt = (options = {}, schema) => {
options.appDir = options.appDir || path.dirname(module.parent.filename) // appDir is either specified by the user or sourced from the parent require
const connections = {}
const app = express() // initialize express
const router = express.Router() // initialize router
let httpServer
let httpsServer
let initialized = false
let checkConnectionsTimeout
let persistProcess
let logger
let appName
let appEnv
// source user-supplied params
const params = require('./lib/sourceParams')(options, schema)
const appDir = params.appDir
const pkg = params.pkg
app.set('params', params) // expose app configuration
// use existence of public folder to determine if this is the first run
if (!fs.pathExistsSync(params.publicFolder) && params.logging.methods.info) {
// run the param audit
require('./lib/scripts/configAuditor').audit(params.appDir)
require('./lib/scripts/deprecationCheck')(params.appDir)
}
// utility functions
async function initServer () {
if (initialized) return
initialized = true
// configure logger with params
logger = new Logger(params.logging)
// expose express variables
app.set('express', express)
app.set('router', router)
app.set('env', params.mode === 'production-proxy' ? 'production' : params.mode)
app.set('logger', logger) // expose instance of roosevelt-logger module
app.set('appDir', appDir)
app.set('package', pkg) // expose contents of package.json if any
app.set('appName', pkg.name || 'Roosevelt Express')
app.set('appVersion', pkg.version)
app.set('routePrefix', params.routePrefix)
app.set('modelsPath', path.join(appDir, params.modelsPath))
app.set('viewsPath', path.join(appDir, params.viewsPath))
app.set('controllersPath', path.join(appDir, params.controllersPath))
app.set('staticsRoot', params.staticsRoot)
app.set('htmlPath', params.html.sourcePath)
app.set('htmlModels', params.html.models)
app.set('cssPath', params.css.sourcePath)
app.set('jsPath', params.js.sourcePath)
app.set('htmlRenderedOutput', params.html.output)
app.set('cssCompiledOutput', params.css.output)
app.set('clientViewsBundledOutput', params.clientViews.output)
app.set('isomorphicControllersListOutput', params.isomorphicControllers.output)
app.set('publicFolder', params.unversionedPublic)
// make the app directory requirable
appModulePath.addPath(appDir)
// make the models directory requirable
appModulePath.addPath(path.join(appDir, params.modelsPath, '../'))
// make the controllers directory requirable
appModulePath.addPath(path.join(appDir, params.controllersPath, '../'))
appName = app.get('appName')
appEnv = app.get('env')
// app starting message
if (params.makeBuildArtifacts === 'staticsOnly') logger.info('💭', `Building ${appName} static site in ${appEnv} mode...`.bold)
else logger.info('💭', `Starting ${appName} in ${appEnv} mode...`.bold)
// generate express session secret
if (params.expressSession && params.makeBuildArtifacts !== 'staticsOnly') {
if (!fs.pathExistsSync(params.secretsDir) || !fs.pathExistsSync(params.secretsDir + '/sessionSecret.json')) {
sessionSecretGenerator()
}
}
// generate csrf secret
if (params.csrfProtection && params.makeBuildArtifacts !== 'staticsOnly') {
if (!fs.pathExistsSync(params.secretsDir) || !fs.pathExistsSync(params.secretsDir + '/csrfSecret.json')) {
csrfSecretGenerator()
}
}
// assign individual keys to connections when opened so they can be destroyed gracefully
function mapConnections (conn) {
const key = conn.remoteAddress + ':' + conn.remotePort
connections[key] = conn
// once the connection closes, remove
conn.on('close', function () {
delete connections[key]
if (app.get('roosevelt:state') === 'disconnecting') {
Object.keys(connections).length === 0 && closeServer() // this will close the server if there are no connections
}
})
}
// set up http server
if (!params.https.force || !params.https.enable) {
httpServer = require('http').Server(app)
httpServer.on('connection', mapConnections)
}
// setup https server if https is enabled
if (params.https.enable && params.makeBuildArtifacts !== 'staticsOnly') {
const authInfoPath = params.https.authInfoPath
const httpsOptions = {}
// runs the certGenerator if params.https.enable
if (params.https.autoCert && authInfoPath?.authCertAndKey) {
const { authCertAndKey } = authInfoPath
if ((!fs.pathExistsSync(params.secretsDir) || (!fs.pathExistsSync(authCertAndKey.key) || (!fs.pathExistsSync(authCertAndKey.cert))))) {
certsGenerator()
}
}
function isCertString (stringToTest) {
let testString = stringToTest
if (typeof testString !== 'string') {
testString = testString.toString()
}
const lastChar = testString.substring(testString.length - 1)
// A file path string won't have an end of line character at the end
// Looking for either \n or \r allows for nearly any OS someone could
// use, and a few that node doesn't work on.
if (lastChar === '\n' || lastChar === '\r') {
return true
}
return false
}
if (authInfoPath) {
if (authInfoPath.p12?.p12Path) {
// if the string ends with a dot and 3 alphanumeric characters (including _)
// then we assume it's a filepath.
if (typeof authInfoPath.p12.p12Path === 'string' && authInfoPath.p12.p12Path.match(/\.\w{3}$/)) {
httpsOptions.pfx = fs.readFileSync(path.join(params.appDir, params.secretsDir, authInfoPath.p12.p12Path))
} else { // if the string doesn't end that way, we assume it's an encrypted string
httpsOptions.pfx = authInfoPath.p12.p12Path
}
} else if (authInfoPath.authCertAndKey) {
function assignCertStringByKey (key) {
const { authCertAndKey } = authInfoPath
const certString = authCertAndKey[key]
if (isCertString(certString)) httpsOptions[key] = certString
else httpsOptions[key] = fs.readFileSync(path.join(params.appDir, params.secretsDir, certString))
}
if (authInfoPath.authCertAndKey.cert) assignCertStringByKey('cert')
if (authInfoPath.authCertAndKey.key) assignCertStringByKey('key')
}
// set passphrase if in use
if (params.https.passphrase) httpsOptions.passphrase = params.https.passphrase
}
if (params.https.caCert) {
if (typeof params.https.caCert === 'string') {
if (isCertString(params.https.caCert)) { // then it's the cert(s) as a string, not a file path
httpsOptions.ca = params.https.caCert
} else { // it's a file path to the file, so read file
httpsOptions.ca = fs.readFileSync(path.join(params.appDir, params.secretsDir, params.https.caCert))
}
} else if (params.https.caCert instanceof Array) {
httpsOptions.ca = []
for (const certOrPath of params.https.caCert) {
let certStr = certOrPath
if (!isCertString(certOrPath)) certStr = fs.readFileSync(certOrPath)
httpsOptions.ca.push(certStr)
}
}
}
httpsOptions.requestCert = params.https.requestCert
httpsOptions.rejectUnauthorized = params.https.rejectUnauthorized
httpsServer = require('https').Server(httpsOptions, app)
httpsServer.on('connection', mapConnections)
}
// expose http server(s) to the user via express var
app.set('httpServer', httpServer)
app.set('httpsServer', httpsServer)
// fire user-defined onBeforeMiddleware event
if (params.onBeforeMiddleware && typeof params.onBeforeMiddleware === 'function') {
await Promise.resolve(params.onBeforeMiddleware(app))
}
// enable gzip compression
app.use(require('compression')())
// enable favicon support
if (params.favicon !== 'none' && params.favicon !== null) {
const faviconPath = path.join(params.staticsRoot, params.favicon)
if (fs.pathExistsSync(faviconPath)) app.use(require('serve-favicon')(faviconPath))
else logger.warn(`Favicon ${params.favicon} does not exist. Please ensure the "favicon" param is configured correctly.`)
}
// configure express, express-session, and csrf
require('./lib/setExpressConfigs')(app)
require('./lib/generateSymlinks')(app)
require('./lib/htmlMinifier')(app)
await require('./lib/preprocessStaticPages')(app)
await require('./lib/preprocessCss')(app)
await require('./lib/viewsBundler')(app)
await require('./lib/jsBundler')(app)
if (app.get('env') === 'development' && params.htmlValidator.enable) {
// instantiate the validator if it's installed
try {
require('express-html-validator')(app, params.htmlValidator)
} catch { }
}
require('./lib/injectReload')(app)
// map routes
await require('./lib/mapRoutes')(app)
// custom error page
app.use((err, req, res, next) => {
logger.error(err.stack)
require(params.errorPages.internalServerError)(app, err, req, res)
})
await require('./lib/isomorphicControllersFinder')(app)
// fire user-defined onServerInit event
if (params.onServerInit && typeof params.onServerInit === 'function') {
await Promise.resolve(params.onServerInit(app))
}
}
async function startServer () {
await initServer()
const numberOfServers = params.https.enable && !params.https.force ? 2 : 1
let listeningServers = 0
// code that executes after the server starts
function startupCallback (proto, port) {
async function startReloadServer (proto, server) {
const reload = require('reload')
const config = {
verbose: !!params.logging.methods.verbose,
webSocketServerWaitStart: true
}
if (proto === 'HTTP') {
config.route = '/reloadHttp'
config.port = params.frontendReload.port
} else {
config.route = '/reloadHttps'
config.port = params.frontendReload.httpsPort
config.forceWss = false // lets this work in self-signed cert situations
config.https = {
p12: server.pfx,
certAndKey: {
cert: server.cert,
key: server.key
},
passphrase: server.passphrase
}
}
return reload(router, config)
}
return async function () {
// spin up reload http(s) service if enabled in dev mode
if (appEnv === 'development' && params.frontendReload.enable === true) {
let reloadServer
const config = params.frontendReload
// get reload ready and bind instance to express variable
if (proto === 'HTTP') {
reloadServer = await startReloadServer(proto)
app.set('reloadHttpServer', reloadServer)
} else {
reloadServer = await startReloadServer(proto, httpsServer)
app.set('reloadHttpsServer', reloadServer)
}
// spin up the reload server
await reloadServer.startWebSocketServer()
logger.log('🎧', `${appName} frontend reload ${proto} server is listening on port ${proto === 'HTTP' ? config.port : config.httpsPort}`)
}
logger.info('🎧', `${appName} ${proto} server listening on port ${port} (${appEnv} mode) ➡️ ${proto.toLowerCase()}://localhost:${port}`.bold)
if (params.localhostOnly) {
logger.warn(`${appName} will only respond to requests coming from localhost. If you wish to override this behavior and have it respond to requests coming from outside of localhost, then set "localhostOnly" to false. See the Roosevelt documentation for more information: https://github.com/rooseveltframework/roosevelt`)
}
if (!params.hostPublic) {
logger.warn('Hosting of public folder is disabled. Your CSS, JS, images, and other files served via your public folder will not load unless you serve them via another web server. If you wish to override this behavior and have Roosevelt host your public folder even in production mode, then set "hostPublic" to true. See the Roosevelt documentation for more information: https://github.com/rooseveltframework/roosevelt')
}
listeningServers++
// fire user-defined onServerStart event if all servers are started
if (listeningServers === numberOfServers) {
if (params.onServerStart && typeof params.onServerStart === 'function') {
Promise.resolve(params.onServerStart(app))
}
}
}
}
if (params.makeBuildArtifacts !== 'staticsOnly') {
if (!params.https.force || !params.https.enable) {
httpServer.listen(params.port, (params.localhostOnly ? 'localhost' : null), startupCallback('HTTP', params.port)).on('error', err => {
logger.error(err)
logger.error(`Another process is using port ${params.port}. Either kill that process or change this app's port number.`.bold)
process.exit(1)
})
}
if (params.https.enable) {
httpsServer.listen(params.https.port, (params.localhostOnly ? 'localhost' : null), startupCallback('HTTPS', params.https.port)).on('error', err => {
logger.error(err)
logger.error(`Another process is using port ${params.https.port}. Either kill that process or change this app's port number.`.bold)
process.exit(1)
})
}
}
process.on('SIGTERM', shutdownGracefully)
process.on('SIGINT', shutdownGracefully)
}
// shut down all servers, connections and threads that the roosevelt app is using
function shutdownGracefully (args) {
persistProcess = args?.persistProcess
// fire user-defined onAppExit event
if (params.onAppExit && typeof params.onAppExit === 'function') {
params.onAppExit(app)
}
// force destroy connections if the server takes too long to shut down
checkConnectionsTimeout = setTimeout(() => {
logger.error(`${appName} could not close all connections in time; forcefully shutting down`)
for (const key in connections) {
connections[key].destroy()
}
if (persistProcess) {
if (httpServer) {
httpServer.close()
}
if (httpsServer) {
httpsServer.close()
}
} else {
process.exit()
}
}, params.shutdownTimeout)
app.set('roosevelt:state', 'disconnecting')
logger.info('\n💭 ', `${appName} received kill signal, attempting to shut down gracefully.`.magenta)
// if the app is in development mode, kill all connections instantly and exit
if (appEnv === 'development') {
for (const key in connections) {
connections[key].destroy()
}
closeServer()
} else {
// else do the normal procedure of seeing if there are still connections before closing
Object.keys(connections).length === 0 && closeServer() // this will close the server if there are no connections
}
}
function closeServer () {
clearTimeout(checkConnectionsTimeout)
logger.info('✅', `${appName} successfully closed all connections and shut down gracefully.`.green)
if (persistProcess) {
if (httpServer) {
httpServer.close()
}
if (httpsServer) {
httpsServer.close()
}
} else {
process.exit()
}
}
return {
expressApp: app,
initServer,
init: initServer,
startServer,
start: startServer,
stopServer: shutdownGracefully,
stop: shutdownGracefully
}
}
module.exports = roosevelt