@lwrjs/core 0.9.0-alpha.3 → 0.9.0-alpha.31
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 +86 -0
- package/build/cjs/context/server.cjs +75 -0
- package/build/cjs/index.cjs +69 -153
- package/build/cjs/middleware/asset-middleware.cjs +70 -0
- package/build/cjs/middleware/bundle-middleware.cjs +113 -0
- package/build/cjs/{middlewares → middleware}/hmr-middleware.cjs +6 -6
- 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 +92 -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 +189 -62
- package/build/es/context/provider.d.ts +3 -0
- package/build/es/context/provider.js +40 -0
- package/build/es/context/server.d.ts +3 -0
- package/build/es/context/server.js +49 -0
- package/build/es/index.d.ts +3 -1
- package/build/es/index.js +89 -150
- package/build/es/middleware/asset-middleware.d.ts +3 -0
- package/build/es/middleware/asset-middleware.js +42 -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 +4 -4
- 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 +62 -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 +11 -1
- package/build/es/tools/static-generation.js +229 -79
- package/build/es/tools/types.d.ts +3 -2
- package/package.json +38 -34
- 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,36 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_LWR_BOOTSTRAP_CONFIG, logger } from '@lwrjs/shared-utils';
|
|
2
|
-
import { validateLwrAppConfig } from '@lwrjs/config';
|
|
3
|
-
// Add missing bootstrap objects to routes
|
|
4
|
-
function normalizeRoutesBootstrap({ routes, errorRoutes }) {
|
|
5
|
-
[...routes, ...errorRoutes].forEach((route) => {
|
|
6
|
-
if (!route.bootstrap) {
|
|
7
|
-
route.bootstrap = { ...DEFAULT_LWR_BOOTSTRAP_CONFIG };
|
|
8
|
-
}
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
export async function runConfigurationsHook(hookPlugins, lwrConfig, dataConfig, runtimeConfig) {
|
|
12
|
-
for (const hookPlugin of hookPlugins) {
|
|
13
|
-
// eslint-disable-next-line no-await-in-loop
|
|
14
|
-
await hookPlugin.initConfigs(lwrConfig, dataConfig, runtimeConfig);
|
|
15
|
-
}
|
|
16
|
-
try {
|
|
17
|
-
validateLwrAppConfig(JSON.stringify(lwrConfig), 'post');
|
|
18
|
-
}
|
|
19
|
-
catch (e) {
|
|
20
|
-
// TODO: temporary workaround for https://github.com/salesforce/lwr/issues/825
|
|
21
|
-
if (process.env.UNSAFE_IGNORE_CONFIG_VALIDATION === 'true') {
|
|
22
|
-
logger.warn('ignoring config validation errors due to UNSAFE_IGNORE_CONFIG_VALIDATION flag...proceed with caution');
|
|
23
|
-
console.dir(e, { depth: null });
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
throw e;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
normalizeRoutesBootstrap(lwrConfig);
|
|
30
|
-
return {
|
|
31
|
-
lwrConfig,
|
|
32
|
-
dataConfig,
|
|
33
|
-
runtimeConfig,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
//# sourceMappingURL=hooks.js.map
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { LwrService, ServiceCtor } from '@lwrjs/types';
|
|
2
|
-
interface SubLwrConfig {
|
|
3
|
-
cacheDir: string;
|
|
4
|
-
rootDir: string;
|
|
5
|
-
}
|
|
6
|
-
export declare function getServiceModule<T extends LwrService>(path: string, { cacheDir, rootDir }: SubLwrConfig): Promise<ServiceCtor<T>>;
|
|
7
|
-
export {};
|
|
8
|
-
//# sourceMappingURL=services.d.ts.map
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import libPath from 'path';
|
|
2
|
-
import { resolveFileExtension, transpileTs } from '@lwrjs/shared-utils';
|
|
3
|
-
export async function getServiceModule(path, { cacheDir, rootDir }) {
|
|
4
|
-
try {
|
|
5
|
-
if (path.endsWith('.ts')) {
|
|
6
|
-
const fullPath = resolveFileExtension(path);
|
|
7
|
-
path = await transpileTs(fullPath, { rootDir, cacheDir: libPath.join(cacheDir, 'services') });
|
|
8
|
-
}
|
|
9
|
-
const moduleEntry = await import(path);
|
|
10
|
-
return moduleEntry.default || moduleEntry;
|
|
11
|
-
}
|
|
12
|
-
catch (err) {
|
|
13
|
-
console.log(err);
|
|
14
|
-
throw new Error(`Unable to get Service: ${path}`);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=services.js.map
|
|
@@ -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
|