@lwrjs/core 0.11.0-alpha.1 → 0.11.0-alpha.11
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 +2 -0
- package/build/cjs/context/server.cjs +1 -1
- package/build/cjs/index.cjs +3 -2
- package/build/cjs/middleware/asset-middleware.cjs +2 -1
- package/build/cjs/middleware/bundle-middleware.cjs +17 -6
- package/build/cjs/middleware/hmr-middleware.cjs +6 -5
- package/build/cjs/middleware/locale-middleware.cjs +2 -2
- package/build/cjs/middleware/mapping-middleware.cjs +9 -3
- package/build/cjs/middleware/module-middleware.cjs +8 -3
- package/build/cjs/middleware/request-processor-middleware.cjs +64 -0
- package/build/cjs/middleware/resource-middleware.cjs +8 -3
- package/build/cjs/middleware/utils/error-handling.cjs +5 -4
- package/build/cjs/middleware/view-middleware.cjs +16 -14
- package/build/cjs/middleware.cjs +2 -0
- package/build/cjs/tools/server-warmup.cjs +7 -7
- package/build/cjs/tools/static-generation.cjs +68 -67
- package/build/cjs/tools/utils/network-dispatcher.cjs +12 -11
- package/build/es/context/provider.js +2 -1
- package/build/es/context/server.js +1 -2
- package/build/es/index.js +5 -4
- package/build/es/middleware/asset-middleware.js +2 -2
- package/build/es/middleware/bundle-middleware.js +6 -6
- package/build/es/middleware/hmr-middleware.js +6 -5
- package/build/es/middleware/locale-middleware.js +2 -2
- package/build/es/middleware/mapping-middleware.js +3 -3
- package/build/es/middleware/module-middleware.js +3 -3
- package/build/es/middleware/request-processor-middleware.d.ts +3 -0
- package/build/es/middleware/request-processor-middleware.js +53 -0
- package/build/es/middleware/resource-middleware.js +3 -3
- package/build/es/middleware/utils/error-handling.js +5 -4
- package/build/es/middleware/view-middleware.js +17 -14
- package/build/es/middleware.d.ts +1 -0
- package/build/es/middleware.js +1 -0
- package/build/es/tools/server-warmup.js +3 -3
- package/build/es/tools/static-generation.d.ts +6 -2
- package/build/es/tools/static-generation.js +62 -54
- package/build/es/tools/types.d.ts +2 -1
- package/build/es/tools/utils/network-dispatcher.js +2 -1
- package/package.json +31 -29
|
@@ -28,6 +28,7 @@ __export(exports, {
|
|
|
28
28
|
default: () => static_generation_default
|
|
29
29
|
});
|
|
30
30
|
var import_perf_hooks = __toModule(require("perf_hooks"));
|
|
31
|
+
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
31
32
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
32
33
|
var import_site_metadata = __toModule(require("@lwrjs/static/site-metadata"));
|
|
33
34
|
var import_path = __toModule(require("path"));
|
|
@@ -38,74 +39,79 @@ var import_config = __toModule(require("@lwrjs/config"));
|
|
|
38
39
|
var SiteGenerator = class {
|
|
39
40
|
async buildStaticApplication(config, dispatcher) {
|
|
40
41
|
const startTime = import_perf_hooks.performance.now();
|
|
41
|
-
|
|
42
|
-
if (!
|
|
43
|
-
|
|
42
|
+
import_diagnostics.logger.info("[SSG] Static Site Generation");
|
|
43
|
+
if (!import_diagnostics.logger.currentLevel || import_diagnostics.logger.currentLevel == import_diagnostics.WARN || import_diagnostics.logger.currentLevel == import_diagnostics.INFO) {
|
|
44
|
+
import_diagnostics.logger.setOptions({dedupe: new Set([import_diagnostics.WARN])});
|
|
44
45
|
}
|
|
45
|
-
const {
|
|
46
|
+
const {assets, basePath, i18n, rootDir, routes, staticSiteGenerator} = config;
|
|
46
47
|
if (!staticSiteGenerator.outputDir) {
|
|
47
48
|
staticSiteGenerator.outputDir = "site";
|
|
48
49
|
}
|
|
49
50
|
const outputDir = (0, import_path.join)(rootDir, staticSiteGenerator.outputDir);
|
|
50
51
|
if (!staticSiteGenerator.skipCleanOutputDir) {
|
|
51
|
-
|
|
52
|
+
import_diagnostics.logger.info(`[SSG] Clearing output directory: ${outputDir}`);
|
|
52
53
|
import_fs_extra.default.rmSync(outputDir, {recursive: true, force: true});
|
|
53
54
|
} else if (import_fs_extra.default.existsSync(outputDir)) {
|
|
54
|
-
|
|
55
|
+
import_diagnostics.logger.info(`[SSG] Reusing existing output directory: ${outputDir}`);
|
|
55
56
|
}
|
|
56
57
|
const urlRewriteMap = new Map();
|
|
57
|
-
const {basePath} = config;
|
|
58
58
|
const runtimeEnvironment = (0, import_config.getRuntimeEnvironment)(config);
|
|
59
|
-
|
|
60
|
-
await this.generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir, urlRewriteMap);
|
|
59
|
+
import_diagnostics.logger.info(`[SSG] Building routes (this may take some time to complete)`);
|
|
60
|
+
await this.generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir, i18n, urlRewriteMap);
|
|
61
61
|
this.writeNetlifyRedirectConfig(outputDir, urlRewriteMap);
|
|
62
62
|
await this.copyAssets(assets, outputDir, config);
|
|
63
63
|
const endTime = import_perf_hooks.performance.now();
|
|
64
64
|
const timeDiff = (endTime - startTime) / 1e3;
|
|
65
|
-
|
|
65
|
+
import_diagnostics.logger.info(`[SSG] Static Site Generation complete in ${Math.round(timeDiff)} seconds`);
|
|
66
66
|
}
|
|
67
|
-
async generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir, urlRewriteMap = new Map()) {
|
|
68
|
-
if (!staticSiteGenerator.locales) {
|
|
69
|
-
staticSiteGenerator.locales = ["en-US"];
|
|
70
|
-
}
|
|
67
|
+
async generateRoutes(runtimeEnvironment, staticSiteGenerator, routes, basePath, dispatcher, outputDir, i18n, urlRewriteMap = new Map()) {
|
|
71
68
|
const generateUrl = this.createGenerateURLFunction(dispatcher);
|
|
72
69
|
const {skipBaseDocumentGeneration = false} = staticSiteGenerator;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
if (!i18n.uriPattern) {
|
|
71
|
+
await this.generateRoutesForLocale(routes, outputDir, {id: i18n.defaultLocale}, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n, basePath, generateUrl, staticSiteGenerator, dispatcher);
|
|
72
|
+
} else {
|
|
73
|
+
for (const locale of i18n.locales) {
|
|
74
|
+
import_diagnostics.logger.debug(`[SSG] Generate routes of locale: ${locale.id}`);
|
|
75
|
+
await this.generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n, basePath, generateUrl, staticSiteGenerator, dispatcher);
|
|
77
76
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async generateRoutesForLocale(routes, outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n, basePath, generateUrl, staticSiteGenerator, dispatcher) {
|
|
80
|
+
for (const route of routes) {
|
|
81
|
+
const siteConfig = this.createSiteConfig(outputDir, locale.id, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n);
|
|
82
|
+
const localizedPath = (0, import_shared_utils.getViewUri)(route.path, basePath, locale.id, i18n);
|
|
83
|
+
await generateUrl(localizedPath, siteConfig);
|
|
84
|
+
}
|
|
85
|
+
if (staticSiteGenerator._additionalRoutePaths) {
|
|
86
|
+
for (const uri of staticSiteGenerator._additionalRoutePaths) {
|
|
87
|
+
const siteConfig = this.createSiteConfig(outputDir, locale.id, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n);
|
|
88
|
+
await generateUrl(uri, siteConfig);
|
|
83
89
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
90
|
+
}
|
|
91
|
+
const {_additionalModules} = staticSiteGenerator;
|
|
92
|
+
if (_additionalModules) {
|
|
93
|
+
for (const specifier of _additionalModules) {
|
|
94
|
+
import_diagnostics.logger.debug(`[SSG] Additional Module: ${locale.id} ${specifier}`);
|
|
95
|
+
const startTime = import_perf_hooks.performance.now();
|
|
96
|
+
const siteConfig = this.createSiteConfig(outputDir, locale.id, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n);
|
|
97
|
+
await this.dispatchJSResourceRecursive(specifier, dispatcher, siteConfig, true);
|
|
98
|
+
await this.captureAdditionalRouteMetadata(siteConfig);
|
|
99
|
+
const endTime = import_perf_hooks.performance.now();
|
|
100
|
+
const timeDiff = endTime - startTime;
|
|
101
|
+
import_diagnostics.logger.info(`[SSG] Additional Module ${locale.id} ${specifier} in ${Math.round(timeDiff)} ms`);
|
|
96
102
|
}
|
|
97
103
|
}
|
|
98
104
|
}
|
|
99
105
|
createGenerateURLFunction(dispatcher) {
|
|
100
106
|
const generateRoute = async (uri, siteConfig) => {
|
|
101
107
|
const locale = siteConfig.locale;
|
|
102
|
-
|
|
108
|
+
import_diagnostics.logger.debug(`[SSG] Start Generate: ${locale} ${uri}`);
|
|
103
109
|
const startTime = import_perf_hooks.performance.now();
|
|
104
110
|
await this.dispatchResourceRecursive(uri, dispatcher, {resourceType: "route"}, siteConfig);
|
|
105
111
|
await this.captureAdditionalRouteMetadata(siteConfig);
|
|
106
112
|
const endTime = import_perf_hooks.performance.now();
|
|
107
113
|
const timeDiff = endTime - startTime;
|
|
108
|
-
|
|
114
|
+
import_diagnostics.logger.info(`[SSG] ${locale} ${uri} in ${Math.round(timeDiff)} ms`);
|
|
109
115
|
};
|
|
110
116
|
return generateRoute.bind(this);
|
|
111
117
|
}
|
|
@@ -114,11 +120,11 @@ var SiteGenerator = class {
|
|
|
114
120
|
if (!visitedUrls.has(url)) {
|
|
115
121
|
visitedUrls.add(url);
|
|
116
122
|
if ((0, import_shared_utils.isExternalUrl)(url)) {
|
|
117
|
-
|
|
123
|
+
import_diagnostics.logger.warn("[SSG] Skipped generation of external url: " + url);
|
|
118
124
|
return;
|
|
119
125
|
}
|
|
120
126
|
if (url.indexOf("/:") !== -1 || url.indexOf("*") !== -1) {
|
|
121
|
-
|
|
127
|
+
import_diagnostics.logger.warn("[SSG] Skipped generation of url with variable path segment: " + url);
|
|
122
128
|
return;
|
|
123
129
|
}
|
|
124
130
|
let context;
|
|
@@ -179,7 +185,8 @@ var SiteGenerator = class {
|
|
|
179
185
|
}
|
|
180
186
|
addBundleToSiteMetadata(bundleDefinition, url, siteConfig) {
|
|
181
187
|
if (siteConfig.siteMetadata) {
|
|
182
|
-
const
|
|
188
|
+
const locale = siteConfig.locale;
|
|
189
|
+
const specifier = siteConfig.i18n.defaultLocale === locale ? bundleDefinition.specifier : `${bundleDefinition.specifier}|l/${locale}`;
|
|
183
190
|
const imports = bundleDefinition.bundleRecord.imports?.map((moduleRef) => (0, import_shared_utils.getSpecifier)(moduleRef)) || [];
|
|
184
191
|
const bundleMetadata = {
|
|
185
192
|
version: bundleDefinition.version,
|
|
@@ -194,7 +201,7 @@ var SiteGenerator = class {
|
|
|
194
201
|
addResourceToSiteMetadata(resourceDefinition, url, siteConfig) {
|
|
195
202
|
if (siteConfig.siteMetadata) {
|
|
196
203
|
if (!resourceDefinition.specifier) {
|
|
197
|
-
|
|
204
|
+
import_diagnostics.logger.warn("[SSG] Could not save resource metadata. There was no specifier.", resourceDefinition);
|
|
198
205
|
} else {
|
|
199
206
|
const specifier = resourceDefinition.specifier;
|
|
200
207
|
const resourceMetadata = {
|
|
@@ -210,7 +217,7 @@ var SiteGenerator = class {
|
|
|
210
217
|
addAssetToSiteMetadata(assetDefinition, url, siteConfig) {
|
|
211
218
|
if (siteConfig.siteMetadata) {
|
|
212
219
|
if (!assetDefinition.uri) {
|
|
213
|
-
|
|
220
|
+
import_diagnostics.logger.warn("[SSG] Could not save asset metadata. There was no uri.", assetDefinition);
|
|
214
221
|
} else {
|
|
215
222
|
const specifier = assetDefinition.uri;
|
|
216
223
|
const resourceMetadata = {
|
|
@@ -221,7 +228,7 @@ var SiteGenerator = class {
|
|
|
221
228
|
if (!siteAssets.assets[specifier]) {
|
|
222
229
|
siteAssets.assets[specifier] = resourceMetadata;
|
|
223
230
|
} else {
|
|
224
|
-
|
|
231
|
+
import_diagnostics.logger.debug(`[SSG] Ignore asset redefinition ${specifier}`);
|
|
225
232
|
}
|
|
226
233
|
}
|
|
227
234
|
}
|
|
@@ -244,7 +251,7 @@ var SiteGenerator = class {
|
|
|
244
251
|
await Promise.all(dispatchRequests);
|
|
245
252
|
} else {
|
|
246
253
|
const body = context.fs?.body;
|
|
247
|
-
|
|
254
|
+
import_diagnostics.logger.warn(`[SSG] Failed to fetch ${url}: (${statusCode}) ${body}`);
|
|
248
255
|
}
|
|
249
256
|
}
|
|
250
257
|
async handleHtmlResource(url, context, siteConfig, dispatcher) {
|
|
@@ -258,16 +265,9 @@ var SiteGenerator = class {
|
|
|
258
265
|
directoryPath = url.substring(0, lastPathIndex);
|
|
259
266
|
}
|
|
260
267
|
const dir = (0, import_dir.createResourceDir)(directoryPath, outputDir);
|
|
261
|
-
const localeDir = (0, import_dir.createResourceDir)(directoryPath, (0, import_path.join)(outputDir, siteConfig.locale));
|
|
262
268
|
const filePath = (0, import_path.join)(dir, fileName);
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
siteConfig.viewPaths.add(filePath);
|
|
266
|
-
await (0, import_stream.writeResponse)(context, filePath);
|
|
267
|
-
}
|
|
268
|
-
(0, import_dir.createDir)(localeDir);
|
|
269
|
-
siteConfig.viewPaths.add(fileLocalePath);
|
|
270
|
-
await (0, import_stream.writeResponse)(context, fileLocalePath);
|
|
269
|
+
siteConfig.viewPaths.add(filePath);
|
|
270
|
+
await (0, import_stream.writeResponse)(context, filePath);
|
|
271
271
|
}
|
|
272
272
|
const viewDefinition = context.fs?.metadata?.viewDefinition;
|
|
273
273
|
if (viewDefinition) {
|
|
@@ -310,7 +310,7 @@ var SiteGenerator = class {
|
|
|
310
310
|
siteConfig.viewConfigPath = this.getResourcePathFromUrl(siteConfig, resourceUri);
|
|
311
311
|
}
|
|
312
312
|
} else {
|
|
313
|
-
|
|
313
|
+
import_diagnostics.logger.warn("[SSG] Skipped inline bootstrap resource: %j", resource);
|
|
314
314
|
}
|
|
315
315
|
}
|
|
316
316
|
}
|
|
@@ -320,7 +320,7 @@ var SiteGenerator = class {
|
|
|
320
320
|
if (resourceUri.startsWith("/")) {
|
|
321
321
|
dispatchRequests.push(this.dispatchResourceRecursive(resourceUri, dispatcher, {resourceType: "resource"}, siteConfig));
|
|
322
322
|
} else {
|
|
323
|
-
|
|
323
|
+
import_diagnostics.logger.warn("[SSG] Skipped resource: %j", resource);
|
|
324
324
|
}
|
|
325
325
|
}
|
|
326
326
|
await Promise.all(dispatchRequests);
|
|
@@ -336,7 +336,7 @@ var SiteGenerator = class {
|
|
|
336
336
|
const mappingURL = siteConfig.endpoints?.uris?.mapping + encodeURIComponent(jsUri);
|
|
337
337
|
await this.dispatchResourceRecursive(mappingURL, dispatcher, {resourceType: "mapping"}, siteConfig);
|
|
338
338
|
} else {
|
|
339
|
-
|
|
339
|
+
import_diagnostics.logger.warn('[SSG] Unable to fetch mapping for bare specifier or variable dynamic import: "' + jsUri + '"');
|
|
340
340
|
}
|
|
341
341
|
} else if (isAdditionalModulesRequest) {
|
|
342
342
|
const uri = `${siteConfig.endpoints?.uris.legacyDefault}${encodeURIComponent(jsUri)}`;
|
|
@@ -358,7 +358,7 @@ var SiteGenerator = class {
|
|
|
358
358
|
for (const ref of assetReferences) {
|
|
359
359
|
const refUrl = ref.override?.uri || ref.url;
|
|
360
360
|
dispatchRequests.push(this.dispatchResourceRecursive(refUrl, dispatcher, {resourceType: "asset", asset: metadata?.asset}, siteConfig).catch((err) => {
|
|
361
|
-
|
|
361
|
+
import_diagnostics.logger.warn(`[SSG] Failed to fetch asset reference => ${refUrl} from ${url}`, err);
|
|
362
362
|
}));
|
|
363
363
|
}
|
|
364
364
|
return Promise.all(dispatchRequests);
|
|
@@ -391,7 +391,7 @@ var SiteGenerator = class {
|
|
|
391
391
|
async copyAssets(assets, outputDir, config) {
|
|
392
392
|
const {basePath} = config;
|
|
393
393
|
const runtimeEnvironment = (0, import_config.getRuntimeEnvironment)(config);
|
|
394
|
-
const siteConfig = this.createSiteConfig(outputDir, "en-US", new Map(), true, runtimeEnvironment);
|
|
394
|
+
const siteConfig = this.createSiteConfig(outputDir, "en-US", new Map(), true, runtimeEnvironment, config.i18n);
|
|
395
395
|
for (const asset of assets) {
|
|
396
396
|
try {
|
|
397
397
|
const assetSrcFile = asset.file;
|
|
@@ -418,11 +418,11 @@ var SiteGenerator = class {
|
|
|
418
418
|
import_fs_extra.default.copySync(assetSrcFile, assetsPath);
|
|
419
419
|
this.addAssetToMetadata(assetsPath, siteConfig);
|
|
420
420
|
} else {
|
|
421
|
-
|
|
421
|
+
import_diagnostics.logger.warn("[SSG] Could not find assets to copy at path: " + assetsPath);
|
|
422
422
|
}
|
|
423
423
|
} catch (e) {
|
|
424
|
-
|
|
425
|
-
|
|
424
|
+
import_diagnostics.logger.error("[SSG] Error occurred processing asset config: " + JSON.stringify(asset));
|
|
425
|
+
import_diagnostics.logger.error(e);
|
|
426
426
|
}
|
|
427
427
|
}
|
|
428
428
|
await siteConfig.siteMetadata?.persistSiteMetadata();
|
|
@@ -439,7 +439,7 @@ var SiteGenerator = class {
|
|
|
439
439
|
}
|
|
440
440
|
}
|
|
441
441
|
} catch (err) {
|
|
442
|
-
|
|
442
|
+
import_diagnostics.logger.warn(`[SSG] Unexpected error collecting asset directory metadata for ${directoryPath}`, err);
|
|
443
443
|
}
|
|
444
444
|
}
|
|
445
445
|
addAssetToMetadata(filePath, siteConfig) {
|
|
@@ -448,22 +448,22 @@ var SiteGenerator = class {
|
|
|
448
448
|
this.addAssetToSiteMetadata({
|
|
449
449
|
uri,
|
|
450
450
|
type: "asset",
|
|
451
|
-
stream: function(
|
|
451
|
+
stream: function(_encoding) {
|
|
452
452
|
throw new Error("Function not implemented.");
|
|
453
453
|
},
|
|
454
454
|
entry: filePath,
|
|
455
455
|
ext: (0, import_path.extname)(filePath),
|
|
456
456
|
mime: (0, import_shared_utils.mimeLookup)(filePath),
|
|
457
457
|
ownHash: "not-provided",
|
|
458
|
-
content: function(
|
|
458
|
+
content: function(_encoding) {
|
|
459
459
|
throw new Error("Function not implemented.");
|
|
460
460
|
}
|
|
461
461
|
}, uri, siteConfig);
|
|
462
462
|
} catch (err) {
|
|
463
|
-
|
|
463
|
+
import_diagnostics.logger.warn(`[SSG] Unexpected error collecting asset metadata for ${filePath}`, err);
|
|
464
464
|
}
|
|
465
465
|
}
|
|
466
|
-
createSiteConfig(outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment) {
|
|
466
|
+
createSiteConfig(outputDir, locale, urlRewriteMap, skipBaseDocumentGeneration, runtimeEnvironment, i18n) {
|
|
467
467
|
const featureFlags = this.filterFeatureFlags();
|
|
468
468
|
const endpoints = {
|
|
469
469
|
uris: {
|
|
@@ -480,7 +480,8 @@ var SiteGenerator = class {
|
|
|
480
480
|
endpoints,
|
|
481
481
|
skipBaseDocumentGeneration,
|
|
482
482
|
...featureFlags,
|
|
483
|
-
siteMetadata: new import_site_metadata.SiteMetadataImpl({rootDir: outputDir})
|
|
483
|
+
siteMetadata: new import_site_metadata.SiteMetadataImpl({rootDir: outputDir}),
|
|
484
|
+
i18n
|
|
484
485
|
};
|
|
485
486
|
}
|
|
486
487
|
filterFeatureFlags() {
|
|
@@ -28,6 +28,7 @@ __export(exports, {
|
|
|
28
28
|
});
|
|
29
29
|
var import_http = __toModule(require("http"));
|
|
30
30
|
var import_https = __toModule(require("https"));
|
|
31
|
+
var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
31
32
|
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
32
33
|
var NetworkDispatcher = class {
|
|
33
34
|
constructor(port, internalRequestKey) {
|
|
@@ -59,46 +60,46 @@ var NetworkDispatcher = class {
|
|
|
59
60
|
return new Promise((resolve, reject) => {
|
|
60
61
|
const httpClient = options.port == 443 ? import_https.default : import_http.default;
|
|
61
62
|
const bodyChunks = [];
|
|
62
|
-
|
|
63
|
+
import_diagnostics.logger.verbose(`[NetworkDispatcher] Request: [${method}][${lang}] ${url}`);
|
|
63
64
|
const req = httpClient.request(options, (res) => {
|
|
64
65
|
res.on("data", (chunk) => {
|
|
65
66
|
bodyChunks.push(chunk);
|
|
66
67
|
});
|
|
67
68
|
res.on("end", () => {
|
|
68
|
-
|
|
69
|
+
import_diagnostics.logger.verbose(`[END][NetworkDispatcher] Request: [${method}][${lang}] ${url}`);
|
|
69
70
|
if (!res.statusCode || res.statusCode >= 200 && res.statusCode < 300) {
|
|
70
71
|
const body = Buffer.concat(bodyChunks).toString();
|
|
71
72
|
try {
|
|
72
73
|
const jsonResponse = JSON.parse(body);
|
|
73
74
|
resolve(jsonResponse);
|
|
74
75
|
} catch (err) {
|
|
75
|
-
if (
|
|
76
|
-
|
|
76
|
+
if (import_diagnostics.logger.currentLevel == import_diagnostics.DEBUG || import_diagnostics.logger.currentLevel == import_diagnostics.VERBOSE) {
|
|
77
|
+
import_diagnostics.logger.warn(`[NetworkDispatcher] unexpected response body: [${method}][${lang}] ${url}: '${body}'`, err);
|
|
77
78
|
} else {
|
|
78
|
-
|
|
79
|
+
import_diagnostics.logger.warn(`[NetworkDispatcher] unexpected response body: [${method}][${lang}] ${url}: '${body}'`);
|
|
79
80
|
}
|
|
80
81
|
resolve({});
|
|
81
82
|
}
|
|
82
83
|
} else if (res.statusCode === 301 || res.statusCode === 302) {
|
|
83
84
|
if (res.headers?.location && (0, import_shared_utils.isModuleOrBundleUrl)(res.headers?.location)) {
|
|
84
|
-
|
|
85
|
+
import_diagnostics.logger.debug(`[NetworkDispatcher] Follow redirect: [${method}][${lang}][${res.statusCode}] ${url} -> ${res.headers.location}`);
|
|
85
86
|
const location = res.headers.location;
|
|
86
87
|
return this.handleRequest(this.createRequestOptions(location, method, lang), location, method, lang).then((resRedirect) => resolve(resRedirect)).catch((rejectRedirect) => reject(rejectRedirect));
|
|
87
88
|
} else {
|
|
88
|
-
|
|
89
|
+
import_diagnostics.logger.warn(`Redirect not followed: [${method}][${lang}][${res.statusCode}] ${url} -> ${res.headers.location}`);
|
|
89
90
|
resolve({});
|
|
90
91
|
}
|
|
91
92
|
} else {
|
|
92
|
-
|
|
93
|
+
import_diagnostics.logger.warn(`Unexpected status code: [${method}][${lang}][${res.statusCode}] ${url}`);
|
|
93
94
|
resolve({});
|
|
94
95
|
}
|
|
95
96
|
});
|
|
96
97
|
});
|
|
97
98
|
req.on("error", (err) => {
|
|
98
|
-
if (
|
|
99
|
-
|
|
99
|
+
if (import_diagnostics.logger.currentLevel == import_diagnostics.DEBUG || import_diagnostics.logger.currentLevel == import_diagnostics.VERBOSE) {
|
|
100
|
+
import_diagnostics.logger.warn(`[NetworkDispatcher] Request Failed: [${method}][${lang}] ${url}`, err);
|
|
100
101
|
} else {
|
|
101
|
-
|
|
102
|
+
import_diagnostics.logger.warn(`[NetworkDispatcher] Request Failed: [${method}][${lang}] ${url}`);
|
|
102
103
|
}
|
|
103
104
|
resolve({});
|
|
104
105
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { deepFreeze } from '@lwrjs/shared-utils';
|
|
2
2
|
export function createProviderContext(serverContext) {
|
|
3
3
|
// This is a subset of config to user-land code
|
|
4
|
-
const { assets, cacheDir, lwc: { modules = [] }, routes, errorRoutes, rootDir, contentDir, layoutsDir, locker, amdLoader, esmLoader, environment, basePath, bundleConfig, } = serverContext.appConfig;
|
|
4
|
+
const { assets, cacheDir, i18n, lwc: { modules = [] }, routes, errorRoutes, rootDir, contentDir, layoutsDir, locker, amdLoader, esmLoader, environment, basePath, bundleConfig, } = serverContext.appConfig;
|
|
5
5
|
const { onModuleDefinitionChange, onModuleSourceChange } = serverContext.appObserver;
|
|
6
6
|
const { notifyModuleDefinitionChanged, notifyModuleSourceChanged, notifyViewSourceChanged, notifyAssetSourceChanged, } = serverContext.appEmitter;
|
|
7
7
|
return {
|
|
@@ -20,6 +20,7 @@ export function createProviderContext(serverContext) {
|
|
|
20
20
|
config: deepFreeze({
|
|
21
21
|
assets,
|
|
22
22
|
cacheDir,
|
|
23
|
+
i18n,
|
|
23
24
|
modules,
|
|
24
25
|
routes,
|
|
25
26
|
errorRoutes,
|
|
@@ -4,8 +4,7 @@ import { LwrModuleRegistry } from '@lwrjs/module-registry';
|
|
|
4
4
|
import { LwrResourceRegistry } from '@lwrjs/resource-registry';
|
|
5
5
|
import { LwrApplicationObserver } from '@lwrjs/shared-utils';
|
|
6
6
|
import { LwrViewRegistry } from '@lwrjs/view-registry';
|
|
7
|
-
|
|
8
|
-
import { WatcherFactoryImpl } from '@lwrjs/shared-utils/fs-watch';
|
|
7
|
+
import { WatcherFactoryImpl } from '@lwrjs/fs-watch';
|
|
9
8
|
export function createServerContext(appConfig, runtimeEnvironment, globalData) {
|
|
10
9
|
const appObserver = new LwrApplicationObserver();
|
|
11
10
|
const appEmitter = appObserver.createLwrEmitter();
|
package/build/es/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { getFeatureFlags, DEFAULT_LWR_BOOTSTRAP_CONFIG
|
|
1
|
+
import { getFeatureFlags, DEFAULT_LWR_BOOTSTRAP_CONFIG } from '@lwrjs/shared-utils';
|
|
2
2
|
import { createInternalServer } from '@lwrjs/server';
|
|
3
|
-
import { LwrServerError, createSingleDiagnosticError, descriptions } from '@lwrjs/diagnostics';
|
|
3
|
+
import { LwrServerError, createSingleDiagnosticError, descriptions, logger } from '@lwrjs/diagnostics';
|
|
4
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';
|
|
8
8
|
import { createServerContext } from './context/server.js';
|
|
9
9
|
import { createProviderContext } from './context/provider.js';
|
|
10
|
+
import { requestProcessorMiddleware } from './middleware/request-processor-middleware.js';
|
|
10
11
|
import { localeMiddleware } from './middleware/locale-middleware.js';
|
|
11
12
|
import { moduleMiddleware } from './middleware/module-middleware.js';
|
|
12
13
|
import { bundleMiddleware } from './middleware/bundle-middleware.js';
|
|
@@ -17,8 +18,8 @@ import { resourceMiddleware } from './middleware/resource-middleware.js';
|
|
|
17
18
|
import { hmrMiddleware } from './middleware/hmr-middleware.js';
|
|
18
19
|
import { getTracer, CoreSpan } from '@lwrjs/instrumentation';
|
|
19
20
|
function initMiddleware(app, server, serverContext) {
|
|
20
|
-
//
|
|
21
|
-
app
|
|
21
|
+
// TODO if we knew the target here we could make this middleware optional to target=mrt
|
|
22
|
+
requestProcessorMiddleware(app, serverContext);
|
|
22
23
|
localeMiddleware(app, serverContext);
|
|
23
24
|
moduleMiddleware(app, serverContext);
|
|
24
25
|
bundleMiddleware(app, serverContext);
|
|
@@ -3,9 +3,9 @@ import { getAssetIdentity } from './utils/identity.js';
|
|
|
3
3
|
import { handleErrors } from './utils/error-handling.js';
|
|
4
4
|
import { DiagnosticsError } from '@lwrjs/diagnostics';
|
|
5
5
|
function createAssetMiddleware(context) {
|
|
6
|
-
const { assetRegistry, runtimeEnvironment: { basePath }, } = context;
|
|
6
|
+
const { appConfig: { i18n }, assetRegistry, runtimeEnvironment: { basePath }, } = context;
|
|
7
7
|
return async (req, res, next) => {
|
|
8
|
-
const { runtimeEnvironment } = req.getRuntimeContext(context.runtimeEnvironment);
|
|
8
|
+
const { runtimeEnvironment } = req.getRuntimeContext(context.runtimeEnvironment, i18n);
|
|
9
9
|
const { assetId, immutable, signature } = getAssetIdentity(req);
|
|
10
10
|
if (basePath && !assetId.specifier.startsWith(basePath)) {
|
|
11
11
|
assetId.specifier = path.join(basePath, assetId.specifier);
|
|
@@ -6,10 +6,10 @@ import { normalizeResolvedUris } from './utils/metadata.js';
|
|
|
6
6
|
import { createUnsignedBundleRedirect } from './redirects/unsigned-module-redirect.js';
|
|
7
7
|
import { handleErrors } from './utils/error-handling.js';
|
|
8
8
|
function createBundleMiddleware(context) {
|
|
9
|
-
const { moduleRegistry, moduleBundler } = context;
|
|
9
|
+
const { appConfig, appConfig: { i18n }, moduleRegistry, moduleBundler, runtimeEnvironment: defaultRuntimeEnvironment, } = context;
|
|
10
10
|
const unsignedBundleRedirect = createUnsignedBundleRedirect(moduleBundler);
|
|
11
11
|
return async (req, res) => {
|
|
12
|
-
if (!req.validateEnvironmentRequest(
|
|
12
|
+
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
13
13
|
res.status(400);
|
|
14
14
|
res.send(descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
15
15
|
return;
|
|
@@ -19,7 +19,7 @@ function createBundleMiddleware(context) {
|
|
|
19
19
|
res.send(descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(
|
|
22
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment, i18n);
|
|
23
23
|
const importer = req.query.importer
|
|
24
24
|
? await getRequestImporter(req, moduleRegistry, runtimeParams)
|
|
25
25
|
: undefined;
|
|
@@ -45,14 +45,14 @@ function createBundleMiddleware(context) {
|
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
function createSourceMapMiddleware(context) {
|
|
48
|
-
const { moduleBundler } = context;
|
|
48
|
+
const { appConfig, appConfig: { i18n }, moduleBundler, runtimeEnvironment: defaultRuntimeEnvironment, } = context;
|
|
49
49
|
return async (req, res) => {
|
|
50
|
-
if (!req.validateEnvironmentRequest(
|
|
50
|
+
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
51
51
|
res.status(400);
|
|
52
52
|
res.send(descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
53
53
|
return;
|
|
54
54
|
}
|
|
55
|
-
const { runtimeEnvironment } = req.getRuntimeContext(
|
|
55
|
+
const { runtimeEnvironment } = req.getRuntimeContext(defaultRuntimeEnvironment, i18n);
|
|
56
56
|
const { moduleId, signature } = getModuleIdentity(req);
|
|
57
57
|
const bundleDef = await moduleBundler.getModuleBundle(moduleId, runtimeEnvironment);
|
|
58
58
|
if (signature !== LATEST_SIGNATURE) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { WebSocketServer } from 'ws';
|
|
2
|
+
import { logger } from '@lwrjs/diagnostics';
|
|
2
3
|
import { getCacheKeyFromJson } from '@lwrjs/shared-utils';
|
|
3
4
|
import { getRequestProperties } from './utils/request.js';
|
|
4
5
|
let hmr;
|
|
@@ -33,10 +34,10 @@ class Hmr {
|
|
|
33
34
|
});
|
|
34
35
|
wss.on('error', (error) => {
|
|
35
36
|
if (error.code === 'EADDRINUSE') {
|
|
36
|
-
|
|
37
|
+
logger.error(`HMR Socket Server: Port ${this.context.appConfig.port} already in use.`);
|
|
37
38
|
}
|
|
38
39
|
else {
|
|
39
|
-
|
|
40
|
+
logger.error(error);
|
|
40
41
|
}
|
|
41
42
|
});
|
|
42
43
|
}
|
|
@@ -44,7 +45,7 @@ class Hmr {
|
|
|
44
45
|
const { moduleRegistry } = this;
|
|
45
46
|
const { specifier, namespace, name, version, ownHash: signature, runtimeEnvironment, runtimeParams, } = moduleDefinition;
|
|
46
47
|
const { format, debug, compat } = runtimeEnvironment;
|
|
47
|
-
|
|
48
|
+
logger.info(`Recompiled module: ${specifier}, ${signature}`);
|
|
48
49
|
const moduleId = {
|
|
49
50
|
specifier,
|
|
50
51
|
namespace,
|
|
@@ -72,7 +73,7 @@ class Hmr {
|
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
updateTemplateOnClients(compiledView) {
|
|
75
|
-
|
|
76
|
+
logger.info('Recompiled view source', compiledView.viewId);
|
|
76
77
|
const { contentTemplate } = compiledView.viewId;
|
|
77
78
|
if (this.connectedClients && contentTemplate) {
|
|
78
79
|
for (const ws of this.connectedClients.keys()) {
|
|
@@ -87,7 +88,7 @@ class Hmr {
|
|
|
87
88
|
}
|
|
88
89
|
updateAssetOnClients(asset) {
|
|
89
90
|
const assetId = asset.entry;
|
|
90
|
-
|
|
91
|
+
logger.info(`Updated asset: ${assetId}`);
|
|
91
92
|
if (this.connectedClients) {
|
|
92
93
|
for (const ws of this.connectedClients.keys()) {
|
|
93
94
|
ws.send(JSON.stringify({
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export function localeMiddleware(app, context) {
|
|
2
2
|
const { defaultLocale } = context.runtimeEnvironment;
|
|
3
|
-
app.use(async (req,
|
|
3
|
+
app.use(async (req, _res, next) => {
|
|
4
4
|
const langs = req.acceptsLanguages();
|
|
5
|
-
req.locale = langs?.length > 0 ? langs[0] : defaultLocale;
|
|
5
|
+
req.locale = langs?.length > 0 && langs[0] != '*' ? langs[0] : defaultLocale;
|
|
6
6
|
// await is required when calling next to support koa
|
|
7
7
|
await next();
|
|
8
8
|
});
|
|
@@ -3,14 +3,14 @@ import { descriptions } from '@lwrjs/diagnostics';
|
|
|
3
3
|
import { getMappingIdentity } from './utils/identity.js';
|
|
4
4
|
import { handleErrors } from './utils/error-handling.js';
|
|
5
5
|
function createMappingMiddleware(context) {
|
|
6
|
-
const { moduleBundler, moduleRegistry } = context;
|
|
6
|
+
const { appConfig, appConfig: { i18n }, moduleBundler, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment, } = context;
|
|
7
7
|
return async (req, res) => {
|
|
8
|
-
if (!req.validateEnvironmentRequest(
|
|
8
|
+
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
9
9
|
res.status(400);
|
|
10
10
|
res.send(descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
13
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(
|
|
13
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment, i18n);
|
|
14
14
|
const { moduleIds } = getMappingIdentity(req);
|
|
15
15
|
const importMetadata = await getImportMetadataMappings(moduleIds, runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler);
|
|
16
16
|
if (req.isSiteGeneration()) {
|
|
@@ -5,10 +5,10 @@ import { getModuleIdentity } from './utils/identity.js';
|
|
|
5
5
|
import { createUnsignedModuleRedirect } from './redirects/unsigned-module-redirect.js';
|
|
6
6
|
import { handleErrors } from './utils/error-handling.js';
|
|
7
7
|
function createModuleMiddleware(context) {
|
|
8
|
-
const { moduleRegistry } = context;
|
|
8
|
+
const { appConfig, appConfig: { i18n }, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment, } = context;
|
|
9
9
|
const unsignedRedirect = createUnsignedModuleRedirect(moduleRegistry);
|
|
10
10
|
return async (req, res) => {
|
|
11
|
-
if (!req.validateEnvironmentRequest(
|
|
11
|
+
if (!req.validateEnvironmentRequest(appConfig)) {
|
|
12
12
|
res.status(400);
|
|
13
13
|
res.send(descriptions.UNRESOLVABLE.INVALID_ENVIRONMENT(req.params.environment).message);
|
|
14
14
|
return;
|
|
@@ -18,7 +18,7 @@ function createModuleMiddleware(context) {
|
|
|
18
18
|
res.send(descriptions.UNRESOLVABLE.INVALID_JSON().message);
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(
|
|
21
|
+
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment, i18n);
|
|
22
22
|
const importer = req.query.importer
|
|
23
23
|
? await getRequestImporter(req, moduleRegistry, runtimeParams)
|
|
24
24
|
: undefined;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ServerContext, InternalAppServer, ServerTypes } from '@lwrjs/types';
|
|
2
|
+
export declare function requestProcessorMiddleware<T extends ServerTypes>(app: InternalAppServer<T>, context: ServerContext): void;
|
|
3
|
+
//# sourceMappingURL=request-processor-middleware.d.ts.map
|