@lwrjs/core 0.19.0-alpha.0 → 0.19.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,37 +28,35 @@ __export(exports, {
28
28
  });
29
29
  var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
30
30
  var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
31
+ var import_config = __toModule(require("@lwrjs/config"));
31
32
  function requestProcessorMiddleware(app, context) {
32
33
  const {basePath} = context.runtimeEnvironment;
33
34
  app.use(async (req, res, next) => {
34
- let requestClass;
35
- let requestDepth;
36
- if (req.headers) {
37
- if (import_diagnostics.logger.isDebugEnabled()) {
38
- for (const headerName in req.headers) {
39
- import_diagnostics.logger.debug({
40
- label: `request-processor-middleware`,
41
- message: `Header ${headerName}: ${req.headers[headerName]}`
42
- });
43
- }
44
- }
45
- requestClass = req.headers[import_shared_utils.MRT_REQUEST_CLASS];
46
- requestDepth = (0, import_shared_utils.parseRequestDepth)(req.headers, req.query);
47
- const trueClientIP = req.headers[import_shared_utils.TRUE_CLIENT_IP];
48
- const correlationID = req.headers[import_shared_utils.CORRELATION_ID];
35
+ if (!req.headers) {
36
+ req.basePath = basePath;
37
+ await next();
38
+ return;
39
+ }
40
+ logRequestHeaders(req.headers);
41
+ const requestClass = req.headers[import_shared_utils.MRT_REQUEST_CLASS];
42
+ const requestDepth = (0, import_shared_utils.parseRequestDepth)(req.headers, req.query);
43
+ if ((0, import_shared_utils.isLambdaEnv)()) {
49
44
  const forwarded = req.headers["forwarded"];
50
- const host = req.headers["host"];
51
45
  const forwardedProto = req.headers["x-forwarded-proto"];
46
+ const host = req.headers["host"];
47
+ const trueClientIP = req.headers[import_shared_utils.TRUE_CLIENT_IP];
48
+ const correlationID = req.headers[import_shared_utils.CORRELATION_ID];
52
49
  const protocol = req.protocol;
53
50
  const cookieLength = req.headers["cookie"]?.length || 0;
54
- let cookieMsg = `Cookie length: ${cookieLength}`;
55
- if (cookieLength) {
56
- cookieMsg += `, Cookie has '__Secure-has-sid': ${!!req.cookie("__Secure-has-sid")}, Cookie has 'sid': ${!!req.cookie("sid")}`;
57
- }
58
- if ((0, import_shared_utils.isLambdaEnv)()) {
59
- import_diagnostics.logger.info({
60
- label: `request-processor-middleware`,
61
- message: `Original Url: ${req.originalUrl}, Forwarded: ${forwarded}, X-Forwarded-Proto: ${forwardedProto}, Host: ${host}, Protocol: ${protocol}, ${cookieMsg}, ${import_shared_utils.MRT_REQUEST_CLASS}: ${requestClass}, ${import_shared_utils.TRUE_CLIENT_IP}: ${trueClientIP}, ${import_shared_utils.CORRELATION_ID}: ${correlationID}, ${import_shared_utils.REQUEST_DEPTH_HEADER}: ${requestDepth}`
51
+ const cookieMsg = buildCookieMessage(req, cookieLength);
52
+ import_diagnostics.logger.info({
53
+ label: "request-processor-middleware",
54
+ message: `Original Url: ${req.originalUrl}, Forwarded: ${forwarded}, X-Forwarded-Proto: ${forwardedProto}, Host: ${host}, Protocol: ${protocol}, ${cookieMsg}, ${import_shared_utils.MRT_REQUEST_CLASS}: ${requestClass}, ${import_shared_utils.TRUE_CLIENT_IP}: ${trueClientIP}, ${import_shared_utils.CORRELATION_ID}: ${correlationID}, ${import_shared_utils.REQUEST_DEPTH_HEADER}: ${requestDepth}, LWR Version: ${import_config.LWR_VERSION}`
55
+ });
56
+ if (!forwarded) {
57
+ return res.status(400).send({
58
+ error: "Bad Request",
59
+ message: "The 'Forwarded' header is missing."
62
60
  });
63
61
  }
64
62
  }
@@ -69,37 +67,60 @@ function requestProcessorMiddleware(app, context) {
69
67
  });
70
68
  return res.status(400).send("Request depth limit reached");
71
69
  }
72
- if (req.headers && requestClass && typeof requestClass === "string") {
73
- const parsedRequestClass = parseRequestClass(requestClass);
74
- import_diagnostics.logger.debug({
75
- label: `request-processor-middleware`,
76
- message: `parsedRequestClass?.basePath: ${parsedRequestClass?.basePath}`
77
- });
78
- const pathValue = parsedRequestClass?.basePath || basePath || "";
79
- req.basePath = pathValue === "" || pathValue.indexOf("/") === 0 ? pathValue : `/${pathValue}`;
80
- const expressRequest = req.req;
81
- if (expressRequest?.url && parsedRequestClass?.basePath) {
82
- const {pathname, search} = new URL(expressRequest.url, "http://localhost");
83
- if (pathname.startsWith(parsedRequestClass.basePath)) {
84
- const newPath = pathname.slice(parsedRequestClass.basePath.length) || "/";
85
- expressRequest.url = newPath + search;
86
- expressRequest.originalUrl = newPath + search;
87
- } else {
88
- import_diagnostics.logger.warn({
89
- label: `request-processor-middleware`,
90
- message: `The URL requested for doesn't start with the Base path`
91
- });
92
- }
93
- }
94
- } else {
70
+ handleRequestClass(req, basePath, requestClass);
71
+ await next();
72
+ });
73
+ }
74
+ function logRequestHeaders(headers) {
75
+ if (import_diagnostics.logger.isDebugEnabled()) {
76
+ for (const headerName in headers) {
95
77
  import_diagnostics.logger.debug({
96
- label: `request-processor-middleware`,
97
- message: `${import_shared_utils.MRT_REQUEST_CLASS} ignored ${req.headers ? req.headers[import_shared_utils.MRT_REQUEST_CLASS] : "no-headers"}`
78
+ label: "request-processor-middleware",
79
+ message: `Header ${headerName}: ${headers[headerName]}`
98
80
  });
99
- req.basePath = basePath;
100
81
  }
101
- await next();
102
- });
82
+ }
83
+ }
84
+ function buildCookieMessage(req, length) {
85
+ let msg = `Cookie length: ${length}`;
86
+ if (length) {
87
+ msg += `, Cookie has '__Secure-has-sid': ${!!req.cookie("__Secure-has-sid")}, Cookie has 'sid': ${!!req.cookie("sid")}`;
88
+ }
89
+ return msg;
90
+ }
91
+ function handleRequestClass(req, defaultBasePath, requestClass) {
92
+ if (requestClass && typeof requestClass === "string") {
93
+ const parsed = parseRequestClass(requestClass);
94
+ import_diagnostics.logger.debug({
95
+ label: "request-processor-middleware",
96
+ message: `parsedRequestClass?.basePath: ${parsed?.basePath}`
97
+ });
98
+ const basePath = parsed?.basePath || defaultBasePath || "";
99
+ req.basePath = basePath === "" || basePath.startsWith("/") ? basePath : `/${basePath}`;
100
+ adjustExpressUrlForBasePath(req, parsed?.basePath);
101
+ } else {
102
+ import_diagnostics.logger.debug({
103
+ label: "request-processor-middleware",
104
+ message: `${import_shared_utils.MRT_REQUEST_CLASS} ignored ${requestClass ?? "no-headers"}`
105
+ });
106
+ req.basePath = defaultBasePath;
107
+ }
108
+ }
109
+ function adjustExpressUrlForBasePath(req, basePath) {
110
+ const expressRequest = req.req;
111
+ if (!expressRequest?.url || !basePath)
112
+ return;
113
+ const {pathname, search} = new URL(expressRequest.url, "http://localhost");
114
+ if (pathname.startsWith(basePath)) {
115
+ const newPath = pathname.slice(basePath.length) || "/";
116
+ expressRequest.url = newPath + search;
117
+ expressRequest.originalUrl = newPath + search;
118
+ } else {
119
+ import_diagnostics.logger.warn({
120
+ label: "request-processor-middleware",
121
+ message: `The URL requested for doesn't start with the Base path`
122
+ });
123
+ }
103
124
  }
104
125
  function parseRequestClass(requestClass) {
105
126
  if (!requestClass) {
@@ -12,5 +12,5 @@ __export(exports, {
12
12
  });
13
13
  var MAX_LAMBDA_RESPONSE_SIZE = 6 * 1024 * 1024;
14
14
  function getMrtCompressionThreshold() {
15
- return MAX_LAMBDA_RESPONSE_SIZE - 5 * 1024 * 1024;
15
+ return MAX_LAMBDA_RESPONSE_SIZE - 1 * 1024 * 1024;
16
16
  }
@@ -1,39 +1,34 @@
1
1
  import { logger } from '@lwrjs/diagnostics';
2
2
  import { CORRELATION_ID, MRT_REQUEST_CLASS, REQUEST_DEPTH_HEADER, TRUE_CLIENT_IP, isLambdaEnv, parseRequestDepth, } from '@lwrjs/shared-utils';
3
+ import { LWR_VERSION } from '@lwrjs/config';
3
4
  export function requestProcessorMiddleware(app, context) {
4
5
  const { basePath } = context.runtimeEnvironment;
5
6
  app.use(async (req, res, next) => {
6
- let requestClass;
7
- let requestDepth;
8
- if (req.headers) {
9
- // If debug print log all the headers
10
- if (logger.isDebugEnabled()) {
11
- // Loop through and print each header
12
- for (const headerName in req.headers) {
13
- logger.debug({
14
- label: `request-processor-middleware`,
15
- message: `Header ${headerName}: ${req.headers[headerName]}`,
16
- });
17
- }
18
- }
19
- requestClass = req.headers[MRT_REQUEST_CLASS];
20
- requestDepth = parseRequestDepth(req.headers, req.query);
21
- const trueClientIP = req.headers[TRUE_CLIENT_IP];
22
- const correlationID = req.headers[CORRELATION_ID];
7
+ if (!req.headers) {
8
+ req.basePath = basePath;
9
+ await next();
10
+ return;
11
+ }
12
+ logRequestHeaders(req.headers);
13
+ const requestClass = req.headers[MRT_REQUEST_CLASS];
14
+ const requestDepth = parseRequestDepth(req.headers, req.query);
15
+ if (isLambdaEnv()) {
23
16
  const forwarded = req.headers['forwarded'];
24
- const host = req.headers['host'];
25
17
  const forwardedProto = req.headers['x-forwarded-proto'];
18
+ const host = req.headers['host'];
19
+ const trueClientIP = req.headers[TRUE_CLIENT_IP];
20
+ const correlationID = req.headers[CORRELATION_ID];
26
21
  const protocol = req.protocol;
27
22
  const cookieLength = req.headers['cookie']?.length || 0;
28
- let cookieMsg = `Cookie length: ${cookieLength}`;
29
- if (cookieLength) {
30
- cookieMsg += `, Cookie has '__Secure-has-sid': ${!!req.cookie('__Secure-has-sid')}, Cookie has 'sid': ${!!req.cookie('sid')}`;
31
- }
32
- // Only Add this log when on MRT. Useful to catch issues where CDN did not send us the right info.
33
- if (isLambdaEnv()) {
34
- logger.info({
35
- label: `request-processor-middleware`,
36
- message: `Original Url: ${req.originalUrl}, Forwarded: ${forwarded}, X-Forwarded-Proto: ${forwardedProto}, Host: ${host}, Protocol: ${protocol}, ${cookieMsg}, ${MRT_REQUEST_CLASS}: ${requestClass}, ${TRUE_CLIENT_IP}: ${trueClientIP}, ${CORRELATION_ID}: ${correlationID}, ${REQUEST_DEPTH_HEADER}: ${requestDepth}`,
23
+ const cookieMsg = buildCookieMessage(req, cookieLength);
24
+ logger.info({
25
+ label: 'request-processor-middleware',
26
+ message: `Original Url: ${req.originalUrl}, Forwarded: ${forwarded}, X-Forwarded-Proto: ${forwardedProto}, Host: ${host}, Protocol: ${protocol}, ${cookieMsg}, ${MRT_REQUEST_CLASS}: ${requestClass}, ${TRUE_CLIENT_IP}: ${trueClientIP}, ${CORRELATION_ID}: ${correlationID}, ${REQUEST_DEPTH_HEADER}: ${requestDepth}, LWR Version: ${LWR_VERSION}`,
27
+ });
28
+ if (!forwarded) {
29
+ return res.status(400).send({
30
+ error: 'Bad Request',
31
+ message: "The 'Forwarded' header is missing.",
37
32
  });
38
33
  }
39
34
  }
@@ -42,49 +37,65 @@ export function requestProcessorMiddleware(app, context) {
42
37
  label: 'request-processor-middleware',
43
38
  message: `Lambda SSR request cycle detected: ${req.originalUrl}`,
44
39
  });
45
- // Return 400 Too Many Requests status
46
40
  return res.status(400).send('Request depth limit reached');
47
41
  }
48
- if (req.headers && requestClass && typeof requestClass === 'string') {
49
- const parsedRequestClass = parseRequestClass(requestClass);
50
- logger.debug({
51
- label: `request-processor-middleware`,
52
- message: `parsedRequestClass?.basePath: ${parsedRequestClass?.basePath}`,
53
- });
54
- const pathValue = parsedRequestClass?.basePath || basePath || '';
55
- // If base path is '' or starts with / leave it alone
56
- req.basePath = pathValue === '' || pathValue.indexOf('/') === 0 ? pathValue : `/${pathValue}`;
57
- const expressRequest = req.req;
58
- // This section is code added for the 103 hints support. If CDN passes us a basePath in the header
59
- // If the basePath is at the start of the URL we need to remove it so we can match the expected route.
60
- if (expressRequest?.url && parsedRequestClass?.basePath) {
61
- // Separate the path and the query param using dummy local host here since we do not use it
62
- const { pathname, search } = new URL(expressRequest.url, 'http://localhost');
63
- if (pathname.startsWith(parsedRequestClass.basePath)) {
64
- // Remove the basePath from the pathname
65
- const newPath = pathname.slice(parsedRequestClass.basePath.length) || '/';
66
- // Reconstruct the URL with the modified path and original query string
67
- expressRequest.url = newPath + search;
68
- expressRequest.originalUrl = newPath + search;
69
- }
70
- else {
71
- logger.warn({
72
- label: `request-processor-middleware`,
73
- message: `The URL requested for doesn't start with the Base path`,
74
- });
75
- }
76
- }
77
- }
78
- else {
42
+ handleRequestClass(req, basePath, requestClass);
43
+ await next();
44
+ });
45
+ }
46
+ // --- Helper functions ---
47
+ function logRequestHeaders(headers) {
48
+ if (logger.isDebugEnabled()) {
49
+ for (const headerName in headers) {
79
50
  logger.debug({
80
- label: `request-processor-middleware`,
81
- message: `${MRT_REQUEST_CLASS} ignored ${req.headers ? req.headers[MRT_REQUEST_CLASS] : 'no-headers'}`,
51
+ label: 'request-processor-middleware',
52
+ message: `Header ${headerName}: ${headers[headerName]}`,
82
53
  });
83
- req.basePath = basePath;
84
54
  }
85
- // await is required when calling next to support koa
86
- await next();
87
- });
55
+ }
56
+ }
57
+ function buildCookieMessage(req, length) {
58
+ let msg = `Cookie length: ${length}`;
59
+ if (length) {
60
+ msg += `, Cookie has '__Secure-has-sid': ${!!req.cookie('__Secure-has-sid')}, Cookie has 'sid': ${!!req.cookie('sid')}`;
61
+ }
62
+ return msg;
63
+ }
64
+ function handleRequestClass(req, defaultBasePath, requestClass) {
65
+ if (requestClass && typeof requestClass === 'string') {
66
+ const parsed = parseRequestClass(requestClass);
67
+ logger.debug({
68
+ label: 'request-processor-middleware',
69
+ message: `parsedRequestClass?.basePath: ${parsed?.basePath}`,
70
+ });
71
+ const basePath = parsed?.basePath || defaultBasePath || '';
72
+ req.basePath = basePath === '' || basePath.startsWith('/') ? basePath : `/${basePath}`;
73
+ adjustExpressUrlForBasePath(req, parsed?.basePath);
74
+ }
75
+ else {
76
+ logger.debug({
77
+ label: 'request-processor-middleware',
78
+ message: `${MRT_REQUEST_CLASS} ignored ${requestClass ?? 'no-headers'}`,
79
+ });
80
+ req.basePath = defaultBasePath;
81
+ }
82
+ }
83
+ function adjustExpressUrlForBasePath(req, basePath) {
84
+ const expressRequest = req.req;
85
+ if (!expressRequest?.url || !basePath)
86
+ return;
87
+ const { pathname, search } = new URL(expressRequest.url, 'http://localhost');
88
+ if (pathname.startsWith(basePath)) {
89
+ const newPath = pathname.slice(basePath.length) || '/';
90
+ expressRequest.url = newPath + search;
91
+ expressRequest.originalUrl = newPath + search;
92
+ }
93
+ else {
94
+ logger.warn({
95
+ label: 'request-processor-middleware',
96
+ message: `The URL requested for doesn't start with the Base path`,
97
+ });
98
+ }
88
99
  }
89
100
  /**
90
101
  * Parse the basePath passed via the X-Mobify-Request-Class header.
@@ -1,6 +1,6 @@
1
1
  // currently documented AWS Lambda limit, 6MB
2
2
  const MAX_LAMBDA_RESPONSE_SIZE = 6 * 1024 * 1024;
3
3
  export function getMrtCompressionThreshold() {
4
- return MAX_LAMBDA_RESPONSE_SIZE - 5 * 1024 * 1024; // Trigger compression within 1MB of limit
4
+ return MAX_LAMBDA_RESPONSE_SIZE - 1 * 1024 * 1024; // Trigger compression within 1MB of limit
5
5
  }
6
6
  //# sourceMappingURL=compression.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.19.0-alpha.0",
7
+ "version": "0.19.0-alpha.10",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -43,34 +43,34 @@
43
43
  "build": "tsc -b"
44
44
  },
45
45
  "dependencies": {
46
- "@lwrjs/app-service": "0.19.0-alpha.0",
47
- "@lwrjs/asset-registry": "0.19.0-alpha.0",
48
- "@lwrjs/asset-transformer": "0.19.0-alpha.0",
49
- "@lwrjs/base-view-provider": "0.19.0-alpha.0",
50
- "@lwrjs/base-view-transformer": "0.19.0-alpha.0",
51
- "@lwrjs/client-modules": "0.19.0-alpha.0",
52
- "@lwrjs/config": "0.19.0-alpha.0",
53
- "@lwrjs/diagnostics": "0.19.0-alpha.0",
54
- "@lwrjs/esbuild": "0.19.0-alpha.0",
55
- "@lwrjs/fs-asset-provider": "0.19.0-alpha.0",
56
- "@lwrjs/fs-watch": "0.19.0-alpha.0",
57
- "@lwrjs/html-view-provider": "0.19.0-alpha.0",
58
- "@lwrjs/instrumentation": "0.19.0-alpha.0",
59
- "@lwrjs/loader": "0.19.0-alpha.0",
60
- "@lwrjs/lwc-module-provider": "0.19.0-alpha.0",
61
- "@lwrjs/lwc-ssr": "0.19.0-alpha.0",
62
- "@lwrjs/markdown-view-provider": "0.19.0-alpha.0",
63
- "@lwrjs/module-bundler": "0.19.0-alpha.0",
64
- "@lwrjs/module-registry": "0.19.0-alpha.0",
65
- "@lwrjs/npm-module-provider": "0.19.0-alpha.0",
66
- "@lwrjs/nunjucks-view-provider": "0.19.0-alpha.0",
67
- "@lwrjs/o11y": "0.19.0-alpha.0",
68
- "@lwrjs/resource-registry": "0.19.0-alpha.0",
69
- "@lwrjs/router": "0.19.0-alpha.0",
70
- "@lwrjs/server": "0.19.0-alpha.0",
71
- "@lwrjs/shared-utils": "0.19.0-alpha.0",
72
- "@lwrjs/static": "0.19.0-alpha.0",
73
- "@lwrjs/view-registry": "0.19.0-alpha.0",
46
+ "@lwrjs/app-service": "0.19.0-alpha.10",
47
+ "@lwrjs/asset-registry": "0.19.0-alpha.10",
48
+ "@lwrjs/asset-transformer": "0.19.0-alpha.10",
49
+ "@lwrjs/base-view-provider": "0.19.0-alpha.10",
50
+ "@lwrjs/base-view-transformer": "0.19.0-alpha.10",
51
+ "@lwrjs/client-modules": "0.19.0-alpha.10",
52
+ "@lwrjs/config": "0.19.0-alpha.10",
53
+ "@lwrjs/diagnostics": "0.19.0-alpha.10",
54
+ "@lwrjs/esbuild": "0.19.0-alpha.10",
55
+ "@lwrjs/fs-asset-provider": "0.19.0-alpha.10",
56
+ "@lwrjs/fs-watch": "0.19.0-alpha.10",
57
+ "@lwrjs/html-view-provider": "0.19.0-alpha.10",
58
+ "@lwrjs/instrumentation": "0.19.0-alpha.10",
59
+ "@lwrjs/loader": "0.19.0-alpha.10",
60
+ "@lwrjs/lwc-module-provider": "0.19.0-alpha.10",
61
+ "@lwrjs/lwc-ssr": "0.19.0-alpha.10",
62
+ "@lwrjs/markdown-view-provider": "0.19.0-alpha.10",
63
+ "@lwrjs/module-bundler": "0.19.0-alpha.10",
64
+ "@lwrjs/module-registry": "0.19.0-alpha.10",
65
+ "@lwrjs/npm-module-provider": "0.19.0-alpha.10",
66
+ "@lwrjs/nunjucks-view-provider": "0.19.0-alpha.10",
67
+ "@lwrjs/o11y": "0.19.0-alpha.10",
68
+ "@lwrjs/resource-registry": "0.19.0-alpha.10",
69
+ "@lwrjs/router": "0.19.0-alpha.10",
70
+ "@lwrjs/server": "0.19.0-alpha.10",
71
+ "@lwrjs/shared-utils": "0.19.0-alpha.10",
72
+ "@lwrjs/static": "0.19.0-alpha.10",
73
+ "@lwrjs/view-registry": "0.19.0-alpha.10",
74
74
  "chokidar": "^3.6.0",
75
75
  "esbuild": "^0.9.7",
76
76
  "fs-extra": "^11.2.0",
@@ -80,7 +80,7 @@
80
80
  "ws": "^8.18.0"
81
81
  },
82
82
  "devDependencies": {
83
- "@lwrjs/types": "0.19.0-alpha.0",
83
+ "@lwrjs/types": "0.19.0-alpha.10",
84
84
  "@types/ws": "^8.5.12",
85
85
  "memfs": "^4.13.0"
86
86
  },
@@ -93,5 +93,5 @@
93
93
  "volta": {
94
94
  "extends": "../../../package.json"
95
95
  },
96
- "gitHead": "bf42316347f32c9ed0d1e30ae06b224ab61f8fc1"
96
+ "gitHead": "1c0d9cbabc81c0ded8080664debf86b9533bfc27"
97
97
  }