@lwrjs/core 0.10.0-alpha.19 → 0.10.0-alpha.20

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.
@@ -45,6 +45,7 @@ var import_asset_middleware = __toModule(require("./middleware/asset-middleware.
45
45
  var import_view_middleware = __toModule(require("./middleware/view-middleware.cjs"));
46
46
  var import_resource_middleware = __toModule(require("./middleware/resource-middleware.cjs"));
47
47
  var import_hmr_middleware = __toModule(require("./middleware/hmr-middleware.cjs"));
48
+ var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
48
49
  function initMiddleware(app, server, serverContext) {
49
50
  app.useCompression();
50
51
  (0, import_locale_middleware.localeMiddleware)(app, serverContext);
@@ -67,10 +68,11 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
67
68
  if (hooks.length) {
68
69
  const skipValidation = true;
69
70
  await (0, import_config.executeConfigHooks)(hooks, appConfig, runtimeEnvironment, globalData, skipValidation);
71
+ (0, import_config.executeInstrumentationHooks)(hooks);
70
72
  (0, import_config.executeStartHooks)(hooks, appConfig);
71
73
  }
72
74
  const services = await (0, import_modules.loadServices)(appConfig);
73
- const serverContext = (0, import_server2.createServerContext)(appConfig, runtimeEnvironment, globalData);
75
+ const serverContext = (0, import_instrumentation.getTracer)().trace({name: import_instrumentation.CoreSpan.CreateServerContext}, () => (0, import_server2.createServerContext)(appConfig, runtimeEnvironment, globalData));
74
76
  const providerContext = (0, import_provider.createProviderContext)(serverContext);
75
77
  const {moduleRegistry, assetRegistry, resourceRegistry, viewRegistry, moduleBundler} = serverContext;
76
78
  const moduleProviders = createServices(services.moduleProviders, providerContext);
@@ -98,6 +100,7 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
98
100
  var LwrApp = class {
99
101
  constructor(config = {}) {
100
102
  this.initialized = false;
103
+ const span = (0, import_instrumentation.getTracer)().startSpan({name: import_instrumentation.CoreSpan.CreateServer});
101
104
  const {appConfig, runtimeEnvironment, globalData} = (0, import_config.loadConfig)(config);
102
105
  this.config = appConfig;
103
106
  this.runtimeEnvironment = runtimeEnvironment;
@@ -105,6 +108,7 @@ var LwrApp = class {
105
108
  const {basePath} = this.config;
106
109
  this.app = (0, import_server.createInternalServer)(this.config.serverType, {basePath});
107
110
  this.server = this.app.createHttpServer();
111
+ span.end();
108
112
  }
109
113
  setConfig(config) {
110
114
  const {appConfig, runtimeEnvironment, globalData} = (0, import_config.loadConfig)(config);
@@ -116,6 +120,7 @@ var LwrApp = class {
116
120
  return this.config;
117
121
  }
118
122
  async init() {
123
+ const span = (0, import_instrumentation.getTracer)().startSpan({name: import_instrumentation.CoreSpan.InitServer});
119
124
  if (this.initialized) {
120
125
  return;
121
126
  }
@@ -127,8 +132,10 @@ var LwrApp = class {
127
132
  this.initialized = false;
128
133
  throw e;
129
134
  }
135
+ span.end();
130
136
  }
131
137
  async listen(callback) {
138
+ const span = (0, import_instrumentation.getTracer)().startSpan({name: import_instrumentation.CoreSpan.StartServer});
132
139
  await this.init();
133
140
  const {server, config, app} = this;
134
141
  const {serverMode, port} = config;
@@ -155,6 +162,7 @@ var LwrApp = class {
155
162
  }
156
163
  const opts = {serverMode, port};
157
164
  callback?.(opts);
165
+ span.end();
158
166
  resolve(opts);
159
167
  });
160
168
  });
@@ -29,45 +29,63 @@ __export(exports, {
29
29
  var import_path = __toModule(require("path"));
30
30
  var import_identity = __toModule(require("./utils/identity.cjs"));
31
31
  var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
32
+ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
32
33
  function createAssetMiddleware(context) {
33
34
  const {
34
35
  assetRegistry,
35
36
  runtimeEnvironment: {basePath}
36
37
  } = context;
37
- return async (req, res) => {
38
+ return async (req, res, next) => {
38
39
  const {runtimeEnvironment} = req.getRuntimeContext(context.runtimeEnvironment);
39
40
  const {assetId, immutable, signature} = (0, import_identity.getAssetIdentity)(req);
40
41
  if (basePath && !assetId.specifier.startsWith(basePath)) {
41
42
  assetId.specifier = import_path.default.join(basePath, assetId.specifier);
42
43
  }
43
- const assetUri = await assetRegistry.resolveAssetUri(assetId, runtimeEnvironment);
44
- if (assetUri.external) {
45
- res.set({
46
- Location: assetUri.uri,
47
- "cache-control": "public, max-age=60"
48
- });
49
- res.sendStatus(302);
50
- return;
44
+ try {
45
+ const assetUri = await assetRegistry.resolveAssetUri(assetId, runtimeEnvironment);
46
+ if (assetUri.external) {
47
+ res.set({
48
+ Location: assetUri.uri,
49
+ "cache-control": "public, max-age=60"
50
+ });
51
+ res.sendStatus(302);
52
+ return;
53
+ }
54
+ const asset = await assetRegistry.getAsset({...assetId, signature}, runtimeEnvironment, req.isSiteGeneration());
55
+ if (req.isSiteGeneration()) {
56
+ res.setSiteGenerationMetadata({asset});
57
+ }
58
+ if (asset.mime) {
59
+ res.type(asset.mime);
60
+ }
61
+ if (runtimeEnvironment.immutableAssets && immutable) {
62
+ res.setHeader("Cache-control", "public, max-age=12895706, immutable");
63
+ } else if (runtimeEnvironment.immutableAssets) {
64
+ res.setHeader("Cache-control", "public, max-age=60");
65
+ }
66
+ res.status(200).stream(asset.stream());
67
+ } catch (error) {
68
+ if (import_path.default.dirname(assetId.specifier) === (basePath ? basePath : "/") && error instanceof import_diagnostics.DiagnosticsError && error?.diagnostics[0]?.description.category === "lwrUnresolvable/asset") {
69
+ await next();
70
+ return;
71
+ }
72
+ throw error;
51
73
  }
52
- const asset = await assetRegistry.getAsset({...assetId, signature}, runtimeEnvironment, req.isSiteGeneration());
53
- if (req.isSiteGeneration()) {
54
- res.setSiteGenerationMetadata({asset});
55
- }
56
- if (asset.mime) {
57
- res.type(asset.mime);
58
- }
59
- if (runtimeEnvironment.immutableAssets && immutable) {
60
- res.setHeader("Cache-control", "public, max-age=12895706, immutable");
61
- } else if (runtimeEnvironment.immutableAssets) {
62
- res.setHeader("Cache-control", "public, max-age=60");
63
- }
64
- res.status(200).stream(asset.stream());
65
74
  };
66
75
  }
67
76
  function assetMiddleware(app, context) {
68
- const paths = context.appConfig.assets.map((a) => a.urlPath + app.getRegexWildcard());
77
+ const paths = context.appConfig.assets.map((config) => {
78
+ const assetDirConfig = config;
79
+ let urlPath = config.urlPath;
80
+ if (assetDirConfig.root) {
81
+ urlPath = "/:filename";
82
+ } else if (assetDirConfig.dir) {
83
+ urlPath += app.getRegexWildcard();
84
+ }
85
+ return urlPath;
86
+ });
69
87
  app.get([
70
- ...paths,
88
+ ...[...new Set(paths)],
71
89
  "/:apiVersion/:assetType(asset|content-asset)/:immutable?/s/:signature/" + app.getRegexWildcard(),
72
90
  "/:apiVersion/:assetType(asset|content-asset)/:immutable?/" + app.getRegexWildcard()
73
91
  ], (0, import_error_handling.handleErrors)(createAssetMiddleware(context)));
@@ -199,7 +199,8 @@ var SiteGenerator = class {
199
199
  const specifier = resourceDefinition.specifier;
200
200
  const resourceMetadata = {
201
201
  path: decodeURIComponent(url),
202
- mimeType: resourceDefinition.type
202
+ mimeType: resourceDefinition.type,
203
+ inline: resourceDefinition.inline
203
204
  };
204
205
  const siteResources = siteConfig.siteMetadata.getSiteResources();
205
206
  siteResources.resources[specifier] = resourceMetadata;
@@ -395,7 +396,8 @@ var SiteGenerator = class {
395
396
  try {
396
397
  const assetSrcFile = asset.file;
397
398
  const assetSrcDir = asset.dir;
398
- const assetsPath = (0, import_path.join)(outputDir, basePath ? basePath + asset.urlPath : asset.urlPath);
399
+ const urlPath = asset.urlPath;
400
+ const assetsPath = (0, import_path.join)(outputDir, basePath ? basePath + urlPath : urlPath);
399
401
  if (assetSrcDir && import_fs_extra.default.existsSync(assetSrcDir)) {
400
402
  import_fs_extra.default.copySync(assetSrcDir, assetsPath);
401
403
  this.addAssetsToMetadata(assetsPath, siteConfig);
package/build/es/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { getFeatureFlags, DEFAULT_LWR_BOOTSTRAP_CONFIG, logger } from '@lwrjs/shared-utils';
2
2
  import { createInternalServer } from '@lwrjs/server';
3
3
  import { LwrServerError, createSingleDiagnosticError, descriptions } from '@lwrjs/diagnostics';
4
- import { loadConfig, executeConfigHooks, executeStartHooks } from '@lwrjs/config';
4
+ import { loadConfig, executeConfigHooks, executeStartHooks, executeInstrumentationHooks, } from '@lwrjs/config';
5
5
  import { loadHooks, loadServices, loadRouteHandlers } from '@lwrjs/config/modules';
6
6
  import SiteGenerator from './tools/static-generation.js';
7
7
  import { warmupServer } from './tools/server-warmup.js';
@@ -15,6 +15,7 @@ import { assetMiddleware } from './middleware/asset-middleware.js';
15
15
  import { viewMiddleware } from './middleware/view-middleware.js';
16
16
  import { resourceMiddleware } from './middleware/resource-middleware.js';
17
17
  import { hmrMiddleware } from './middleware/hmr-middleware.js';
18
+ import { getTracer, CoreSpan } from '@lwrjs/instrumentation';
18
19
  function initMiddleware(app, server, serverContext) {
19
20
  // all middleware attached AFTER compression will have compressed responses
20
21
  app.useCompression();
@@ -44,12 +45,13 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
44
45
  if (hooks.length) {
45
46
  const skipValidation = true; // skip for config hook, since `executeStartHooks` hook will validate
46
47
  await executeConfigHooks(hooks, appConfig, runtimeEnvironment, globalData, skipValidation);
48
+ executeInstrumentationHooks(hooks);
47
49
  executeStartHooks(hooks, appConfig);
48
50
  }
49
51
  // load all configurable modules
50
52
  const services = await loadServices(appConfig);
51
53
  // create all framework components(ie. registries)
52
- const serverContext = createServerContext(appConfig, runtimeEnvironment, globalData);
54
+ const serverContext = getTracer().trace({ name: CoreSpan.CreateServerContext }, () => createServerContext(appConfig, runtimeEnvironment, globalData));
53
55
  // create public subset of configurations
54
56
  const providerContext = createProviderContext(serverContext);
55
57
  const { moduleRegistry, assetRegistry, resourceRegistry, viewRegistry, moduleBundler } = serverContext;
@@ -87,6 +89,7 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
87
89
  export class LwrApp {
88
90
  constructor(config = {}) {
89
91
  this.initialized = false;
92
+ const span = getTracer().startSpan({ name: CoreSpan.CreateServer });
90
93
  const { appConfig, runtimeEnvironment, globalData } = loadConfig(config);
91
94
  this.config = appConfig;
92
95
  this.runtimeEnvironment = runtimeEnvironment;
@@ -94,6 +97,7 @@ export class LwrApp {
94
97
  const { basePath } = this.config;
95
98
  this.app = createInternalServer(this.config.serverType, { basePath });
96
99
  this.server = this.app.createHttpServer();
100
+ span.end();
97
101
  }
98
102
  setConfig(config) {
99
103
  const { appConfig, runtimeEnvironment, globalData } = loadConfig(config);
@@ -105,6 +109,7 @@ export class LwrApp {
105
109
  return this.config;
106
110
  }
107
111
  async init() {
112
+ const span = getTracer().startSpan({ name: CoreSpan.InitServer });
108
113
  if (this.initialized) {
109
114
  return;
110
115
  }
@@ -121,8 +126,10 @@ export class LwrApp {
121
126
  this.initialized = false;
122
127
  throw e;
123
128
  }
129
+ span.end();
124
130
  }
125
131
  async listen(callback) {
132
+ const span = getTracer().startSpan({ name: CoreSpan.StartServer });
126
133
  await this.init();
127
134
  const { server, config, app } = this;
128
135
  const { serverMode, port } = config;
@@ -150,6 +157,7 @@ export class LwrApp {
150
157
  }
151
158
  const opts = { serverMode, port };
152
159
  callback?.(opts);
160
+ span.end(); // TODO is this the right tracing for start server?
153
161
  resolve(opts);
154
162
  });
155
163
  });
@@ -1,44 +1,71 @@
1
1
  import path from 'path';
2
2
  import { getAssetIdentity } from './utils/identity.js';
3
3
  import { handleErrors } from './utils/error-handling.js';
4
+ import { DiagnosticsError } from '@lwrjs/diagnostics';
4
5
  function createAssetMiddleware(context) {
5
6
  const { assetRegistry, runtimeEnvironment: { basePath }, } = context;
6
- return async (req, res) => {
7
+ return async (req, res, next) => {
7
8
  const { runtimeEnvironment } = req.getRuntimeContext(context.runtimeEnvironment);
8
9
  const { assetId, immutable, signature } = getAssetIdentity(req);
9
10
  if (basePath && !assetId.specifier.startsWith(basePath)) {
10
11
  assetId.specifier = path.join(basePath, assetId.specifier);
11
12
  }
12
- // Redirect if this is an external asset
13
- const assetUri = await assetRegistry.resolveAssetUri(assetId, runtimeEnvironment);
14
- if (assetUri.external) {
15
- res.set({
16
- Location: assetUri.uri,
17
- 'cache-control': 'public, max-age=60',
18
- });
19
- res.sendStatus(302);
20
- return;
13
+ try {
14
+ // Redirect if this is an external asset
15
+ const assetUri = await assetRegistry.resolveAssetUri(assetId, runtimeEnvironment);
16
+ if (assetUri.external) {
17
+ res.set({
18
+ Location: assetUri.uri,
19
+ 'cache-control': 'public, max-age=60',
20
+ });
21
+ res.sendStatus(302);
22
+ return;
23
+ }
24
+ const asset = await assetRegistry.getAsset({ ...assetId, signature }, runtimeEnvironment, req.isSiteGeneration());
25
+ if (req.isSiteGeneration()) {
26
+ res.setSiteGenerationMetadata({ asset });
27
+ }
28
+ if (asset.mime) {
29
+ res.type(asset.mime);
30
+ }
31
+ if (runtimeEnvironment.immutableAssets && immutable) {
32
+ res.setHeader('Cache-control', 'public, max-age=12895706, immutable');
33
+ }
34
+ else if (runtimeEnvironment.immutableAssets) {
35
+ res.setHeader('Cache-control', 'public, max-age=60');
36
+ }
37
+ res.status(200).stream(asset.stream());
21
38
  }
22
- const asset = await assetRegistry.getAsset({ ...assetId, signature }, runtimeEnvironment, req.isSiteGeneration());
23
- if (req.isSiteGeneration()) {
24
- res.setSiteGenerationMetadata({ asset });
39
+ catch (error) {
40
+ // Ignore asset not found for root asset requests
41
+ if (path.dirname(assetId.specifier) === (basePath ? basePath : '/') &&
42
+ error instanceof DiagnosticsError &&
43
+ error?.diagnostics[0]?.description.category === 'lwrUnresolvable/asset') {
44
+ // Must await other middleware or this can respond before expected.
45
+ await next();
46
+ return;
47
+ }
48
+ // else throw the error
49
+ throw error;
25
50
  }
26
- if (asset.mime) {
27
- res.type(asset.mime);
28
- }
29
- if (runtimeEnvironment.immutableAssets && immutable) {
30
- res.setHeader('Cache-control', 'public, max-age=12895706, immutable');
31
- }
32
- else if (runtimeEnvironment.immutableAssets) {
33
- res.setHeader('Cache-control', 'public, max-age=60');
34
- }
35
- res.status(200).stream(asset.stream());
36
51
  };
37
52
  }
38
53
  export function assetMiddleware(app, context) {
39
- const paths = context.appConfig.assets.map((a) => a.urlPath + app.getRegexWildcard());
54
+ const paths = context.appConfig.assets.map((config) => {
55
+ const assetDirConfig = config;
56
+ let urlPath = config.urlPath;
57
+ // 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.
58
+ if (assetDirConfig.root) {
59
+ urlPath = '/:filename';
60
+ }
61
+ else if (assetDirConfig.dir) {
62
+ urlPath += app.getRegexWildcard();
63
+ }
64
+ return urlPath;
65
+ });
40
66
  app.get([
41
- ...paths,
67
+ // De-dupe paths (i.e. root path may have been added more than once)
68
+ ...[...new Set(paths)],
42
69
  '/:apiVersion/:assetType(asset|content-asset)/:immutable?/s/:signature/' + app.getRegexWildcard(),
43
70
  '/:apiVersion/:assetType(asset|content-asset)/:immutable?/' + app.getRegexWildcard(),
44
71
  ], handleErrors(createAssetMiddleware(context)));
@@ -255,6 +255,7 @@ export default class SiteGenerator {
255
255
  const resourceMetadata = {
256
256
  path: decodeURIComponent(url),
257
257
  mimeType: resourceDefinition.type,
258
+ inline: resourceDefinition.inline,
258
259
  };
259
260
  const siteResources = siteConfig.siteMetadata.getSiteResources();
260
261
  siteResources.resources[specifier] = resourceMetadata;
@@ -553,7 +554,8 @@ export default class SiteGenerator {
553
554
  try {
554
555
  const assetSrcFile = asset.file;
555
556
  const assetSrcDir = asset.dir;
556
- const assetsPath = join(outputDir, basePath ? basePath + asset.urlPath : asset.urlPath);
557
+ const urlPath = asset.urlPath;
558
+ const assetsPath = join(outputDir, basePath ? basePath + urlPath : urlPath);
557
559
  if (assetSrcDir && fs.existsSync(assetSrcDir)) {
558
560
  fs.copySync(assetSrcDir, assetsPath);
559
561
  this.addAssetsToMetadata(assetsPath, siteConfig);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
7
- "version": "0.10.0-alpha.19",
7
+ "version": "0.10.0-alpha.20",
8
8
  "homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
9
9
  "repository": {
10
10
  "type": "git",
@@ -40,30 +40,31 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@locker/compiler": "0.19.5",
43
- "@lwrjs/app-service": "0.10.0-alpha.19",
44
- "@lwrjs/asset-registry": "0.10.0-alpha.19",
45
- "@lwrjs/asset-transformer": "0.10.0-alpha.19",
46
- "@lwrjs/base-view-provider": "0.10.0-alpha.19",
47
- "@lwrjs/base-view-transformer": "0.10.0-alpha.19",
48
- "@lwrjs/client-modules": "0.10.0-alpha.19",
49
- "@lwrjs/config": "0.10.0-alpha.19",
50
- "@lwrjs/diagnostics": "0.10.0-alpha.19",
51
- "@lwrjs/fs-asset-provider": "0.10.0-alpha.19",
52
- "@lwrjs/html-view-provider": "0.10.0-alpha.19",
53
- "@lwrjs/loader": "0.10.0-alpha.19",
54
- "@lwrjs/lwc-module-provider": "0.10.0-alpha.19",
55
- "@lwrjs/markdown-view-provider": "0.10.0-alpha.19",
56
- "@lwrjs/module-bundler": "0.10.0-alpha.19",
57
- "@lwrjs/module-registry": "0.10.0-alpha.19",
58
- "@lwrjs/npm-module-provider": "0.10.0-alpha.19",
59
- "@lwrjs/nunjucks-view-provider": "0.10.0-alpha.19",
60
- "@lwrjs/o11y": "0.10.0-alpha.19",
61
- "@lwrjs/resource-registry": "0.10.0-alpha.19",
62
- "@lwrjs/router": "0.10.0-alpha.19",
63
- "@lwrjs/server": "0.10.0-alpha.19",
64
- "@lwrjs/shared-utils": "0.10.0-alpha.19",
65
- "@lwrjs/static": "0.10.0-alpha.19",
66
- "@lwrjs/view-registry": "0.10.0-alpha.19",
43
+ "@lwrjs/app-service": "0.10.0-alpha.20",
44
+ "@lwrjs/asset-registry": "0.10.0-alpha.20",
45
+ "@lwrjs/asset-transformer": "0.10.0-alpha.20",
46
+ "@lwrjs/base-view-provider": "0.10.0-alpha.20",
47
+ "@lwrjs/base-view-transformer": "0.10.0-alpha.20",
48
+ "@lwrjs/client-modules": "0.10.0-alpha.20",
49
+ "@lwrjs/config": "0.10.0-alpha.20",
50
+ "@lwrjs/diagnostics": "0.10.0-alpha.20",
51
+ "@lwrjs/fs-asset-provider": "0.10.0-alpha.20",
52
+ "@lwrjs/html-view-provider": "0.10.0-alpha.20",
53
+ "@lwrjs/instrumentation": "0.10.0-alpha.20",
54
+ "@lwrjs/loader": "0.10.0-alpha.20",
55
+ "@lwrjs/lwc-module-provider": "0.10.0-alpha.20",
56
+ "@lwrjs/markdown-view-provider": "0.10.0-alpha.20",
57
+ "@lwrjs/module-bundler": "0.10.0-alpha.20",
58
+ "@lwrjs/module-registry": "0.10.0-alpha.20",
59
+ "@lwrjs/npm-module-provider": "0.10.0-alpha.20",
60
+ "@lwrjs/nunjucks-view-provider": "0.10.0-alpha.20",
61
+ "@lwrjs/o11y": "0.10.0-alpha.20",
62
+ "@lwrjs/resource-registry": "0.10.0-alpha.20",
63
+ "@lwrjs/router": "0.10.0-alpha.20",
64
+ "@lwrjs/server": "0.10.0-alpha.20",
65
+ "@lwrjs/shared-utils": "0.10.0-alpha.20",
66
+ "@lwrjs/static": "0.10.0-alpha.20",
67
+ "@lwrjs/view-registry": "0.10.0-alpha.20",
67
68
  "chokidar": "^3.5.3",
68
69
  "esbuild": "^0.9.7",
69
70
  "fs-extra": "^11.1.1",
@@ -73,7 +74,7 @@
73
74
  "ws": "^8.8.1"
74
75
  },
75
76
  "devDependencies": {
76
- "@lwrjs/types": "0.10.0-alpha.19",
77
+ "@lwrjs/types": "0.10.0-alpha.20",
77
78
  "@types/ws": "^8.5.3"
78
79
  },
79
80
  "peerDependencies": {
@@ -85,5 +86,5 @@
85
86
  "volta": {
86
87
  "extends": "../../../package.json"
87
88
  },
88
- "gitHead": "0bd55139c08ddf5a532978cfa9e037eaf27e78e2"
89
+ "gitHead": "45a867fc54e98f77dd442ccd5f668e23027b9246"
89
90
  }