@lwrjs/core 0.12.0-alpha.11 → 0.12.0-alpha.13

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.
@@ -31,6 +31,24 @@ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
31
31
  var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
32
32
  var import_identity = __toModule(require("./utils/identity.cjs"));
33
33
  var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
34
+ var import_fs_extra = __toModule(require("fs-extra"));
35
+ function assetMiddleware(app, context) {
36
+ const paths = context.appConfig.assets.map((config) => {
37
+ const assetDirConfig = config;
38
+ let urlPath = config.urlPath;
39
+ if (assetDirConfig.root) {
40
+ urlPath = "/:filename";
41
+ } else if (assetDirConfig.dir) {
42
+ urlPath += app.getRegexWildcard();
43
+ }
44
+ return urlPath;
45
+ });
46
+ app.get([
47
+ ...[...new Set(paths)],
48
+ "/:apiVersion/:assetType(asset|content-asset)/:immutable?/s/:signature/" + app.getRegexWildcard(),
49
+ "/:apiVersion/:assetType(asset|content-asset)/:immutable?/" + app.getRegexWildcard()
50
+ ], (0, import_error_handling.handleErrors)(createAssetMiddleware(context)));
51
+ }
34
52
  function createAssetMiddleware(context) {
35
53
  const {
36
54
  assetRegistry,
@@ -43,24 +61,23 @@ function createAssetMiddleware(context) {
43
61
  assetId.specifier = import_path.default.join(basePath, assetId.specifier);
44
62
  }
45
63
  try {
64
+ let asset;
46
65
  const assetUri = await assetRegistry.resolveAssetUri(assetId, runtimeEnvironment);
47
66
  if (assetUri.external) {
48
- res.set({
49
- Location: assetUri.uri,
50
- "cache-control": "public, max-age=60"
51
- });
52
- res.sendStatus(302);
53
- return;
54
- }
55
- const asset = await (0, import_instrumentation.getTracer)().trace({
56
- name: import_instrumentation.RequestHandlerSpan.GetAsset,
57
- attributes: {
58
- specifier: assetId.specifier,
59
- url: req.originalUrl
67
+ try {
68
+ asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
69
+ if (!import_fs_extra.default.existsSync(asset.entry)) {
70
+ return sendRedirect(res, assetUri);
71
+ }
72
+ } catch (error) {
73
+ if (import_path.default.dirname(assetId.specifier) === (basePath ? basePath : "/") && error instanceof import_diagnostics.DiagnosticsError && error?.diagnostics[0]?.description.category === "lwrUnresolvable/asset") {
74
+ return sendRedirect(res, assetUri);
75
+ }
76
+ throw error;
60
77
  }
61
- }, () => {
62
- return assetRegistry.getAsset({...assetId, signature}, runtimeEnvironment, req.isSiteGeneration());
63
- });
78
+ } else {
79
+ asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
80
+ }
64
81
  if (req.isSiteGeneration()) {
65
82
  res.setSiteGenerationMetadata({asset});
66
83
  }
@@ -82,20 +99,22 @@ function createAssetMiddleware(context) {
82
99
  }
83
100
  };
84
101
  }
85
- function assetMiddleware(app, context) {
86
- const paths = context.appConfig.assets.map((config) => {
87
- const assetDirConfig = config;
88
- let urlPath = config.urlPath;
89
- if (assetDirConfig.root) {
90
- urlPath = "/:filename";
91
- } else if (assetDirConfig.dir) {
92
- urlPath += app.getRegexWildcard();
102
+ async function getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment) {
103
+ const asset = await (0, import_instrumentation.getTracer)().trace({
104
+ name: import_instrumentation.RequestHandlerSpan.GetAsset,
105
+ attributes: {
106
+ specifier: assetId.specifier,
107
+ url: req.originalUrl
93
108
  }
94
- return urlPath;
109
+ }, () => {
110
+ return assetRegistry.getAsset({...assetId, signature}, runtimeEnvironment, req.isSiteGeneration());
95
111
  });
96
- app.get([
97
- ...[...new Set(paths)],
98
- "/:apiVersion/:assetType(asset|content-asset)/:immutable?/s/:signature/" + app.getRegexWildcard(),
99
- "/:apiVersion/:assetType(asset|content-asset)/:immutable?/" + app.getRegexWildcard()
100
- ], (0, import_error_handling.handleErrors)(createAssetMiddleware(context)));
112
+ return asset;
113
+ }
114
+ function sendRedirect(res, assetUri) {
115
+ res.set({
116
+ Location: assetUri.uri,
117
+ "cache-control": "public, max-age=60"
118
+ });
119
+ res.sendStatus(302);
101
120
  }
@@ -35,7 +35,7 @@ function createReturnStatus(error, url) {
35
35
  if (error instanceof import_diagnostics.LwrUnresolvableError) {
36
36
  return {status: 404, message: error.message};
37
37
  }
38
- return {status: 500, message: import_diagnostics.descriptions.UNRESOLVABLE.SERVER_ERROR(url).message};
38
+ return {status: 500, message: import_diagnostics.descriptions.SERVER.SERVER_ERROR(url).message};
39
39
  }
40
40
  function handleErrors(middleware) {
41
41
  return async (req, res, next) => {
@@ -66,6 +66,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
66
66
  const resolve = req.isJsonRequest() ? viewHandler.getViewJson : viewHandler.getViewContent;
67
67
  let viewResponse;
68
68
  let resolvedRoute;
69
+ let traceId;
69
70
  try {
70
71
  viewResponse = await (0, import_instrumentation.getTracer)().trace({
71
72
  name: import_instrumentation.RequestHandlerSpan.GetView,
@@ -73,7 +74,8 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
73
74
  view: route.id,
74
75
  url: req.originalUrl
75
76
  }
76
- }, () => {
77
+ }, (span) => {
78
+ traceId = span.traceId;
77
79
  return resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
78
80
  });
79
81
  resolvedRoute = route;
@@ -106,6 +108,9 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
106
108
  res.setHeader("cache-control", `public, max-age=${cacheTtl}`);
107
109
  }
108
110
  }
111
+ if (traceId?.length) {
112
+ res.setHeader("x-trace-id", traceId);
113
+ }
109
114
  const status = resolvedRoute.status || viewResponse.status || 200;
110
115
  res.status(status);
111
116
  res.send(viewResponse.body);
@@ -3,6 +3,30 @@ import { DiagnosticsError } from '@lwrjs/diagnostics';
3
3
  import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
4
4
  import { getAssetIdentity } from './utils/identity.js';
5
5
  import { handleErrors } from './utils/error-handling.js';
6
+ import fs from 'fs-extra';
7
+ export function assetMiddleware(app, context) {
8
+ const paths = context.appConfig.assets.map((config) => {
9
+ const assetDirConfig = config;
10
+ let urlPath = config.urlPath;
11
+ // If this is a root config add a /:filename path to match any file in the root. The middleware will fall through if there is no match.
12
+ if (assetDirConfig.root) {
13
+ urlPath = '/:filename';
14
+ }
15
+ else if (assetDirConfig.dir) {
16
+ urlPath += app.getRegexWildcard();
17
+ }
18
+ return urlPath;
19
+ });
20
+ app.get([
21
+ // De-dupe paths (i.e. root path may have been added more than once)
22
+ ...[...new Set(paths)],
23
+ '/:apiVersion/:assetType(asset|content-asset)/:immutable?/s/:signature/' + app.getRegexWildcard(),
24
+ '/:apiVersion/:assetType(asset|content-asset)/:immutable?/' + app.getRegexWildcard(),
25
+ ], handleErrors(createAssetMiddleware(context)));
26
+ }
27
+ /**
28
+ * Create middleware function to handle get assets requests
29
+ */
6
30
  function createAssetMiddleware(context) {
7
31
  const { assetRegistry, runtimeEnvironment: { basePath }, } = context;
8
32
  return async (req, res, next) => {
@@ -12,25 +36,34 @@ function createAssetMiddleware(context) {
12
36
  assetId.specifier = path.join(basePath, assetId.specifier);
13
37
  }
14
38
  try {
15
- // Redirect if this is an external asset
39
+ let asset;
16
40
  const assetUri = await assetRegistry.resolveAssetUri(assetId, runtimeEnvironment);
41
+ // Check if this asset is available externally
17
42
  if (assetUri.external) {
18
- res.set({
19
- Location: assetUri.uri,
20
- 'cache-control': 'public, max-age=60',
21
- });
22
- res.sendStatus(302);
23
- return;
43
+ // This asset is marked external but hit the middleware anyway
44
+ // Check if we have this file locally, if not, send a 302 to
45
+ // redirect to the external URL
46
+ try {
47
+ asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
48
+ // Verify the content actually exists locally
49
+ if (!fs.existsSync(asset.entry)) {
50
+ return sendRedirect(res, assetUri);
51
+ }
52
+ }
53
+ catch (error) {
54
+ // Expected asset not found locally re-direct to external URL
55
+ if (path.dirname(assetId.specifier) === (basePath ? basePath : '/') &&
56
+ error instanceof DiagnosticsError &&
57
+ error?.diagnostics[0]?.description.category === 'lwrUnresolvable/asset') {
58
+ return sendRedirect(res, assetUri);
59
+ }
60
+ // unexpected throw the error
61
+ throw error;
62
+ }
63
+ }
64
+ else {
65
+ asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
24
66
  }
25
- const asset = await getTracer().trace({
26
- name: RequestHandlerSpan.GetAsset,
27
- attributes: {
28
- specifier: assetId.specifier,
29
- url: req.originalUrl,
30
- },
31
- }, () => {
32
- return assetRegistry.getAsset({ ...assetId, signature }, runtimeEnvironment, req.isSiteGeneration());
33
- });
34
67
  if (req.isSiteGeneration()) {
35
68
  res.setSiteGenerationMetadata({ asset });
36
69
  }
@@ -59,24 +92,29 @@ function createAssetMiddleware(context) {
59
92
  }
60
93
  };
61
94
  }
62
- export function assetMiddleware(app, context) {
63
- const paths = context.appConfig.assets.map((config) => {
64
- const assetDirConfig = config;
65
- let urlPath = config.urlPath;
66
- // If this is a root config add a /:filename path to match any file in the root. The middleware will fall through if there is no match.
67
- if (assetDirConfig.root) {
68
- urlPath = '/:filename';
69
- }
70
- else if (assetDirConfig.dir) {
71
- urlPath += app.getRegexWildcard();
72
- }
73
- return urlPath;
95
+ /**
96
+ * Get the asset definition if asset is local
97
+ */
98
+ async function getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment) {
99
+ const asset = await getTracer().trace({
100
+ name: RequestHandlerSpan.GetAsset,
101
+ attributes: {
102
+ specifier: assetId.specifier,
103
+ url: req.originalUrl,
104
+ },
105
+ }, () => {
106
+ return assetRegistry.getAsset({ ...assetId, signature }, runtimeEnvironment, req.isSiteGeneration());
74
107
  });
75
- app.get([
76
- // De-dupe paths (i.e. root path may have been added more than once)
77
- ...[...new Set(paths)],
78
- '/:apiVersion/:assetType(asset|content-asset)/:immutable?/s/:signature/' + app.getRegexWildcard(),
79
- '/:apiVersion/:assetType(asset|content-asset)/:immutable?/' + app.getRegexWildcard(),
80
- ], handleErrors(createAssetMiddleware(context)));
108
+ return asset;
109
+ }
110
+ /**
111
+ * Send a redirect (302) response
112
+ */
113
+ function sendRedirect(res, assetUri) {
114
+ res.set({
115
+ Location: assetUri.uri,
116
+ 'cache-control': 'public, max-age=60',
117
+ });
118
+ res.sendStatus(302);
81
119
  }
82
120
  //# sourceMappingURL=asset-middleware.js.map
@@ -8,7 +8,7 @@ function createReturnStatus(error, url) {
8
8
  if (error instanceof LwrUnresolvableError) {
9
9
  return { status: 404, message: error.message };
10
10
  }
11
- return { status: 500, message: descriptions.UNRESOLVABLE.SERVER_ERROR(url).message };
11
+ return { status: 500, message: descriptions.SERVER.SERVER_ERROR(url).message };
12
12
  }
13
13
  export function handleErrors(middleware) {
14
14
  return async (req, res, next) => {
@@ -40,6 +40,7 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
40
40
  const resolve = req.isJsonRequest() ? viewHandler.getViewJson : viewHandler.getViewContent;
41
41
  let viewResponse;
42
42
  let resolvedRoute;
43
+ let traceId;
43
44
  try {
44
45
  viewResponse = await getTracer().trace({
45
46
  name: RequestHandlerSpan.GetView,
@@ -47,7 +48,8 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
47
48
  view: route.id,
48
49
  url: req.originalUrl,
49
50
  },
50
- }, () => {
51
+ }, (span) => {
52
+ traceId = span.traceId;
51
53
  return resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
52
54
  });
53
55
  resolvedRoute = route;
@@ -85,6 +87,9 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
85
87
  res.setHeader('cache-control', `public, max-age=${cacheTtl}`);
86
88
  }
87
89
  }
90
+ if (traceId?.length) {
91
+ res.setHeader('x-trace-id', traceId);
92
+ }
88
93
  const status = resolvedRoute.status || viewResponse.status || 200;
89
94
  res.status(status);
90
95
  res.send(viewResponse.body);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.12.0-alpha.11",
7
+ "version": "0.12.0-alpha.13",
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.12.0-alpha.11",
47
- "@lwrjs/asset-registry": "0.12.0-alpha.11",
48
- "@lwrjs/asset-transformer": "0.12.0-alpha.11",
49
- "@lwrjs/base-view-provider": "0.12.0-alpha.11",
50
- "@lwrjs/base-view-transformer": "0.12.0-alpha.11",
51
- "@lwrjs/client-modules": "0.12.0-alpha.11",
52
- "@lwrjs/config": "0.12.0-alpha.11",
53
- "@lwrjs/diagnostics": "0.12.0-alpha.11",
54
- "@lwrjs/esbuild": "0.12.0-alpha.11",
55
- "@lwrjs/fs-asset-provider": "0.12.0-alpha.11",
56
- "@lwrjs/fs-watch": "0.12.0-alpha.11",
57
- "@lwrjs/html-view-provider": "0.12.0-alpha.11",
58
- "@lwrjs/instrumentation": "0.12.0-alpha.11",
59
- "@lwrjs/loader": "0.12.0-alpha.11",
60
- "@lwrjs/lwc-module-provider": "0.12.0-alpha.11",
61
- "@lwrjs/lwc-ssr": "0.12.0-alpha.11",
62
- "@lwrjs/markdown-view-provider": "0.12.0-alpha.11",
63
- "@lwrjs/module-bundler": "0.12.0-alpha.11",
64
- "@lwrjs/module-registry": "0.12.0-alpha.11",
65
- "@lwrjs/npm-module-provider": "0.12.0-alpha.11",
66
- "@lwrjs/nunjucks-view-provider": "0.12.0-alpha.11",
67
- "@lwrjs/o11y": "0.12.0-alpha.11",
68
- "@lwrjs/resource-registry": "0.12.0-alpha.11",
69
- "@lwrjs/router": "0.12.0-alpha.11",
70
- "@lwrjs/server": "0.12.0-alpha.11",
71
- "@lwrjs/shared-utils": "0.12.0-alpha.11",
72
- "@lwrjs/static": "0.12.0-alpha.11",
73
- "@lwrjs/view-registry": "0.12.0-alpha.11",
46
+ "@lwrjs/app-service": "0.12.0-alpha.13",
47
+ "@lwrjs/asset-registry": "0.12.0-alpha.13",
48
+ "@lwrjs/asset-transformer": "0.12.0-alpha.13",
49
+ "@lwrjs/base-view-provider": "0.12.0-alpha.13",
50
+ "@lwrjs/base-view-transformer": "0.12.0-alpha.13",
51
+ "@lwrjs/client-modules": "0.12.0-alpha.13",
52
+ "@lwrjs/config": "0.12.0-alpha.13",
53
+ "@lwrjs/diagnostics": "0.12.0-alpha.13",
54
+ "@lwrjs/esbuild": "0.12.0-alpha.13",
55
+ "@lwrjs/fs-asset-provider": "0.12.0-alpha.13",
56
+ "@lwrjs/fs-watch": "0.12.0-alpha.13",
57
+ "@lwrjs/html-view-provider": "0.12.0-alpha.13",
58
+ "@lwrjs/instrumentation": "0.12.0-alpha.13",
59
+ "@lwrjs/loader": "0.12.0-alpha.13",
60
+ "@lwrjs/lwc-module-provider": "0.12.0-alpha.13",
61
+ "@lwrjs/lwc-ssr": "0.12.0-alpha.13",
62
+ "@lwrjs/markdown-view-provider": "0.12.0-alpha.13",
63
+ "@lwrjs/module-bundler": "0.12.0-alpha.13",
64
+ "@lwrjs/module-registry": "0.12.0-alpha.13",
65
+ "@lwrjs/npm-module-provider": "0.12.0-alpha.13",
66
+ "@lwrjs/nunjucks-view-provider": "0.12.0-alpha.13",
67
+ "@lwrjs/o11y": "0.12.0-alpha.13",
68
+ "@lwrjs/resource-registry": "0.12.0-alpha.13",
69
+ "@lwrjs/router": "0.12.0-alpha.13",
70
+ "@lwrjs/server": "0.12.0-alpha.13",
71
+ "@lwrjs/shared-utils": "0.12.0-alpha.13",
72
+ "@lwrjs/static": "0.12.0-alpha.13",
73
+ "@lwrjs/view-registry": "0.12.0-alpha.13",
74
74
  "chokidar": "^3.5.3",
75
75
  "esbuild": "^0.9.7",
76
76
  "fs-extra": "^11.1.1",
@@ -80,7 +80,7 @@
80
80
  "ws": "^8.8.1"
81
81
  },
82
82
  "devDependencies": {
83
- "@lwrjs/types": "0.12.0-alpha.11",
83
+ "@lwrjs/types": "0.12.0-alpha.13",
84
84
  "@types/ws": "^8.5.3"
85
85
  },
86
86
  "peerDependencies": {
@@ -92,5 +92,5 @@
92
92
  "volta": {
93
93
  "extends": "../../../package.json"
94
94
  },
95
- "gitHead": "781951a5ef92ce735cbffce7c7a2a32c60bd3c16"
95
+ "gitHead": "7a63e416f9e5f27b44331311a371f68e20ce685c"
96
96
  }