-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
768 lines (614 loc) · 22.1 KB
/
index.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
var path = require('path');
var fs = require('fs');
var temp = require('temp');
var url = require('url');
var async = require("async");
var util = require("./util/util");
var http = require('http');
var https = require('https');
var cluster = require("cluster");
var logFactory = require("./util/logger");
// system logger
var systemLogger = logFactory("system", { wid: true });
if (typeof(process.env.CLOUDCMS_SYSTEM_LOGGER_LEVEL) !== "undefined") {
systemLogger.setLevel(("" + process.env.CLOUDCMS_SYSTEM_LOGGER_LEVEL).toLowerCase(), true);
}
else {
systemLogger.setLevel("info");
}
process.logInfo = process.log = function(text, level)
{
systemLogger.log(text, level);
};
// by default, set up Gitana driver so that it limits to five concurrent HTTP requests back to Cloud CMS API at at time
var Gitana = require("gitana");
// default keep alive (3 minutes)
process.defaultKeepAliveMs = (3 * 60 * 1000);
// default http timeout (2 minutes)
process.defaultHttpTimeoutMs = 2 * 60 * 1000;
// default exclusive lock timeout (2 minutes)
process.defaultExclusiveLockTimeoutMs = 2 * 60 * 1000;
if (process.env.DEFAULT_HTTP_TIMEOUT_MS)
{
try
{
process.defaultHttpTimeoutMs = parseInt(process.env.DEFAULT_HTTP_TIMEOUT_MS);
}
catch (e)
{
}
}
// dns fix for Node 17 +
// see: https://nodejs.org/api/dns.html#dnssetdefaultresultorderorder
var dns = require("dns");
if (typeof(dns.setDefaultResultOrder) !== "undefined") {
dns.setDefaultResultOrder("ipv4first");
}
// default agents
var HttpKeepAliveAgent = require('agentkeepalive');
var HttpsKeepAliveAgent = require('agentkeepalive').HttpsAgent;
http.globalAgent = new HttpKeepAliveAgent({
keepAlive: true,
keepAliveMsecs: process.defaultKeepAliveMs,
maxSockets: 1024,
maxFreeSockets: 256,
timeout: process.defaultHttpTimeoutMs,
freeSocketTimeout: 5000
});
https.globalAgent = new HttpsKeepAliveAgent({
keepAlive: true,
keepAliveMsecs: process.defaultKeepAliveMs,
maxSockets: 1024,
maxFreeSockets: 256,
timeout: process.defaultHttpTimeoutMs,
freeSocketTimeout: 5000
});
// install dns cache
const CacheableLookup = require("cacheable-lookup");
const cacheable = new CacheableLookup({
// Set any custom options here
});
cacheable.install(http.globalAgent);
cacheable.install(https.globalAgent);
// disable for now
/*
// report http/https socket state every minute
var socketReportFn = function()
{
setTimeout(function() {
var http = require("http");
var https = require("https");
process.log("--- START SOCKET REPORT ---");
process.log("[http]: " + JSON.stringify(http.globalAgent.getCurrentStatus(), null, " "));
process.log("[https]:" + JSON.stringify(https.globalAgent.getCurrentStatus(), null, " "));
process.log("--- END SOCKET REPORT ---");
socketReportFn();
}, 60 * 1000);
};
socketReportFn();
*/
// root ssl ca's
require("ssl-root-cas").inject();
/**
* Supports the following directory structure:
*
*
* /hosts
*
* /<host>
*
* /public
index.html
*
* /content
* /local
* /en_us
* image.jpg
*
* /cloudcms
* /<branchId>
* /en_us
* image.jpg
*
* @type {exports}
*/
exports = module.exports = function()
{
// track temporary files
temp.track();
// TODO: this is to disable really annoying Express 3.0 deprecated's for multipart() which should hopefully
// TODO: be resolved soon
console.warn = function() {};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// assume app-server base path if none provided
if (!process.env.CLOUDCMS_APPSERVER_BASE_PATH) {
process.env.CLOUDCMS_APPSERVER_BASE_PATH = process.cwd();
}
// if there is a "gitana.json" file in the app server base path, we load proxy settings from there if they're
// not already specified
var defaultGitanaProxyScheme = "https";
var defaultGitanaProxyHost = "api.cloudcms.com";
var defaultGitanaProxyPath = "";
var defaultGitanaProxyPort = 443;
var gitanaJsonPath = path.join(process.env.CLOUDCMS_APPSERVER_BASE_PATH, "gitana.json");
if (fs.existsSync(gitanaJsonPath))
{
var gitanaJson = util.jsonParse("" + fs.readFileSync(gitanaJsonPath));
if (gitanaJson && gitanaJson.baseURL)
{
var parsedUrl = url.parse(gitanaJson.baseURL);
defaultGitanaProxyHost = parsedUrl.hostname;
defaultGitanaProxyPath = parsedUrl.path;
defaultGitanaProxyScheme = parsedUrl.protocol.substring(0, parsedUrl.protocol.length - 1); // remove the :
if (parsedUrl.port)
{
defaultGitanaProxyPort = parsedUrl.port;
}
else
{
defaultGitanaProxyPort = 80;
if (defaultGitanaProxyScheme === "https")
{
defaultGitanaProxyPort = 443;
}
}
}
}
// init
if (!process.env.GITANA_PROXY_SCHEME) {
process.env.GITANA_PROXY_SCHEME = defaultGitanaProxyScheme;
}
if (!process.env.GITANA_PROXY_HOST) {
process.env.GITANA_PROXY_HOST = defaultGitanaProxyHost;
}
if (!process.env.GITANA_PROXY_PATH) {
process.env.GITANA_PROXY_PATH = defaultGitanaProxyPath;
}
if (!process.env.GITANA_PROXY_PORT) {
process.env.GITANA_PROXY_PORT = defaultGitanaProxyPort;
}
if (cluster.isMaster)
{
process.log("Gitana Proxy pointed to: " + util.asURL(process.env.GITANA_PROXY_SCHEME, process.env.GITANA_PROXY_HOST, process.env.GITANA_PROXY_PORT, process.env.GITANA_PROXY_PATH));
}
// all web modules are included by default
if (!process.includeWebModule) {
process.includeWebModule = function(host, moduleId) {
return true;
};
}
// middleware
var admin = require("./middleware/admin/admin");
var authentication = require("./middleware/authentication/authentication");
var authorization = require("./middleware/authorization/authorization");
var cache = require("./middleware/cache/cache");
var cloudcms = require("./middleware/cloudcms/cloudcms");
var config = require("./middleware/config/config");
var debug = require("./middleware/debug/debug");
var deployment = require("./middleware/deployment/deployment");
var driver = require("./middleware/driver/driver");
var driverConfig = require("./middleware/driver-config/driver-config");
var final = require("./middleware/final/final");
var flow = require("./middleware/flow/flow");
var form = require("./middleware/form/form");
var healthcheck = require("./middleware/healthcheck/healthcheck");
var host = require("./middleware/host/host");
var graphql = require("./middleware/graphql/graphql");
var libraries = require("./middleware/libraries/libraries");
var local = require("./middleware/local/local");
var locale = require("./middleware/locale/locale");
var modules = require("./middleware/modules/modules");
var perf = require("./middleware/perf/perf");
var proxy = require("./middleware/proxy/proxy");
var registration = require("./middleware/registration/registration");
var resources = require("./middleware/resources/resources");
var runtime = require("./middleware/runtime/runtime");
var storeService = require("./middleware/stores/stores");
var templates = require("./middleware/templates/templates");
var virtualConfig = require("./middleware/virtual-config/virtual-config");
var virtualFiles = require("./middleware/virtual-files/virtual-files");
var welcome = require("./middleware/welcome/welcome");
var awareness = require("./middleware/awareness/awareness");
var userAgent = require('express-useragent');
// services
var notifications = require("./notifications/notifications");
var broadcast = require("./broadcast/broadcast");
var locks = require("./locks/locks");
// cache
//process.cache = cache;
// read the package.json file and determine the build timestamp
var packageJsonPath = path.resolve(__dirname, "package.json");
if (fs.existsSync(packageJsonPath))
{
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
process.env.CLOUDCMS_APPSERVER_PACKAGE_NAME = packageJson.name;
process.env.CLOUDCMS_APPSERVER_PACKAGE_VERSION = packageJson.version;
}
// object that we hand back
var r = {};
r.init = function(app, callback)
{
if (process.configuration && process.configuration.gitana)
{
if (typeof(process.configuration.gitana.httpWorkQueueSize) !== "undefined")
{
if (process.configuration.gitana.httpWorkQueueSize > -1)
{
Gitana.HTTP_WORK_QUEUE_SIZE = process.configuration.gitana.httpWorkQueueSize;
process.log("Limiting Gitana Driver HTTP work queue to size: " + Gitana.HTTP_WORK_QUEUE_SIZE);
}
}
}
process.cache = app.cache = cache;
process.broadcast = app.broadcast = broadcast;
process.locks = app.locks = locks;
process.authentication = app.authentication = authentication;
// app specific
app.filter = process.authentication.filter;
var fns = [
locks.init,
broadcast.start,
storeService.init,
notifications.start,
cache.init,
awareness.init
];
async.series(fns, function(err) {
callback(err);
});
};
r.common1 = function(app)
{
// app config interceptor
applyApplicationConfiguration(app);
// sets locale onto the request
app.use(locale.localeInterceptor());
// sets host onto the request
app.use(host.hostInterceptor());
};
r.common2 = function(app)
{
// bind stores into the request
app.use(storeService.storesInterceptor());
// puts req.descriptor into the request and req.virtualFiles = true
app.use(virtualFiles.interceptor());
// puts req.runtime into the request
app.use(runtime.interceptor());
// if virtual hosting is enabled, loads "gitana.json" from cloud cms and places it into rootStore
// for convenience, also populates req.gitanaConfig
app.use(virtualConfig.interceptor());
// general method for finding "gitana.json" in root store and populating req.gitanaConfig
app.use(driverConfig.interceptor());
};
r.common3 = function(app)
{
// binds "req.gitana" into the request for the loaded "req.gitanaConfig"
app.use(driver.driverInterceptor());
};
r.common4 = function(app, includeCloudCMS)
{
var configuration = app.configuration;
if (includeCloudCMS)
{
// bind a cache helper
app.use(cache.cacheInterceptor());
// auto-select the application
app.use(cloudcms.applicationInterceptor());
// auto-select the application settings
app.use(cloudcms.applicationSettingsInterceptor());
// auto-select which gitana repository to use
app.use(cloudcms.repositoryInterceptor());
// auto-select which gitana branch to use
// allows for branch specification via request parameter
app.use(cloudcms.branchInterceptor());
// auto-select which gitana domain to use
app.use(cloudcms.domainInterceptor());
// enables ICE menu
// app.use(cloudcms.iceInterceptor());
// enables cms logging
app.use(cloudcms.cmsLogInterceptor());
}
// graphql
app.use(graphql.interceptor());
};
r.perf1 = function(app)
{
app.use(perf.pathPerformanceInterceptor());
};
r.proxy = function(app)
{
app.use(proxy.proxy());
};
r.perf2 = function(app)
{
app.use(perf.mimeTypePerformanceInterceptor());
};
r.perf3 = function(app)
{
app.use(perf.developmentPerformanceInterceptor());
};
var applyApplicationConfiguration = r.applyApplicationConfiguration = function(app)
{
// binds req.config describing the proper app config to use for the request's current application
app.use(function(req, res, next) {
var finish = function(configuration)
{
req.configuration = function(name, callback)
{
if (typeof(name) === "function")
{
return callback(null, configuration);
}
if (!name)
{
return callback();
}
callback(null, configuration[name]);
};
req.isEnabled = function(name)
{
return (configuration && configuration[name] && configuration[name].enabled);
};
next();
};
var configuration = util.clone(process.configuration);
if (req.application)
{
req.application(function(err, application) {
if (application)
{
var applicationConfiguration = application.runtime;
if (applicationConfiguration)
{
// merge configs
util.merge(applicationConfiguration, configuration);
}
finish(configuration);
}
else
{
finish(configuration);
}
});
}
else
{
finish(configuration);
}
});
};
r.welcome = function(app)
{
// support for "welcome" files (i.e. index.html)
app.use(welcome.welcomeInterceptor());
};
r.healthcheck = function(app)
{
// support for healthcheck urls
app.use(healthcheck.handler());
};
r.interceptors = function(app, includeCloudCMS)
{
var configuration = app.configuration;
// authentication interceptor (binds helper methods)
app.use(authentication.interceptor());
// authorization interceptor
app.use(authorization.authorizationInterceptor());
// supports user-configured dynamic configuration
app.use(config.remoteConfigInterceptor());
};
r.handlers = function(app, includeCloudCMS)
{
if (includeCloudCMS)
{
// handles /login and /logout for cloudcms principals
app.use(cloudcms.authenticationHandler(app));
}
// handles awareness commands
app.use(awareness.handler());
// handles admin commands
app.use(admin.handler());
// handles debug commands
app.use(debug.handler());
// handles deploy/undeploy commands
app.use(deployment.handler());
// serve back static configuration
app.use(config.staticConfigHandler());
// serve back dynamic configuration
app.use(config.remoteConfigHandler());
// handles calls to the templates service
app.use(templates.handler());
// handles calls to the modules service
app.use(modules.handler());
// handles calls to resources
app.use(resources.handler());
// handles thirdparty browser libraries that are included with cloudcms-server
app.use(libraries.handler());
// authentication
app.use(authentication.handler(app));
if (includeCloudCMS)
{
// handles virtualized content retrieval from cloud cms
app.use(cloudcms.virtualNodeHandler());
// handles virtualized principal retrieval from cloud cms
app.use(cloudcms.virtualPrincipalHandler());
}
// registration
app.use(registration.handler());
// handles calls to web flow controllers
app.use(flow.handlers());
// handles calls to form controllers
app.use(form.formHandler());
// handles runtime status calls
app.use(runtime.handler());
// handles virtualized local content retrieval from disk
//app.use(local.webStoreHandler());
// handles default content retrieval from disk (this includes virtualized)
app.use(local.defaultHandler());
// add User-Agent device info to req
app.use(userAgent.express());
// handles 404
app.use(final.finalHandler());
};
r.bodyParser = function()
{
return function(req, res, next)
{
if (req._body)
{
return next();
}
var contentType = req.get("Content-Type");
//if (contentType == "application/json" && req.method.toLowerCase() == "post") {
if (req.method.toLowerCase() == "post") {
req._body = true;
var responseString = "";
req.on('data', function(data) {
responseString += data;
});
req.on('end', function() {
if (responseString.length > 0) {
try {
var b = JSON.parse(responseString);
if (b)
{
req.body = b;
}
} catch (e) { }
}
next();
});
}
else
{
next();
}
};
};
/**
* Ensures that headers are set to enable CORS cross-domain functionality.
*
* @returns {Function}
*/
r.ensureCORS = function()
{
return util.createInterceptor("cors", function(req, res, next, stores, cache, configuration) {
var origin = configuration.origin;
if (!origin)
{
origin = req.headers["origin"];
}
if (!origin)
{
origin = req.headers["x-cloudcms-origin"];
}
if (!origin)
{
origin = "*";
}
var methods = configuration.methods;
var headers = configuration.headers;
var credentials = configuration.credentials;
util.setHeaderOnce(res, "Access-Control-Allow-Origin", origin);
if (methods)
{
util.setHeaderOnce(res, "Access-Control-Allow-Methods", methods);
}
if (headers)
{
util.setHeaderOnce(res, "Access-Control-Allow-Headers", headers);
}
if (credentials)
{
util.setHeaderOnce(res, "Access-Control-Allow-Credentials", "" + credentials);
}
// res.set('Access-Control-Allow-Max-Age', 3600);
if ('OPTIONS' === req.method) {
return res.sendStatus(200);
}
next();
});
};
r.ensureHeaders = function()
{
return function(req, res, next) {
// defaults
var xFrameOptions = "SAMEORIGIN";
var xXssProtection = "1; mode=block";
// TODO: allow overrides here?
if (xFrameOptions)
{
util.setHeaderOnce(res, "X-Frame-Options", xFrameOptions);
}
if (xXssProtection)
{
util.setHeaderOnce(res, "X-XSS-Protection", xXssProtection)
}
util.setHeaderOnce(res, "X-Powered-By", "Cloud CMS");
next();
};
};
var stringifyError = function(err)
{
var stack = err.stack;
if (stack) {
return String(stack)
}
return JSON.stringify(err, null, " ");
};
r.consoleErrorLogger = function(app, callback)
{
// generic logger to console
app.use(function(err, req, res, next) {
console.error(stringifyError(err));
next(err);
});
callback();
};
var errorHandler = require("errorhandler");
r.refreshTokenErrorHandler = function(app, callback)
{
app.use(function(err, req, res, next) {
if (err)
{
if (req.method.toLowerCase() === "get")
{
if (err.status === 401)
{
if (err.message)
{
if (err.message.toLowerCase().indexOf("expired") > -1)
{
if (err.message.toLowerCase().indexOf("refresh") > -1)
{
var url = req.path;
process.log("Refresh Token Expired, re-requesting resource: " + url);
var html = "";
html += "<html>";
html += "<head>";
html += "<meta http-equiv='refresh' content='1;URL=" + url + "'>";
html += "</head>";
html += "<body>";
html += "</body>";
html += "</html>";
return res.status(200).type("text/html").send(html);
}
}
}
}
}
}
next(err);
});
callback();
};
r.defaultErrorHandler = function(app, callback)
{
app.use(function(err, req, res, next) {
// use the stock error handler
errorHandler()(err, req, res, next);
});
callback();
};
return r;
}();