@lwrjs/core 0.12.0-alpha.11 → 0.12.0-alpha.12
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.
|
@@ -31,6 +31,24 @@ var import_diagnostics = __toModule(require("@lwrjs/diagnostics"));
|
|
|
31
31
|
var import_instrumentation = __toModule(require("@lwrjs/instrumentation"));
|
|
32
32
|
var import_identity = __toModule(require("./utils/identity.cjs"));
|
|
33
33
|
var import_error_handling = __toModule(require("./utils/error-handling.cjs"));
|
|
34
|
+
var import_fs_extra = __toModule(require("fs-extra"));
|
|
35
|
+
function assetMiddleware(app, context) {
|
|
36
|
+
const paths = context.appConfig.assets.map((config) => {
|
|
37
|
+
const assetDirConfig = config;
|
|
38
|
+
let urlPath = config.urlPath;
|
|
39
|
+
if (assetDirConfig.root) {
|
|
40
|
+
urlPath = "/:filename";
|
|
41
|
+
} else if (assetDirConfig.dir) {
|
|
42
|
+
urlPath += app.getRegexWildcard();
|
|
43
|
+
}
|
|
44
|
+
return urlPath;
|
|
45
|
+
});
|
|
46
|
+
app.get([
|
|
47
|
+
...[...new Set(paths)],
|
|
48
|
+
"/:apiVersion/:assetType(asset|content-asset)/:immutable?/s/:signature/" + app.getRegexWildcard(),
|
|
49
|
+
"/:apiVersion/:assetType(asset|content-asset)/:immutable?/" + app.getRegexWildcard()
|
|
50
|
+
], (0, import_error_handling.handleErrors)(createAssetMiddleware(context)));
|
|
51
|
+
}
|
|
34
52
|
function createAssetMiddleware(context) {
|
|
35
53
|
const {
|
|
36
54
|
assetRegistry,
|
|
@@ -43,24 +61,23 @@ function createAssetMiddleware(context) {
|
|
|
43
61
|
assetId.specifier = import_path.default.join(basePath, assetId.specifier);
|
|
44
62
|
}
|
|
45
63
|
try {
|
|
64
|
+
let asset;
|
|
46
65
|
const assetUri = await assetRegistry.resolveAssetUri(assetId, runtimeEnvironment);
|
|
47
66
|
if (assetUri.external) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
specifier: assetId.specifier,
|
|
59
|
-
url: req.originalUrl
|
|
67
|
+
try {
|
|
68
|
+
asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
|
|
69
|
+
if (!import_fs_extra.default.existsSync(asset.entry)) {
|
|
70
|
+
return sendRedirect(res, assetUri);
|
|
71
|
+
}
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (import_path.default.dirname(assetId.specifier) === (basePath ? basePath : "/") && error instanceof import_diagnostics.DiagnosticsError && error?.diagnostics[0]?.description.category === "lwrUnresolvable/asset") {
|
|
74
|
+
return sendRedirect(res, assetUri);
|
|
75
|
+
}
|
|
76
|
+
throw error;
|
|
60
77
|
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
}
|
|
78
|
+
} else {
|
|
79
|
+
asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
|
|
80
|
+
}
|
|
64
81
|
if (req.isSiteGeneration()) {
|
|
65
82
|
res.setSiteGenerationMetadata({asset});
|
|
66
83
|
}
|
|
@@ -82,20 +99,22 @@ function createAssetMiddleware(context) {
|
|
|
82
99
|
}
|
|
83
100
|
};
|
|
84
101
|
}
|
|
85
|
-
function
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
} else if (assetDirConfig.dir) {
|
|
92
|
-
urlPath += app.getRegexWildcard();
|
|
102
|
+
async function getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment) {
|
|
103
|
+
const asset = await (0, import_instrumentation.getTracer)().trace({
|
|
104
|
+
name: import_instrumentation.RequestHandlerSpan.GetAsset,
|
|
105
|
+
attributes: {
|
|
106
|
+
specifier: assetId.specifier,
|
|
107
|
+
url: req.originalUrl
|
|
93
108
|
}
|
|
94
|
-
|
|
109
|
+
}, () => {
|
|
110
|
+
return assetRegistry.getAsset({...assetId, signature}, runtimeEnvironment, req.isSiteGeneration());
|
|
95
111
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
112
|
+
return asset;
|
|
113
|
+
}
|
|
114
|
+
function sendRedirect(res, assetUri) {
|
|
115
|
+
res.set({
|
|
116
|
+
Location: assetUri.uri,
|
|
117
|
+
"cache-control": "public, max-age=60"
|
|
118
|
+
});
|
|
119
|
+
res.sendStatus(302);
|
|
101
120
|
}
|
|
@@ -3,6 +3,30 @@ import { DiagnosticsError } from '@lwrjs/diagnostics';
|
|
|
3
3
|
import { RequestHandlerSpan, getTracer } from '@lwrjs/instrumentation';
|
|
4
4
|
import { getAssetIdentity } from './utils/identity.js';
|
|
5
5
|
import { handleErrors } from './utils/error-handling.js';
|
|
6
|
+
import fs from 'fs-extra';
|
|
7
|
+
export function assetMiddleware(app, context) {
|
|
8
|
+
const paths = context.appConfig.assets.map((config) => {
|
|
9
|
+
const assetDirConfig = config;
|
|
10
|
+
let urlPath = config.urlPath;
|
|
11
|
+
// If this is a root config add a /:filename path to match any file in the root. The middleware will fall through if there is no match.
|
|
12
|
+
if (assetDirConfig.root) {
|
|
13
|
+
urlPath = '/:filename';
|
|
14
|
+
}
|
|
15
|
+
else if (assetDirConfig.dir) {
|
|
16
|
+
urlPath += app.getRegexWildcard();
|
|
17
|
+
}
|
|
18
|
+
return urlPath;
|
|
19
|
+
});
|
|
20
|
+
app.get([
|
|
21
|
+
// De-dupe paths (i.e. root path may have been added more than once)
|
|
22
|
+
...[...new Set(paths)],
|
|
23
|
+
'/:apiVersion/:assetType(asset|content-asset)/:immutable?/s/:signature/' + app.getRegexWildcard(),
|
|
24
|
+
'/:apiVersion/:assetType(asset|content-asset)/:immutable?/' + app.getRegexWildcard(),
|
|
25
|
+
], handleErrors(createAssetMiddleware(context)));
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create middleware function to handle get assets requests
|
|
29
|
+
*/
|
|
6
30
|
function createAssetMiddleware(context) {
|
|
7
31
|
const { assetRegistry, runtimeEnvironment: { basePath }, } = context;
|
|
8
32
|
return async (req, res, next) => {
|
|
@@ -12,25 +36,34 @@ function createAssetMiddleware(context) {
|
|
|
12
36
|
assetId.specifier = path.join(basePath, assetId.specifier);
|
|
13
37
|
}
|
|
14
38
|
try {
|
|
15
|
-
|
|
39
|
+
let asset;
|
|
16
40
|
const assetUri = await assetRegistry.resolveAssetUri(assetId, runtimeEnvironment);
|
|
41
|
+
// Check if this asset is available externally
|
|
17
42
|
if (assetUri.external) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
43
|
+
// This asset is marked external but hit the middleware anyway
|
|
44
|
+
// Check if we have this file locally, if not, send a 302 to
|
|
45
|
+
// redirect to the external URL
|
|
46
|
+
try {
|
|
47
|
+
asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
|
|
48
|
+
// Verify the content actually exists locally
|
|
49
|
+
if (!fs.existsSync(asset.entry)) {
|
|
50
|
+
return sendRedirect(res, assetUri);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
// Expected asset not found locally re-direct to external URL
|
|
55
|
+
if (path.dirname(assetId.specifier) === (basePath ? basePath : '/') &&
|
|
56
|
+
error instanceof DiagnosticsError &&
|
|
57
|
+
error?.diagnostics[0]?.description.category === 'lwrUnresolvable/asset') {
|
|
58
|
+
return sendRedirect(res, assetUri);
|
|
59
|
+
}
|
|
60
|
+
// unexpected throw the error
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
asset = await getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment);
|
|
24
66
|
}
|
|
25
|
-
const asset = await getTracer().trace({
|
|
26
|
-
name: RequestHandlerSpan.GetAsset,
|
|
27
|
-
attributes: {
|
|
28
|
-
specifier: assetId.specifier,
|
|
29
|
-
url: req.originalUrl,
|
|
30
|
-
},
|
|
31
|
-
}, () => {
|
|
32
|
-
return assetRegistry.getAsset({ ...assetId, signature }, runtimeEnvironment, req.isSiteGeneration());
|
|
33
|
-
});
|
|
34
67
|
if (req.isSiteGeneration()) {
|
|
35
68
|
res.setSiteGenerationMetadata({ asset });
|
|
36
69
|
}
|
|
@@ -59,24 +92,29 @@ function createAssetMiddleware(context) {
|
|
|
59
92
|
}
|
|
60
93
|
};
|
|
61
94
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
return
|
|
95
|
+
/**
|
|
96
|
+
* Get the asset definition if asset is local
|
|
97
|
+
*/
|
|
98
|
+
async function getAssetDefinition(assetId, req, assetRegistry, signature, runtimeEnvironment) {
|
|
99
|
+
const asset = await getTracer().trace({
|
|
100
|
+
name: RequestHandlerSpan.GetAsset,
|
|
101
|
+
attributes: {
|
|
102
|
+
specifier: assetId.specifier,
|
|
103
|
+
url: req.originalUrl,
|
|
104
|
+
},
|
|
105
|
+
}, () => {
|
|
106
|
+
return assetRegistry.getAsset({ ...assetId, signature }, runtimeEnvironment, req.isSiteGeneration());
|
|
74
107
|
});
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
108
|
+
return asset;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Send a redirect (302) response
|
|
112
|
+
*/
|
|
113
|
+
function sendRedirect(res, assetUri) {
|
|
114
|
+
res.set({
|
|
115
|
+
Location: assetUri.uri,
|
|
116
|
+
'cache-control': 'public, max-age=60',
|
|
117
|
+
});
|
|
118
|
+
res.sendStatus(302);
|
|
81
119
|
}
|
|
82
120
|
//# sourceMappingURL=asset-middleware.js.map
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.12.0-alpha.
|
|
7
|
+
"version": "0.12.0-alpha.12",
|
|
8
8
|
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -43,34 +43,34 @@
|
|
|
43
43
|
"build": "tsc -b"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@lwrjs/app-service": "0.12.0-alpha.
|
|
47
|
-
"@lwrjs/asset-registry": "0.12.0-alpha.
|
|
48
|
-
"@lwrjs/asset-transformer": "0.12.0-alpha.
|
|
49
|
-
"@lwrjs/base-view-provider": "0.12.0-alpha.
|
|
50
|
-
"@lwrjs/base-view-transformer": "0.12.0-alpha.
|
|
51
|
-
"@lwrjs/client-modules": "0.12.0-alpha.
|
|
52
|
-
"@lwrjs/config": "0.12.0-alpha.
|
|
53
|
-
"@lwrjs/diagnostics": "0.12.0-alpha.
|
|
54
|
-
"@lwrjs/esbuild": "0.12.0-alpha.
|
|
55
|
-
"@lwrjs/fs-asset-provider": "0.12.0-alpha.
|
|
56
|
-
"@lwrjs/fs-watch": "0.12.0-alpha.
|
|
57
|
-
"@lwrjs/html-view-provider": "0.12.0-alpha.
|
|
58
|
-
"@lwrjs/instrumentation": "0.12.0-alpha.
|
|
59
|
-
"@lwrjs/loader": "0.12.0-alpha.
|
|
60
|
-
"@lwrjs/lwc-module-provider": "0.12.0-alpha.
|
|
61
|
-
"@lwrjs/lwc-ssr": "0.12.0-alpha.
|
|
62
|
-
"@lwrjs/markdown-view-provider": "0.12.0-alpha.
|
|
63
|
-
"@lwrjs/module-bundler": "0.12.0-alpha.
|
|
64
|
-
"@lwrjs/module-registry": "0.12.0-alpha.
|
|
65
|
-
"@lwrjs/npm-module-provider": "0.12.0-alpha.
|
|
66
|
-
"@lwrjs/nunjucks-view-provider": "0.12.0-alpha.
|
|
67
|
-
"@lwrjs/o11y": "0.12.0-alpha.
|
|
68
|
-
"@lwrjs/resource-registry": "0.12.0-alpha.
|
|
69
|
-
"@lwrjs/router": "0.12.0-alpha.
|
|
70
|
-
"@lwrjs/server": "0.12.0-alpha.
|
|
71
|
-
"@lwrjs/shared-utils": "0.12.0-alpha.
|
|
72
|
-
"@lwrjs/static": "0.12.0-alpha.
|
|
73
|
-
"@lwrjs/view-registry": "0.12.0-alpha.
|
|
46
|
+
"@lwrjs/app-service": "0.12.0-alpha.12",
|
|
47
|
+
"@lwrjs/asset-registry": "0.12.0-alpha.12",
|
|
48
|
+
"@lwrjs/asset-transformer": "0.12.0-alpha.12",
|
|
49
|
+
"@lwrjs/base-view-provider": "0.12.0-alpha.12",
|
|
50
|
+
"@lwrjs/base-view-transformer": "0.12.0-alpha.12",
|
|
51
|
+
"@lwrjs/client-modules": "0.12.0-alpha.12",
|
|
52
|
+
"@lwrjs/config": "0.12.0-alpha.12",
|
|
53
|
+
"@lwrjs/diagnostics": "0.12.0-alpha.12",
|
|
54
|
+
"@lwrjs/esbuild": "0.12.0-alpha.12",
|
|
55
|
+
"@lwrjs/fs-asset-provider": "0.12.0-alpha.12",
|
|
56
|
+
"@lwrjs/fs-watch": "0.12.0-alpha.12",
|
|
57
|
+
"@lwrjs/html-view-provider": "0.12.0-alpha.12",
|
|
58
|
+
"@lwrjs/instrumentation": "0.12.0-alpha.12",
|
|
59
|
+
"@lwrjs/loader": "0.12.0-alpha.12",
|
|
60
|
+
"@lwrjs/lwc-module-provider": "0.12.0-alpha.12",
|
|
61
|
+
"@lwrjs/lwc-ssr": "0.12.0-alpha.12",
|
|
62
|
+
"@lwrjs/markdown-view-provider": "0.12.0-alpha.12",
|
|
63
|
+
"@lwrjs/module-bundler": "0.12.0-alpha.12",
|
|
64
|
+
"@lwrjs/module-registry": "0.12.0-alpha.12",
|
|
65
|
+
"@lwrjs/npm-module-provider": "0.12.0-alpha.12",
|
|
66
|
+
"@lwrjs/nunjucks-view-provider": "0.12.0-alpha.12",
|
|
67
|
+
"@lwrjs/o11y": "0.12.0-alpha.12",
|
|
68
|
+
"@lwrjs/resource-registry": "0.12.0-alpha.12",
|
|
69
|
+
"@lwrjs/router": "0.12.0-alpha.12",
|
|
70
|
+
"@lwrjs/server": "0.12.0-alpha.12",
|
|
71
|
+
"@lwrjs/shared-utils": "0.12.0-alpha.12",
|
|
72
|
+
"@lwrjs/static": "0.12.0-alpha.12",
|
|
73
|
+
"@lwrjs/view-registry": "0.12.0-alpha.12",
|
|
74
74
|
"chokidar": "^3.5.3",
|
|
75
75
|
"esbuild": "^0.9.7",
|
|
76
76
|
"fs-extra": "^11.1.1",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"ws": "^8.8.1"
|
|
81
81
|
},
|
|
82
82
|
"devDependencies": {
|
|
83
|
-
"@lwrjs/types": "0.12.0-alpha.
|
|
83
|
+
"@lwrjs/types": "0.12.0-alpha.12",
|
|
84
84
|
"@types/ws": "^8.5.3"
|
|
85
85
|
},
|
|
86
86
|
"peerDependencies": {
|
|
@@ -92,5 +92,5 @@
|
|
|
92
92
|
"volta": {
|
|
93
93
|
"extends": "../../../package.json"
|
|
94
94
|
},
|
|
95
|
-
"gitHead": "
|
|
95
|
+
"gitHead": "bc8a88bce246e9ec00641955152de526ea030ebb"
|
|
96
96
|
}
|