@lwrjs/core 0.15.0-alpha.21 → 0.15.0-alpha.23

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.
@@ -64,7 +64,7 @@ function requestProcessorMiddleware(app, context) {
64
64
  });
65
65
  return res.status(400).send("Request depth limit reached");
66
66
  }
67
- if (req.headers && typeof requestClass === "string") {
67
+ if (req.headers && requestClass && typeof requestClass === "string") {
68
68
  const parsedRequestClass = parseRequestClass(requestClass);
69
69
  import_diagnostics.logger.debug({
70
70
  label: `request-processor-middleware`,
@@ -73,14 +73,18 @@ function requestProcessorMiddleware(app, context) {
73
73
  const pathValue = parsedRequestClass?.basePath || basePath || "";
74
74
  req.basePath = pathValue === "" || pathValue.indexOf("/") === 0 ? pathValue : `/${pathValue}`;
75
75
  const expressRequest = req.req;
76
- if (expressRequest?.url?.startsWith(parsedRequestClass?.basePath)) {
77
- expressRequest.url = expressRequest.url.split(parsedRequestClass?.basePath)[1] || "/";
78
- expressRequest.originalUrl = expressRequest.url.split(parsedRequestClass?.basePath)[1] || "/";
79
- } else {
80
- import_diagnostics.logger.warn({
81
- label: `request-processor-middleware`,
82
- message: `The URL requested for doesn't start with the Base path`
83
- });
76
+ if (expressRequest?.url && parsedRequestClass?.basePath) {
77
+ const {pathname, search} = new URL(expressRequest.url, "http://localhost");
78
+ if (pathname.startsWith(parsedRequestClass.basePath)) {
79
+ const newPath = pathname.slice(parsedRequestClass.basePath.length) || "/";
80
+ expressRequest.url = newPath + search;
81
+ expressRequest.originalUrl = newPath + search;
82
+ } else {
83
+ import_diagnostics.logger.warn({
84
+ label: `request-processor-middleware`,
85
+ message: `The URL requested for doesn't start with the Base path`
86
+ });
87
+ }
84
88
  }
85
89
  } else {
86
90
  import_diagnostics.logger.debug({
@@ -93,6 +97,9 @@ function requestProcessorMiddleware(app, context) {
93
97
  });
94
98
  }
95
99
  function parseRequestClass(requestClass) {
100
+ if (!requestClass) {
101
+ return {};
102
+ }
96
103
  const keyValuePairs = requestClass.split(";");
97
104
  const parsed = {};
98
105
  for (const pair of keyValuePairs) {
@@ -24,7 +24,7 @@ var __toModule = (module2) => {
24
24
  // packages/@lwrjs/core/src/middleware/view-middleware.ts
25
25
  __markAsModule(exports);
26
26
  __export(exports, {
27
- convertMetricsToArrayOfStrings: () => convertMetricsToArrayOfStrings,
27
+ convertMetricsToServerTiming: () => convertMetricsToServerTiming,
28
28
  viewMiddleware: () => viewMiddleware
29
29
  });
30
30
  var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
@@ -65,6 +65,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
65
65
  params: viewRequest.params,
66
66
  query: viewRequest.query,
67
67
  cookie: req.headers?.cookie,
68
+ trueClientIP: req.headers && req.headers["True-Client-IP"],
68
69
  coreProxy: req.getCoreProxy(appConfig.coreProxy ?? void 0, route.bootstrap?.proxyForSSR)
69
70
  };
70
71
  const resolve = req.isJsonRequest() ? viewHandler.getViewJson : viewHandler.getViewContent;
@@ -121,7 +122,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
121
122
  res.setHeader("x-trace-id", traceId);
122
123
  if (reqSpan?.status?.code === 0) {
123
124
  res.set({
124
- "X-Custom-Lwr-Metric": convertMetricsToArrayOfStrings((0, import_aws_exporter.convertToReadableSpan)(reqSpan))
125
+ "Server-Timing": convertMetricsToServerTiming((0, import_aws_exporter.convertToReadableSpan)(reqSpan))
125
126
  });
126
127
  }
127
128
  }
@@ -249,16 +250,7 @@ function addRedirectQueryParam(redirectUrl, depth) {
249
250
  url.searchParams.set(import_shared_utils.REQUEST_DEPTH_KEY, String(depth + 1));
250
251
  return url.toString().replace(fakeOrigin, "");
251
252
  }
252
- function convertMetricsToArrayOfStrings(span) {
253
- const requiredMetrics = ["traceId", "name", "attributes", "duration"];
254
- return requiredMetrics.reduce((metricString, prop) => {
255
- if (metricString && !metricString.endsWith("; ")) {
256
- metricString += "; ";
257
- }
258
- if (span[prop]) {
259
- const propValue = typeof span[prop] === "object" ? JSON.stringify(span[prop]) : span[prop];
260
- metricString += `${prop}: ${propValue}`;
261
- }
262
- return metricString;
263
- }, "");
253
+ function convertMetricsToServerTiming(span) {
254
+ const {name, duration} = span;
255
+ return `${name};dur=${duration.toFixed(2)}`;
264
256
  }
@@ -48,7 +48,7 @@ export function requestProcessorMiddleware(app, context) {
48
48
  // Return 400 Too Many Requests status
49
49
  return res.status(400).send('Request depth limit reached');
50
50
  }
51
- if (req.headers && typeof requestClass === 'string') {
51
+ if (req.headers && requestClass && typeof requestClass === 'string') {
52
52
  const parsedRequestClass = parseRequestClass(requestClass);
53
53
  logger.debug({
54
54
  label: `request-processor-middleware`,
@@ -58,15 +58,24 @@ export function requestProcessorMiddleware(app, context) {
58
58
  // If base path is '' or starts with / leave it alone
59
59
  req.basePath = pathValue === '' || pathValue.indexOf('/') === 0 ? pathValue : `/${pathValue}`;
60
60
  const expressRequest = req.req;
61
- if (expressRequest?.url?.startsWith(parsedRequestClass?.basePath)) {
62
- expressRequest.url = expressRequest.url.split(parsedRequestClass?.basePath)[1] || '/';
63
- expressRequest.originalUrl = expressRequest.url.split(parsedRequestClass?.basePath)[1] || '/';
64
- }
65
- else {
66
- logger.warn({
67
- label: `request-processor-middleware`,
68
- message: `The URL requested for doesn't start with the Base path`,
69
- });
61
+ // This section is code added for the 103 hints support. If CDN passes us a basePath in the header
62
+ // If the basePath is at the start of the URL we need to remove it so we can match the expected route.
63
+ if (expressRequest?.url && parsedRequestClass?.basePath) {
64
+ // Separate the path and the query param using dummy local host here since we do not use it
65
+ const { pathname, search } = new URL(expressRequest.url, 'http://localhost');
66
+ if (pathname.startsWith(parsedRequestClass.basePath)) {
67
+ // Remove the basePath from the pathname
68
+ const newPath = pathname.slice(parsedRequestClass.basePath.length) || '/';
69
+ // Reconstruct the URL with the modified path and original query string
70
+ expressRequest.url = newPath + search;
71
+ expressRequest.originalUrl = newPath + search;
72
+ }
73
+ else {
74
+ logger.warn({
75
+ label: `request-processor-middleware`,
76
+ message: `The URL requested for doesn't start with the Base path`,
77
+ });
78
+ }
70
79
  }
71
80
  }
72
81
  else {
@@ -81,6 +90,10 @@ export function requestProcessorMiddleware(app, context) {
81
90
  });
82
91
  }
83
92
  function parseRequestClass(requestClass) {
93
+ // If there is no requestClass do not bother parsing
94
+ if (!requestClass) {
95
+ return {};
96
+ }
84
97
  // Split the Forwarded header into individual key-value pairs
85
98
  const keyValuePairs = requestClass.split(';');
86
99
  // Create an object to store the parsed values
@@ -1,4 +1,4 @@
1
1
  import type { InternalAppServer, ServerContext, ServerTypes } from '@lwrjs/types';
2
2
  export declare function viewMiddleware<T extends ServerTypes>(app: InternalAppServer<T>, context: ServerContext): void;
3
- export declare function convertMetricsToArrayOfStrings(span: Record<string, any>): string;
3
+ export declare function convertMetricsToServerTiming(span: Record<string, any>): string;
4
4
  //# sourceMappingURL=view-middleware.d.ts.map
@@ -38,6 +38,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
38
38
  params: viewRequest.params,
39
39
  query: viewRequest.query,
40
40
  cookie: req.headers?.cookie,
41
+ trueClientIP: req.headers && req.headers['True-Client-IP'],
41
42
  coreProxy: req.getCoreProxy(appConfig.coreProxy ?? undefined, route.bootstrap?.proxyForSSR),
42
43
  };
43
44
  const resolve = req.isJsonRequest() ? viewHandler.getViewJson : viewHandler.getViewContent;
@@ -99,7 +100,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
99
100
  res.setHeader('x-trace-id', traceId);
100
101
  if (reqSpan?.status?.code === 0) {
101
102
  res.set({
102
- 'X-Custom-Lwr-Metric': convertMetricsToArrayOfStrings(convertToReadableSpan(reqSpan)),
103
+ 'Server-Timing': convertMetricsToServerTiming(convertToReadableSpan(reqSpan)),
103
104
  });
104
105
  }
105
106
  }
@@ -256,17 +257,8 @@ function addRedirectQueryParam(redirectUrl, depth) {
256
257
  url.searchParams.set(REQUEST_DEPTH_KEY, String(depth + 1));
257
258
  return url.toString().replace(fakeOrigin, '');
258
259
  }
259
- export function convertMetricsToArrayOfStrings(span) {
260
- const requiredMetrics = ['traceId', 'name', 'attributes', 'duration'];
261
- return requiredMetrics.reduce((metricString, prop) => {
262
- if (metricString && !metricString.endsWith('; ')) {
263
- metricString += '; ';
264
- }
265
- if (span[prop]) {
266
- const propValue = typeof span[prop] === 'object' ? JSON.stringify(span[prop]) : span[prop];
267
- metricString += `${prop}: ${propValue}`;
268
- }
269
- return metricString;
270
- }, '');
260
+ export function convertMetricsToServerTiming(span) {
261
+ const { name, duration } = span;
262
+ return `${name};dur=${duration.toFixed(2)}`;
271
263
  }
272
264
  //# sourceMappingURL=view-middleware.js.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.15.0-alpha.21",
7
+ "version": "0.15.0-alpha.23",
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.15.0-alpha.21",
47
- "@lwrjs/asset-registry": "0.15.0-alpha.21",
48
- "@lwrjs/asset-transformer": "0.15.0-alpha.21",
49
- "@lwrjs/base-view-provider": "0.15.0-alpha.21",
50
- "@lwrjs/base-view-transformer": "0.15.0-alpha.21",
51
- "@lwrjs/client-modules": "0.15.0-alpha.21",
52
- "@lwrjs/config": "0.15.0-alpha.21",
53
- "@lwrjs/diagnostics": "0.15.0-alpha.21",
54
- "@lwrjs/esbuild": "0.15.0-alpha.21",
55
- "@lwrjs/fs-asset-provider": "0.15.0-alpha.21",
56
- "@lwrjs/fs-watch": "0.15.0-alpha.21",
57
- "@lwrjs/html-view-provider": "0.15.0-alpha.21",
58
- "@lwrjs/instrumentation": "0.15.0-alpha.21",
59
- "@lwrjs/loader": "0.15.0-alpha.21",
60
- "@lwrjs/lwc-module-provider": "0.15.0-alpha.21",
61
- "@lwrjs/lwc-ssr": "0.15.0-alpha.21",
62
- "@lwrjs/markdown-view-provider": "0.15.0-alpha.21",
63
- "@lwrjs/module-bundler": "0.15.0-alpha.21",
64
- "@lwrjs/module-registry": "0.15.0-alpha.21",
65
- "@lwrjs/npm-module-provider": "0.15.0-alpha.21",
66
- "@lwrjs/nunjucks-view-provider": "0.15.0-alpha.21",
67
- "@lwrjs/o11y": "0.15.0-alpha.21",
68
- "@lwrjs/resource-registry": "0.15.0-alpha.21",
69
- "@lwrjs/router": "0.15.0-alpha.21",
70
- "@lwrjs/server": "0.15.0-alpha.21",
71
- "@lwrjs/shared-utils": "0.15.0-alpha.21",
72
- "@lwrjs/static": "0.15.0-alpha.21",
73
- "@lwrjs/view-registry": "0.15.0-alpha.21",
46
+ "@lwrjs/app-service": "0.15.0-alpha.23",
47
+ "@lwrjs/asset-registry": "0.15.0-alpha.23",
48
+ "@lwrjs/asset-transformer": "0.15.0-alpha.23",
49
+ "@lwrjs/base-view-provider": "0.15.0-alpha.23",
50
+ "@lwrjs/base-view-transformer": "0.15.0-alpha.23",
51
+ "@lwrjs/client-modules": "0.15.0-alpha.23",
52
+ "@lwrjs/config": "0.15.0-alpha.23",
53
+ "@lwrjs/diagnostics": "0.15.0-alpha.23",
54
+ "@lwrjs/esbuild": "0.15.0-alpha.23",
55
+ "@lwrjs/fs-asset-provider": "0.15.0-alpha.23",
56
+ "@lwrjs/fs-watch": "0.15.0-alpha.23",
57
+ "@lwrjs/html-view-provider": "0.15.0-alpha.23",
58
+ "@lwrjs/instrumentation": "0.15.0-alpha.23",
59
+ "@lwrjs/loader": "0.15.0-alpha.23",
60
+ "@lwrjs/lwc-module-provider": "0.15.0-alpha.23",
61
+ "@lwrjs/lwc-ssr": "0.15.0-alpha.23",
62
+ "@lwrjs/markdown-view-provider": "0.15.0-alpha.23",
63
+ "@lwrjs/module-bundler": "0.15.0-alpha.23",
64
+ "@lwrjs/module-registry": "0.15.0-alpha.23",
65
+ "@lwrjs/npm-module-provider": "0.15.0-alpha.23",
66
+ "@lwrjs/nunjucks-view-provider": "0.15.0-alpha.23",
67
+ "@lwrjs/o11y": "0.15.0-alpha.23",
68
+ "@lwrjs/resource-registry": "0.15.0-alpha.23",
69
+ "@lwrjs/router": "0.15.0-alpha.23",
70
+ "@lwrjs/server": "0.15.0-alpha.23",
71
+ "@lwrjs/shared-utils": "0.15.0-alpha.23",
72
+ "@lwrjs/static": "0.15.0-alpha.23",
73
+ "@lwrjs/view-registry": "0.15.0-alpha.23",
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.15.0-alpha.21",
83
+ "@lwrjs/types": "0.15.0-alpha.23",
84
84
  "@types/ws": "^8.5.12",
85
85
  "memfs": "^4.9.3"
86
86
  },
@@ -93,5 +93,5 @@
93
93
  "volta": {
94
94
  "extends": "../../../package.json"
95
95
  },
96
- "gitHead": "4c09ff654569ab490fc77f196d3230b31404940e"
96
+ "gitHead": "07592ebcbcfd739a9a8882b2f8f56da50332250d"
97
97
  }