@lwrjs/core 0.17.2-alpha.4 → 0.17.2-alpha.5

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,22 +28,40 @@ __export(exports, {
28
28
  });
29
29
  var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
30
30
  var import_diagnostics2 = __toModule(require("@lwrjs/diagnostics"));
31
- function createReturnStatus(error, url, debug) {
32
- let message = error.message, status = 500;
31
+ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
32
+ function addRedirectQueryParam(redirectUrl, depth) {
33
+ const fakeOrigin = "http://parse.com";
34
+ const url = (0, import_shared_utils.isURL)(redirectUrl) ? new URL(redirectUrl) : new URL(`${fakeOrigin}${redirectUrl}`);
35
+ url.searchParams.set(import_shared_utils.REQUEST_DEPTH_KEY, String(depth + 1));
36
+ return url.toString().replace(fakeOrigin, "");
37
+ }
38
+ function createReturnStatus(error, req, debug) {
39
+ let status = 500, body = error.message || "";
40
+ const headers = {};
41
+ if (error instanceof import_diagnostics.LwrStatusError) {
42
+ status = error.status;
43
+ const rawHeaders = error.headers || {};
44
+ Object.entries(rawHeaders).forEach(([key, value]) => headers[key.toLowerCase()] = value);
45
+ if (status === 301 || status === 302) {
46
+ const location = headers.location;
47
+ if (location && (0, import_shared_utils.isURL)(location) || location?.startsWith("/")) {
48
+ headers.location = addRedirectQueryParam(location, (0, import_shared_utils.parseRequestDepth)(req.headers, req.query));
49
+ } else {
50
+ import_diagnostics2.logger.warn(`[error-middleware] Invalid location header for HTTP status ${status}: "${location}"`);
51
+ }
52
+ }
53
+ }
33
54
  if (error instanceof import_diagnostics.LwrInvalidError) {
34
55
  status = 400;
35
56
  } else if (error instanceof import_diagnostics.LwrUnresolvableError) {
36
57
  status = 404;
37
58
  } else {
38
- message = error instanceof import_diagnostics.LwrError ? message : `${import_diagnostics.descriptions.SERVER.SERVER_ERROR(url)}: ${error.message}`;
59
+ body = error instanceof import_diagnostics.LwrError ? body : `${import_diagnostics.descriptions.SERVER.SERVER_ERROR(req.originalUrl)}: ${body}`;
39
60
  }
40
61
  if (debug) {
41
- return {
42
- status,
43
- message: `<div style="font-family:sans-serif;margin:50px;"><h1>${status}: Document generation failed</h1><p>${message.replace(/</g, "&lt;").replace(/>/g, "&gt;")}</p></div>`
44
- };
62
+ body = `<div style="font-family:sans-serif;margin:50px;"><h1>${status}: Document generation failed</h1><p>${body.replace(/</g, "&lt;").replace(/>/g, "&gt;")}</p></div>`;
45
63
  }
46
- return {status, message};
64
+ return {status, body, headers: Object.keys(headers).length > 0 ? headers : void 0};
47
65
  }
48
66
  function handleErrors(middleware, isBaseDoc = false) {
49
67
  return async (req, res, next) => {
@@ -57,9 +75,10 @@ function handleErrors(middleware, isBaseDoc = false) {
57
75
  } else {
58
76
  import_diagnostics2.logger.error(err);
59
77
  }
60
- const {status, message} = createReturnStatus(err, req.originalUrl, req.query.debug !== void 0 && isBaseDoc);
78
+ const {status, body, headers} = createReturnStatus(err, req, req.query.debug !== void 0 && isBaseDoc);
79
+ headers && res.set(headers);
61
80
  res.status(status);
62
- res.send(message);
81
+ res.send(body);
63
82
  }
64
83
  };
65
84
  }
@@ -136,24 +136,8 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
136
136
  }
137
137
  }
138
138
  let status = resolvedRoute.status || viewResponse.status || 200;
139
- const viewDefinitionStatus = viewResponse.metadata?.viewDefinition?.status;
140
139
  if (viewResponse.status === 301 || viewResponse.status === 302) {
141
140
  status = viewResponse.status;
142
- } else if (viewDefinitionStatus && viewDefinitionStatus.code) {
143
- const origStatus = status;
144
- const {code, location} = viewDefinitionStatus;
145
- const isRedirect = code === 301 || code === 302;
146
- status = code;
147
- if (isRedirect) {
148
- if (location && (0, import_shared_utils.isURL)(location) || location?.startsWith("/")) {
149
- res.set({
150
- location: addRedirectQueryParam(location, (0, import_shared_utils.parseRequestDepth)(req.headers, req.query))
151
- });
152
- } else {
153
- status = origStatus;
154
- import_diagnostics.logger.warn(`[view-middleware] Ignoring invalid location header: "${location}"`);
155
- }
156
- }
157
141
  }
158
142
  res.status(status);
159
143
  const viewResponseBody = viewResponse.body;
@@ -299,12 +283,6 @@ function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, defaultRed
299
283
  return res.sendStatus(301);
300
284
  });
301
285
  }
302
- function addRedirectQueryParam(redirectUrl, depth) {
303
- const fakeOrigin = "http://parse.com";
304
- const url = (0, import_shared_utils.isURL)(redirectUrl) ? new URL(redirectUrl) : new URL(`${fakeOrigin}${redirectUrl}`);
305
- url.searchParams.set(import_shared_utils.REQUEST_DEPTH_KEY, String(depth + 1));
306
- return url.toString().replace(fakeOrigin, "");
307
- }
308
286
  function byteSize(body) {
309
287
  if (Buffer.isBuffer(body)) {
310
288
  return body.length;
@@ -1,7 +1,33 @@
1
- import { descriptions, DiagnosticsError, LwrError, LwrInvalidError, LwrUnresolvableError, } from '@lwrjs/diagnostics';
1
+ import { descriptions, DiagnosticsError, LwrError, LwrInvalidError, LwrStatusError, LwrUnresolvableError, } from '@lwrjs/diagnostics';
2
2
  import { logger } from '@lwrjs/diagnostics';
3
- function createReturnStatus(error, url, debug) {
4
- let message = error.message, status = 500;
3
+ import { isURL, parseRequestDepth, REQUEST_DEPTH_KEY } from '@lwrjs/shared-utils';
4
+ function addRedirectQueryParam(redirectUrl, depth) {
5
+ // add a request depth query param to the URL
6
+ // the depth header cannot be used since headers cannot be added to a redirect
7
+ const fakeOrigin = 'http://parse.com';
8
+ const url = isURL(redirectUrl) ? new URL(redirectUrl) : new URL(`${fakeOrigin}${redirectUrl}`);
9
+ url.searchParams.set(REQUEST_DEPTH_KEY, String(depth + 1));
10
+ return url.toString().replace(fakeOrigin, '');
11
+ }
12
+ function createReturnStatus(error, req, debug) {
13
+ let status = 500, body = error.message || '';
14
+ const headers = {};
15
+ if (error instanceof LwrStatusError) {
16
+ // handle special HTTP statuses, eg: 301, 302, 429, 503
17
+ status = error.status;
18
+ const rawHeaders = error.headers || {};
19
+ Object.entries(rawHeaders).forEach(([key, value]) => (headers[key.toLowerCase()] = value));
20
+ if (status === 301 || status === 302) {
21
+ // handle redirect
22
+ const location = headers.location;
23
+ if ((location && isURL(location)) || location?.startsWith('/')) {
24
+ headers.location = addRedirectQueryParam(location, parseRequestDepth(req.headers, req.query));
25
+ }
26
+ else {
27
+ logger.warn(`[error-middleware] Invalid location header for HTTP status ${status}: "${location}"`);
28
+ }
29
+ }
30
+ }
5
31
  if (error instanceof LwrInvalidError) {
6
32
  status = 400;
7
33
  }
@@ -10,19 +36,16 @@ function createReturnStatus(error, url, debug) {
10
36
  }
11
37
  else {
12
38
  // catchall: likely a LwrServerError
13
- message =
39
+ body =
14
40
  error instanceof LwrError
15
- ? message
16
- : `${descriptions.SERVER.SERVER_ERROR(url)}: ${error.message}`;
41
+ ? body
42
+ : `${descriptions.SERVER.SERVER_ERROR(req.originalUrl)}: ${body}`;
17
43
  }
18
44
  if (debug) {
19
45
  // return a debug base doc if debug mode is on
20
- return {
21
- status,
22
- message: `<div style="font-family:sans-serif;margin:50px;"><h1>${status}: Document generation failed</h1><p>${message.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</p></div>`,
23
- };
46
+ body = `<div style="font-family:sans-serif;margin:50px;"><h1>${status}: Document generation failed</h1><p>${body.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</p></div>`;
24
47
  }
25
- return { status, message };
48
+ return { status, body, headers: Object.keys(headers).length > 0 ? headers : undefined };
26
49
  }
27
50
  export function handleErrors(middleware, isBaseDoc = false) {
28
51
  return async (req, res, next) => {
@@ -38,11 +61,12 @@ export function handleErrors(middleware, isBaseDoc = false) {
38
61
  else {
39
62
  logger.error(err);
40
63
  }
41
- const { status, message } = createReturnStatus(err, req.originalUrl,
64
+ const { status, body, headers } = createReturnStatus(err, req,
42
65
  // only return HTML error screen for base doc requests in debug mode
43
66
  req.query.debug !== undefined && isBaseDoc);
67
+ headers && res.set(headers);
44
68
  res.status(status);
45
- res.send(message);
69
+ res.send(body);
46
70
  }
47
71
  };
48
72
  }
@@ -2,7 +2,7 @@ import { TextEncoder } from 'util';
2
2
  import { URLSearchParams } from 'url';
3
3
  import { descriptions, logger } from '@lwrjs/diagnostics';
4
4
  import { getClientRoutes } from '@lwrjs/router';
5
- import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, isURL, parseRequestDepth, REQUEST_DEPTH_KEY, isLocalDev, shortestTtl, isLambdaEnv, } from '@lwrjs/shared-utils';
5
+ import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, isLocalDev, shortestTtl, isLambdaEnv, } from '@lwrjs/shared-utils';
6
6
  import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
7
7
  import { handleErrors } from './utils/error-handling.js';
8
8
  import { LwrViewHandler } from '@lwrjs/view-registry';
@@ -121,29 +121,10 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
121
121
  }
122
122
  }
123
123
  let status = resolvedRoute.status || viewResponse.status || 200;
124
- const viewDefinitionStatus = viewResponse.metadata?.viewDefinition?.status;
125
124
  if (viewResponse.status === 301 || viewResponse.status === 302) {
126
125
  // route handle redirect status takes precedence
127
126
  status = viewResponse.status;
128
127
  }
129
- else if (viewDefinitionStatus && viewDefinitionStatus.code) {
130
- const origStatus = status;
131
- const { code, location } = viewDefinitionStatus;
132
- const isRedirect = code === 301 || code === 302;
133
- status = code;
134
- if (isRedirect) {
135
- if ((location && isURL(location)) || location?.startsWith('/')) {
136
- res.set({
137
- location: addRedirectQueryParam(location, parseRequestDepth(req.headers, req.query)),
138
- });
139
- }
140
- else {
141
- // reset the status in the event of an invalid location when redirecting
142
- status = origStatus;
143
- logger.warn(`[view-middleware] Ignoring invalid location header: "${location}"`);
144
- }
145
- }
146
- }
147
128
  res.status(status);
148
129
  // LWR@MRT 254 temporary safeguard for "dupe styles" issue causing huge base docs
149
130
  // Re-evaluate for removal once we determine it is no longer needed: W-17201070
@@ -327,14 +308,6 @@ function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, defaultRed
327
308
  return res.sendStatus(301);
328
309
  });
329
310
  }
330
- function addRedirectQueryParam(redirectUrl, depth) {
331
- // add a request depth query param to the URL
332
- // the depth header cannot be used since headers cannot be added to a redirect
333
- const fakeOrigin = 'http://parse.com';
334
- const url = isURL(redirectUrl) ? new URL(redirectUrl) : new URL(`${fakeOrigin}${redirectUrl}`);
335
- url.searchParams.set(REQUEST_DEPTH_KEY, String(depth + 1));
336
- return url.toString().replace(fakeOrigin, '');
337
- }
338
311
  // Get number of bytes from a string. Different char encodings can effect size per char.
339
312
  function byteSize(body) {
340
313
  if (Buffer.isBuffer(body)) {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.17.2-alpha.4",
7
+ "version": "0.17.2-alpha.5",
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.17.2-alpha.4",
47
- "@lwrjs/asset-registry": "0.17.2-alpha.4",
48
- "@lwrjs/asset-transformer": "0.17.2-alpha.4",
49
- "@lwrjs/base-view-provider": "0.17.2-alpha.4",
50
- "@lwrjs/base-view-transformer": "0.17.2-alpha.4",
51
- "@lwrjs/client-modules": "0.17.2-alpha.4",
52
- "@lwrjs/config": "0.17.2-alpha.4",
53
- "@lwrjs/diagnostics": "0.17.2-alpha.4",
54
- "@lwrjs/esbuild": "0.17.2-alpha.4",
55
- "@lwrjs/fs-asset-provider": "0.17.2-alpha.4",
56
- "@lwrjs/fs-watch": "0.17.2-alpha.4",
57
- "@lwrjs/html-view-provider": "0.17.2-alpha.4",
58
- "@lwrjs/instrumentation": "0.17.2-alpha.4",
59
- "@lwrjs/loader": "0.17.2-alpha.4",
60
- "@lwrjs/lwc-module-provider": "0.17.2-alpha.4",
61
- "@lwrjs/lwc-ssr": "0.17.2-alpha.4",
62
- "@lwrjs/markdown-view-provider": "0.17.2-alpha.4",
63
- "@lwrjs/module-bundler": "0.17.2-alpha.4",
64
- "@lwrjs/module-registry": "0.17.2-alpha.4",
65
- "@lwrjs/npm-module-provider": "0.17.2-alpha.4",
66
- "@lwrjs/nunjucks-view-provider": "0.17.2-alpha.4",
67
- "@lwrjs/o11y": "0.17.2-alpha.4",
68
- "@lwrjs/resource-registry": "0.17.2-alpha.4",
69
- "@lwrjs/router": "0.17.2-alpha.4",
70
- "@lwrjs/server": "0.17.2-alpha.4",
71
- "@lwrjs/shared-utils": "0.17.2-alpha.4",
72
- "@lwrjs/static": "0.17.2-alpha.4",
73
- "@lwrjs/view-registry": "0.17.2-alpha.4",
46
+ "@lwrjs/app-service": "0.17.2-alpha.5",
47
+ "@lwrjs/asset-registry": "0.17.2-alpha.5",
48
+ "@lwrjs/asset-transformer": "0.17.2-alpha.5",
49
+ "@lwrjs/base-view-provider": "0.17.2-alpha.5",
50
+ "@lwrjs/base-view-transformer": "0.17.2-alpha.5",
51
+ "@lwrjs/client-modules": "0.17.2-alpha.5",
52
+ "@lwrjs/config": "0.17.2-alpha.5",
53
+ "@lwrjs/diagnostics": "0.17.2-alpha.5",
54
+ "@lwrjs/esbuild": "0.17.2-alpha.5",
55
+ "@lwrjs/fs-asset-provider": "0.17.2-alpha.5",
56
+ "@lwrjs/fs-watch": "0.17.2-alpha.5",
57
+ "@lwrjs/html-view-provider": "0.17.2-alpha.5",
58
+ "@lwrjs/instrumentation": "0.17.2-alpha.5",
59
+ "@lwrjs/loader": "0.17.2-alpha.5",
60
+ "@lwrjs/lwc-module-provider": "0.17.2-alpha.5",
61
+ "@lwrjs/lwc-ssr": "0.17.2-alpha.5",
62
+ "@lwrjs/markdown-view-provider": "0.17.2-alpha.5",
63
+ "@lwrjs/module-bundler": "0.17.2-alpha.5",
64
+ "@lwrjs/module-registry": "0.17.2-alpha.5",
65
+ "@lwrjs/npm-module-provider": "0.17.2-alpha.5",
66
+ "@lwrjs/nunjucks-view-provider": "0.17.2-alpha.5",
67
+ "@lwrjs/o11y": "0.17.2-alpha.5",
68
+ "@lwrjs/resource-registry": "0.17.2-alpha.5",
69
+ "@lwrjs/router": "0.17.2-alpha.5",
70
+ "@lwrjs/server": "0.17.2-alpha.5",
71
+ "@lwrjs/shared-utils": "0.17.2-alpha.5",
72
+ "@lwrjs/static": "0.17.2-alpha.5",
73
+ "@lwrjs/view-registry": "0.17.2-alpha.5",
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.17.2-alpha.4",
83
+ "@lwrjs/types": "0.17.2-alpha.5",
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": "d7fb1605cec0bf9fef18e3daeb17dc28b35a80d3"
96
+ "gitHead": "b87daf19f22c54126ccaf01e0f899aabe6265885"
97
97
  }