Skip to content

Commit 4d2da6e

Browse files
committed
Handle local dependencies correctly
Especially libraries where not handled correctly. ABAP URI retrieval now moved to type formatters
1 parent 9317a77 commit 4d2da6e

File tree

2 files changed

+73
-21
lines changed

2 files changed

+73
-21
lines changed

lib/middleware/proxyRewrite.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
const log = require("@ui5/logger").getLogger("server:middleware:proxyRewrite");
22

33
function createMiddleware({resources, configuration, cdnUrl}) {
4-
const resourceRootPath = !configuration.appOnly && configuration.destination.ui5Root;
5-
const applicationRootPath = configuration.destination.appRoot;
4+
const rewriteRootPaths = configuration.rewriteRootPaths;
5+
66
const cacheBusterRegex = /~.*~[A-Z0-9]?\/?/;
77
const preloadRegex = /^.*(?:Component-preload\.js|library-preload\.js|library-preload\.json)$/i;
88

99
return function proxyRewrite(req, res, next) {
10-
if ((!resourceRootPath || req.path.indexOf(resourceRootPath) === -1) &&
11-
(!applicationRootPath || req.path.indexOf(applicationRootPath) === -1)) {
10+
const rewriteApplicable = Object.keys(rewriteRootPaths).some((resourceRootPath) => {
11+
return req.path.indexOf(resourceRootPath) !== -1;
12+
});
13+
14+
if (!rewriteApplicable) {
1215
// No normalization applicable
1316
next();
1417
return;
@@ -82,14 +85,19 @@ function createMiddleware({resources, configuration, cdnUrl}) {
8285
async function normalizeRequestPath(reqPath) {
8386
let normalizedPath = reqPath;
8487

85-
if (resourceRootPath && normalizedPath.indexOf(resourceRootPath) !== -1) {
86-
normalizedPath = normalizedPath.substr(resourceRootPath.length);
87-
} else if (applicationRootPath && normalizedPath.indexOf(applicationRootPath) !== -1) {
88-
normalizedPath = normalizedPath.substr(applicationRootPath.length);
88+
// Strip off first matching rewrite root path
89+
for (const rootPath in rewriteRootPaths) {
90+
if (rewriteRootPaths.hasOwnProperty(rootPath)) {
91+
if (normalizedPath.indexOf(rootPath) !== -1) {
92+
normalizedPath = normalizedPath.substr(rootPath.length);
93+
if (rewriteRootPaths[rootPath].rewriteTo) {
94+
normalizedPath = rewriteRootPaths[rootPath].rewriteTo + normalizedPath;
95+
}
96+
break;
97+
}
98+
}
8999
}
90-
91100
normalizedPath = normalizedPath.replace(cacheBusterRegex, "");
92-
93101
return rewriteSpecials(normalizedPath);
94102
}
95103

lib/proxyConfiguration.js

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const log = require("@ui5/logger").getLogger("proxyConfiguration");
2-
const {resourceFactory} = require("@ui5/fs");
1+
const log = require("@ui5/logger").getLogger("server:proxyConfiguration");
32

43
const proxyConfigurations = {};
54

@@ -10,6 +9,9 @@ function addConfiguration(name, proxyConfig) {
109
if (proxyConfigurations[name]) {
1110
throw new Error(`proxyConfiguration: A configuration with name ${name} is already known`);
1211
}
12+
if (proxyConfig.rewriteRootPaths) {
13+
throw new Error(`Proxy Configuration ${name} must not define "rewriteRootPaths"`);
14+
}
1315

1416
if (!proxyConfig.destination) {
1517
proxyConfig.destination = {};
@@ -26,23 +28,65 @@ async function getConfigurationForProject(project) {
2628
throw new Error(`Found multiple proxy configurations. ` +
2729
`This is not yet supported.`); // TODO
2830
}
31+
32+
log.verbose(`Applying proxy configuration ${configNames[0]} to project ${project.metadata.name}...`);
2933
const config = JSON.parse(JSON.stringify(proxyConfigurations[configNames[0]]));
34+
config.rewriteRootPaths = {};
35+
36+
if (config.destination.ui5Root && !config.appOnly) {
37+
log.verbose(`Using configured "destination.ui5Root": ${config.destination.ui5Root}`);
38+
config.rewriteRootPaths[config.destination.ui5Root] = {
39+
rewriteTo: ""
40+
};
41+
}
42+
43+
mapProjectDependencies(project, (proj) => {
44+
if (proj.specVersion !== "1.1a") {
45+
log.warn(`Project ${project.metadata.name} defines specification version ${proj.specVersion}. ` +
46+
`Some proxy configuration features require projects to define specification version 1.1a`);
47+
}
48+
log.verbose(`Using ABAP URI ${proj.metadata.abapUri} from metadata of project ${proj.metadata.name}`);
49+
let prefix = "";
50+
if (proj.type !== "application") {
51+
if (project.resources.pathMappings["/resources"]) {
52+
// If the project defines a /resources path mapping,
53+
// we expect this to match the ABAP URI deployment path
54+
prefix += "/resources/";
55+
56+
// If this is not an application and there is no /resources path mapping, somebody does something wild
57+
// and hopefully knows what he/she does
58+
}
59+
prefix += proj.metadata.namespace;
60+
}
61+
config.rewriteRootPaths[proj.metadata.abapUri] = {
62+
rewriteTo: prefix
63+
};
64+
});
3065

31-
const {source} = resourceFactory.createCollectionsForTree(project);
32-
const manifestResource = await source.byPath("/manifest.json");
33-
if (manifestResource) {
34-
const manifest = JSON.parse(await manifestResource.getBuffer());
35-
if (manifest["sap.platform.abap"] && manifest["sap.platform.abap"].uri) {
36-
log.verbose(`Using sap.platform.abap URI configuration as application root ` +
37-
`path: ${manifest["sap.platform.abap"].uri}`);
38-
config.destination.appRoot = manifest["sap.platform.abap"].uri;
66+
if (log.isLevelEnabled("verbose")) {
67+
log.verbose(`Configured ${config.rewriteRootPaths.length} root paths to rewrite for ` +
68+
`project ${project.metadata.name};`);
69+
for (const abapUri in config.rewriteRootPaths) {
70+
if (config.rewriteRootPaths.hasOwnProperty(abapUri)) {
71+
if (config.rewriteRootPaths[abapUri].rewriteTo) {
72+
log.verbose(`Rewriting ${abapUri} to ${config.rewriteRootPaths[abapUri].rewriteTo}`);
73+
} else {
74+
log.verbose(`Rewriting ${abapUri}`);
75+
}
76+
}
3977
}
4078
}
41-
log.verbose(`UI5 root path configured as: ${config.destination.ui5Root}`);
4279

4380
return config;
4481
}
4582

83+
function mapProjectDependencies(tree, handler) {
84+
handler(tree);
85+
tree.dependencies.map((dep) => {
86+
mapProjectDependencies(dep, handler);
87+
});
88+
}
89+
4690
module.exports = {
4791
addConfiguration,
4892
getConfigurationForProject

0 commit comments

Comments
 (0)