@lwrjs/core 0.15.0-alpha.28 → 0.15.0-alpha.29
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.
|
@@ -24,7 +24,6 @@ var __toModule = (module2) => {
|
|
|
24
24
|
// packages/@lwrjs/core/src/middleware/view-middleware.ts
|
|
25
25
|
__markAsModule(exports);
|
|
26
26
|
__export(exports, {
|
|
27
|
-
convertMetricsToServerTiming: () => convertMetricsToServerTiming,
|
|
28
27
|
viewMiddleware: () => viewMiddleware
|
|
29
28
|
});
|
|
30
29
|
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
@@ -33,7 +32,7 @@ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
|
33
32
|
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
34
33
|
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
35
34
|
var import_view_registry = __toModule(require("@lwrjs/view-registry"));
|
|
36
|
-
var
|
|
35
|
+
var import_instrumentation2 = __toModule(require("@lwrjs/instrumentation"));
|
|
37
36
|
function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
38
37
|
const errorRoute = errorRoutes.find((route2) => route2.status === 500);
|
|
39
38
|
const appConfig = context.appConfig;
|
|
@@ -72,7 +71,6 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
72
71
|
let viewResponse;
|
|
73
72
|
let resolvedRoute;
|
|
74
73
|
let traceId;
|
|
75
|
-
let reqSpan;
|
|
76
74
|
try {
|
|
77
75
|
viewResponse = await (0, import_instrumentation.getTracer)().trace({
|
|
78
76
|
name: import_instrumentation.RequestHandlerSpan.GetView,
|
|
@@ -85,7 +83,6 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
85
83
|
}
|
|
86
84
|
}, (span) => {
|
|
87
85
|
traceId = span.traceId;
|
|
88
|
-
reqSpan = span.reqSpan;
|
|
89
86
|
return resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
90
87
|
});
|
|
91
88
|
resolvedRoute = route;
|
|
@@ -104,6 +101,14 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
104
101
|
return resolve.call(viewHandler, viewRequest, errorRoute, runtimeEnvironment, runtimeParams);
|
|
105
102
|
});
|
|
106
103
|
resolvedRoute = errorRoute;
|
|
104
|
+
} finally {
|
|
105
|
+
if (traceId?.length) {
|
|
106
|
+
res.setHeader("x-trace-id", traceId);
|
|
107
|
+
res.set({
|
|
108
|
+
"Server-Timing": (0, import_instrumentation2.getTraceCollector)().getSpansInTrace(traceId)
|
|
109
|
+
});
|
|
110
|
+
(0, import_instrumentation2.getTraceCollector)().dropTrace(traceId);
|
|
111
|
+
}
|
|
107
112
|
}
|
|
108
113
|
if (req.isSiteGeneration()) {
|
|
109
114
|
res.setSiteGenerationMetadata(viewResponse.metadata);
|
|
@@ -118,14 +123,6 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
118
123
|
res.setHeader("cache-control", `public, max-age=${cacheTtl}`);
|
|
119
124
|
}
|
|
120
125
|
}
|
|
121
|
-
if (traceId?.length) {
|
|
122
|
-
res.setHeader("x-trace-id", traceId);
|
|
123
|
-
if (reqSpan?.status?.code === 0) {
|
|
124
|
-
res.set({
|
|
125
|
-
"Server-Timing": convertMetricsToServerTiming((0, import_aws_exporter.convertToReadableSpan)(reqSpan))
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
126
|
let status = resolvedRoute.status || viewResponse.status || 200;
|
|
130
127
|
const redirect = viewResponse.metadata?.viewDefinition?.redirect;
|
|
131
128
|
if (viewResponse.status === 301 || viewResponse.status === 302) {
|
|
@@ -224,7 +221,7 @@ function viewMiddleware(app, context) {
|
|
|
224
221
|
const localizedPath = `/:locale(${supportedStr})${routePath}`;
|
|
225
222
|
paths.push(localizedPath);
|
|
226
223
|
});
|
|
227
|
-
addDefaultLocaleRedirects(i18n.defaultLocale, defaultLocalePaths, app);
|
|
224
|
+
addDefaultLocaleRedirects(i18n.defaultLocale, defaultLocalePaths, i18n.defaultRedirectParams, app);
|
|
228
225
|
}
|
|
229
226
|
}
|
|
230
227
|
import_diagnostics.logger.debug({label: `view-middleware`, message: `Add view paths ${paths}`});
|
|
@@ -233,13 +230,18 @@ function viewMiddleware(app, context) {
|
|
|
233
230
|
app.get((0, import_shared_utils.getClientBootstrapConfigurationRoutes)(), (0, import_error_handling.handleErrors)(createConfigMiddleware(routes, context, viewHandler)));
|
|
234
231
|
app.get("/" + app.getRegexWildcard(), (0, import_error_handling.handleErrors)(createNotFoundMiddleware(errorRoutes, context, viewHandler)));
|
|
235
232
|
}
|
|
236
|
-
function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, app) {
|
|
233
|
+
function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, defaultRedirectParams, app) {
|
|
237
234
|
import_diagnostics.logger.debug({label: `view-middleware`, message: `Add default localized paths ${defaultLocalePaths}`});
|
|
238
235
|
app.get(defaultLocalePaths, (req, res) => {
|
|
239
236
|
let modifiedPath = req.originalUrl.replace(`/${defaultLocale}`, "");
|
|
240
237
|
if (modifiedPath.indexOf("/") !== 0) {
|
|
241
238
|
modifiedPath = `/${modifiedPath}`;
|
|
242
239
|
}
|
|
240
|
+
if (defaultRedirectParams) {
|
|
241
|
+
Object.entries(defaultRedirectParams).forEach(([key, value]) => {
|
|
242
|
+
modifiedPath += `${modifiedPath.includes("?") ? "&" : "?"}${key}=${value}`;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
243
245
|
res.setHeader("Location", modifiedPath);
|
|
244
246
|
return res.sendStatus(301);
|
|
245
247
|
});
|
|
@@ -250,7 +252,3 @@ function addRedirectQueryParam(redirectUrl, depth) {
|
|
|
250
252
|
url.searchParams.set(import_shared_utils.REQUEST_DEPTH_KEY, String(depth + 1));
|
|
251
253
|
return url.toString().replace(fakeOrigin, "");
|
|
252
254
|
}
|
|
253
|
-
function convertMetricsToServerTiming(span) {
|
|
254
|
-
const {name, duration} = span;
|
|
255
|
-
return `${name};dur=${duration.toFixed(2)}`;
|
|
256
|
-
}
|
|
@@ -1,4 +1,3 @@
|
|
|
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 convertMetricsToServerTiming(span: Record<string, any>): string;
|
|
4
3
|
//# sourceMappingURL=view-middleware.d.ts.map
|
|
@@ -4,7 +4,7 @@ import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRo
|
|
|
4
4
|
import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
|
|
5
5
|
import { handleErrors } from './utils/error-handling.js';
|
|
6
6
|
import { LwrViewHandler } from '@lwrjs/view-registry';
|
|
7
|
-
import {
|
|
7
|
+
import { getTraceCollector } from '@lwrjs/instrumentation';
|
|
8
8
|
function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
9
9
|
const errorRoute = errorRoutes.find((route) => route.status === 500);
|
|
10
10
|
const appConfig = context.appConfig;
|
|
@@ -45,7 +45,6 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
45
45
|
let viewResponse;
|
|
46
46
|
let resolvedRoute;
|
|
47
47
|
let traceId;
|
|
48
|
-
let reqSpan;
|
|
49
48
|
try {
|
|
50
49
|
viewResponse = await getTracer().trace({
|
|
51
50
|
name: RequestHandlerSpan.GetView,
|
|
@@ -58,7 +57,6 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
58
57
|
},
|
|
59
58
|
}, (span) => {
|
|
60
59
|
traceId = span.traceId;
|
|
61
|
-
reqSpan = span.reqSpan;
|
|
62
60
|
return resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
63
61
|
});
|
|
64
62
|
resolvedRoute = route;
|
|
@@ -80,6 +78,15 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
80
78
|
});
|
|
81
79
|
resolvedRoute = errorRoute;
|
|
82
80
|
}
|
|
81
|
+
finally {
|
|
82
|
+
if (traceId?.length) {
|
|
83
|
+
res.setHeader('x-trace-id', traceId);
|
|
84
|
+
res.set({
|
|
85
|
+
'Server-Timing': getTraceCollector().getSpansInTrace(traceId),
|
|
86
|
+
});
|
|
87
|
+
getTraceCollector().dropTrace(traceId);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
83
90
|
if (req.isSiteGeneration()) {
|
|
84
91
|
res.setSiteGenerationMetadata(viewResponse.metadata);
|
|
85
92
|
}
|
|
@@ -96,14 +103,6 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
96
103
|
res.setHeader('cache-control', `public, max-age=${cacheTtl}`);
|
|
97
104
|
}
|
|
98
105
|
}
|
|
99
|
-
if (traceId?.length) {
|
|
100
|
-
res.setHeader('x-trace-id', traceId);
|
|
101
|
-
if (reqSpan?.status?.code === 0) {
|
|
102
|
-
res.set({
|
|
103
|
-
'Server-Timing': convertMetricsToServerTiming(convertToReadableSpan(reqSpan)),
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
106
|
let status = resolvedRoute.status || viewResponse.status || 200;
|
|
108
107
|
const redirect = viewResponse.metadata?.viewDefinition?.redirect;
|
|
109
108
|
if (viewResponse.status === 301 || viewResponse.status === 302) {
|
|
@@ -220,7 +219,7 @@ export function viewMiddleware(app, context) {
|
|
|
220
219
|
const localizedPath = `/:locale(${supportedStr})${routePath}`;
|
|
221
220
|
paths.push(localizedPath);
|
|
222
221
|
});
|
|
223
|
-
addDefaultLocaleRedirects(i18n.defaultLocale, defaultLocalePaths, app);
|
|
222
|
+
addDefaultLocaleRedirects(i18n.defaultLocale, defaultLocalePaths, i18n.defaultRedirectParams, app);
|
|
224
223
|
}
|
|
225
224
|
}
|
|
226
225
|
logger.debug({ label: `view-middleware`, message: `Add view paths ${paths}` });
|
|
@@ -235,7 +234,7 @@ export function viewMiddleware(app, context) {
|
|
|
235
234
|
/**
|
|
236
235
|
* Adds a 301 redirect if attempting to route with default locale as the path prefix
|
|
237
236
|
*/
|
|
238
|
-
function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, app) {
|
|
237
|
+
function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, defaultRedirectParams, app) {
|
|
239
238
|
logger.debug({ label: `view-middleware`, message: `Add default localized paths ${defaultLocalePaths}` });
|
|
240
239
|
app.get(defaultLocalePaths, (req, res) => {
|
|
241
240
|
// Get the original URL path and split it into segments
|
|
@@ -244,6 +243,11 @@ function addDefaultLocaleRedirects(defaultLocale, defaultLocalePaths, app) {
|
|
|
244
243
|
if (modifiedPath.indexOf('/') !== 0) {
|
|
245
244
|
modifiedPath = `/${modifiedPath}`;
|
|
246
245
|
}
|
|
246
|
+
if (defaultRedirectParams) {
|
|
247
|
+
Object.entries(defaultRedirectParams).forEach(([key, value]) => {
|
|
248
|
+
modifiedPath += `${modifiedPath.includes('?') ? '&' : '?'}${key}=${value}`;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
247
251
|
// Perform a 301 redirect to the modified URL
|
|
248
252
|
res.setHeader('Location', modifiedPath);
|
|
249
253
|
return res.sendStatus(301);
|
|
@@ -257,8 +261,4 @@ function addRedirectQueryParam(redirectUrl, depth) {
|
|
|
257
261
|
url.searchParams.set(REQUEST_DEPTH_KEY, String(depth + 1));
|
|
258
262
|
return url.toString().replace(fakeOrigin, '');
|
|
259
263
|
}
|
|
260
|
-
export function convertMetricsToServerTiming(span) {
|
|
261
|
-
const { name, duration } = span;
|
|
262
|
-
return `${name};dur=${duration.toFixed(2)}`;
|
|
263
|
-
}
|
|
264
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.
|
|
7
|
+
"version": "0.15.0-alpha.29",
|
|
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.
|
|
47
|
-
"@lwrjs/asset-registry": "0.15.0-alpha.
|
|
48
|
-
"@lwrjs/asset-transformer": "0.15.0-alpha.
|
|
49
|
-
"@lwrjs/base-view-provider": "0.15.0-alpha.
|
|
50
|
-
"@lwrjs/base-view-transformer": "0.15.0-alpha.
|
|
51
|
-
"@lwrjs/client-modules": "0.15.0-alpha.
|
|
52
|
-
"@lwrjs/config": "0.15.0-alpha.
|
|
53
|
-
"@lwrjs/diagnostics": "0.15.0-alpha.
|
|
54
|
-
"@lwrjs/esbuild": "0.15.0-alpha.
|
|
55
|
-
"@lwrjs/fs-asset-provider": "0.15.0-alpha.
|
|
56
|
-
"@lwrjs/fs-watch": "0.15.0-alpha.
|
|
57
|
-
"@lwrjs/html-view-provider": "0.15.0-alpha.
|
|
58
|
-
"@lwrjs/instrumentation": "0.15.0-alpha.
|
|
59
|
-
"@lwrjs/loader": "0.15.0-alpha.
|
|
60
|
-
"@lwrjs/lwc-module-provider": "0.15.0-alpha.
|
|
61
|
-
"@lwrjs/lwc-ssr": "0.15.0-alpha.
|
|
62
|
-
"@lwrjs/markdown-view-provider": "0.15.0-alpha.
|
|
63
|
-
"@lwrjs/module-bundler": "0.15.0-alpha.
|
|
64
|
-
"@lwrjs/module-registry": "0.15.0-alpha.
|
|
65
|
-
"@lwrjs/npm-module-provider": "0.15.0-alpha.
|
|
66
|
-
"@lwrjs/nunjucks-view-provider": "0.15.0-alpha.
|
|
67
|
-
"@lwrjs/o11y": "0.15.0-alpha.
|
|
68
|
-
"@lwrjs/resource-registry": "0.15.0-alpha.
|
|
69
|
-
"@lwrjs/router": "0.15.0-alpha.
|
|
70
|
-
"@lwrjs/server": "0.15.0-alpha.
|
|
71
|
-
"@lwrjs/shared-utils": "0.15.0-alpha.
|
|
72
|
-
"@lwrjs/static": "0.15.0-alpha.
|
|
73
|
-
"@lwrjs/view-registry": "0.15.0-alpha.
|
|
46
|
+
"@lwrjs/app-service": "0.15.0-alpha.29",
|
|
47
|
+
"@lwrjs/asset-registry": "0.15.0-alpha.29",
|
|
48
|
+
"@lwrjs/asset-transformer": "0.15.0-alpha.29",
|
|
49
|
+
"@lwrjs/base-view-provider": "0.15.0-alpha.29",
|
|
50
|
+
"@lwrjs/base-view-transformer": "0.15.0-alpha.29",
|
|
51
|
+
"@lwrjs/client-modules": "0.15.0-alpha.29",
|
|
52
|
+
"@lwrjs/config": "0.15.0-alpha.29",
|
|
53
|
+
"@lwrjs/diagnostics": "0.15.0-alpha.29",
|
|
54
|
+
"@lwrjs/esbuild": "0.15.0-alpha.29",
|
|
55
|
+
"@lwrjs/fs-asset-provider": "0.15.0-alpha.29",
|
|
56
|
+
"@lwrjs/fs-watch": "0.15.0-alpha.29",
|
|
57
|
+
"@lwrjs/html-view-provider": "0.15.0-alpha.29",
|
|
58
|
+
"@lwrjs/instrumentation": "0.15.0-alpha.29",
|
|
59
|
+
"@lwrjs/loader": "0.15.0-alpha.29",
|
|
60
|
+
"@lwrjs/lwc-module-provider": "0.15.0-alpha.29",
|
|
61
|
+
"@lwrjs/lwc-ssr": "0.15.0-alpha.29",
|
|
62
|
+
"@lwrjs/markdown-view-provider": "0.15.0-alpha.29",
|
|
63
|
+
"@lwrjs/module-bundler": "0.15.0-alpha.29",
|
|
64
|
+
"@lwrjs/module-registry": "0.15.0-alpha.29",
|
|
65
|
+
"@lwrjs/npm-module-provider": "0.15.0-alpha.29",
|
|
66
|
+
"@lwrjs/nunjucks-view-provider": "0.15.0-alpha.29",
|
|
67
|
+
"@lwrjs/o11y": "0.15.0-alpha.29",
|
|
68
|
+
"@lwrjs/resource-registry": "0.15.0-alpha.29",
|
|
69
|
+
"@lwrjs/router": "0.15.0-alpha.29",
|
|
70
|
+
"@lwrjs/server": "0.15.0-alpha.29",
|
|
71
|
+
"@lwrjs/shared-utils": "0.15.0-alpha.29",
|
|
72
|
+
"@lwrjs/static": "0.15.0-alpha.29",
|
|
73
|
+
"@lwrjs/view-registry": "0.15.0-alpha.29",
|
|
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.
|
|
83
|
+
"@lwrjs/types": "0.15.0-alpha.29",
|
|
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": "
|
|
96
|
+
"gitHead": "69be18491fa9b68311835d5a957165daf415ea30"
|
|
97
97
|
}
|