@lwrjs/core 0.12.0-alpha.12 → 0.12.0-alpha.14

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.
@@ -102,24 +102,23 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
102
102
  return serverContext;
103
103
  }
104
104
  var LwrApp = class {
105
- constructor(config = {}) {
105
+ constructor(configs) {
106
106
  this.initialized = false;
107
107
  const span = (0, import_instrumentation.getTracer)().startSpan({name: import_instrumentation.CoreSpan.CreateServer});
108
- const {appConfig, runtimeEnvironment, globalData} = (0, import_config.loadConfig)(config);
108
+ const {appConfig, runtimeEnvironment, globalData} = configs;
109
109
  this.config = appConfig;
110
110
  this.runtimeEnvironment = runtimeEnvironment;
111
111
  this.globalData = globalData;
112
- const {basePath} = this.config;
113
- this.app = (0, import_server.createInternalServer)(this.config.serverType, {basePath});
112
+ const {basePath, serverType} = this.config;
113
+ this.serverType = serverType;
114
+ this.app = (0, import_server.createInternalServer)(serverType, {basePath});
114
115
  this.server = this.app.createHttpServer();
116
+ this.use = this.app.use.bind(this.app);
117
+ this.all = this.app.all.bind(this.app);
118
+ this.get = this.app.get.bind(this.app);
119
+ this.post = this.app.post.bind(this.app);
115
120
  span.end();
116
121
  }
117
- setConfig(config) {
118
- const {appConfig, runtimeEnvironment, globalData} = (0, import_config.loadConfig)(config);
119
- this.config = appConfig;
120
- this.runtimeEnvironment = runtimeEnvironment;
121
- this.globalData = globalData;
122
- }
123
122
  getConfig() {
124
123
  return this.config;
125
124
  }
@@ -171,24 +170,16 @@ var LwrApp = class {
171
170
  });
172
171
  });
173
172
  }
174
- async close() {
175
- this.server?.close && await this.server.close();
173
+ close() {
174
+ this.server?.close && this.server.close();
176
175
  }
177
176
  getInternalServer() {
178
177
  return this.app.getImpl();
179
178
  }
180
- getServer() {
181
- return {
182
- use: this.app.use.bind(this.app),
183
- all: this.app.all.bind(this.app),
184
- get: this.app.get.bind(this.app),
185
- post: this.app.post.bind(this.app),
186
- getRegexWildcard: this.app.getRegexWildcard.bind(this.app)
187
- };
188
- }
189
179
  };
190
180
  function createServer(config) {
191
- return new LwrApp(config);
181
+ const configs = (0, import_config.loadConfig)(config);
182
+ return new LwrApp(configs);
192
183
  }
193
184
  async function generateStaticSite(config) {
194
185
  config = config || {};
@@ -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);
@@ -1,4 +1,4 @@
1
- import type { LwrGlobalConfig, NormalizedLwrGlobalConfig, ServerTypeImpl, PublicAppServer, ServerTypes } from '@lwrjs/types';
1
+ import type { LwrGlobalConfig, NormalizedLwrGlobalConfig, ServerTypeImpl, ServerTypes, RuntimeEnvironment, GlobalData, MiddlewareFunction } from '@lwrjs/types';
2
2
  export declare class LwrApp {
3
3
  private app;
4
4
  private server;
@@ -6,8 +6,16 @@ export declare class LwrApp {
6
6
  private config;
7
7
  private runtimeEnvironment;
8
8
  private globalData;
9
- constructor(config?: LwrGlobalConfig);
10
- setConfig(config: LwrGlobalConfig): void;
9
+ serverType: ServerTypes;
10
+ use: (m: MiddlewareFunction) => void;
11
+ all: (path: string | string[], m: MiddlewareFunction) => void;
12
+ get: (path: string | string[], m: MiddlewareFunction) => void;
13
+ post: (path: string | string[], m: MiddlewareFunction) => void;
14
+ constructor(configs: {
15
+ appConfig: NormalizedLwrGlobalConfig;
16
+ runtimeEnvironment: RuntimeEnvironment;
17
+ globalData: GlobalData;
18
+ });
11
19
  getConfig(): NormalizedLwrGlobalConfig;
12
20
  init(): Promise<void>;
13
21
  listen(callback?: ((opts: {
@@ -17,9 +25,8 @@ export declare class LwrApp {
17
25
  serverMode: string;
18
26
  port: number;
19
27
  }>;
20
- close(): Promise<void>;
28
+ close(): void;
21
29
  getInternalServer<S extends ServerTypes>(): ServerTypeImpl<S>;
22
- getServer(): PublicAppServer<ServerTypes>;
23
30
  }
24
31
  export declare function createServer(config?: LwrGlobalConfig): LwrApp;
25
32
  export declare function generateStaticSite(config?: LwrGlobalConfig): Promise<NormalizedLwrGlobalConfig>;
package/build/es/index.js CHANGED
@@ -91,24 +91,23 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
91
91
  return serverContext;
92
92
  }
93
93
  export class LwrApp {
94
- constructor(config = {}) {
94
+ constructor(configs) {
95
95
  this.initialized = false;
96
96
  const span = getTracer().startSpan({ name: CoreSpan.CreateServer });
97
- const { appConfig, runtimeEnvironment, globalData } = loadConfig(config);
97
+ const { appConfig, runtimeEnvironment, globalData } = configs;
98
98
  this.config = appConfig;
99
99
  this.runtimeEnvironment = runtimeEnvironment;
100
100
  this.globalData = globalData;
101
- const { basePath } = this.config;
102
- this.app = createInternalServer(this.config.serverType, { basePath });
101
+ const { basePath, serverType } = this.config;
102
+ this.serverType = serverType;
103
+ this.app = createInternalServer(serverType, { basePath });
103
104
  this.server = this.app.createHttpServer();
105
+ this.use = this.app.use.bind(this.app);
106
+ this.all = this.app.all.bind(this.app);
107
+ this.get = this.app.get.bind(this.app);
108
+ this.post = this.app.post.bind(this.app);
104
109
  span.end();
105
110
  }
106
- setConfig(config) {
107
- const { appConfig, runtimeEnvironment, globalData } = loadConfig(config);
108
- this.config = appConfig;
109
- this.runtimeEnvironment = runtimeEnvironment;
110
- this.globalData = globalData;
111
- }
112
111
  getConfig() {
113
112
  return this.config;
114
113
  }
@@ -166,26 +165,17 @@ export class LwrApp {
166
165
  });
167
166
  });
168
167
  }
169
- async close() {
170
- this.server?.close && (await this.server.close());
168
+ close() {
169
+ this.server?.close && this.server.close();
171
170
  }
172
171
  // Get the underlying server (e.g. express, koa...)
173
172
  getInternalServer() {
174
173
  return this.app.getImpl();
175
174
  }
176
- // Return the public server interface which is compatible with all server types
177
- getServer() {
178
- return {
179
- use: this.app.use.bind(this.app),
180
- all: this.app.all.bind(this.app),
181
- get: this.app.get.bind(this.app),
182
- post: this.app.post.bind(this.app),
183
- getRegexWildcard: this.app.getRegexWildcard.bind(this.app),
184
- };
185
- }
186
175
  }
187
176
  export function createServer(config) {
188
- return new LwrApp(config);
177
+ const configs = loadConfig(config);
178
+ return new LwrApp(configs);
189
179
  }
190
180
  export async function generateStaticSite(config) {
191
181
  config = config || {};
@@ -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.12",
7
+ "version": "0.12.0-alpha.14",
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.12",
47
- "@lwrjs/asset-registry": "0.12.0-alpha.12",
48
- "@lwrjs/asset-transformer": "0.12.0-alpha.12",
49
- "@lwrjs/base-view-provider": "0.12.0-alpha.12",
50
- "@lwrjs/base-view-transformer": "0.12.0-alpha.12",
51
- "@lwrjs/client-modules": "0.12.0-alpha.12",
52
- "@lwrjs/config": "0.12.0-alpha.12",
53
- "@lwrjs/diagnostics": "0.12.0-alpha.12",
54
- "@lwrjs/esbuild": "0.12.0-alpha.12",
55
- "@lwrjs/fs-asset-provider": "0.12.0-alpha.12",
56
- "@lwrjs/fs-watch": "0.12.0-alpha.12",
57
- "@lwrjs/html-view-provider": "0.12.0-alpha.12",
58
- "@lwrjs/instrumentation": "0.12.0-alpha.12",
59
- "@lwrjs/loader": "0.12.0-alpha.12",
60
- "@lwrjs/lwc-module-provider": "0.12.0-alpha.12",
61
- "@lwrjs/lwc-ssr": "0.12.0-alpha.12",
62
- "@lwrjs/markdown-view-provider": "0.12.0-alpha.12",
63
- "@lwrjs/module-bundler": "0.12.0-alpha.12",
64
- "@lwrjs/module-registry": "0.12.0-alpha.12",
65
- "@lwrjs/npm-module-provider": "0.12.0-alpha.12",
66
- "@lwrjs/nunjucks-view-provider": "0.12.0-alpha.12",
67
- "@lwrjs/o11y": "0.12.0-alpha.12",
68
- "@lwrjs/resource-registry": "0.12.0-alpha.12",
69
- "@lwrjs/router": "0.12.0-alpha.12",
70
- "@lwrjs/server": "0.12.0-alpha.12",
71
- "@lwrjs/shared-utils": "0.12.0-alpha.12",
72
- "@lwrjs/static": "0.12.0-alpha.12",
73
- "@lwrjs/view-registry": "0.12.0-alpha.12",
46
+ "@lwrjs/app-service": "0.12.0-alpha.14",
47
+ "@lwrjs/asset-registry": "0.12.0-alpha.14",
48
+ "@lwrjs/asset-transformer": "0.12.0-alpha.14",
49
+ "@lwrjs/base-view-provider": "0.12.0-alpha.14",
50
+ "@lwrjs/base-view-transformer": "0.12.0-alpha.14",
51
+ "@lwrjs/client-modules": "0.12.0-alpha.14",
52
+ "@lwrjs/config": "0.12.0-alpha.14",
53
+ "@lwrjs/diagnostics": "0.12.0-alpha.14",
54
+ "@lwrjs/esbuild": "0.12.0-alpha.14",
55
+ "@lwrjs/fs-asset-provider": "0.12.0-alpha.14",
56
+ "@lwrjs/fs-watch": "0.12.0-alpha.14",
57
+ "@lwrjs/html-view-provider": "0.12.0-alpha.14",
58
+ "@lwrjs/instrumentation": "0.12.0-alpha.14",
59
+ "@lwrjs/loader": "0.12.0-alpha.14",
60
+ "@lwrjs/lwc-module-provider": "0.12.0-alpha.14",
61
+ "@lwrjs/lwc-ssr": "0.12.0-alpha.14",
62
+ "@lwrjs/markdown-view-provider": "0.12.0-alpha.14",
63
+ "@lwrjs/module-bundler": "0.12.0-alpha.14",
64
+ "@lwrjs/module-registry": "0.12.0-alpha.14",
65
+ "@lwrjs/npm-module-provider": "0.12.0-alpha.14",
66
+ "@lwrjs/nunjucks-view-provider": "0.12.0-alpha.14",
67
+ "@lwrjs/o11y": "0.12.0-alpha.14",
68
+ "@lwrjs/resource-registry": "0.12.0-alpha.14",
69
+ "@lwrjs/router": "0.12.0-alpha.14",
70
+ "@lwrjs/server": "0.12.0-alpha.14",
71
+ "@lwrjs/shared-utils": "0.12.0-alpha.14",
72
+ "@lwrjs/static": "0.12.0-alpha.14",
73
+ "@lwrjs/view-registry": "0.12.0-alpha.14",
74
74
  "chokidar": "^3.5.3",
75
75
  "esbuild": "^0.9.7",
76
76
  "fs-extra": "^11.1.1",
@@ -80,8 +80,9 @@
80
80
  "ws": "^8.8.1"
81
81
  },
82
82
  "devDependencies": {
83
- "@lwrjs/types": "0.12.0-alpha.12",
84
- "@types/ws": "^8.5.3"
83
+ "@lwrjs/types": "0.12.0-alpha.14",
84
+ "@types/ws": "^8.5.3",
85
+ "mock-fs": "^5.2.0"
85
86
  },
86
87
  "peerDependencies": {
87
88
  "lwc": ">= 2.x"
@@ -92,5 +93,5 @@
92
93
  "volta": {
93
94
  "extends": "../../../package.json"
94
95
  },
95
- "gitHead": "bc8a88bce246e9ec00641955152de526ea030ebb"
96
+ "gitHead": "ef27c4a463498992b66b7b668e7be6e318f9c8a1"
96
97
  }