@lwrjs/core 0.9.0-alpha.2 → 0.9.0-alpha.21
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 +84 -0
- package/build/cjs/context/server.cjs +75 -0
- package/build/cjs/index.cjs +69 -153
- package/build/cjs/middleware/asset-middleware.cjs +56 -0
- package/build/cjs/middleware/bundle-middleware.cjs +113 -0
- package/build/cjs/{middlewares → middleware}/hmr-middleware.cjs +4 -4
- package/build/cjs/{middlewares → middleware}/locale-middleware.cjs +2 -2
- package/build/cjs/middleware/mapping-middleware.cjs +61 -0
- package/build/cjs/middleware/module-middleware.cjs +88 -0
- package/build/cjs/middleware/redirects/unsigned-module-redirect.cjs +37 -0
- package/build/cjs/middleware/resource-middleware.cjs +64 -0
- package/build/cjs/{context/services.cjs → middleware/utils/error-handling.cjs} +28 -15
- package/build/cjs/middleware/utils/identity.cjs +93 -0
- package/build/cjs/{context/hooks.cjs → middleware/utils/metadata.cjs} +17 -26
- package/build/cjs/{middlewares/utils.cjs → middleware/utils/request.cjs} +17 -16
- package/build/cjs/middleware/view-middleware.cjs +151 -0
- package/build/cjs/middleware.cjs +43 -0
- package/build/cjs/tools/server-warmup.cjs +2 -9
- package/build/cjs/tools/static-generation.cjs +127 -49
- package/build/es/context/provider.d.ts +3 -0
- package/build/es/context/provider.js +39 -0
- package/build/es/context/server.d.ts +3 -0
- package/build/es/context/server.js +49 -0
- package/build/es/index.d.ts +4 -2
- package/build/es/index.js +89 -150
- package/build/es/middleware/asset-middleware.d.ts +3 -0
- package/build/es/middleware/asset-middleware.js +26 -0
- package/build/es/middleware/bundle-middleware.d.ts +3 -0
- package/build/es/middleware/bundle-middleware.js +88 -0
- package/build/es/middleware/hmr-middleware.d.ts +5 -0
- package/build/es/{middlewares → middleware}/hmr-middleware.js +2 -2
- package/build/es/middleware/locale-middleware.d.ts +3 -0
- package/build/es/{middlewares → middleware}/locale-middleware.js +2 -1
- package/build/es/middleware/mapping-middleware.d.ts +3 -0
- package/build/es/middleware/mapping-middleware.js +34 -0
- package/build/es/middleware/module-middleware.d.ts +3 -0
- package/build/es/middleware/module-middleware.js +64 -0
- package/build/es/middleware/redirects/unsigned-module-redirect.d.ts +6 -0
- package/build/es/middleware/redirects/unsigned-module-redirect.js +25 -0
- package/build/es/middleware/resource-middleware.d.ts +3 -0
- package/build/es/middleware/resource-middleware.js +37 -0
- package/build/es/middleware/utils/error-handling.d.ts +3 -0
- package/build/es/middleware/utils/error-handling.js +32 -0
- package/build/es/middleware/utils/identity.d.ts +6 -0
- package/build/es/middleware/utils/identity.js +64 -0
- package/build/es/middleware/utils/metadata.d.ts +3 -0
- package/build/es/middleware/utils/metadata.js +24 -0
- package/build/es/middleware/utils/request.d.ts +15 -0
- package/build/es/{middlewares/utils.js → middleware/utils/request.js} +15 -23
- package/build/es/middleware/view-middleware.d.ts +3 -0
- package/build/es/middleware/view-middleware.js +136 -0
- package/build/es/middleware.d.ts +9 -0
- package/build/es/middleware.js +13 -0
- package/build/es/tools/server-warmup.js +3 -10
- package/build/es/tools/static-generation.d.ts +8 -1
- package/build/es/tools/static-generation.js +155 -62
- package/build/es/tools/types.d.ts +3 -2
- package/package.json +36 -32
- package/build/cjs/context/global-data.cjs +0 -54
- package/build/cjs/middlewares/api-middleware.cjs +0 -359
- package/build/cjs/middlewares/base-middleware.cjs +0 -15
- package/build/cjs/middlewares/ui-middleware.cjs +0 -183
- package/build/es/context/global-data.d.ts +0 -3
- package/build/es/context/global-data.js +0 -29
- package/build/es/context/hooks.d.ts +0 -9
- package/build/es/context/hooks.js +0 -36
- package/build/es/context/services.d.ts +0 -8
- package/build/es/context/services.js +0 -17
- package/build/es/middlewares/api-middleware.d.ts +0 -3
- package/build/es/middlewares/api-middleware.js +0 -410
- package/build/es/middlewares/base-middleware.d.ts +0 -3
- package/build/es/middlewares/base-middleware.js +0 -4
- package/build/es/middlewares/hmr-middleware.d.ts +0 -5
- package/build/es/middlewares/locale-middleware.d.ts +0 -3
- package/build/es/middlewares/ui-middleware.d.ts +0 -3
- package/build/es/middlewares/ui-middleware.js +0 -186
- package/build/es/middlewares/utils.d.ts +0 -22
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
import { createReturnStatus, LwrUnresolvableError, createSingleDiagnosticError as createDiagnostic, descriptions, } from '@lwrjs/diagnostics';
|
|
2
|
-
import { LATEST_SIGNATURE, explodeSpecifier, getImportMetadataMappings, serializeModuleToJson, getModuleIdentity, getResourceIdentity, getAssetIdentity, getMappingIdentity, getVersionedModuleId, } from '@lwrjs/shared-utils';
|
|
3
|
-
import { isSupportedEnvironment } from './utils.js';
|
|
4
|
-
export default function apiMiddleware(app, context) {
|
|
5
|
-
const { appConfig: { environment: environmentConfig }, moduleRegistry, moduleBundler, resourceRegistry, runtimeEnvironment: defaultRuntimeEnvironment, } = context;
|
|
6
|
-
const errors = descriptions.UNRESOLVABLE;
|
|
7
|
-
// Bundle APIs
|
|
8
|
-
// {apiversion}/bundle/{format}/{compat}/bi/{bundlingId}-{bundlingSet}/module/mi/{specifier}/s/{signature}?{modifiers}
|
|
9
|
-
app.get([
|
|
10
|
-
// Full URL
|
|
11
|
-
`/:apiVersion/bundle/:format/:compat?/l/:locale/e/:environment/bi/:bundleSpecifier/module/mi/:specifier/latest/:prettyUrl?`,
|
|
12
|
-
`/:apiVersion/bundle/:format/:compat?/l/:locale/e/:environment/bi/:bundleSpecifier/module/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
13
|
-
// Without Environment
|
|
14
|
-
`/:apiVersion/bundle/:format/:compat?/l/:locale/bi/:bundleSpecifier/module/mi/:specifier/latest/:prettyUrl?`,
|
|
15
|
-
`/:apiVersion/bundle/:format/:compat?/l/:locale/bi/:bundleSpecifier/module/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
16
|
-
// Without Locale
|
|
17
|
-
`/:apiVersion/bundle/:format/:compat?/e/:environment/bi/:bundleSpecifier/module/mi/:specifier/latest/:prettyUrl?`,
|
|
18
|
-
`/:apiVersion/bundle/:format/:compat?/e/:environment/bi/:bundleSpecifier/module/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
19
|
-
// Without Optional Segments
|
|
20
|
-
`/:apiVersion/bundle/:format/:compat?/bi/:bundleSpecifier/module/mi/:specifier/latest/:prettyUrl?`,
|
|
21
|
-
`/:apiVersion/bundle/:format/:compat?/bi/:bundleSpecifier/module/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
22
|
-
], async (req, res) => {
|
|
23
|
-
const runtimeContext = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
24
|
-
const { runtimeEnvironment, runtimeParams } = runtimeContext;
|
|
25
|
-
const targetEnvironment = runtimeParams.environment;
|
|
26
|
-
if (!isSupportedEnvironment(environmentConfig, targetEnvironment)) {
|
|
27
|
-
res.status(400).send({ error: `Environment "${targetEnvironment}" is not supported` });
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
let moduleId, signature;
|
|
31
|
-
try {
|
|
32
|
-
({ moduleId, signature } = getModuleIdentity(req));
|
|
33
|
-
const sourceMapUrl = req.path.replace('/bundle/', '/sourcemaps/bundle/');
|
|
34
|
-
const sourceMapRuntimeContext = { ...runtimeEnvironment, sourceMapUrl };
|
|
35
|
-
// Redirect module requests to the concrete URL when requested with an importer
|
|
36
|
-
const importerSpecifier = req.query.importer;
|
|
37
|
-
if (importerSpecifier) {
|
|
38
|
-
// Get the importer's file path entry
|
|
39
|
-
const importerModuleId = explodeSpecifier(importerSpecifier);
|
|
40
|
-
const { entry } = await moduleRegistry.getModuleEntry(importerModuleId);
|
|
41
|
-
const versionedModuleEntry = await moduleRegistry.getModuleEntry({
|
|
42
|
-
...moduleId,
|
|
43
|
-
importer: entry,
|
|
44
|
-
});
|
|
45
|
-
const uri = await moduleRegistry.resolveModuleUri({ ...moduleId, version: versionedModuleEntry.version }, runtimeEnvironment, runtimeParams);
|
|
46
|
-
res.set({
|
|
47
|
-
// This redirects to a signed URI
|
|
48
|
-
// A prettifier is added to the end so resources have a meaningful name in browser devtools
|
|
49
|
-
Location: uri,
|
|
50
|
-
});
|
|
51
|
-
res.sendStatus(302);
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const bundleDef = await moduleBundler.getModuleBundle(moduleId, sourceMapRuntimeContext, runtimeParams);
|
|
55
|
-
if (signature !== LATEST_SIGNATURE) {
|
|
56
|
-
res.setHeader('Cache-control', 'public, max-age=31536000, immutable');
|
|
57
|
-
}
|
|
58
|
-
// URIs for generate site
|
|
59
|
-
const resolvedUris = [];
|
|
60
|
-
if (req.isSiteGeneration()) {
|
|
61
|
-
// TODO might be able to remove this in the future but not sure how to properly send
|
|
62
|
-
// back bundleRecord imports with absolute uris (lwc is the main one)
|
|
63
|
-
if (bundleDef.bundleRecord.imports) {
|
|
64
|
-
for (const theImport of bundleDef.bundleRecord.imports) {
|
|
65
|
-
const childSpecifier = theImport.specifier;
|
|
66
|
-
// eslint-disable-next-line no-await-in-loop
|
|
67
|
-
const id = await getVersionedModuleId(childSpecifier, moduleRegistry);
|
|
68
|
-
// eslint-disable-next-line no-await-in-loop
|
|
69
|
-
const uri = await moduleRegistry.resolveModuleUri(id, runtimeEnvironment, runtimeParams);
|
|
70
|
-
resolvedUris.push(uri);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (bundleDef.bundleRecord.dynamicImports) {
|
|
74
|
-
for (const theImport of bundleDef.bundleRecord.dynamicImports) {
|
|
75
|
-
const childSpecifier = theImport.specifier;
|
|
76
|
-
// eslint-disable-next-line no-await-in-loop
|
|
77
|
-
const id = await getVersionedModuleId(childSpecifier, moduleRegistry);
|
|
78
|
-
// eslint-disable-next-line no-await-in-loop
|
|
79
|
-
const uri = await moduleRegistry.resolveModuleUri(id, runtimeEnvironment, runtimeParams);
|
|
80
|
-
resolvedUris.push(uri);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
res.setMetadata({ moduleDefinition: bundleDef, resolvedUris });
|
|
85
|
-
res.type('application/javascript').send(bundleDef.code);
|
|
86
|
-
}
|
|
87
|
-
catch (e) {
|
|
88
|
-
console.log(e);
|
|
89
|
-
const error = createReturnStatus(`bundle for "${moduleId?.specifier || req.params?.specifier}"`, e);
|
|
90
|
-
res.status(error.status).type('text/plain').send(error.message);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
// Unsigned Bundles => 302
|
|
94
|
-
app.get([
|
|
95
|
-
// Full URL
|
|
96
|
-
`/:apiVersion/bundle/:format/:compat?/l/:locale/e/:environment/bi/:bundleSpecifier/module/mi/:specifier`,
|
|
97
|
-
// Without Environment
|
|
98
|
-
`/:apiVersion/bundle/:format/:compat?/l/:locale/bi/:bundleSpecifier/module/mi/:specifier`,
|
|
99
|
-
// Without Locale
|
|
100
|
-
`/:apiVersion/bundle/:format/:compat?/e/:environment/bi/:bundleSpecifier/module/mi/:specifier`,
|
|
101
|
-
// Without Optional Segments
|
|
102
|
-
`/:apiVersion/bundle/:format/:compat?/bi/:bundleSpecifier/module/mi/:specifier`,
|
|
103
|
-
], async (req, res) => {
|
|
104
|
-
const ctx = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
105
|
-
const targetEnvironment = ctx.runtimeParams.environment;
|
|
106
|
-
if (!isSupportedEnvironment(environmentConfig, targetEnvironment)) {
|
|
107
|
-
res.status(400).send({ error: `Environment "${targetEnvironment}" is not supported` });
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
let moduleId;
|
|
111
|
-
try {
|
|
112
|
-
({ moduleId } = getModuleIdentity(req));
|
|
113
|
-
// Get the importer's specifier via the "importer" query param
|
|
114
|
-
const importerSpecifier = req.query.importer;
|
|
115
|
-
if (importerSpecifier) {
|
|
116
|
-
// Get the importer's file path entry
|
|
117
|
-
const importerModuleId = explodeSpecifier(importerSpecifier);
|
|
118
|
-
const { entry } = await moduleRegistry.getModuleEntry(importerModuleId);
|
|
119
|
-
moduleId = { ...moduleId, importer: entry };
|
|
120
|
-
}
|
|
121
|
-
const { ownHash, moduleEntry } = await moduleRegistry.getModule(moduleId, ctx.runtimeParams);
|
|
122
|
-
if (ownHash) {
|
|
123
|
-
const jsonQuery = req.isJsonRequest()
|
|
124
|
-
? `${ctx.runtimeEnvironment.debug ? '&' : '?'}json`
|
|
125
|
-
: '';
|
|
126
|
-
const uri = await moduleRegistry.resolveModuleUri({ ...moduleId, version: moduleEntry.version }, ctx.runtimeEnvironment, ctx.runtimeParams, ownHash);
|
|
127
|
-
res.set({
|
|
128
|
-
// This redirects to a signed URI
|
|
129
|
-
Location: `${uri}${jsonQuery}`,
|
|
130
|
-
});
|
|
131
|
-
res.sendStatus(302);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
catch (e) {
|
|
135
|
-
console.log(e);
|
|
136
|
-
const error = createReturnStatus(`bundle for "${moduleId?.specifier || req.params?.specifier}"`, e);
|
|
137
|
-
res.status(error.status).type('text/plain').send(error.message);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
// Bundle sourcemaps
|
|
141
|
-
app.get([
|
|
142
|
-
// Full URL
|
|
143
|
-
`/:apiVersion/sourcemaps/bundle/:format/:compat?/l/:locale/e/:environment/bi/:bundleSpecifier/module/mi/:specifier/latest/:prettyUrl?`,
|
|
144
|
-
`/:apiVersion/sourcemaps/bundle/:format/:compat?/l/:locale/e/:environment/bi/:bundleSpecifier/module/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
145
|
-
// Without Environment
|
|
146
|
-
`/:apiVersion/sourcemaps/bundle/:format/:compat?/l/:locale/bi/:bundleSpecifier/module/mi/:specifier/latest/:prettyUrl?`,
|
|
147
|
-
`/:apiVersion/sourcemaps/bundle/:format/:compat?/l/:locale/bi/:bundleSpecifier/module/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
148
|
-
// Without Locale
|
|
149
|
-
`/:apiVersion/sourcemaps/bundle/:format/:compat?/e/:environment/bi/:bundleSpecifier/module/mi/:specifier/latest/:prettyUrl?`,
|
|
150
|
-
`/:apiVersion/sourcemaps/bundle/:format/:compat?/e/:environment/bi/:bundleSpecifier/module/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
151
|
-
// Without Optional Segments
|
|
152
|
-
`/:apiVersion/sourcemaps/bundle/:format/:compat?/bi/:bundleSpecifier/module/mi/:specifier/latest/:prettyUrl?`,
|
|
153
|
-
`/:apiVersion/sourcemaps/bundle/:format/:compat?/bi/:bundleSpecifier/module/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
154
|
-
], async (req, res) => {
|
|
155
|
-
const runtimeContext = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
156
|
-
const { runtimeParams, runtimeEnvironment } = runtimeContext;
|
|
157
|
-
const targetEnvironment = runtimeParams.environment;
|
|
158
|
-
if (!isSupportedEnvironment(environmentConfig, targetEnvironment)) {
|
|
159
|
-
res.status(400).send({ error: `Environment "${targetEnvironment}" is not supported` });
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
let moduleId, signature;
|
|
163
|
-
try {
|
|
164
|
-
({ moduleId, signature } = getModuleIdentity(req));
|
|
165
|
-
const bundleDef = await moduleBundler.getModuleBundle(moduleId, runtimeEnvironment);
|
|
166
|
-
if (signature !== LATEST_SIGNATURE) {
|
|
167
|
-
res.setHeader('Cache-control', 'public, max-age=31536000, immutable');
|
|
168
|
-
}
|
|
169
|
-
res.type('application/json').send(bundleDef.map);
|
|
170
|
-
}
|
|
171
|
-
catch (e) {
|
|
172
|
-
console.log(e);
|
|
173
|
-
const error = createReturnStatus(`bundle sourcemap for "${moduleId?.specifier || req.params?.specifier}"`, e);
|
|
174
|
-
res.status(error.status).type('text/plain').send(error.message);
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
// Signed Modules
|
|
178
|
-
// /{apiversion}/module/{format}/{compat}/l/{language}/mi/{specifier}/s/{signature}
|
|
179
|
-
app.get([
|
|
180
|
-
// Full URL
|
|
181
|
-
`/:apiVersion/module/:format/:compat/l/:locale/e/:environment/mi/:specifier/latest/:prettyUrl?`,
|
|
182
|
-
`/:apiVersion/module/:format/:compat/l/:locale/e/:environment/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
183
|
-
// Without Environment
|
|
184
|
-
`/:apiVersion/module/:format/:compat/l/:locale/mi/:specifier/latest/:prettyUrl?`,
|
|
185
|
-
`/:apiVersion/module/:format/:compat/l/:locale/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
186
|
-
// Without Locale
|
|
187
|
-
`/:apiVersion/module/:format/:compat/e/:environment/mi/:specifier/latest/:prettyUrl?`,
|
|
188
|
-
`/:apiVersion/module/:format/:compat/e/:environment/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
189
|
-
// Without Optional Segments
|
|
190
|
-
`/:apiVersion/module/:format/:compat/mi/:specifier/latest/:prettyUrl?`,
|
|
191
|
-
`/:apiVersion/module/:format/:compat/mi/:specifier/s/:signature/:prettyUrl?`,
|
|
192
|
-
], async (req, res) => {
|
|
193
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
194
|
-
const targetEnvironment = runtimeParams.environment;
|
|
195
|
-
if (!isSupportedEnvironment(environmentConfig, targetEnvironment)) {
|
|
196
|
-
res.status(400).send({ error: `Environment "${targetEnvironment}" is not supported` });
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
let moduleId, signature;
|
|
200
|
-
try {
|
|
201
|
-
({ moduleId, signature } = getModuleIdentity(req));
|
|
202
|
-
if (!req.validateJsonRequest()) {
|
|
203
|
-
throw createDiagnostic({ description: errors.INVALID_JSON() }, LwrUnresolvableError);
|
|
204
|
-
}
|
|
205
|
-
// retrieve the latest module
|
|
206
|
-
const theModule = await moduleRegistry.getLinkedModule(moduleId, runtimeEnvironment, runtimeParams);
|
|
207
|
-
const { ownHash, linkedSource } = theModule;
|
|
208
|
-
// validate the requested instances exists
|
|
209
|
-
if (signature === LATEST_SIGNATURE || ownHash === signature) {
|
|
210
|
-
const code = linkedSource;
|
|
211
|
-
res.setMetadata({ moduleDefinition: theModule });
|
|
212
|
-
if (req.isJsonRequest()) {
|
|
213
|
-
const json = await serializeModuleToJson(code, theModule, moduleRegistry, runtimeParams);
|
|
214
|
-
res.type('application/json').send(json);
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
res.type('application/javascript').send(code);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
throw createDiagnostic({ description: errors.SIGNED_MODULE(moduleId.specifier, signature) }, LwrUnresolvableError);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
catch (e) {
|
|
225
|
-
console.log(e);
|
|
226
|
-
const error = createReturnStatus(`module "${moduleId?.specifier || req.params?.specifier}"`, e);
|
|
227
|
-
res.status(error.status).type('text/plain').send(error.message);
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
// Unsigned Modules => 302
|
|
231
|
-
app.get([
|
|
232
|
-
// Full URL
|
|
233
|
-
`/:apiVersion/module/:format/:compat/l/:locale/e/:environment/mi/:specifier`,
|
|
234
|
-
// Without Environment
|
|
235
|
-
`/:apiVersion/module/:format/:compat/l/:locale/mi/:specifier`,
|
|
236
|
-
// Without Locale
|
|
237
|
-
`/:apiVersion/module/:format/:compat/e/:environment/mi/:specifier`,
|
|
238
|
-
// Without Optional Segments
|
|
239
|
-
`/:apiVersion/module/:format/:compat/mi/:specifier`,
|
|
240
|
-
], async (req, res) => {
|
|
241
|
-
const ctx = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
242
|
-
const targetEnvironment = ctx.runtimeParams.environment;
|
|
243
|
-
if (!isSupportedEnvironment(environmentConfig, targetEnvironment)) {
|
|
244
|
-
res.status(400).send({ error: `Environment "${targetEnvironment}" is not supported` });
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
let moduleId;
|
|
248
|
-
try {
|
|
249
|
-
({ moduleId } = getModuleIdentity(req));
|
|
250
|
-
// Get the importer's specifier via the "importer" query param
|
|
251
|
-
const importerSpecifier = req.query.importer;
|
|
252
|
-
if (importerSpecifier) {
|
|
253
|
-
// Get the importer's file path entry
|
|
254
|
-
const importerModuleId = explodeSpecifier(importerSpecifier);
|
|
255
|
-
const { entry } = await moduleRegistry.getModuleEntry(importerModuleId);
|
|
256
|
-
moduleId = { ...moduleId, importer: entry };
|
|
257
|
-
}
|
|
258
|
-
// Get the module's signature from the registry
|
|
259
|
-
const { ownHash, moduleEntry } = await moduleRegistry.getModule(moduleId, ctx.runtimeParams);
|
|
260
|
-
if (ownHash) {
|
|
261
|
-
const jsonQuery = req.isJsonRequest()
|
|
262
|
-
? `${ctx.runtimeEnvironment.debug ? '&' : '?'}json`
|
|
263
|
-
: '';
|
|
264
|
-
const uri = await moduleRegistry.resolveModuleUri({ ...moduleId, version: moduleEntry.version }, ctx.runtimeEnvironment, ctx.runtimeParams, ownHash);
|
|
265
|
-
res.set({
|
|
266
|
-
// This redirects to a signed URI
|
|
267
|
-
Location: `${uri}${jsonQuery}`,
|
|
268
|
-
});
|
|
269
|
-
res.sendStatus(302);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
catch (e) {
|
|
273
|
-
console.log(e);
|
|
274
|
-
const error = createReturnStatus(`module "${moduleId?.specifier || req.params?.specifier}"`, e);
|
|
275
|
-
res.status(error.status).type('text/plain').send(error.message);
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
// Mapping Services
|
|
279
|
-
// /{apiversion}/mapping/{format}/{compat}(/l/{language})?(/e/{environment})?(/bi/{bundleStrategyId})?/mp/{specifiers}
|
|
280
|
-
app.get([
|
|
281
|
-
// Full URL (3 optional segments)
|
|
282
|
-
`/:apiVersion/mapping/:format/:compat/l/:locale/e/:environment/bi/:bundleSpecifier/mp/:specifiers`,
|
|
283
|
-
// With 2 optional segments: l & e, l & bi, e & bi
|
|
284
|
-
`/:apiVersion/mapping/:format/:compat/l/:locale/e/:environment/mp/:specifiers`,
|
|
285
|
-
`/:apiVersion/mapping/:format/:compat/l/:locale/bi/:bundleSpecifier/mp/:specifiers`,
|
|
286
|
-
`/:apiVersion/mapping/:format/:compat/e/:environment/bi/:bundleSpecifier/mp/:specifiers`,
|
|
287
|
-
// With 1 optional segment
|
|
288
|
-
`/:apiVersion/mapping/:format/:compat/l/:locale/mp/:specifiers`,
|
|
289
|
-
`/:apiVersion/mapping/:format/:compat/e/:environment/mp/:specifiers`,
|
|
290
|
-
`/:apiVersion/mapping/:format/:compat/bi/:bundleSpecifier/mp/:specifiers`,
|
|
291
|
-
// With 0 optional segments
|
|
292
|
-
`/:apiVersion/mapping/:format/:compat/mp/:specifiers`,
|
|
293
|
-
], async (req, res) => {
|
|
294
|
-
const requestContext = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
295
|
-
const targetEnvironment = requestContext.runtimeParams.environment;
|
|
296
|
-
if (!isSupportedEnvironment(environmentConfig, targetEnvironment)) {
|
|
297
|
-
res.status(400).send({ error: `Environment "${targetEnvironment}" is not supported` });
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
let moduleIds;
|
|
301
|
-
try {
|
|
302
|
-
({ moduleIds } = getMappingIdentity(req));
|
|
303
|
-
const { runtimeEnvironment, runtimeParams } = requestContext;
|
|
304
|
-
const importMetadata = await getImportMetadataMappings(moduleIds, runtimeEnvironment, runtimeParams, moduleRegistry, moduleBundler);
|
|
305
|
-
// Include import metadata as an object for use with the static site generator
|
|
306
|
-
res.setMetadata({ importMetadata });
|
|
307
|
-
// Respond to request
|
|
308
|
-
res.status(200).type('application/json').send(importMetadata);
|
|
309
|
-
}
|
|
310
|
-
catch (e) {
|
|
311
|
-
console.log(e);
|
|
312
|
-
const error = createReturnStatus(`mappings for "${JSON.stringify(moduleIds || req.params?.specifiers)}"`, e);
|
|
313
|
-
res.status(error.status).send(error.message);
|
|
314
|
-
}
|
|
315
|
-
});
|
|
316
|
-
// Resources
|
|
317
|
-
app.get([
|
|
318
|
-
// Full URL
|
|
319
|
-
`/:apiVersion/resource/:format/l/:locale/e/:environment/:specifier`,
|
|
320
|
-
// Without Environment
|
|
321
|
-
`/:apiVersion/resource/:format/l/:locale/:specifier`,
|
|
322
|
-
// Without Locale
|
|
323
|
-
`/:apiVersion/resource/:format/e/:environment/:specifier`,
|
|
324
|
-
// Without Optional Segments
|
|
325
|
-
`/:apiVersion/resource/:format/:specifier`,
|
|
326
|
-
], async (req, res) => {
|
|
327
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
328
|
-
const targetEnvironment = runtimeParams.environment;
|
|
329
|
-
if (!isSupportedEnvironment(environmentConfig, targetEnvironment)) {
|
|
330
|
-
res.status(400).send({ error: `Environment "${targetEnvironment}" is not supported` });
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
let resourceId;
|
|
334
|
-
try {
|
|
335
|
-
({ resourceId } = getResourceIdentity(req));
|
|
336
|
-
const resource = await resourceRegistry.getResource(resourceId, runtimeEnvironment, runtimeParams);
|
|
337
|
-
if (resource && resource.content) {
|
|
338
|
-
// Static resource
|
|
339
|
-
const type = resource.type === 'text/css' ? resource.type : 'application/javascript';
|
|
340
|
-
res.type(type).send(resource.content);
|
|
341
|
-
}
|
|
342
|
-
else if (resource && resource.stream) {
|
|
343
|
-
// Stream-able resource
|
|
344
|
-
res.type(resource.type).stream(resource.stream);
|
|
345
|
-
}
|
|
346
|
-
else {
|
|
347
|
-
throw createDiagnostic({ description: errors.RESOURCE(resourceId.specifier) }, LwrUnresolvableError);
|
|
348
|
-
}
|
|
349
|
-
res.setMetadata({ resource });
|
|
350
|
-
}
|
|
351
|
-
catch (e) {
|
|
352
|
-
console.log(e);
|
|
353
|
-
const error = createReturnStatus(`resource "${resourceId?.specifier || req.params?.specifier}"`, e);
|
|
354
|
-
res.status(error.status).type('text/plain').send(error.message);
|
|
355
|
-
}
|
|
356
|
-
});
|
|
357
|
-
// -- Asset configuration -----------------------------------------------------------
|
|
358
|
-
// Custom defined assets
|
|
359
|
-
const paths = context.appConfig.assets.map((a) => a.urlPath);
|
|
360
|
-
// Workaround: https://github.com/pillarjs/path-to-regexp#compatibility-with-express--4x
|
|
361
|
-
// Koa routing (Path-To-RegExp) breaks compatibility with Express routing & asterisk wildcards
|
|
362
|
-
for (let i = 0; i < paths.length; i++) {
|
|
363
|
-
paths[i] = paths[i] + app.getRegexWildcard();
|
|
364
|
-
}
|
|
365
|
-
app.all(paths, async (req, res) => {
|
|
366
|
-
const { runtimeEnvironment } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
367
|
-
try {
|
|
368
|
-
const { assetId: { specifier, type }, signature, } = getAssetIdentity(req);
|
|
369
|
-
const assetObj = await context.assetRegistry.getAsset({ specifier, signature, type }, runtimeEnvironment);
|
|
370
|
-
if (assetObj.mime) {
|
|
371
|
-
res.type(assetObj.mime);
|
|
372
|
-
}
|
|
373
|
-
res.setMetadata({ asset: assetObj });
|
|
374
|
-
res.stream(assetObj.stream());
|
|
375
|
-
}
|
|
376
|
-
catch (e) {
|
|
377
|
-
console.log(e);
|
|
378
|
-
const error = createReturnStatus(`asset at "${req.originalUrl}"`, e);
|
|
379
|
-
res.status(error.status).send(error.message);
|
|
380
|
-
}
|
|
381
|
-
});
|
|
382
|
-
// LWR specific assets
|
|
383
|
-
// Workaround: https://github.com/pillarjs/path-to-regexp#compatibility-with-express--4x
|
|
384
|
-
// Koa routing (Path-To-RegExp) breaks compatibility with Express routing & asterisk wildcards
|
|
385
|
-
app.all('/:apiVersion/:assetType/:immutable?/s/:signature/' + app.getRegexWildcard(), async (req, res) => {
|
|
386
|
-
const { runtimeEnvironment } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
387
|
-
const { basePath } = runtimeEnvironment;
|
|
388
|
-
const { signature } = req.params;
|
|
389
|
-
const { immutable, assetType: type } = req.params;
|
|
390
|
-
const specifier = req.params[0] ? `/${req.params[0]}` : req.originalUrl;
|
|
391
|
-
const basePathSpecifier = `${basePath}${specifier}`;
|
|
392
|
-
try {
|
|
393
|
-
const assetObj = await context.assetRegistry.getAsset({ specifier: basePathSpecifier, signature, type: type }, runtimeEnvironment, req.isSiteGeneration());
|
|
394
|
-
if (immutable) {
|
|
395
|
-
// WIP: ?
|
|
396
|
-
}
|
|
397
|
-
if (assetObj.mime) {
|
|
398
|
-
res.type(assetObj.mime);
|
|
399
|
-
}
|
|
400
|
-
res.setMetadata({ asset: assetObj });
|
|
401
|
-
res.stream(assetObj.stream());
|
|
402
|
-
}
|
|
403
|
-
catch (e) {
|
|
404
|
-
console.log(e);
|
|
405
|
-
const error = createReturnStatus(`asset at "${specifier}"`, e);
|
|
406
|
-
res.status(error.status).type('text/plain').send(error.message);
|
|
407
|
-
}
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
//# sourceMappingURL=api-middleware.js.map
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import ms from 'ms';
|
|
2
|
-
import { DiagnosticsError } from '@lwrjs/diagnostics';
|
|
3
|
-
import { LwrViewHandler } from '@lwrjs/view-registry';
|
|
4
|
-
import { isSupportedEnvironment } from './utils.js';
|
|
5
|
-
import { decodeViewPath, getClientBootstrapConfigurationRoutes, extractRequestParams, } from '@lwrjs/shared-utils';
|
|
6
|
-
import { getClientRoutes } from '@lwrjs/router';
|
|
7
|
-
export default function uiMiddleware(app, context) {
|
|
8
|
-
const { viewRegistry, moduleRegistry, runtimeEnvironment: defaultRuntimeEnvironment } = context;
|
|
9
|
-
const { environment: environmentConfig, routes, errorRoutes } = context.appConfig;
|
|
10
|
-
const route404 = errorRoutes.find((r) => r.status === 404);
|
|
11
|
-
const route500 = errorRoutes.find((r) => r.status === 500);
|
|
12
|
-
const viewHandler = new LwrViewHandler({ viewRegistry, moduleRegistry }, context.appConfig);
|
|
13
|
-
async function sendViewResponse(req, res, route, defaultStatus = 200) {
|
|
14
|
-
if (!req.validateJsonRequest()) {
|
|
15
|
-
res.status(400).send({ error: 'Accept header and json query parameter are incompatible' });
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
// UI URIs do not contain bundle IDs, so always use the environment default
|
|
19
|
-
req.params.bundleSpecifier = defaultRuntimeEnvironment.bundle ? '0' : undefined;
|
|
20
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
21
|
-
const targetEnvironment = runtimeParams.environment;
|
|
22
|
-
if (!isSupportedEnvironment(environmentConfig, targetEnvironment)) {
|
|
23
|
-
res.status(400).send({ error: `Environment "${targetEnvironment}" is not supported` });
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (!targetEnvironment && environmentConfig?.default) {
|
|
27
|
-
runtimeParams.environment = environmentConfig.default;
|
|
28
|
-
}
|
|
29
|
-
try {
|
|
30
|
-
// HTML document
|
|
31
|
-
const viewRequest = {
|
|
32
|
-
url: req.originalUrl,
|
|
33
|
-
params: req.params,
|
|
34
|
-
query: req.query,
|
|
35
|
-
requestPath: req.path, // Runtime resolved version vs. the original route path
|
|
36
|
-
};
|
|
37
|
-
const response = req.isJsonRequest()
|
|
38
|
-
? await viewHandler.getViewJson(viewRequest, route, runtimeEnvironment, runtimeParams)
|
|
39
|
-
: await viewHandler.getViewContent(viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
40
|
-
const cacheTtl = response.cache?.ttl || route.cache?.ttl;
|
|
41
|
-
let ttl;
|
|
42
|
-
if (cacheTtl) {
|
|
43
|
-
ttl = typeof cacheTtl === 'string' ? ms(cacheTtl) / 1000 : cacheTtl;
|
|
44
|
-
res.setHeader('Cache-control', `public, max-age=${ttl}`);
|
|
45
|
-
}
|
|
46
|
-
const headers = response.headers || {
|
|
47
|
-
'Content-Type': 'text/html',
|
|
48
|
-
...(cacheTtl && { 'Cache-control': `public, max-age=${ttl}` }),
|
|
49
|
-
};
|
|
50
|
-
res.set(headers);
|
|
51
|
-
res.setMetadata(response.metadata);
|
|
52
|
-
const status = response.status ? response.status : defaultStatus;
|
|
53
|
-
res.status(status).send(response.body);
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
if (error instanceof DiagnosticsError) {
|
|
57
|
-
console.error('LWR Diagnostic Error: ' + error.message);
|
|
58
|
-
console.error(error.diagnostics);
|
|
59
|
-
console.error(error.stack);
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
console.error(error);
|
|
63
|
-
}
|
|
64
|
-
if (route500 && defaultStatus !== 500) {
|
|
65
|
-
await sendViewResponse(req, res, route500, 500);
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
res.status(500).send(`500 - Error retrieving view for route "${route.id}"`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
async function sendConfigurationResponse(req, res, defaultStatus = 200) {
|
|
73
|
-
// UI URIs do not contain bundle IDs, so always use the environment default
|
|
74
|
-
req.params.bundleSpecifier = defaultRuntimeEnvironment.bundle ? '0' : undefined;
|
|
75
|
-
const { runtimeEnvironment, runtimeParams } = req.getRuntimeContext(defaultRuntimeEnvironment);
|
|
76
|
-
const { appId, encodedViewPath } = req.params;
|
|
77
|
-
// Match the route id
|
|
78
|
-
const route = routes.find((route) => route.id === appId);
|
|
79
|
-
if (!route) {
|
|
80
|
-
res.status(404).send({ error: `LWR configuration for appId "${appId}" is not available` });
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
// decode the resolved view path and extract any params.
|
|
85
|
-
const url = decodeViewPath(encodedViewPath);
|
|
86
|
-
const requestPath = route.path;
|
|
87
|
-
const params = extractRequestParams(requestPath, url, req.params);
|
|
88
|
-
// HTML document
|
|
89
|
-
const viewRequest = {
|
|
90
|
-
url,
|
|
91
|
-
params,
|
|
92
|
-
query: req.query,
|
|
93
|
-
requestPath, // Runtime resolved version vs. the original route path
|
|
94
|
-
};
|
|
95
|
-
const response = await viewHandler.getViewConfiguration(viewRequest, route, runtimeEnvironment, runtimeParams);
|
|
96
|
-
if (!response) {
|
|
97
|
-
res.status(404).send({ error: `LWR configuration for viewPath "${url}" is not available` });
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
// Same cache ttl as the Content response
|
|
101
|
-
const cacheTtl = response.cache?.ttl || route.cache?.ttl;
|
|
102
|
-
let ttl;
|
|
103
|
-
if (cacheTtl) {
|
|
104
|
-
ttl = typeof cacheTtl === 'string' ? ms(cacheTtl) / 1000 : cacheTtl;
|
|
105
|
-
}
|
|
106
|
-
res.set({
|
|
107
|
-
'Content-Type': 'application/javascript',
|
|
108
|
-
...(cacheTtl && { 'Cache-control': `public, max-age=${ttl}` }),
|
|
109
|
-
});
|
|
110
|
-
const status = response.status ? response.status : defaultStatus;
|
|
111
|
-
res.status(status).send(response.body);
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
if (error instanceof DiagnosticsError) {
|
|
115
|
-
console.error('LWR Diagnostic Error: ' + error.message);
|
|
116
|
-
console.error(error.diagnostics);
|
|
117
|
-
console.error(error.stack);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
console.error(error);
|
|
121
|
-
}
|
|
122
|
-
if (route500 && defaultStatus !== 500) {
|
|
123
|
-
await sendViewResponse(req, res, route500, 500);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
res.status(500).send(`500 - Error retrieving route "${route.id}"`);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
// -- Register each view ------------------------------------------------------------
|
|
131
|
-
routes.forEach((route) => {
|
|
132
|
-
// Seems like we only support get or post
|
|
133
|
-
if (route.method === 'post') {
|
|
134
|
-
app.post(route.path, async (req, res) => {
|
|
135
|
-
await sendViewResponse(req, res, route);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
// Get the client-side routes
|
|
140
|
-
const serverPath = route.path;
|
|
141
|
-
const paths = [serverPath];
|
|
142
|
-
const subRoutes = route.subRoutes ? getClientRoutes(route.subRoutes) : undefined;
|
|
143
|
-
if (subRoutes) {
|
|
144
|
-
// Concatenate each client uri to the server route.path, creating the full list of composite paths
|
|
145
|
-
const prefix = serverPath === '/' ? '' : serverPath;
|
|
146
|
-
subRoutes.routes.forEach((r) => paths.push(`${prefix}${r.uri}`));
|
|
147
|
-
}
|
|
148
|
-
// Register the server route.path and each composite client path, to enable full page refreshes of client pages
|
|
149
|
-
paths.forEach((path) => {
|
|
150
|
-
// vanity urls
|
|
151
|
-
app.get(path, async (req, res) => {
|
|
152
|
-
await sendViewResponse(req, res, route);
|
|
153
|
-
});
|
|
154
|
-
// canonical URL
|
|
155
|
-
app.get([
|
|
156
|
-
`/:apiVersion/application/:format/l/:locale/ai/:appId${path}`,
|
|
157
|
-
`/:apiVersion/application/:format/l/:locale/e/:environment/ai/:appId${path}`,
|
|
158
|
-
`/:apiVersion/application/:format/ai/:appId${path}`,
|
|
159
|
-
`/:apiVersion/application/:format/e/:environment/ai/:appId${path}`,
|
|
160
|
-
], async (req, res) => {
|
|
161
|
-
await sendViewResponse(req, res, route);
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
const clientBootstrapConfigurationRoutes = getClientBootstrapConfigurationRoutes();
|
|
167
|
-
app.get(clientBootstrapConfigurationRoutes, async (req, res) => {
|
|
168
|
-
await sendConfigurationResponse(req, res);
|
|
169
|
-
});
|
|
170
|
-
// TODO nrkruk - This needs to be more generalized and visible at the top level of our middleware code.
|
|
171
|
-
// This is the only time we call initRoutes() - which adds all the routing middleware that has been declared previously
|
|
172
|
-
// to the app (get()/post()/all()). Any routing middleware declared after this point will not be initialized
|
|
173
|
-
// (for KOA and other server types where routing is not included in the base package).
|
|
174
|
-
// NOTE: THIS MUST BE ALWAYS CALLED BEFORE THE DEFAULT 404 ROUTE BELOW
|
|
175
|
-
app.initRoutes();
|
|
176
|
-
// -- Default 404 route -------------------------------------------------------------
|
|
177
|
-
app.use(async (req, res) => {
|
|
178
|
-
if (route404) {
|
|
179
|
-
await sendViewResponse(req, res, route404, 404);
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
res.status(404).send('404 - This page does not exist');
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
//# sourceMappingURL=ui-middleware.js.map
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import http from 'http';
|
|
3
|
-
import { EnvironmentConfig } from '@lwrjs/types';
|
|
4
|
-
import qs from 'qs';
|
|
5
|
-
interface Params {
|
|
6
|
-
[key: string]: string;
|
|
7
|
-
}
|
|
8
|
-
export declare function getRequestProperties(pattern: string, req: http.IncomingMessage): {
|
|
9
|
-
url: string;
|
|
10
|
-
params: Params;
|
|
11
|
-
query: qs.ParsedQs;
|
|
12
|
-
} | undefined;
|
|
13
|
-
/**
|
|
14
|
-
* Check if the target environment is supported by the application
|
|
15
|
-
*
|
|
16
|
-
* @param environmentConfig - environment property from app config
|
|
17
|
-
* @param targetEnvironment - environment value from module uri
|
|
18
|
-
* @returns boolean
|
|
19
|
-
*/
|
|
20
|
-
export declare function isSupportedEnvironment(environmentConfig: EnvironmentConfig, targetEnvironment: string): boolean;
|
|
21
|
-
export {};
|
|
22
|
-
//# sourceMappingURL=utils.d.ts.map
|