From f5067ce38b89442948ce096e5d89651e8970bdb9 Mon Sep 17 00:00:00 2001 From: Frank Weigel Date: Thu, 24 Sep 2020 19:22:45 +0200 Subject: [PATCH] [FIX] Discovery middleware shouldn't fail when lib names overlap (#362) When a library's name is a prefix of another library's name (like with sap.m and sap.me), the discovery service mistakenly assigned test pages to both libraries. By including the slash in the prefix check, only full segment matches will be taken into account. By checking the prefixes from longest to shortest and by aborting after the first match, non-prefix-free lib names can be handled properly. The longest matching prefix will win and there won't be double assignments. --- lib/middleware/discovery.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/middleware/discovery.js b/lib/middleware/discovery.js index 88036aed..6242085f 100644 --- a/lib/middleware/discovery.js +++ b/lib/middleware/discovery.js @@ -83,20 +83,22 @@ function createMiddleware({resources}) { const match = librariesPattern.exec(relPath); if (match) { const lib = match[1]; - libs[lib] = lib.replace(/\//g, "."); + libs[lib + "/"] = lib.replace(/\//g, "."); } }); + const libPrefixes = Object.keys(libs).sort().reverse(); testPageResources.forEach(function(resource) { const relPath = resource.getPath().substr(16); // cut off leading "/test-resources/" if (testPagesPattern.test(relPath)) { - Object.keys(libs).forEach(function(lib) { - if (relPath.indexOf(lib) === 0) { + libPrefixes.some(function(lib) { + if (relPath.startsWith(lib)) { response.push({ lib: libs[lib], - name: relPath.substr(lib.length + 1), + name: relPath.substr(lib.length), url: "../" + relPath }); + return true; // abort loop } }); }