@lwrjs/core 0.12.0-alpha.3 → 0.12.0-alpha.30
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/context/provider.cjs +7 -2
- package/build/cjs/index.cjs +19 -25
- package/build/cjs/info/route-handler.cjs +149 -0
- package/build/cjs/middleware/asset-middleware.cjs +41 -28
- package/build/cjs/middleware/bundle-middleware.cjs +7 -1
- package/build/cjs/middleware/mapping-middleware.cjs +4 -1
- package/build/cjs/middleware/module-middleware.cjs +7 -1
- package/build/cjs/middleware/request-processor-middleware.cjs +15 -1
- package/build/cjs/middleware/utils/error-handling.cjs +1 -1
- package/build/cjs/middleware/utils/identity.cjs +1 -1
- package/build/cjs/middleware/view-middleware.cjs +11 -2
- package/build/cjs/tools/static-generation.cjs +41 -6
- package/build/cjs/tools/utils/network-dispatcher.cjs +3 -2
- package/build/es/context/provider.js +7 -1
- package/build/es/index.d.ts +12 -5
- package/build/es/index.js +21 -28
- package/build/es/info/route-handler.d.ts +3 -0
- package/build/es/info/route-handler.js +123 -0
- package/build/es/middleware/asset-middleware.js +60 -33
- package/build/es/middleware/bundle-middleware.js +7 -0
- package/build/es/middleware/mapping-middleware.js +3 -0
- package/build/es/middleware/module-middleware.js +7 -0
- package/build/es/middleware/request-processor-middleware.js +17 -1
- package/build/es/middleware/utils/error-handling.js +1 -1
- package/build/es/middleware/utils/identity.js +5 -1
- package/build/es/middleware/view-middleware.js +10 -1
- package/build/es/tools/static-generation.d.ts +4 -0
- package/build/es/tools/static-generation.js +51 -11
- package/build/es/tools/utils/network-dispatcher.js +5 -2
- package/package.json +41 -35
|
@@ -26,7 +26,9 @@ __markAsModule(exports);
|
|
|
26
26
|
__export(exports, {
|
|
27
27
|
createProviderContext: () => createProviderContext
|
|
28
28
|
});
|
|
29
|
+
var import_fs = __toModule(require("fs"));
|
|
29
30
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
31
|
+
var import_site_metadata = __toModule(require("@lwrjs/static/site-metadata"));
|
|
30
32
|
function createProviderContext(serverContext) {
|
|
31
33
|
const {
|
|
32
34
|
assets,
|
|
@@ -43,7 +45,8 @@ function createProviderContext(serverContext) {
|
|
|
43
45
|
esmLoader,
|
|
44
46
|
environment,
|
|
45
47
|
basePath,
|
|
46
|
-
bundleConfig
|
|
48
|
+
bundleConfig,
|
|
49
|
+
staticSiteGenerator
|
|
47
50
|
} = serverContext.appConfig;
|
|
48
51
|
const {onModuleDefinitionChange, onModuleSourceChange} = serverContext.appObserver;
|
|
49
52
|
const {
|
|
@@ -52,6 +55,7 @@ function createProviderContext(serverContext) {
|
|
|
52
55
|
notifyViewSourceChanged,
|
|
53
56
|
notifyAssetSourceChanged
|
|
54
57
|
} = serverContext.appEmitter;
|
|
58
|
+
const siteMetadata = staticSiteGenerator.outputDir && import_fs.default.existsSync(staticSiteGenerator.outputDir) ? new import_site_metadata.SiteMetadataImpl({rootDir: staticSiteGenerator.outputDir}) : void 0;
|
|
55
59
|
return {
|
|
56
60
|
appObserver: (0, import_shared_utils.deepFreeze)({onModuleDefinitionChange, onModuleSourceChange}),
|
|
57
61
|
appEmitter: {
|
|
@@ -83,6 +87,7 @@ function createProviderContext(serverContext) {
|
|
|
83
87
|
bundleConfig
|
|
84
88
|
}),
|
|
85
89
|
runtimeEnvironment: (0, import_shared_utils.deepFreeze)(serverContext.runtimeEnvironment),
|
|
86
|
-
watcherFactory: serverContext.watcherFactory
|
|
90
|
+
watcherFactory: serverContext.watcherFactory,
|
|
91
|
+
siteMetadata
|
|
87
92
|
};
|
|
88
93
|
}
|
package/build/cjs/index.cjs
CHANGED
|
@@ -70,10 +70,15 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
|
|
|
70
70
|
const skipValidation = true;
|
|
71
71
|
await (0, import_config.executeConfigHooks)(hooks, appConfig, runtimeEnvironment, globalData, skipValidation);
|
|
72
72
|
(0, import_config.executeInstrumentationHooks)(hooks);
|
|
73
|
-
(0, import_config.executeStartHooks)(hooks, appConfig, runtimeEnvironment);
|
|
74
73
|
}
|
|
75
74
|
const services = await (0, import_modules.loadServices)(appConfig);
|
|
76
75
|
const serverContext = (0, import_instrumentation.getTracer)().trace({name: import_instrumentation.CoreSpan.CreateServerContext}, () => (0, import_server2.createServerContext)(appConfig, runtimeEnvironment, globalData));
|
|
76
|
+
const routeHandlers = await (0, import_modules.loadRouteHandlers)(appConfig);
|
|
77
|
+
serverContext.routeHandlers = routeHandlers;
|
|
78
|
+
if (hooks.length) {
|
|
79
|
+
await (0, import_config.executeContextHooks)(hooks, serverContext);
|
|
80
|
+
(0, import_config.executeStartHooks)(hooks, appConfig, runtimeEnvironment);
|
|
81
|
+
}
|
|
77
82
|
const providerContext = (0, import_provider.createProviderContext)(serverContext);
|
|
78
83
|
const {moduleRegistry, assetRegistry, resourceRegistry, viewRegistry, moduleBundler} = serverContext;
|
|
79
84
|
const moduleProviders = createServices(services.moduleProviders, providerContext);
|
|
@@ -94,29 +99,26 @@ async function initContext(appConfig, runtimeEnvironment, globalData) {
|
|
|
94
99
|
viewRegistry.addViewProviders(viewProviders);
|
|
95
100
|
viewRegistry.addViewTransformers(viewTransformers);
|
|
96
101
|
await serverContext.viewRegistry.initializeViewProviders();
|
|
97
|
-
const routeHandlers = await (0, import_modules.loadRouteHandlers)(appConfig);
|
|
98
|
-
serverContext.routeHandlers = routeHandlers;
|
|
99
102
|
return serverContext;
|
|
100
103
|
}
|
|
101
104
|
var LwrApp = class {
|
|
102
|
-
constructor(
|
|
105
|
+
constructor(configs) {
|
|
103
106
|
this.initialized = false;
|
|
104
107
|
const span = (0, import_instrumentation.getTracer)().startSpan({name: import_instrumentation.CoreSpan.CreateServer});
|
|
105
|
-
const {appConfig, runtimeEnvironment, globalData} =
|
|
108
|
+
const {appConfig, runtimeEnvironment, globalData} = configs;
|
|
106
109
|
this.config = appConfig;
|
|
107
110
|
this.runtimeEnvironment = runtimeEnvironment;
|
|
108
111
|
this.globalData = globalData;
|
|
109
|
-
const {basePath} = this.config;
|
|
110
|
-
this.
|
|
112
|
+
const {basePath, serverType} = this.config;
|
|
113
|
+
this.serverType = serverType;
|
|
114
|
+
this.app = (0, import_server.createInternalServer)(serverType, {basePath});
|
|
111
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);
|
|
112
120
|
span.end();
|
|
113
121
|
}
|
|
114
|
-
setConfig(config) {
|
|
115
|
-
const {appConfig, runtimeEnvironment, globalData} = (0, import_config.loadConfig)(config);
|
|
116
|
-
this.config = appConfig;
|
|
117
|
-
this.runtimeEnvironment = runtimeEnvironment;
|
|
118
|
-
this.globalData = globalData;
|
|
119
|
-
}
|
|
120
122
|
getConfig() {
|
|
121
123
|
return this.config;
|
|
122
124
|
}
|
|
@@ -168,24 +170,16 @@ var LwrApp = class {
|
|
|
168
170
|
});
|
|
169
171
|
});
|
|
170
172
|
}
|
|
171
|
-
|
|
172
|
-
this.server?.close &&
|
|
173
|
+
close() {
|
|
174
|
+
this.server?.close && this.server.close();
|
|
173
175
|
}
|
|
174
176
|
getInternalServer() {
|
|
175
177
|
return this.app.getImpl();
|
|
176
178
|
}
|
|
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
179
|
};
|
|
187
180
|
function createServer(config) {
|
|
188
|
-
|
|
181
|
+
const configs = (0, import_config.loadConfig)(config);
|
|
182
|
+
return new LwrApp(configs);
|
|
189
183
|
}
|
|
190
184
|
async function generateStaticSite(config) {
|
|
191
185
|
config = config || {};
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, {get: all[name], enumerable: true});
|
|
11
|
+
};
|
|
12
|
+
var __exportStar = (target, module2, desc) => {
|
|
13
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(module2))
|
|
15
|
+
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
16
|
+
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
|
|
17
|
+
}
|
|
18
|
+
return target;
|
|
19
|
+
};
|
|
20
|
+
var __toModule = (module2) => {
|
|
21
|
+
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// packages/@lwrjs/core/src/info/route-handler.ts
|
|
25
|
+
__markAsModule(exports);
|
|
26
|
+
__export(exports, {
|
|
27
|
+
default: () => siteInfoHandler
|
|
28
|
+
});
|
|
29
|
+
var import_config = __toModule(require("@lwrjs/config"));
|
|
30
|
+
var import_fs = __toModule(require("fs"));
|
|
31
|
+
var baseHtml = `<!DOCTYPE html>
|
|
32
|
+
<html lang="en">
|
|
33
|
+
<head>
|
|
34
|
+
<meta charset="utf-8" />
|
|
35
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
|
|
36
|
+
<title>Site Information</title>
|
|
37
|
+
<style>
|
|
38
|
+
body {
|
|
39
|
+
font-family: Arial, sans-serif;
|
|
40
|
+
margin: 0;
|
|
41
|
+
padding: 20px;
|
|
42
|
+
background-color: #f9f9f9;
|
|
43
|
+
color: #333;
|
|
44
|
+
display: flex;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.container {
|
|
48
|
+
max-width: 800px;
|
|
49
|
+
width: 100%;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
h1, h2 {
|
|
53
|
+
color: #555;
|
|
54
|
+
margin-bottom: 16px;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
ul {
|
|
58
|
+
list-style-type: none;
|
|
59
|
+
padding: 0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
li {
|
|
63
|
+
background-color: #fff;
|
|
64
|
+
border: 1px solid #ddd;
|
|
65
|
+
margin-bottom: 10px;
|
|
66
|
+
padding: 10px;
|
|
67
|
+
border-radius: 4px;
|
|
68
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
69
|
+
display: flex;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.key {
|
|
73
|
+
margin-right: 10px;
|
|
74
|
+
color: #777;
|
|
75
|
+
font-weight: normal;
|
|
76
|
+
min-width: 200px;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.val {
|
|
80
|
+
font-weight: bold;
|
|
81
|
+
color: steelblue;
|
|
82
|
+
font-size: 1.1em;
|
|
83
|
+
flex-grow: 1;
|
|
84
|
+
text-align: left;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.data-container {
|
|
88
|
+
padding: 20px;
|
|
89
|
+
border: 1px solid #333;
|
|
90
|
+
border-radius: 5px;
|
|
91
|
+
white-space: pre;
|
|
92
|
+
font-family: monospace;
|
|
93
|
+
text-align: justify;
|
|
94
|
+
};
|
|
95
|
+
</style>
|
|
96
|
+
</head>
|
|
97
|
+
<body>
|
|
98
|
+
<div class="container">
|
|
99
|
+
<h1>Site Information</h1>
|
|
100
|
+
<h2>Environment</h2>
|
|
101
|
+
{{ versionInfo }}
|
|
102
|
+
<h2>Metadata</h2>
|
|
103
|
+
{{ metadata }}
|
|
104
|
+
</div>
|
|
105
|
+
</body>
|
|
106
|
+
</html>
|
|
107
|
+
`;
|
|
108
|
+
async function siteInfoHandler(_request, context) {
|
|
109
|
+
const versionInfo = {
|
|
110
|
+
"LWR Version": import_config.LWR_VERSION,
|
|
111
|
+
"LWC Version": import_config.LWC_VERSION,
|
|
112
|
+
"Node Version": import_config.NODE_VERSION,
|
|
113
|
+
"PWA Kit Runtime Version": import_config.PWA_KIT_RUNTIME_VERSION
|
|
114
|
+
};
|
|
115
|
+
let versionInfoString = "<ul>";
|
|
116
|
+
for (const key in versionInfo) {
|
|
117
|
+
if (versionInfo[key] !== "note-provided") {
|
|
118
|
+
versionInfoString += `<li><span class="key">${key}:</span><span class="val">${versionInfo[key]}</span></li>
|
|
119
|
+
`;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
versionInfoString += "</ul>";
|
|
123
|
+
const providedMetadataPath = `${context.rootDir}/site/.metadata/runtime-info.json`;
|
|
124
|
+
let providedMetadata = {};
|
|
125
|
+
if (import_fs.default.existsSync(providedMetadataPath)) {
|
|
126
|
+
providedMetadata = JSON.parse(import_fs.default.readFileSync(providedMetadataPath, "utf-8"));
|
|
127
|
+
}
|
|
128
|
+
let providedInfoString = "<ul>";
|
|
129
|
+
for (const key in providedMetadata) {
|
|
130
|
+
if (providedMetadata[key] !== "note-provided") {
|
|
131
|
+
const value = typeof providedMetadata[key] === "object" ? `<div class="data-container val">${JSON.stringify(providedMetadata[key], null, 2)}</div>` : `<span class="val">${providedMetadata[key]}</span>`;
|
|
132
|
+
providedInfoString += `<li><span class="key">${key}:</span>${value}</li>
|
|
133
|
+
`;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
providedInfoString += "</ul>";
|
|
137
|
+
let infoHtml = baseHtml;
|
|
138
|
+
infoHtml = infoHtml.replace("{{ versionInfo }}", versionInfoString);
|
|
139
|
+
infoHtml = infoHtml.replace("{{ metadata }}", providedInfoString);
|
|
140
|
+
return {
|
|
141
|
+
body: infoHtml,
|
|
142
|
+
headers: {
|
|
143
|
+
"content-type": `text/html; charset=utf-8`
|
|
144
|
+
},
|
|
145
|
+
cache: {
|
|
146
|
+
ttl: "200s"
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
@@ -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,23 +61,16 @@ 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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
|
67
|
+
asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
|
|
68
|
+
if (!import_fs_extra.default.existsSync(asset.entry) && assetUri.uri.startsWith("/mobify/bundle")) {
|
|
69
|
+
return sendRedirect(res, assetUri);
|
|
59
70
|
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
}
|
|
71
|
+
} else {
|
|
72
|
+
asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
|
|
73
|
+
}
|
|
63
74
|
if (req.isSiteGeneration()) {
|
|
64
75
|
res.setSiteGenerationMetadata({asset});
|
|
65
76
|
}
|
|
@@ -81,20 +92,22 @@ function createAssetMiddleware(context) {
|
|
|
81
92
|
}
|
|
82
93
|
};
|
|
83
94
|
}
|
|
84
|
-
function
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
} else if (assetDirConfig.dir) {
|
|
91
|
-
urlPath += app.getRegexWildcard();
|
|
95
|
+
async function getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment) {
|
|
96
|
+
const asset = await (0, import_instrumentation.getTracer)().trace({
|
|
97
|
+
name: import_instrumentation.RequestHandlerSpan.GetAsset,
|
|
98
|
+
attributes: {
|
|
99
|
+
specifier: assetId.specifier,
|
|
100
|
+
url: req.originalUrl
|
|
92
101
|
}
|
|
93
|
-
|
|
102
|
+
}, () => {
|
|
103
|
+
return assetRegistry.getAsset({...assetId, signature}, runtimeEnvironment, req.isSiteGeneration());
|
|
94
104
|
});
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
105
|
+
return asset;
|
|
106
|
+
}
|
|
107
|
+
function sendRedirect(res, assetUri) {
|
|
108
|
+
res.set({
|
|
109
|
+
Location: assetUri.uri,
|
|
110
|
+
"cache-control": "public, max-age=60"
|
|
111
|
+
});
|
|
112
|
+
res.sendStatus(302);
|
|
100
113
|
}
|
|
@@ -53,6 +53,11 @@ function createBundleMiddleware(context) {
|
|
|
53
53
|
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
56
|
+
if (!req.validateApiVersion(appConfig)) {
|
|
57
|
+
res.status(400);
|
|
58
|
+
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_API_VERSION(req.params.apiVersion, appConfig.apiVersion).message);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
56
61
|
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
57
62
|
const importer = req.query.importer ? await (0, import_request.getRequestImporter)(req, moduleRegistry, runtimeParams) : void 0;
|
|
58
63
|
const {moduleId, signature} = (0, import_identity.getModuleIdentity)(req, importer);
|
|
@@ -64,7 +69,8 @@ function createBundleMiddleware(context) {
|
|
|
64
69
|
const bundleDefinition = await (0, import_instrumentation.getTracer)().trace({
|
|
65
70
|
name: import_instrumentation.RequestHandlerSpan.GetBundle,
|
|
66
71
|
attributes: {
|
|
67
|
-
specifier: moduleId.specifier
|
|
72
|
+
specifier: moduleId.specifier,
|
|
73
|
+
url: req.originalUrl
|
|
68
74
|
}
|
|
69
75
|
}, () => {
|
|
70
76
|
return moduleBundler.getModuleBundle(moduleId, {...runtimeEnvironment, bundle: true, sourceMapUrl}, runtimeParams);
|
|
@@ -47,7 +47,10 @@ function createMappingMiddleware(context) {
|
|
|
47
47
|
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
48
48
|
const {moduleIds} = (0, import_identity.getMappingIdentity)(req);
|
|
49
49
|
const importMetadata = await (0, import_instrumentation.getTracer)().trace({
|
|
50
|
-
name: import_instrumentation.RequestHandlerSpan.GetMapping
|
|
50
|
+
name: import_instrumentation.RequestHandlerSpan.GetMapping,
|
|
51
|
+
attributes: {
|
|
52
|
+
url: req.originalUrl
|
|
53
|
+
}
|
|
51
54
|
}, () => {
|
|
52
55
|
return (0, import_shared_utils.getImportMetadataMappings)(moduleIds, runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler);
|
|
53
56
|
});
|
|
@@ -47,6 +47,11 @@ function createModuleMiddleware(context) {
|
|
|
47
47
|
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
48
48
|
return;
|
|
49
49
|
}
|
|
50
|
+
if (!req.validateApiVersion(appConfig)) {
|
|
51
|
+
res.status(400);
|
|
52
|
+
res.send(import_diagnostics.descriptions.UNRESOLVABLE.INVALID_API_VERSION(req.params.apiVersion, appConfig.apiVersion).message);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
50
55
|
const {runtimeEnvironment, runtimeParams} = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
51
56
|
const importer = req.query.importer ? await (0, import_request.getRequestImporter)(req, moduleRegistry, runtimeParams) : void 0;
|
|
52
57
|
const {moduleId, signature} = (0, import_identity.getModuleIdentity)(req, importer);
|
|
@@ -57,7 +62,8 @@ function createModuleMiddleware(context) {
|
|
|
57
62
|
const moduleDefinition = await (0, import_instrumentation.getTracer)().trace({
|
|
58
63
|
name: import_instrumentation.RequestHandlerSpan.GetModule,
|
|
59
64
|
attributes: {
|
|
60
|
-
specifier: moduleId.specifier
|
|
65
|
+
specifier: moduleId.specifier,
|
|
66
|
+
url: req.originalUrl
|
|
61
67
|
}
|
|
62
68
|
}, () => {
|
|
63
69
|
return moduleRegistry.getLinkedModule(moduleId, {...runtimeEnvironment, bundle: false}, runtimeParams);
|
|
@@ -27,14 +27,20 @@ __export(exports, {
|
|
|
27
27
|
requestProcessorMiddleware: () => requestProcessorMiddleware
|
|
28
28
|
});
|
|
29
29
|
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
30
|
+
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
30
31
|
var MRT_REQUEST_CLASS = "X-Mobify-Request-Class";
|
|
31
32
|
var MRT_REQUEST_CLASS_KEY = MRT_REQUEST_CLASS.toLowerCase();
|
|
32
33
|
function requestProcessorMiddleware(app, context) {
|
|
33
34
|
const {basePath} = context.runtimeEnvironment;
|
|
34
|
-
app.use(async (req,
|
|
35
|
+
app.use(async (req, res, next) => {
|
|
35
36
|
let requestClass;
|
|
37
|
+
let requestDepth;
|
|
36
38
|
if (req.headers) {
|
|
37
39
|
if (import_diagnostics.logger.isDebugEnabled()) {
|
|
40
|
+
import_diagnostics.logger.debug({
|
|
41
|
+
label: `request-processor-middleware`,
|
|
42
|
+
message: `Request: ${req.originalUrl}`
|
|
43
|
+
});
|
|
38
44
|
for (const headerName in req.headers) {
|
|
39
45
|
import_diagnostics.logger.debug({
|
|
40
46
|
label: `request-processor-middleware`,
|
|
@@ -43,6 +49,14 @@ function requestProcessorMiddleware(app, context) {
|
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
51
|
requestClass = req.headers[MRT_REQUEST_CLASS_KEY];
|
|
52
|
+
requestDepth = (0, import_shared_utils.parseRequestDepthHeader)(req.headers);
|
|
53
|
+
}
|
|
54
|
+
if (requestDepth && requestDepth > 0) {
|
|
55
|
+
import_diagnostics.logger.error({
|
|
56
|
+
label: "request-processor-middleware",
|
|
57
|
+
message: `Lambda SSR request cycle detected: ${req.originalUrl}`
|
|
58
|
+
});
|
|
59
|
+
return res.status(400).send("Request depth limit reached");
|
|
46
60
|
}
|
|
47
61
|
if (req.headers && typeof requestClass === "string") {
|
|
48
62
|
const parsedRequestClass = parseRequestClass(requestClass);
|
|
@@ -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.
|
|
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) => {
|
|
@@ -75,7 +75,7 @@ function getResourceIdentity(req) {
|
|
|
75
75
|
}
|
|
76
76
|
function getAssetIdentity(req) {
|
|
77
77
|
const {signature, immutable, assetType: type} = req.params;
|
|
78
|
-
const specifier = type ? "/" + req.params[0] : req.originalUrl.split("?")[0];
|
|
78
|
+
const specifier = type ? process.platform === "win32" ? req.params[0] : "/" + req.params[0] : req.originalUrl.split("?")[0];
|
|
79
79
|
if (validateSpecifier(specifier) === false) {
|
|
80
80
|
throw (0, import_diagnostics.createSingleDiagnosticError)({
|
|
81
81
|
description: import_diagnostics.descriptions.UNRESOLVABLE.INVALID_SPECIFIER(specifier)
|
|
@@ -66,13 +66,19 @@ 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,
|
|
72
73
|
attributes: {
|
|
73
|
-
view: route.id
|
|
74
|
+
view: route.id,
|
|
75
|
+
url: req.originalUrl,
|
|
76
|
+
host: runtimeParams.host,
|
|
77
|
+
basePath: runtimeParams.basePath,
|
|
78
|
+
locale: runtimeParams.locale
|
|
74
79
|
}
|
|
75
|
-
}, () => {
|
|
80
|
+
}, (span) => {
|
|
81
|
+
traceId = span.traceId;
|
|
76
82
|
return resolve.call(viewHandler, viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
77
83
|
});
|
|
78
84
|
resolvedRoute = route;
|
|
@@ -105,6 +111,9 @@ function createViewMiddleware(route, errorRoutes, context, viewHandler) {
|
|
|
105
111
|
res.setHeader("cache-control", `public, max-age=${cacheTtl}`);
|
|
106
112
|
}
|
|
107
113
|
}
|
|
114
|
+
if (traceId?.length) {
|
|
115
|
+
res.setHeader("x-trace-id", traceId);
|
|
116
|
+
}
|
|
108
117
|
const status = resolvedRoute.status || viewResponse.status || 200;
|
|
109
118
|
res.status(status);
|
|
110
119
|
res.send(viewResponse.body);
|
|
@@ -36,6 +36,7 @@ var import_fs_extra = __toModule(require("fs-extra"));
|
|
|
36
36
|
var import_stream = __toModule(require("./utils/stream.cjs"));
|
|
37
37
|
var import_dir = __toModule(require("./utils/dir.cjs"));
|
|
38
38
|
var import_config = __toModule(require("@lwrjs/config"));
|
|
39
|
+
var import_url = __toModule(require("url"));
|
|
39
40
|
var SiteGenerator = class {
|
|
40
41
|
async buildStaticApplication(config, dispatcher) {
|
|
41
42
|
const startTime = import_perf_hooks.performance.now();
|
|
@@ -161,6 +162,14 @@ var SiteGenerator = class {
|
|
|
161
162
|
}
|
|
162
163
|
async handleJavascriptResource(url, context, siteConfig, dispatcher) {
|
|
163
164
|
const {outputDir} = siteConfig;
|
|
165
|
+
const moduleDefinition = context.fs?.metadata?.moduleDefinition;
|
|
166
|
+
const bundleConfig = moduleDefinition?.config || {};
|
|
167
|
+
const siteBundles = siteConfig?.siteMetadata?.getSiteBundles()?.bundles;
|
|
168
|
+
const specifier = moduleDefinition?.specifier;
|
|
169
|
+
if ((0, import_shared_utils.isExternalSpecifier)(specifier, bundleConfig)) {
|
|
170
|
+
this.handleExternalBundle(specifier, siteBundles, bundleConfig.external, outputDir);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
164
173
|
const normalizedUrl = decodeURIComponent(url);
|
|
165
174
|
(0, import_dir.createResourceDir)((0, import_path.dirname)(normalizedUrl), outputDir);
|
|
166
175
|
const ext = (0, import_path.extname)(normalizedUrl);
|
|
@@ -173,17 +182,26 @@ var SiteGenerator = class {
|
|
|
173
182
|
siteConfig.urlRewriteMap.set(normalizedUrl.substring(0, normalizedUrl.indexOf("/v/")), normalizedUrl);
|
|
174
183
|
siteConfig.urlRewriteMap.set(url.substring(0, url.indexOf("%2Fv%2F")), normalizedUrl);
|
|
175
184
|
}
|
|
176
|
-
const moduleDefinition = context.fs?.metadata?.moduleDefinition;
|
|
177
185
|
if (moduleDefinition) {
|
|
178
186
|
const imports = moduleDefinition.linkedModuleRecord?.imports || moduleDefinition.bundleRecord?.imports || [];
|
|
179
187
|
for (const importModule of imports) {
|
|
180
|
-
const
|
|
181
|
-
|
|
188
|
+
const importSpecifier = importModule.specifier;
|
|
189
|
+
if ((0, import_shared_utils.isExternalSpecifier)(importSpecifier, bundleConfig)) {
|
|
190
|
+
this.handleExternalBundle(importSpecifier, siteBundles, bundleConfig.external, outputDir);
|
|
191
|
+
} else {
|
|
192
|
+
const jsUri = importSpecifier.startsWith("/") ? importSpecifier : (0, import_shared_utils.getSpecifier)(importModule);
|
|
193
|
+
dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
|
|
194
|
+
}
|
|
182
195
|
}
|
|
183
196
|
const dynamicImports = moduleDefinition.linkedModuleRecord?.dynamicImports || moduleDefinition.bundleRecord?.dynamicImports || [];
|
|
184
197
|
for (const importModule of dynamicImports) {
|
|
185
|
-
const
|
|
186
|
-
|
|
198
|
+
const importSpecifier = importModule.specifier;
|
|
199
|
+
if ((0, import_shared_utils.isExternalSpecifier)(importSpecifier, bundleConfig)) {
|
|
200
|
+
this.handleExternalBundle(importSpecifier, siteBundles, bundleConfig.external, outputDir);
|
|
201
|
+
} else {
|
|
202
|
+
const jsUri = importSpecifier.startsWith("/") ? importSpecifier : (0, import_shared_utils.getSpecifier)(importModule);
|
|
203
|
+
dispatchRequests.push(this.dispatchJSResourceRecursive(jsUri, dispatcher, siteConfig));
|
|
204
|
+
}
|
|
187
205
|
}
|
|
188
206
|
if (moduleDefinition.bundleRecord) {
|
|
189
207
|
this.addBundleToSiteMetadata(moduleDefinition, url, siteConfig);
|
|
@@ -195,14 +213,31 @@ var SiteGenerator = class {
|
|
|
195
213
|
}
|
|
196
214
|
await Promise.all(dispatchRequests);
|
|
197
215
|
}
|
|
216
|
+
handleExternalBundle(specifier, siteBundles, externals, outputDir) {
|
|
217
|
+
if (siteBundles && externals[specifier]?.startsWith(import_shared_utils.PROTOCOL_FILE) && !siteBundles[specifier]) {
|
|
218
|
+
const path = (0, import_shared_utils.normalizeFromFileURL)(externals[specifier], process.cwd());
|
|
219
|
+
const normalizedPath = decodeURIComponent(path);
|
|
220
|
+
(0, import_dir.createResourceDir)((0, import_path.dirname)(normalizedPath), outputDir);
|
|
221
|
+
const ext = (0, import_path.extname)(normalizedPath);
|
|
222
|
+
const fullPath = (0, import_path.join)(outputDir, `${normalizedPath}${ext ? "" : ".js"}`);
|
|
223
|
+
import_fs_extra.default.copyFileSync((0, import_url.fileURLToPath)(externals[specifier]), fullPath);
|
|
224
|
+
const bundleMetadata = {
|
|
225
|
+
specifier,
|
|
226
|
+
path: normalizedPath,
|
|
227
|
+
imports: []
|
|
228
|
+
};
|
|
229
|
+
siteBundles[specifier] = bundleMetadata;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
198
232
|
addBundleToSiteMetadata(bundleDefinition, url, siteConfig) {
|
|
199
233
|
if (siteConfig.siteMetadata) {
|
|
200
234
|
const locale = siteConfig.locale;
|
|
201
235
|
const specifier = siteConfig.i18n.defaultLocale === locale ? bundleDefinition.specifier : `${bundleDefinition.specifier}|l/${locale}`;
|
|
202
236
|
const imports = bundleDefinition.bundleRecord.imports?.map((moduleRef) => (0, import_shared_utils.getSpecifier)(moduleRef)) || [];
|
|
203
237
|
const dynamicImports = bundleDefinition.bundleRecord.dynamicImports?.map((moduleRef) => (0, import_shared_utils.getSpecifier)(moduleRef));
|
|
238
|
+
const version = bundleDefinition.version === import_shared_utils.VERSION_NOT_PROVIDED ? void 0 : bundleDefinition.version;
|
|
204
239
|
const bundleMetadata = {
|
|
205
|
-
version
|
|
240
|
+
version,
|
|
206
241
|
path: decodeURIComponent(url),
|
|
207
242
|
includedModules: bundleDefinition.bundleRecord.includedModules || [],
|
|
208
243
|
imports,
|
|
@@ -79,16 +79,17 @@ var NetworkDispatcher = class {
|
|
|
79
79
|
const jsonResponse = JSON.parse(body);
|
|
80
80
|
resolve(jsonResponse);
|
|
81
81
|
} catch (err) {
|
|
82
|
+
const message = process.env.MRT_HMR !== "true" ? `unexpected response body: [${method}][${lang}] ${url}: '${body}'` : `unexpected response body: [${method}][${lang}] ${url}`;
|
|
82
83
|
if (import_diagnostics.logger.isDebugEnabled()) {
|
|
83
84
|
import_diagnostics.logger.warn({
|
|
84
85
|
label: `NetworkDispatcher`,
|
|
85
|
-
message
|
|
86
|
+
message,
|
|
86
87
|
additionalInfo: err
|
|
87
88
|
});
|
|
88
89
|
} else {
|
|
89
90
|
import_diagnostics.logger.warn({
|
|
90
91
|
label: `NetworkDispatcher`,
|
|
91
|
-
message
|
|
92
|
+
message
|
|
92
93
|
});
|
|
93
94
|
}
|
|
94
95
|
resolve({});
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
1
2
|
import { deepFreeze } from '@lwrjs/shared-utils';
|
|
3
|
+
import { SiteMetadataImpl } from '@lwrjs/static/site-metadata';
|
|
2
4
|
export function createProviderContext(serverContext) {
|
|
3
5
|
// This is a subset of config to user-land code
|
|
4
|
-
const { assets, cacheDir, i18n, lwc: { modules = [] }, routes, errorRoutes, rootDir, contentDir, layoutsDir, locker, amdLoader, esmLoader, environment, basePath, bundleConfig, } = serverContext.appConfig;
|
|
6
|
+
const { assets, cacheDir, i18n, lwc: { modules = [] }, routes, errorRoutes, rootDir, contentDir, layoutsDir, locker, amdLoader, esmLoader, environment, basePath, bundleConfig, staticSiteGenerator, } = serverContext.appConfig;
|
|
5
7
|
const { onModuleDefinitionChange, onModuleSourceChange } = serverContext.appObserver;
|
|
6
8
|
const { notifyModuleDefinitionChanged, notifyModuleSourceChanged, notifyViewSourceChanged, notifyAssetSourceChanged, } = serverContext.appEmitter;
|
|
9
|
+
const siteMetadata = staticSiteGenerator.outputDir && fs.existsSync(staticSiteGenerator.outputDir)
|
|
10
|
+
? new SiteMetadataImpl({ rootDir: staticSiteGenerator.outputDir })
|
|
11
|
+
: undefined;
|
|
7
12
|
return {
|
|
8
13
|
appObserver: deepFreeze({ onModuleDefinitionChange, onModuleSourceChange }),
|
|
9
14
|
appEmitter: {
|
|
@@ -36,6 +41,7 @@ export function createProviderContext(serverContext) {
|
|
|
36
41
|
}),
|
|
37
42
|
runtimeEnvironment: deepFreeze(serverContext.runtimeEnvironment),
|
|
38
43
|
watcherFactory: serverContext.watcherFactory,
|
|
44
|
+
siteMetadata,
|
|
39
45
|
};
|
|
40
46
|
}
|
|
41
47
|
//# sourceMappingURL=provider.js.map
|