@lwrjs/core 0.17.2-alpha.8 → 0.17.2
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.
- package/build/cjs/middleware/request-processor-middleware.cjs +10 -5
- package/build/cjs/middleware/utils/error-handling.cjs +6 -6
- package/build/cjs/middleware/view-middleware.cjs +20 -9
- package/build/es/index.js +12 -1
- package/build/es/middleware/hmr-middleware.js +5 -0
- package/build/es/middleware/request-processor-middleware.js +21 -15
- package/build/es/middleware/utils/error-handling.js +10 -8
- package/build/es/middleware/view-middleware.js +22 -8
- package/build/es/tools/static-generation.js +2 -0
- package/build/es/tools/utils/network-dispatcher.js +3 -0
- package/package.json +32 -32
|
@@ -28,8 +28,6 @@ __export(exports, {
|
|
|
28
28
|
});
|
|
29
29
|
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
30
30
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
31
|
-
var MRT_REQUEST_CLASS = "X-Mobify-Request-Class";
|
|
32
|
-
var MRT_REQUEST_CLASS_KEY = MRT_REQUEST_CLASS.toLowerCase();
|
|
33
31
|
function requestProcessorMiddleware(app, context) {
|
|
34
32
|
const {basePath} = context.runtimeEnvironment;
|
|
35
33
|
app.use(async (req, res, next) => {
|
|
@@ -44,16 +42,23 @@ function requestProcessorMiddleware(app, context) {
|
|
|
44
42
|
});
|
|
45
43
|
}
|
|
46
44
|
}
|
|
47
|
-
requestClass = req.headers[
|
|
45
|
+
requestClass = req.headers[import_shared_utils.MRT_REQUEST_CLASS];
|
|
48
46
|
requestDepth = (0, import_shared_utils.parseRequestDepth)(req.headers, req.query);
|
|
47
|
+
const trueClientIP = req.headers[import_shared_utils.TRUE_CLIENT_IP];
|
|
48
|
+
const correlationID = req.headers[import_shared_utils.CORRELATION_ID];
|
|
49
49
|
const forwarded = req.headers["forwarded"];
|
|
50
50
|
const host = req.headers["host"];
|
|
51
51
|
const forwardedProto = req.headers["x-forwarded-proto"];
|
|
52
52
|
const protocol = req.protocol;
|
|
53
|
+
const cookieLength = req.headers["cookie"]?.length || 0;
|
|
54
|
+
let cookieMsg = `Cookie length: ${cookieLength}`;
|
|
55
|
+
if (cookieLength) {
|
|
56
|
+
cookieMsg += `, Cookie has '__Secure-has-sid': ${!!req.cookie("__Secure-has-sid")}, Cookie has 'sid': ${!!req.cookie("sid")}`;
|
|
57
|
+
}
|
|
53
58
|
if ((0, import_shared_utils.isLambdaEnv)()) {
|
|
54
59
|
import_diagnostics.logger.info({
|
|
55
60
|
label: `request-processor-middleware`,
|
|
56
|
-
message: `Original Url: ${req.originalUrl}, Forwarded: ${forwarded}, X-Forwarded-Proto: ${forwardedProto}, Host: ${host},
|
|
61
|
+
message: `Original Url: ${req.originalUrl}, Forwarded: ${forwarded}, X-Forwarded-Proto: ${forwardedProto}, Host: ${host}, Protocol: ${protocol}, ${cookieMsg}, ${import_shared_utils.MRT_REQUEST_CLASS}: ${requestClass}, ${import_shared_utils.TRUE_CLIENT_IP}: ${trueClientIP}, ${import_shared_utils.CORRELATION_ID}: ${correlationID}, ${import_shared_utils.REQUEST_DEPTH_HEADER}: ${requestDepth}`
|
|
57
62
|
});
|
|
58
63
|
}
|
|
59
64
|
}
|
|
@@ -89,7 +94,7 @@ function requestProcessorMiddleware(app, context) {
|
|
|
89
94
|
} else {
|
|
90
95
|
import_diagnostics.logger.debug({
|
|
91
96
|
label: `request-processor-middleware`,
|
|
92
|
-
message: `${
|
|
97
|
+
message: `${import_shared_utils.MRT_REQUEST_CLASS} ignored ${req.headers ? req.headers[import_shared_utils.MRT_REQUEST_CLASS] : "no-headers"}`
|
|
93
98
|
});
|
|
94
99
|
req.basePath = basePath;
|
|
95
100
|
}
|
|
@@ -32,10 +32,10 @@ var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
|
32
32
|
function addRedirectQueryParam(redirectUrl, depth) {
|
|
33
33
|
const fakeOrigin = "http://parse.com";
|
|
34
34
|
const url = (0, import_shared_utils.isURL)(redirectUrl) ? new URL(redirectUrl) : new URL(`${fakeOrigin}${redirectUrl}`);
|
|
35
|
-
url.searchParams.set(import_shared_utils.
|
|
35
|
+
url.searchParams.set(import_shared_utils.REQUEST_DEPTH_HEADER, String(depth + 1));
|
|
36
36
|
return url.toString().replace(fakeOrigin, "");
|
|
37
37
|
}
|
|
38
|
-
function createReturnStatus(error, req, debug) {
|
|
38
|
+
function createReturnStatus(error, req, debug, isBaseDoc) {
|
|
39
39
|
let status = 500, body = error.message || "";
|
|
40
40
|
const headers = {};
|
|
41
41
|
if (error instanceof import_diagnostics.LwrStatusError) {
|
|
@@ -54,12 +54,13 @@ function createReturnStatus(error, req, debug) {
|
|
|
54
54
|
if (error instanceof import_diagnostics.LwrInvalidError) {
|
|
55
55
|
status = 400;
|
|
56
56
|
} else if (error instanceof import_diagnostics.LwrUnresolvableError) {
|
|
57
|
-
status = 404;
|
|
57
|
+
status = isBaseDoc ? 500 : 404;
|
|
58
58
|
} else {
|
|
59
59
|
body = error instanceof import_diagnostics.LwrError ? body : `${import_diagnostics.descriptions.SERVER.SERVER_ERROR(req.originalUrl)}: ${body}`;
|
|
60
60
|
}
|
|
61
61
|
if (debug) {
|
|
62
62
|
body = `<div style="font-family:sans-serif;margin:50px;"><h1>${status}: Document generation failed</h1><p>${body.replace(/</g, "<").replace(/>/g, ">")}</p></div>`;
|
|
63
|
+
status = 200;
|
|
63
64
|
}
|
|
64
65
|
return {status, body, headers: Object.keys(headers).length > 0 ? headers : void 0};
|
|
65
66
|
}
|
|
@@ -75,10 +76,9 @@ function handleErrors(middleware, isBaseDoc = false) {
|
|
|
75
76
|
} else {
|
|
76
77
|
import_diagnostics2.logger.error(err);
|
|
77
78
|
}
|
|
78
|
-
const {status, body, headers} = createReturnStatus(err, req, req.query.debug !== void 0 && isBaseDoc);
|
|
79
|
+
const {status, body, headers} = createReturnStatus(err, req, req.query.debug !== void 0 && isBaseDoc, isBaseDoc);
|
|
79
80
|
headers && res.set(headers);
|
|
80
|
-
res.status(status);
|
|
81
|
-
res.send(body);
|
|
81
|
+
res.status(status).send(body);
|
|
82
82
|
}
|
|
83
83
|
};
|
|
84
84
|
}
|
|
@@ -26,17 +26,16 @@ __markAsModule(exports);
|
|
|
26
26
|
__export(exports, {
|
|
27
27
|
viewMiddleware: () => viewMiddleware
|
|
28
28
|
});
|
|
29
|
-
var import_util = __toModule(require("util"));
|
|
30
29
|
var import_url = __toModule(require("url"));
|
|
30
|
+
var import_util = __toModule(require("util"));
|
|
31
|
+
var import_zlib = __toModule(require("zlib"));
|
|
31
32
|
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
33
|
+
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
32
34
|
var import_router = __toModule(require("@lwrjs/router"));
|
|
33
35
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
34
|
-
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
35
|
-
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
36
36
|
var import_view_registry = __toModule(require("@lwrjs/view-registry"));
|
|
37
|
-
var import_instrumentation2 = __toModule(require("@lwrjs/instrumentation"));
|
|
38
|
-
var import_zlib = __toModule(require("zlib"));
|
|
39
37
|
var import_compression = __toModule(require("./utils/compression.cjs"));
|
|
38
|
+
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
40
39
|
function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
41
40
|
const errorRoute = errorRoutes.find((route2) => route2.status === 500);
|
|
42
41
|
const appConfig = context.appConfig;
|
|
@@ -69,13 +68,15 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
69
68
|
params: viewRequest.params,
|
|
70
69
|
query: viewRequest.query,
|
|
71
70
|
cookie: req.headers?.cookie,
|
|
72
|
-
trueClientIP: req.headers
|
|
71
|
+
trueClientIP: req.headers?.[import_shared_utils.TRUE_CLIENT_IP],
|
|
72
|
+
correlationID: req.headers?.[import_shared_utils.CORRELATION_ID],
|
|
73
73
|
coreProxy: req.getCoreProxy(appConfig.coreProxy ?? void 0, route.bootstrap?.proxyForSSR)
|
|
74
74
|
};
|
|
75
75
|
const resolve = req.isJsonRequest() ? viewHandler.getViewJson : viewHandler.getViewContent;
|
|
76
76
|
let viewResponse;
|
|
77
77
|
let resolvedRoute;
|
|
78
78
|
let traceId;
|
|
79
|
+
let headers = {};
|
|
79
80
|
try {
|
|
80
81
|
viewResponse = await (0, import_instrumentation.getTracer)().trace({
|
|
81
82
|
name: import_instrumentation.RequestHandlerSpan.GetView,
|
|
@@ -87,7 +88,9 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
87
88
|
locale: runtimeParams.locale
|
|
88
89
|
}
|
|
89
90
|
}, async (span) => {
|
|
91
|
+
runtimeParams.viewSpanID = span.spanId;
|
|
90
92
|
traceId = span.traceId;
|
|
93
|
+
headers = (0, import_shared_utils.getTraceHeaders)(runtimeParams, span);
|
|
91
94
|
const res2 = await resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
92
95
|
span.setAttributes({
|
|
93
96
|
size: byteSize(res2.body)
|
|
@@ -111,18 +114,26 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
111
114
|
});
|
|
112
115
|
resolvedRoute = errorRoute;
|
|
113
116
|
} finally {
|
|
117
|
+
if (Object.keys(headers).length)
|
|
118
|
+
res.set(headers);
|
|
114
119
|
if (traceId?.length) {
|
|
115
|
-
res.setHeader("x-trace-id", traceId);
|
|
116
120
|
res.set({
|
|
117
|
-
"Server-Timing": (0,
|
|
121
|
+
"Server-Timing": (0, import_instrumentation.getTraceCollector)().getSpansInTrace(traceId)
|
|
118
122
|
});
|
|
119
|
-
(0,
|
|
123
|
+
(0, import_instrumentation.getTraceCollector)().dropTrace(traceId);
|
|
120
124
|
}
|
|
121
125
|
}
|
|
122
126
|
if (req.isSiteGeneration()) {
|
|
123
127
|
res.setSiteGenerationMetadata(viewResponse.metadata);
|
|
124
128
|
}
|
|
125
129
|
res.type("text/html");
|
|
130
|
+
if ((0, import_shared_utils.isLocalDev)() || (0, import_shared_utils.isLocalPreview)()) {
|
|
131
|
+
if ((0, import_shared_utils.isLocalAuthEnabled)()) {
|
|
132
|
+
res.setHeader("Set-Cookie", "LocalAuthenticationEnabled=1;");
|
|
133
|
+
} else {
|
|
134
|
+
res.setHeader("Set-Cookie", "LocalAuthenticationEnabled=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT;");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
126
137
|
if (viewResponse.headers) {
|
|
127
138
|
if ((0, import_shared_utils.isLocalDev)()) {
|
|
128
139
|
delete viewResponse.headers["content-security-policy"];
|
package/build/es/index.js
CHANGED
|
@@ -91,8 +91,19 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
|
|
|
91
91
|
return serverContext;
|
|
92
92
|
}
|
|
93
93
|
export class LwrApp {
|
|
94
|
+
app;
|
|
95
|
+
server;
|
|
96
|
+
initialized = false;
|
|
97
|
+
config;
|
|
98
|
+
runtimeEnvironment;
|
|
99
|
+
globalData;
|
|
100
|
+
// generic server API
|
|
101
|
+
serverType;
|
|
102
|
+
use;
|
|
103
|
+
all;
|
|
104
|
+
get;
|
|
105
|
+
post;
|
|
94
106
|
constructor(configs) {
|
|
95
|
-
this.initialized = false;
|
|
96
107
|
const span = getTracer().startSpan({ name: CoreSpan.CreateServer });
|
|
97
108
|
const { appConfig, runtimeEnvironment, globalData } = configs;
|
|
98
109
|
this.config = appConfig;
|
|
@@ -5,6 +5,11 @@ import { getRequestProperties } from './utils/request.js';
|
|
|
5
5
|
let hmr;
|
|
6
6
|
const HMR_URI = '/:apiVersion/hmr/:format/:compat';
|
|
7
7
|
class Hmr {
|
|
8
|
+
connectedClients;
|
|
9
|
+
server;
|
|
10
|
+
context;
|
|
11
|
+
// Restriction: HMR only supports format=ESM and bundle=false
|
|
12
|
+
moduleRegistry;
|
|
8
13
|
constructor(server, context) {
|
|
9
14
|
this.server = server;
|
|
10
15
|
this.context = context;
|
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parse passed to us via the X-Mobify-Request-Class.
|
|
3
|
-
*
|
|
4
|
-
* basePath: The dynamic base path
|
|
5
|
-
* '' or undefined -> LWR basePath ''
|
|
6
|
-
* token or /token -> LWR basePath '/token'
|
|
7
|
-
*
|
|
8
|
-
*/
|
|
9
1
|
import { logger } from '@lwrjs/diagnostics';
|
|
10
|
-
import { REQUEST_DEPTH_HEADER, isLambdaEnv, parseRequestDepth } from '@lwrjs/shared-utils';
|
|
11
|
-
const MRT_REQUEST_CLASS = 'X-Mobify-Request-Class';
|
|
12
|
-
const MRT_REQUEST_CLASS_KEY = MRT_REQUEST_CLASS.toLowerCase();
|
|
2
|
+
import { CORRELATION_ID, MRT_REQUEST_CLASS, REQUEST_DEPTH_HEADER, TRUE_CLIENT_IP, isLambdaEnv, parseRequestDepth, } from '@lwrjs/shared-utils';
|
|
13
3
|
export function requestProcessorMiddleware(app, context) {
|
|
14
4
|
const { basePath } = context.runtimeEnvironment;
|
|
15
5
|
app.use(async (req, res, next) => {
|
|
@@ -26,17 +16,24 @@ export function requestProcessorMiddleware(app, context) {
|
|
|
26
16
|
});
|
|
27
17
|
}
|
|
28
18
|
}
|
|
29
|
-
requestClass = req.headers[
|
|
19
|
+
requestClass = req.headers[MRT_REQUEST_CLASS];
|
|
30
20
|
requestDepth = parseRequestDepth(req.headers, req.query);
|
|
21
|
+
const trueClientIP = req.headers[TRUE_CLIENT_IP];
|
|
22
|
+
const correlationID = req.headers[CORRELATION_ID];
|
|
31
23
|
const forwarded = req.headers['forwarded'];
|
|
32
24
|
const host = req.headers['host'];
|
|
33
25
|
const forwardedProto = req.headers['x-forwarded-proto'];
|
|
34
26
|
const protocol = req.protocol;
|
|
27
|
+
const cookieLength = req.headers['cookie']?.length || 0;
|
|
28
|
+
let cookieMsg = `Cookie length: ${cookieLength}`;
|
|
29
|
+
if (cookieLength) {
|
|
30
|
+
cookieMsg += `, Cookie has '__Secure-has-sid': ${!!req.cookie('__Secure-has-sid')}, Cookie has 'sid': ${!!req.cookie('sid')}`;
|
|
31
|
+
}
|
|
35
32
|
// Only Add this log when on MRT. Useful to catch issues where CDN did not send us the right info.
|
|
36
33
|
if (isLambdaEnv()) {
|
|
37
34
|
logger.info({
|
|
38
35
|
label: `request-processor-middleware`,
|
|
39
|
-
message: `Original Url: ${req.originalUrl}, Forwarded: ${forwarded}, X-Forwarded-Proto: ${forwardedProto}, Host: ${host},
|
|
36
|
+
message: `Original Url: ${req.originalUrl}, Forwarded: ${forwarded}, X-Forwarded-Proto: ${forwardedProto}, Host: ${host}, Protocol: ${protocol}, ${cookieMsg}, ${MRT_REQUEST_CLASS}: ${requestClass}, ${TRUE_CLIENT_IP}: ${trueClientIP}, ${CORRELATION_ID}: ${correlationID}, ${REQUEST_DEPTH_HEADER}: ${requestDepth}`,
|
|
40
37
|
});
|
|
41
38
|
}
|
|
42
39
|
}
|
|
@@ -81,7 +78,7 @@ export function requestProcessorMiddleware(app, context) {
|
|
|
81
78
|
else {
|
|
82
79
|
logger.debug({
|
|
83
80
|
label: `request-processor-middleware`,
|
|
84
|
-
message: `${
|
|
81
|
+
message: `${MRT_REQUEST_CLASS} ignored ${req.headers ? req.headers[MRT_REQUEST_CLASS] : 'no-headers'}`,
|
|
85
82
|
});
|
|
86
83
|
req.basePath = basePath;
|
|
87
84
|
}
|
|
@@ -89,12 +86,21 @@ export function requestProcessorMiddleware(app, context) {
|
|
|
89
86
|
await next();
|
|
90
87
|
});
|
|
91
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Parse the basePath passed via the X-Mobify-Request-Class header.
|
|
91
|
+
* Example: basePath=/token
|
|
92
|
+
*
|
|
93
|
+
* basePath: The dynamic base path
|
|
94
|
+
* '' or undefined -> LWR basePath ''
|
|
95
|
+
* token or /token -> LWR basePath '/token'
|
|
96
|
+
*
|
|
97
|
+
*/
|
|
92
98
|
function parseRequestClass(requestClass) {
|
|
93
99
|
// If there is no requestClass do not bother parsing
|
|
94
100
|
if (!requestClass) {
|
|
95
101
|
return {};
|
|
96
102
|
}
|
|
97
|
-
// Split the
|
|
103
|
+
// Split the header into individual key-value pairs
|
|
98
104
|
const keyValuePairs = requestClass.split(';');
|
|
99
105
|
// Create an object to store the parsed values
|
|
100
106
|
const parsed = {};
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { descriptions, DiagnosticsError, LwrError, LwrInvalidError, LwrStatusError, LwrUnresolvableError, } from '@lwrjs/diagnostics';
|
|
2
2
|
import { logger } from '@lwrjs/diagnostics';
|
|
3
|
-
import { isURL, parseRequestDepth,
|
|
3
|
+
import { isURL, parseRequestDepth, REQUEST_DEPTH_HEADER } from '@lwrjs/shared-utils';
|
|
4
4
|
function addRedirectQueryParam(redirectUrl, depth) {
|
|
5
5
|
// add a request depth query param to the URL
|
|
6
6
|
// the depth header cannot be used since headers cannot be added to a redirect
|
|
7
7
|
const fakeOrigin = 'http://parse.com';
|
|
8
8
|
const url = isURL(redirectUrl) ? new URL(redirectUrl) : new URL(`${fakeOrigin}${redirectUrl}`);
|
|
9
|
-
url.searchParams.set(
|
|
9
|
+
url.searchParams.set(REQUEST_DEPTH_HEADER, String(depth + 1));
|
|
10
10
|
return url.toString().replace(fakeOrigin, '');
|
|
11
11
|
}
|
|
12
|
-
function createReturnStatus(error, req, debug) {
|
|
12
|
+
function createReturnStatus(error, req, debug, isBaseDoc) {
|
|
13
13
|
let status = 500, body = error.message || '';
|
|
14
14
|
const headers = {};
|
|
15
15
|
if (error instanceof LwrStatusError) {
|
|
@@ -32,7 +32,9 @@ function createReturnStatus(error, req, debug) {
|
|
|
32
32
|
status = 400;
|
|
33
33
|
}
|
|
34
34
|
else if (error instanceof LwrUnresolvableError) {
|
|
35
|
-
status
|
|
35
|
+
// if something is missing during base doc generation, the status is 500
|
|
36
|
+
// eg: a bundle cannot be found during SSR -> the page fails with a 500
|
|
37
|
+
status = isBaseDoc ? 500 : 404;
|
|
36
38
|
}
|
|
37
39
|
else {
|
|
38
40
|
// catchall: likely a LwrServerError
|
|
@@ -42,8 +44,9 @@ function createReturnStatus(error, req, debug) {
|
|
|
42
44
|
: `${descriptions.SERVER.SERVER_ERROR(req.originalUrl)}: ${body}`;
|
|
43
45
|
}
|
|
44
46
|
if (debug) {
|
|
45
|
-
// return a debug base doc if debug mode is on
|
|
47
|
+
// return a debug base doc and a 200 response code if debug mode is on
|
|
46
48
|
body = `<div style="font-family:sans-serif;margin:50px;"><h1>${status}: Document generation failed</h1><p>${body.replace(/</g, '<').replace(/>/g, '>')}</p></div>`;
|
|
49
|
+
status = 200;
|
|
47
50
|
}
|
|
48
51
|
return { status, body, headers: Object.keys(headers).length > 0 ? headers : undefined };
|
|
49
52
|
}
|
|
@@ -63,10 +66,9 @@ export function handleErrors(middleware, isBaseDoc = false) {
|
|
|
63
66
|
}
|
|
64
67
|
const { status, body, headers } = createReturnStatus(err, req,
|
|
65
68
|
// only return HTML error screen for base doc requests in debug mode
|
|
66
|
-
req.query.debug !== undefined && isBaseDoc);
|
|
69
|
+
req.query.debug !== undefined && isBaseDoc, isBaseDoc);
|
|
67
70
|
headers && res.set(headers);
|
|
68
|
-
res.status(status);
|
|
69
|
-
res.send(body);
|
|
71
|
+
res.status(status).send(body);
|
|
70
72
|
}
|
|
71
73
|
};
|
|
72
74
|
}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { TextEncoder } from 'util';
|
|
2
1
|
import { URLSearchParams } from 'url';
|
|
2
|
+
import { TextEncoder } from 'util';
|
|
3
|
+
import { brotliCompressSync, gzipSync } from 'zlib';
|
|
3
4
|
import { descriptions, logger } from '@lwrjs/diagnostics';
|
|
5
|
+
import { RequestHandlerSpan, getTraceCollector, getTracer } from '@lwrjs/instrumentation';
|
|
4
6
|
import { getClientRoutes } from '@lwrjs/router';
|
|
5
|
-
import { decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, isLocalDev, shortestTtl,
|
|
6
|
-
import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
|
|
7
|
-
import { handleErrors } from './utils/error-handling.js';
|
|
7
|
+
import { CORRELATION_ID, TRUE_CLIENT_IP, decodeViewPath, extractRequestParams, getClientBootstrapConfigurationRoutes, getTraceHeaders, isLambdaEnv, isLocalDev, shortestTtl, isLocalAuthEnabled, isLocalPreview, } from '@lwrjs/shared-utils';
|
|
8
8
|
import { LwrViewHandler } from '@lwrjs/view-registry';
|
|
9
|
-
import { getTraceCollector } from '@lwrjs/instrumentation';
|
|
10
|
-
import { brotliCompressSync, gzipSync } from 'zlib';
|
|
11
9
|
import { getMrtCompressionThreshold } from './utils/compression.js';
|
|
10
|
+
import { handleErrors } from './utils/error-handling.js';
|
|
12
11
|
function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
13
12
|
const errorRoute = errorRoutes.find((route) => route.status === 500);
|
|
14
13
|
const appConfig = context.appConfig;
|
|
@@ -43,13 +42,15 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
43
42
|
params: viewRequest.params,
|
|
44
43
|
query: viewRequest.query,
|
|
45
44
|
cookie: req.headers?.cookie,
|
|
46
|
-
trueClientIP: req.headers
|
|
45
|
+
trueClientIP: req.headers?.[TRUE_CLIENT_IP],
|
|
46
|
+
correlationID: req.headers?.[CORRELATION_ID],
|
|
47
47
|
coreProxy: req.getCoreProxy(appConfig.coreProxy ?? undefined, route.bootstrap?.proxyForSSR),
|
|
48
48
|
};
|
|
49
49
|
const resolve = req.isJsonRequest() ? viewHandler.getViewJson : viewHandler.getViewContent;
|
|
50
50
|
let viewResponse;
|
|
51
51
|
let resolvedRoute;
|
|
52
52
|
let traceId;
|
|
53
|
+
let headers = {};
|
|
53
54
|
try {
|
|
54
55
|
viewResponse = await getTracer().trace({
|
|
55
56
|
name: RequestHandlerSpan.GetView,
|
|
@@ -61,7 +62,9 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
61
62
|
locale: runtimeParams.locale,
|
|
62
63
|
},
|
|
63
64
|
}, async (span) => {
|
|
65
|
+
runtimeParams.viewSpanID = span.spanId;
|
|
64
66
|
traceId = span.traceId;
|
|
67
|
+
headers = getTraceHeaders(runtimeParams, span);
|
|
65
68
|
const res = await resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
66
69
|
// Add the view size metric
|
|
67
70
|
span.setAttributes({
|
|
@@ -91,8 +94,9 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
91
94
|
resolvedRoute = errorRoute;
|
|
92
95
|
}
|
|
93
96
|
finally {
|
|
97
|
+
if (Object.keys(headers).length)
|
|
98
|
+
res.set(headers);
|
|
94
99
|
if (traceId?.length) {
|
|
95
|
-
res.setHeader('x-trace-id', traceId);
|
|
96
100
|
res.set({
|
|
97
101
|
'Server-Timing': getTraceCollector().getSpansInTrace(traceId),
|
|
98
102
|
});
|
|
@@ -104,6 +108,16 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
104
108
|
}
|
|
105
109
|
// view response will be html by default
|
|
106
110
|
res.type('text/html');
|
|
111
|
+
// Add a custom auth cookie to the browser for all view requests during local dev / local preview
|
|
112
|
+
if (isLocalDev() || isLocalPreview()) {
|
|
113
|
+
if (isLocalAuthEnabled()) {
|
|
114
|
+
res.setHeader('Set-Cookie', 'LocalAuthenticationEnabled=1;');
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
// When running locally, expire the previously set cookie if we are previewing with the guest user or no auth token
|
|
118
|
+
res.setHeader('Set-Cookie', 'LocalAuthenticationEnabled=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT;');
|
|
119
|
+
}
|
|
120
|
+
}
|
|
107
121
|
// the default content type can be overridden if specified by the view response
|
|
108
122
|
if (viewResponse.headers) {
|
|
109
123
|
if (isLocalDev()) {
|
|
@@ -847,6 +847,8 @@ export default class SiteGenerator {
|
|
|
847
847
|
}
|
|
848
848
|
// Class used to track import metadata for a view
|
|
849
849
|
export class ViewImportMetadataImpl {
|
|
850
|
+
existing;
|
|
851
|
+
additional;
|
|
850
852
|
constructor(existingImportMetadata, additionalImportMetadata) {
|
|
851
853
|
this.existing = existingImportMetadata;
|
|
852
854
|
this.additional = additionalImportMetadata || { imports: {}, index: {} };
|
|
@@ -3,6 +3,9 @@ import https from 'https';
|
|
|
3
3
|
import { logger } from '@lwrjs/diagnostics';
|
|
4
4
|
import { isLocalDev, isModuleOrBundleUrl } from '@lwrjs/shared-utils';
|
|
5
5
|
export default class NetworkDispatcher {
|
|
6
|
+
port;
|
|
7
|
+
internalRequestKey;
|
|
8
|
+
pool;
|
|
6
9
|
constructor(port, internalRequestKey) {
|
|
7
10
|
this.port = port || 3000;
|
|
8
11
|
const httpClient = this.port == 443 ? https : http;
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.17.2
|
|
7
|
+
"version": "0.17.2",
|
|
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
|
|
47
|
-
"@lwrjs/asset-registry": "0.17.2
|
|
48
|
-
"@lwrjs/asset-transformer": "0.17.2
|
|
49
|
-
"@lwrjs/base-view-provider": "0.17.2
|
|
50
|
-
"@lwrjs/base-view-transformer": "0.17.2
|
|
51
|
-
"@lwrjs/client-modules": "0.17.2
|
|
52
|
-
"@lwrjs/config": "0.17.2
|
|
53
|
-
"@lwrjs/diagnostics": "0.17.2
|
|
54
|
-
"@lwrjs/esbuild": "0.17.2
|
|
55
|
-
"@lwrjs/fs-asset-provider": "0.17.2
|
|
56
|
-
"@lwrjs/fs-watch": "0.17.2
|
|
57
|
-
"@lwrjs/html-view-provider": "0.17.2
|
|
58
|
-
"@lwrjs/instrumentation": "0.17.2
|
|
59
|
-
"@lwrjs/loader": "0.17.2
|
|
60
|
-
"@lwrjs/lwc-module-provider": "0.17.2
|
|
61
|
-
"@lwrjs/lwc-ssr": "0.17.2
|
|
62
|
-
"@lwrjs/markdown-view-provider": "0.17.2
|
|
63
|
-
"@lwrjs/module-bundler": "0.17.2
|
|
64
|
-
"@lwrjs/module-registry": "0.17.2
|
|
65
|
-
"@lwrjs/npm-module-provider": "0.17.2
|
|
66
|
-
"@lwrjs/nunjucks-view-provider": "0.17.2
|
|
67
|
-
"@lwrjs/o11y": "0.17.2
|
|
68
|
-
"@lwrjs/resource-registry": "0.17.2
|
|
69
|
-
"@lwrjs/router": "0.17.2
|
|
70
|
-
"@lwrjs/server": "0.17.2
|
|
71
|
-
"@lwrjs/shared-utils": "0.17.2
|
|
72
|
-
"@lwrjs/static": "0.17.2
|
|
73
|
-
"@lwrjs/view-registry": "0.17.2
|
|
46
|
+
"@lwrjs/app-service": "0.17.2",
|
|
47
|
+
"@lwrjs/asset-registry": "0.17.2",
|
|
48
|
+
"@lwrjs/asset-transformer": "0.17.2",
|
|
49
|
+
"@lwrjs/base-view-provider": "0.17.2",
|
|
50
|
+
"@lwrjs/base-view-transformer": "0.17.2",
|
|
51
|
+
"@lwrjs/client-modules": "0.17.2",
|
|
52
|
+
"@lwrjs/config": "0.17.2",
|
|
53
|
+
"@lwrjs/diagnostics": "0.17.2",
|
|
54
|
+
"@lwrjs/esbuild": "0.17.2",
|
|
55
|
+
"@lwrjs/fs-asset-provider": "0.17.2",
|
|
56
|
+
"@lwrjs/fs-watch": "0.17.2",
|
|
57
|
+
"@lwrjs/html-view-provider": "0.17.2",
|
|
58
|
+
"@lwrjs/instrumentation": "0.17.2",
|
|
59
|
+
"@lwrjs/loader": "0.17.2",
|
|
60
|
+
"@lwrjs/lwc-module-provider": "0.17.2",
|
|
61
|
+
"@lwrjs/lwc-ssr": "0.17.2",
|
|
62
|
+
"@lwrjs/markdown-view-provider": "0.17.2",
|
|
63
|
+
"@lwrjs/module-bundler": "0.17.2",
|
|
64
|
+
"@lwrjs/module-registry": "0.17.2",
|
|
65
|
+
"@lwrjs/npm-module-provider": "0.17.2",
|
|
66
|
+
"@lwrjs/nunjucks-view-provider": "0.17.2",
|
|
67
|
+
"@lwrjs/o11y": "0.17.2",
|
|
68
|
+
"@lwrjs/resource-registry": "0.17.2",
|
|
69
|
+
"@lwrjs/router": "0.17.2",
|
|
70
|
+
"@lwrjs/server": "0.17.2",
|
|
71
|
+
"@lwrjs/shared-utils": "0.17.2",
|
|
72
|
+
"@lwrjs/static": "0.17.2",
|
|
73
|
+
"@lwrjs/view-registry": "0.17.2",
|
|
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
|
|
83
|
+
"@lwrjs/types": "0.17.2",
|
|
84
84
|
"@types/ws": "^8.5.12",
|
|
85
85
|
"memfs": "^4.13.0"
|
|
86
86
|
},
|
|
@@ -88,10 +88,10 @@
|
|
|
88
88
|
"lwc": ">= 2.x"
|
|
89
89
|
},
|
|
90
90
|
"engines": {
|
|
91
|
-
"node": ">=
|
|
91
|
+
"node": ">=20.0.0"
|
|
92
92
|
},
|
|
93
93
|
"volta": {
|
|
94
94
|
"extends": "../../../package.json"
|
|
95
95
|
},
|
|
96
|
-
"gitHead": "
|
|
96
|
+
"gitHead": "d64d8888a28da36c05e3d8d9baf51416551863a9"
|
|
97
97
|
}
|