forked from intershop/intershop-pwa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
502 lines (431 loc) · 15.8 KB
/
server.ts
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
/* eslint-disable no-console, ish-custom-rules/ordered-imports, no-restricted-imports, complexity, @typescript-eslint/no-var-requires */
import 'zone.js/node';
import * as express from 'express';
import { join } from 'path';
import * as robots from 'express-robots-txt';
import * as fs from 'fs';
import * as proxy from 'express-http-proxy';
import {
AppServerModule,
ICM_WEB_URL,
HYBRID_MAPPING_TABLE,
environment,
APP_BASE_HREF,
ICM_CONFIG_MATCH,
} from './src/main.server';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { getDeployURLFromEnv, setDeployUrlInFile } from './src/ssr/deploy-url';
import * as client from 'prom-client';
const collectDefaultMetrics = client.collectDefaultMetrics;
const defaultLabels =
process.env.pm_id && process.env.name ? { theme: process.env.name, pm2_id: process.env.pm_id } : undefined;
client.register.setDefaultLabels(defaultLabels);
const requestCounts = new client.Gauge({
name: 'pwa_http_request_counts',
help: 'counter for requests labeled with: method, status_code, theme, base_href, path',
labelNames: ['method', 'status_code', 'base_href', 'path'],
});
const requestDuration = new client.Histogram({
name: 'pwa_http_request_duration_seconds',
help: 'duration histogram of http responses labeled with: status_code, theme',
buckets: [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30],
labelNames: ['status_code', 'base_href', 'path'],
});
const PM2 = process.env.pm_id && process.env.name ? `${process.env.pm_id} ${process.env.name}` : undefined;
if (PM2) {
const logOriginal = console.log;
console.log = (...args: unknown[]) => {
logOriginal(PM2, ...args);
};
const warnOriginal = console.warn;
console.warn = (...args: unknown[]) => {
warnOriginal(PM2, ...args);
};
const errorOriginal = console.error;
console.error = (...args: unknown[]) => {
errorOriginal(PM2, ...args);
};
}
const PORT = process.env.PORT || 4200;
const DEPLOY_URL = getDeployURLFromEnv();
const DIST_FOLDER = join(process.cwd(), 'dist');
const BROWSER_FOLDER = process.env.BROWSER_FOLDER || join(process.cwd(), 'dist', 'browser');
// uncomment this block to prevent ssr issues with third-party libraries regarding window, document, HTMLElement and navigator
// eslint-disable-next-line etc/no-commented-out-code
/*
const domino = require('domino');
const template = fs.readFileSync(join(BROWSER_FOLDER, 'index.html')).toString();
const win = domino.createWindow(template);
global['window'] = win;
global['document'] = win.document;
global['HTMLElement'] = win.HTMLElement;
global['navigator'] = win.navigator;
*/
// The Express app is exported so that it can be used by serverless Functions.
export function app() {
const logging = /on|1|true|yes/.test(process.env.LOGGING?.toLowerCase());
const ICM_BASE_URL = process.env.ICM_BASE_URL || environment.icmBaseURL;
const SSR_HYBRID_BACKEND = process.env.SSR_HYBRID_BACKEND || ICM_BASE_URL;
if (!ICM_BASE_URL) {
console.error('ICM_BASE_URL not set');
process.exit(1);
}
if (process.env.TRUST_ICM) {
// trust https certificate if self-signed
// see also https://medium.com/nodejs-tips/ssl-certificate-explained-fc86f8aa43d4
// and https://github.com/angular/universal/issues/856#issuecomment-436364729
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
console.warn("ignoring all TLS verification as 'TRUST_ICM' variable is set - never use this in production!");
} else {
const [icmProtocol, icmBase] = ICM_BASE_URL.split('://');
// check for ssl certificate should be done, if https is used
if (icmProtocol === 'https') {
const https = require('https');
const [, icmHost, icmPort] = /^(.*?):?([0-9]+)?[\/]*$/.exec(icmBase);
const options = {
host: icmHost,
port: icmPort || '443',
method: 'get',
path: '/',
};
const req = https.request(options, (res: { socket: { authorized: boolean } }) => {
console.log('Certificate for', ICM_BASE_URL, 'authorized:', res.socket.authorized);
});
const certErrorCodes = [
'CERT_SIGNATURE_FAILURE',
'CERT_NOT_YET_VALID',
'CERT_HAS_EXPIRED',
'ERROR_IN_CERT_NOT_BEFORE_FIELD',
'ERROR_IN_CERT_NOT_AFTER_FIELD',
'DEPTH_ZERO_SELF_SIGNED_CERT',
'SELF_SIGNED_CERT_IN_CHAIN',
'UNABLE_TO_GET_ISSUER_CERT_LOCALLY',
'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
'CERT_CHAIN_TOO_LONG',
'CERT_REVOKED',
'INVALID_CA',
'INVALID_PURPOSE',
'CERT_UNTRUSTED',
'CERT_REJECTED',
'HOSTNAME_MISMATCH',
];
req.on('error', (e: { code: string }) => {
if (certErrorCodes.includes(e.code)) {
console.log(
e.code,
': The given ICM_BASE_URL',
ICM_BASE_URL,
"has a certificate problem. Please set 'TRUST_ICM' variable to avoid further errors for all requests to the ICM_BASE_URL - never use this in production!"
);
} else {
console.error(e);
}
});
req.end();
}
}
// Express server
const server = express();
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine(
'html',
ngExpressEngine({
bootstrap: AppServerModule,
providers: [{ provide: 'SSR_HYBRID', useValue: !!process.env.SSR_HYBRID }],
inlineCriticalCss: false,
})
);
server.set('view engine', 'html');
server.set('views', BROWSER_FOLDER);
if (logging) {
const morgan = require('morgan');
// see https://github.com/expressjs/morgan#predefined-formats
let logFormat = morgan.tiny;
if (PM2) {
logFormat = `${PM2} ${logFormat}`;
}
server.use(
morgan(logFormat, {
skip: (req: express.Request) => req.originalUrl.startsWith('/INTERSHOP/static'),
})
);
}
// seo robots.txt
const pathToRobotsTxt = join(DIST_FOLDER, 'robots.txt');
if (fs.existsSync(pathToRobotsTxt)) {
server.use(robots(pathToRobotsTxt));
} else {
server.use(
robots({
UserAgent: '*',
Disallow: [
'/error',
'/maintenance',
'/account',
'/compare',
'/recently',
'/basket',
'/checkout',
'/register',
'/login',
'/logout',
'/forgotPassword',
'/gdpr-requests',
'/contact',
],
})
);
}
const hybridRedirect = (req: express.Request, res: express.Response, next: express.NextFunction) => {
const url = req.originalUrl;
let newUrl: string;
for (const entry of HYBRID_MAPPING_TABLE) {
const icmUrlRegex = new RegExp(entry.icm);
const pwaUrlRegex = new RegExp(entry.pwa);
const icmMatchArray = icmUrlRegex.exec(url);
if (icmMatchArray && entry.handledBy === 'pwa') {
const config: { [is: string]: string } = {
...icmMatchArray.groups,
application: environment.icmApplication || '-',
};
// Rewrite configuration part of incoming ICM url
const _url = url.replace(new RegExp(ICM_CONFIG_MATCH), buildICMWebURL(config));
// Build pwa URL based on equally named-groups of ICM url
newUrl = _url.replace(icmUrlRegex, `/${entry.pwaBuild}`);
break;
} else if (pwaUrlRegex.exec(url) && entry.handledBy === 'icm') {
const config: { [is: string]: string } = {};
if (/;lang=[\w_]+/.test(url)) {
const [, lang] = /;lang=([\w_]+)/.exec(url);
config.lang = lang;
}
if (!config.lang) {
config.lang = environment.defaultLocale;
}
if (/;currency=[\w_]+/.test(url)) {
const [, currency] = /;currency=([\w_]+)/.exec(url);
config.currency = currency;
}
if (/;channel=[^;]*/.test(url)) {
config.channel = /;channel=([^;]*)/.exec(url)[1];
} else {
config.channel = environment.icmChannel;
}
config.application = environment.hybridApplication || environment.icmApplication;
const build = [buildICMWebURL(config), entry.icmBuild].join('/');
newUrl = url.replace(pwaUrlRegex, build).replace(/;.*/g, '');
break;
}
}
if (newUrl) {
if (logging) {
console.log('RED', newUrl);
}
res.redirect(301, newUrl);
} else {
next();
}
};
const buildICMWebURL = (config: { [is: string]: string } = {}): string =>
ICM_WEB_URL.replace(/\$<(\w+)>/g, (match, group) => config[group] || match);
if (process.env.SSR_HYBRID) {
server.use('*', hybridRedirect);
}
const icmProxy = proxy(SSR_HYBRID_BACKEND, {
// preserve original path
proxyReqPathResolver: (req: express.Request) => req.originalUrl,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
proxyReqOptDecorator: (options: any) => {
if (process.env.TRUST_ICM) {
// https://github.com/villadora/express-http-proxy#q-how-to-ignore-self-signed-certificates-
options.rejectUnauthorized = false;
}
if (process.env.SSR_HYBRID) {
// Force context proto to be https otherwise ICM WA is redirecting us to https
options.headers['X-Forwarded-Proto'] = 'https';
}
return options;
},
// fool ICM so it thinks it's running here
// https://www.npmjs.com/package/express-http-proxy#preservehosthdr
preserveHostHdr: true,
});
if (process.env.PROXY_ICM || process.env.SSR_HYBRID) {
console.log("making ICM available for all requests to '/INTERSHOP'");
server.use('/INTERSHOP', icmProxy);
}
const SOURCE_MAPS_ACTIVE = /on|1|true|yes/.test(process.env.SOURCE_MAPS?.toLowerCase());
if (SOURCE_MAPS_ACTIVE) {
console.warn('SOURCE_MAPS are active - never use this in production!');
}
// Serve static files from browser folder
server.get(/\/.*\.js\.map$/, (req, res, next) => {
if (SOURCE_MAPS_ACTIVE) {
return express.static(BROWSER_FOLDER)(req, res, next);
} else {
return res.sendStatus(404);
}
});
server.get(/\/.*\.(js|css)$/, (req, res) => {
// remove all parameters
const path = req.originalUrl.substring(1).replace(/[;?&].*$/, '');
const filename = join(BROWSER_FOLDER, path);
if (filename.startsWith(BROWSER_FOLDER)) {
fs.readFile(filename, { encoding: 'utf-8' }, (err, data) => {
if (err) {
res.sendStatus(404);
} else {
res.set('Content-Type', `${path.endsWith('css') ? 'text/css' : 'application/javascript'}; charset=UTF-8`);
res.send(setDeployUrlInFile(DEPLOY_URL, path, data));
}
});
} else {
res.sendStatus(404);
}
});
server.get(/\/ngsw\.json/, (_, res) => {
fs.readFile(join(BROWSER_FOLDER, 'ngsw.json'), { encoding: 'utf-8' }, (err, data) => {
if (err) {
res.sendStatus(404);
} else {
res.set('Content-Type', 'application/json; charset=UTF-8');
res.send(data);
}
});
});
server.get(/^(?!\/assets\/).*\/favicon.ico.*/, (req, _, next) => {
req.url = req.originalUrl.replace(/[;?&].*$/, '').replace(/^.*\//g, '/');
next();
});
server.get(
'*.*',
express.static(BROWSER_FOLDER, {
setHeaders: (res, path) => {
if (/\.[0-9a-f]{20,}\./.test(path)) {
// file was output-hashed -> 1y
res.set('Cache-Control', 'public, max-age=31557600');
} else {
// file should be re-checked more frequently -> 5m
res.set('Cache-Control', 'public, max-age=300');
}
// add cors headers for required resources
if (
DEPLOY_URL.startsWith('http') &&
['manifest.webmanifest', 'woff2', 'woff', 'json'].some(match => path.endsWith(match))
) {
res.set('access-control-allow-origin', '*');
}
},
})
);
const angularUniversal = (req: express.Request, res: express.Response) => {
if (logging) {
console.log(`SSR ${req.originalUrl}`);
}
if (req.originalUrl.startsWith('/assets/')) {
if (logging) {
console.log(`RES 404 ${req.originalUrl} - cannot serve static assets with Angular Universal`);
}
return res.sendStatus(404);
}
if (req.headers.accept) {
const accept = req.headers.accept.toLowerCase();
if (!accept.includes('html') && ['css', 'image', 'json', 'javascript'].some(inc => accept.includes(inc))) {
if (logging) {
console.log(`RES 404 ${req.originalUrl} - accept header mismatch '${accept}'`);
}
return res.sendStatus(404);
}
}
// find last baseHref parameter
const regex = /baseHref=([^;\?\#]*)/g;
let baseHref = '/';
for (let match: RegExpExecArray; (match = regex.exec(req.originalUrl)); ) {
baseHref = match[1].replace(/%25/g, '%').replace(/%2F/g, '/');
}
res.render(
'index',
{
req,
res,
providers: [{ provide: APP_BASE_HREF, useValue: baseHref }],
},
(err, html) => {
if (html) {
let newHtml = html;
if (process.env.PROXY_ICM && req.get('host')) {
newHtml = newHtml.replace(
new RegExp(ICM_BASE_URL, 'g'),
process.env.PROXY_ICM.startsWith('http') ? process.env.PROXY_ICM : `${req.protocol}://${req.get('host')}`
);
}
newHtml = newHtml.replace(/<base href="[^>]*>/, `<base href="${baseHref}" />`);
newHtml = setDeployUrlInFile(DEPLOY_URL, req.originalUrl, newHtml);
res.status(res.statusCode).send(newHtml);
} else {
res.status(500).send(err.message);
}
if (logging) {
console.log(`RES ${res.statusCode} ${req.originalUrl}`);
if (err) {
console.log(err);
}
}
}
);
};
if (/^(on|1|true|yes)$/i.test(process.env.PROMETHEUS)) {
const onFinished = require('on-finished');
server.use((req, res, next) => {
const start = Date.now();
onFinished(res, () => {
const duration = Date.now() - start;
const matched = /;baseHref=([^;?]*)/.exec(req.originalUrl);
const base_href = matched?.[1] ? `${decodeURIComponent(decodeURIComponent(matched[1]))}/` : '/';
const cleanUrl = req.originalUrl.replace(/[;?].*/g, '');
const path = cleanUrl.replace(base_href, '');
requestCounts.inc({ method: req.method, status_code: res.statusCode, base_href, path });
requestDuration.labels({ status_code: res.statusCode, base_href, path }).observe(duration / 1000);
});
next();
});
}
// All regular routes use the Universal engine
server.use('*', angularUniversal);
console.log('ICM_BASE_URL is', ICM_BASE_URL);
return server;
}
if (/^(on|1|true|yes)$/i.test(process.env.PROMETHEUS)) {
type MetricsMessage = { topic: string };
process.on('message', (msg: MetricsMessage) => {
if (msg.topic === 'getMetrics') {
client.register.getMetricsAsJSON().then((data: client.MetricObject[]) => {
process.send({
type: 'process:msg',
data: {
body: data,
topic: 'returnMetrics',
},
});
});
}
});
}
function run() {
const http = require('http');
http.createServer(app()).listen(PORT);
collectDefaultMetrics({ prefix: 'pwa_' });
console.log(`Node Express server listening on http://${require('os').hostname()}:${PORT}`);
console.log('serving static files from', BROWSER_FOLDER);
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
// eslint-disable-next-line @typescript-eslint/naming-convention
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule?.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export * from './src/main.server';